Skip to content

Commit

Permalink
code: add unit test for countries - part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
finozzifa committed Dec 19, 2024
2 parents 9d916dd + 30fa657 commit 1ba9863
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
11 changes: 6 additions & 5 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
# coding: utf-8

import pathlib
import zipfile
from urllib.request import urlretrieve

import pandas as pd
import pypsa
import pytest
import yaml
import zipfile

from urllib.request import urlretrieve


@pytest.fixture(scope="function")
Expand Down Expand Up @@ -126,8 +125,10 @@ def download_natural_earth(tmpdir):
zipped_filename = "ne_10m_admin_0_countries_deu.zip"
path_to_zip_file, headers = urlretrieve(url, zipped_filename)
directory_to_extract_to = pathlib.Path(tmpdir, "folder")
with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
with zipfile.ZipFile(path_to_zip_file, "r") as zip_ref:
zip_ref.extractall(directory_to_extract_to)
natural_earth_shape_file_path = pathlib.Path(directory_to_extract_to, "ne_10m_admin_0_countries_deu.shp")
natural_earth_shape_file_path = pathlib.Path(
directory_to_extract_to, "ne_10m_admin_0_countries_deu.shp"
)
yield natural_earth_shape_file_path
pathlib.Path(natural_earth_shape_file_path).unlink(missing_ok=True)
36 changes: 24 additions & 12 deletions test/test_build_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,33 @@
import pandas as pd
import pytest



sys.path.append("./scripts")

from build_shapes import (
_simplify_polys,
countries,
)
from build_shapes import _simplify_polys, countries

path_cwd = pathlib.Path.cwd()


@pytest.mark.parametrize(
"tolerance,expected_tuple",
[
(None, (837421026967.6136, "POINT (1222256.2540812986 4769376.881403567)", 5958197.627333247)),
(10.0, (837420795181.8837, "POINT (1222256.3488327404 4769376.890839249)", 5958195.650234007))],
(
None,
(
837421026967.6136,
"POINT (1222256.2540812986 4769376.881403567)",
5958197.627333247,
),
),
(
10.0,
(
837420795181.8837,
"POINT (1222256.3488327404 4769376.890839249)",
5958195.650234007,
),
),
],
)
def test_simplify_polys(tolerance, expected_tuple):
"""
Expand All @@ -48,16 +58,18 @@ def test_simplify_polys(tolerance, expected_tuple):
gdf_country_simplified["area"] = gdf_country_simplified.area
gdf_country_simplified["centroid"] = gdf_country_simplified.centroid
gdf_country_simplified["perimeter"] = gdf_country_simplified.length
output_tuple = (gdf_country_simplified["area"][0], str(gdf_country_simplified["centroid"][0]), gdf_country_simplified["perimeter"][0])
print(output_tuple)
output_tuple = (
gdf_country_simplified["area"][0],
str(gdf_country_simplified["centroid"][0]),
gdf_country_simplified["perimeter"][0],
)
assert len(output_tuple) == len(expected_tuple)
assert all([x == y for x, y in zip(output_tuple, expected_tuple)])


@pytest.mark.parametrize(
"country_list",
[
["MK"], ["IT"]],
[["MK"], ["IT"]],
)
def test_countries(config, download_natural_earth, country_list):
natural_earth = download_natural_earth
Expand Down

0 comments on commit 1ba9863

Please sign in to comment.