From f092d513dceb88f3b1d93a99a434402f169fa0a8 Mon Sep 17 00:00:00 2001 From: Juho Ervasti Date: Fri, 8 Nov 2024 15:11:30 +0200 Subject: [PATCH] Add more tests --- tests/conftest.py | 46 ++++++++++++++++ tests/core/test_area.py | 7 ++- tests/core/test_area_layer.py | 18 +++++++ .../test_count_trajectories_area.py | 54 ++----------------- 4 files changed, 74 insertions(+), 51 deletions(-) create mode 100644 tests/core/test_area_layer.py diff --git a/tests/conftest.py b/tests/conftest.py index 31259cc..e5a276f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -425,3 +425,49 @@ def four_point_area(): ), "polygon", ) + + +@pytest.fixture +def qgis_area_polygon_layer(): + layer = QgsVectorLayer("Polygon?crs=EPSG:3857", "Polygon Layer", "memory") + + layer.startEditing() + + layer.addAttribute(QgsField("name", QVariant.String)) + + area1 = QgsFeature(layer.fields()) + area1.setAttributes(["area1"]) + area1.setGeometry( + QgsGeometry.fromPolygonXY( + [ + [ + QgsPointXY(1, 2), + QgsPointXY(2, 2), + QgsPointXY(2, 2.5), + QgsPointXY(1, 2.5), + ], + ] + ) + ) + + area2 = QgsFeature(layer.fields()) + area2.setAttributes(["area2"]) + area2.setGeometry( + QgsGeometry.fromPolygonXY( + [ + [ + QgsPointXY(0, 0), + QgsPointXY(1, 0), + QgsPointXY(1, 1), + QgsPointXY(0, 1), + ], + ] + ) + ) + + layer.addFeature(area1) + layer.addFeature(area2) + + layer.commitChanges() + + return layer diff --git a/tests/core/test_area.py b/tests/core/test_area.py index 2aef4cd..3591b7e 100644 --- a/tests/core/test_area.py +++ b/tests/core/test_area.py @@ -5,5 +5,10 @@ def test_area_trajetory_count( four_point_area: Area, two_node_trajectory: Trajectory, three_node_trajectory: Trajectory ): - four_point_area.get_inside_trajectories(trajectories=(two_node_trajectory, three_node_trajectory)) + four_point_area.count_trajectories((two_node_trajectory, three_node_trajectory)) assert four_point_area.trajectory_count() == 2 + + +def test_area_average_speed(four_point_area: Area, two_node_trajectory: Trajectory, three_node_trajectory: Trajectory): + four_point_area.count_trajectories((two_node_trajectory, three_node_trajectory)) + assert four_point_area.average_speed() == 36.0 diff --git a/tests/core/test_area_layer.py b/tests/core/test_area_layer.py new file mode 100644 index 0000000..64d25b5 --- /dev/null +++ b/tests/core/test_area_layer.py @@ -0,0 +1,18 @@ +from fvh3t.core.area_layer import AreaLayer + + +def test_area_layer_create_areas(qgis_area_polygon_layer): + area_layer = AreaLayer( + qgis_area_polygon_layer, + "name", + ) + + areas = area_layer.areas() + + assert len(areas) == 2 + + area1 = areas[0] + area2 = areas[1] + + assert area1.geometry().asWkt() == "Polygon ((1 2, 2 2, 2 2.5, 1 2.5, 1 2))" + assert area2.geometry().asWkt() == "Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))" diff --git a/tests/processing/test_count_trajectories_area.py b/tests/processing/test_count_trajectories_area.py index 5b9b253..9977752 100644 --- a/tests/processing/test_count_trajectories_area.py +++ b/tests/processing/test_count_trajectories_area.py @@ -10,52 +10,6 @@ from fvh3t.fvh3t_processing.traffic_trajectory_toolkit_provider import TTTProvider -@pytest.fixture -def input_area_layer_for_algorithm(): - layer = QgsVectorLayer("Polygon?crs=EPSG:3857", "Polygon Layer", "memory") - - layer.startEditing() - - layer.addAttribute(QgsField("name", QVariant.String)) - - area1 = QgsFeature(layer.fields()) - area1.setAttributes(["area1"]) - area1.setGeometry( - QgsGeometry.fromPolygonXY( - [ - [ - QgsPointXY(1, 2), - QgsPointXY(2, 2), - QgsPointXY(2, 2.5), - QgsPointXY(1, 2.5), - ], - ] - ) - ) - - area2 = QgsFeature(layer.fields()) - area2.setAttributes(["area2"]) - area2.setGeometry( - QgsGeometry.fromPolygonXY( - [ - [ - QgsPointXY(0, 0), - QgsPointXY(1, 0), - QgsPointXY(1, 1), - QgsPointXY(0, 1), - ], - ] - ) - ) - - layer.addFeature(area1) - layer.addFeature(area2) - - layer.commitChanges() - - return layer - - @pytest.fixture def input_point_layer_for_algorithm(): layer = QgsVectorLayer("Point?crs=EPSG:3857", "Point Layer", "memory") @@ -156,7 +110,7 @@ def input_point_layer_for_algorithm(): def test_count_trajectories_area( qgis_app, qgis_processing, # noqa: ARG001 - input_area_layer_for_algorithm: QgsVectorLayer, + qgis_area_polygon_layer: QgsVectorLayer, input_point_layer_for_algorithm: QgsVectorLayer, ): provider = TTTProvider() @@ -166,11 +120,11 @@ def test_count_trajectories_area( ## TEST CASE 1 - NO FILTERING # script requires layers to be added to the project - QgsProject.instance().addMapLayers([input_area_layer_for_algorithm, input_point_layer_for_algorithm]) + QgsProject.instance().addMapLayers([qgis_area_polygon_layer, input_point_layer_for_algorithm]) params = { "INPUT_POINTS": input_point_layer_for_algorithm, - "INPUT_AREAS": input_area_layer_for_algorithm, + "INPUT_AREAS": qgis_area_polygon_layer, "TRAVELER_CLASS": None, "START_TIME": None, "END_TIME": None, @@ -205,7 +159,7 @@ def test_count_trajectories_area( case2_params = { "INPUT_POINTS": input_point_layer_for_algorithm, - "INPUT_AREAS": input_area_layer_for_algorithm, + "INPUT_AREAS": qgis_area_polygon_layer, "TRAVELER_CLASS": "car", # filter by class too "START_TIME": QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0), QTimeZone.utc()), "END_TIME": QDateTime(QDate(1970, 1, 1), QTime(0, 5, 0), QTimeZone.utc()),