Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JuhoErvasti committed Nov 8, 2024
1 parent 85cd69b commit f092d51
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 51 deletions.
46 changes: 46 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion tests/core/test_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions tests/core/test_area_layer.py
Original file line number Diff line number Diff line change
@@ -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))"
54 changes: 4 additions & 50 deletions tests/processing/test_count_trajectories_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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()),
Expand Down

0 comments on commit f092d51

Please sign in to comment.