diff --git a/tests/unit/test_unit_line.py b/tests/unit/test_unit_line.py index c127ca9..3d73477 100644 --- a/tests/unit/test_unit_line.py +++ b/tests/unit/test_unit_line.py @@ -2,120 +2,31 @@ from unittest.mock import patch -import geopandas as gpd -import pytest -from lantmateriet import config from lantmateriet.line import Line class TestUnitLine: """Unit tests of Line.""" - @pytest.mark.parametrize( - "file_name, detail_level, layer, use_arrow, df, expected_result", - [ - ( - "path", - "50", - "vaglinje", - True, - gpd.GeoDataFrame( - { - "objekttyp": [ - k for k in config.config_50.communication["vaglinje"].keys() - ] - } - ), - config.config_50, - ), - ( - "path", - "50", - "vaglinje", - True, - gpd.GeoDataFrame( - { - "objekttyp": [ - k - for k in config.config_50.communication["vaglinje"].keys() - if k not in {"Motorväg"} - ] - } - ), - None, - ), - ], - ) - @patch("lantmateriet.geometry.gpd.read_file") - def test_unit_communication_init( - self, - mock_gpd_read_file, - file_name, - detail_level, - layer, - use_arrow, - df, - expected_result, - ): - """Unit test of Line __init__ method. - - Args; - mock_gpd_read_file: mock of gpd read_file - file_name: file_name - detail_level: detail_level - layer: layer - use_arrow: arrow flag - df: dataframe - expected_result: expected result - """ - mock_gpd_read_file.return_value = df - if expected_result is None: - with pytest.raises(KeyError): - communication = Line(file_name, detail_level, layer, use_arrow) - else: - communication = Line(file_name, detail_level, layer, use_arrow) - mock_gpd_read_file.assert_called_with( - file_name, layer=layer, use_arrow=use_arrow - ) - assert communication.config == expected_result - - @patch("lantmateriet.communication.Line._process") - @patch("lantmateriet.communication.Line.__init__", return_value=None) - def test_unit_communication_process( - self, mock_communication_init, mock_communication_process - ): - """Unit test of communication process method. - - Args: - mock_communication_init: mock of Line __init__ - mock_communication_process: mock of Line _process - """ - communication = Line("path") - communication.item_type = "communication" - communication.layer = "vaglinje" - communication.dissolve = False - - communication.process() - mock_communication_process.assert_called_once_with( - "communication", "vaglinje", False, False, True - ) - - @patch("lantmateriet.communication.Line._save") - @patch("lantmateriet.communication.Line.__init__", return_value=None) - def test_unit_communication_save( - self, mock_communication_init, mock_communication_save - ): - """Unit test of communication save method. - - Args: - mock_communication_init: mock of Line __init__ - mock_communication_save: mock of Line _save - """ - communication = Line("path") - communication.item_type = "communication" - communication.layer = "vaglinje" - - communication.save({}, "path") - mock_communication_save.assert_called_once_with( - "communication", "vaglinje", {}, "path" - ) + @patch("lantmateriet.geometry.Geometry.__init__") + def test_unit_line_init(self, mock_geometry): + """Unit test of Line __init__ method.""" + line = Line("path", "50", "layer", "name", "field") + assert line.dissolve is False + mock_geometry.assert_called_once() + + @patch("lantmateriet.geometry.Geometry._process") + @patch("lantmateriet.geometry.Geometry.__init__") + def test_unit_line_process(self, mock_geometry, mock_geometry_process): + """Unit test of Line process method.""" + line = Line("path", "50", "layer", "name", "field") + line.process(False) + mock_geometry_process.assert_called_once() + + @patch("lantmateriet.geometry.Geometry._save") + @patch("lantmateriet.geometry.Geometry.__init__") + def test_unit_line_save(self, mock_geometry, mock_geometry_save): + """Unit test of Line save method.""" + line = Line("path", "50", "layer", "name", "field") + line.save("path", "file") + mock_geometry_save.assert_called_once() diff --git a/tests/unit/test_unit_point.py b/tests/unit/test_unit_point.py index 9218893..2880013 100644 --- a/tests/unit/test_unit_point.py +++ b/tests/unit/test_unit_point.py @@ -2,120 +2,31 @@ from unittest.mock import patch -import geopandas as gpd -import pytest -from lantmateriet import config from lantmateriet.point import Point class TestUnitPoint: """Unit tests of Point.""" - @pytest.mark.parametrize( - "file_name, detail_level, layer, use_arrow, df, expected_result", - [ - ( - "path", - "50", - "byggnad", - True, - gpd.GeoDataFrame( - { - "objekttyp": [ - k for k in config.config_50.construction["byggnad"].keys() - ] - } - ), - config.config_50, - ), - ( - "path", - "50", - "byggnad", - True, - gpd.GeoDataFrame( - { - "objekttyp": [ - k - for k in config.config_50.construction["byggnad"].keys() - if k not in {"Bostad"} - ] - } - ), - None, - ), - ], - ) - @patch("lantmateriet.geometry.gpd.read_file") - def test_unit_construction_init( - self, - mock_gpd_read_file, - file_name, - detail_level, - layer, - use_arrow, - df, - expected_result, - ): - """Unit test of Point __init__ method. - - Args; - mock_gpd_read_file: mock of gpd read_file - file_name: file_name - detail_level: detail_level - layer: layer - use_arrow: arrow flag - df: dataframe - expected_result: expected result - """ - mock_gpd_read_file.return_value = df - if expected_result is None: - with pytest.raises(KeyError): - construction = Point(file_name, detail_level, layer, use_arrow) - else: - construction = Point(file_name, detail_level, layer, use_arrow) - mock_gpd_read_file.assert_called_with( - file_name, layer=layer, use_arrow=use_arrow - ) - assert construction.config == expected_result - - @patch("lantmateriet.construction.Point._process") - @patch("lantmateriet.construction.Point.__init__", return_value=None) - def test_unit_construction_process( - self, mock_construction_init, mock_construction_process - ): - """Unit test of Point process method. - - Args: - mock_construction_init: mock of Point __init__ - mock_construction_process: mock of Point _process - """ - construction = Point("path") - construction.item_type = "construction" - construction.layer = "byggnad" - construction.dissolve = True - - construction.process() - mock_construction_process.assert_called_once_with( - "construction", "byggnad", True, True, True - ) - - @patch("lantmateriet.construction.Point._save") - @patch("lantmateriet.construction.Point.__init__", return_value=None) - def test_unit_construction_save( - self, mock_construction_init, mock_construction_save - ): - """Unit test of construction save method. - - Args: - mock_construction_init: mock of Point __init__ - mock_construction_save: mock of Point _save - """ - construction = Point("path") - construction.item_type = "construction" - construction.layer = "byggnad" - - construction.save({}, "path") - mock_construction_save.assert_called_once_with( - "construction", "byggnad", {}, "path" - ) + @patch("lantmateriet.geometry.Geometry.__init__") + def test_unit_point_init(self, mock_geometry): + """Unit test of Point __init__ method.""" + point = Point("path", "50", "layer", "name", "field") + assert point.dissolve is False + mock_geometry.assert_called_once() + + @patch("lantmateriet.geometry.Geometry._process") + @patch("lantmateriet.geometry.Geometry.__init__") + def test_unit_point_process(self, mock_geometry, mock_geometry_process): + """Unit test of Point process method.""" + point = Point("path", "50", "layer", "name", "field") + point.process(False) + mock_geometry_process.assert_called_once() + + @patch("lantmateriet.geometry.Geometry._save") + @patch("lantmateriet.geometry.Geometry.__init__") + def test_unit_point_save(self, mock_geometry, mock_geometry_save): + """Unit test of Point save method.""" + point = Point("path", "50", "layer", "name", "field") + point.save("path", "file") + mock_geometry_save.assert_called_once() diff --git a/tests/unit/test_unit_polygon.py b/tests/unit/test_unit_polygon.py index 1fe7542..877a7a1 100644 --- a/tests/unit/test_unit_polygon.py +++ b/tests/unit/test_unit_polygon.py @@ -2,126 +2,31 @@ from unittest.mock import patch -import geopandas as gpd -import pytest -from lantmateriet import config from lantmateriet.polygon import Polygon -from shapely.geometry import Point class TestUnitPolygon: """Unit tests of Polygon.""" - @pytest.mark.parametrize( - "file_name, detail_level, layer, use_arrow, df, expected_result", - [ - ( - "path", - "50", - "mark", - True, - gpd.GeoDataFrame( - {"objekttyp": [k for k in config.config_50.ground["mark"].keys()]} - ), - config.config_50, - ), - ( - "path", - "50", - "mark", - True, - gpd.GeoDataFrame( - { - "objekttyp": [ - k - for k in config.config_50.ground["mark"].keys() - if k not in {"Sjö"} - ] - } - ), - None, - ), - ], - ) - @patch("lantmateriet.geometry.gpd.read_file") - def test_unit_ground_init( - self, - mcck_gpd_read_file, - file_name, - detail_level, - layer, - use_arrow, - df, - expected_result, - ): - """Unit test of Polygon __init__ method. - - Args; - mcck_gpd_read_file: mock of gpd read_file - file_name: file_name - detail_level: detail_level - layer: layer - use_arrow: arrow flag - df: dataframe - expected_result: expected result - """ - mcck_gpd_read_file.return_value = df - if expected_result is None: - with pytest.raises(KeyError): - ground = Polygon(file_name, detail_level, layer, use_arrow) - else: - ground = Polygon(file_name, detail_level, layer, use_arrow) - mcck_gpd_read_file.assert_called_with( - file_name, layer=layer, use_arrow=use_arrow - ) - assert ground.config == expected_result - - @patch( - "lantmateriet.ground.Polygon._process", - return_value={ - "Sverige": gpd.GeoDataFrame( - {"geometry": [Point(0, 0), Point(0, 1)], "objekttyp": "Sverige"} - ) - }, - ) - @patch("lantmateriet.ground.Polygon.__init__", return_value=None) - def test_unit_ground_process(self, mock_ground_init, mock_ground_process): - """Unit test of Polygon process method. - - Args: - mock_ground_init: mock of Polygon __init__ - mock_ground_process: mock of Polygon _process - """ - ground = Polygon("path") - ground.item_type = "ground" - ground.layer = "mark" - ground.dissolve = True - ground.config = config.config_50 - - ground.process() - mock_ground_process.assert_called_once_with("ground", "mark", True, True, True) - - @patch("lantmateriet.ground.Polygon._save") - @patch("lantmateriet.ground.Polygon.__init__", return_value=None) - def test_unit_ground_save(self, mock_ground_init, mock_ground_save): - """Unit test of Polygon save method. - - Args: - mock_ground_init: mock of Polygon __init__ - mock_ground_save: mock of Polygon _save - """ - ground = Polygon("path") - ground.item_type = "ground" - ground.layer = "mark" - ground.config = config.config_50 - expected_data = { - k: v - for k, v in config.config_50.ground["mark"].items() - if k not in config.config_50.exteriorise - } - - ground.save(config.config_50.ground["mark"], "path") - - mock_ground_save.assert_called_once_with( - "ground", "mark", expected_data, "path" - ) + @patch("lantmateriet.geometry.Geometry.__init__") + def test_unit_polygon_init(self, mock_geometry): + """Unit test of Polygon __init__ method.""" + polygon = Polygon("path", "50", "layer", "name", "field") + assert polygon.dissolve is True + mock_geometry.assert_called_once() + + @patch("lantmateriet.geometry.Geometry._process") + @patch("lantmateriet.geometry.Geometry.__init__") + def test_unit_polygon_process(self, mock_geometry, mock_geometry_process): + """Unit test of Polygon process method.""" + polygon = Polygon("path", "50", "layer", "name", "field") + polygon.process(False) + mock_geometry_process.assert_called_once() + + @patch("lantmateriet.geometry.Geometry._save") + @patch("lantmateriet.geometry.Geometry.__init__") + def test_unit_polygon_save(self, mock_geometry, mock_geometry_save): + """Unit test of Polygon save method.""" + polygon = Polygon("path", "50", "layer", "name", "field") + polygon.save("path", "file") + mock_geometry_save.assert_called_once()