Skip to content

Commit b41e377

Browse files
committed
test algorithms
1 parent 0e4cfad commit b41e377

File tree

6 files changed

+188
-13
lines changed

6 files changed

+188
-13
lines changed

travel_time_platform_plugin/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from . import express, resources, tests, tiles, ui
3030
from .provider import Provider
31-
from .utils import tr
31+
from .utils import log, tr
3232

3333

3434
class TTPPlugin:
@@ -166,6 +166,8 @@ def unload(self):
166166
self.iface.removePluginMenu("&TravelTime platform", self.action_show_tiles)
167167
self.iface.removePluginMenu("&TravelTime platform", self.action_show_config)
168168
self.iface.removePluginMenu("&TravelTime platform", self.action_show_help)
169+
self.iface.removePluginMenu("&TravelTime platform", self.action_rerun)
170+
self.iface.removePluginMenu("&TravelTime platform", self.action_run_tests)
169171

170172
# Remove the provider from the registry
171173
QgsApplication.processingRegistry().removeProvider(self.provider)
@@ -234,6 +236,8 @@ def run_tests(self):
234236
result = tests.run_suite(stream=buf)
235237
output = buf.getvalue()
236238

239+
log(output, "-tests")
240+
237241
success = result.wasSuccessful()
238242

239243
box = QMessageBox(

travel_time_platform_plugin/tests/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010

1111

1212
def run_suite(stream) -> unittest.TestResult:
13-
from . import tests_express_tools
1413

1514
loader = unittest.TestLoader()
16-
suite = unittest.TestSuite()
17-
suite = loader.loadTestsFromModule(tests_express_tools)
15+
suite = loader.loadTestsFromNames(
16+
[
17+
"travel_time_platform_plugin.tests.tests_express_tools",
18+
"travel_time_platform_plugin.tests.tests_algorithms",
19+
]
20+
)
1821
runner = unittest.TextTestRunner(stream=stream, verbosity=2)
1922
return runner.run(suite)
2023

travel_time_platform_plugin/tests/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,6 @@ def _click(self, map_pos: QgsPointXY):
135135
)
136136

137137
self._feedback()
138+
139+
def _today_at_noon(self) -> datetime:
140+
return datetime.now().replace(hour=12, minute=30, second=0)
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import processing
2+
from qgis.core import QgsFeature, QgsProject
3+
4+
from ..algorithms.utilities import GeocodingAlgorithmBase
5+
from .base import TestCaseBase
6+
7+
8+
class AlgorithmsBasicTest(TestCaseBase):
9+
"""Testing algorithms with basic parameters (mostly default)"""
10+
11+
def test_processing_time_map_simple(self):
12+
input_lyr = self._make_layer(["POINT(-3.1 55.9)"])
13+
results = processing.runAndLoadResults(
14+
"ttp_v4:time_map_simple",
15+
{
16+
"INPUT_SEARCHES": input_lyr,
17+
"INPUT_TIME": self._today_at_noon().isoformat(),
18+
"OUTPUT": "memory:",
19+
# TODO: these inputs should work without values
20+
"INPUT_SEARCH_TYPE": 0,
21+
"INPUT_TRNSPT_TYPE": 0,
22+
"OUTPUT_RESULT_TYPE": 0,
23+
},
24+
)
25+
output_layer = QgsProject.instance().mapLayer(results["OUTPUT"])
26+
self.assertEqual(output_layer.featureCount(), 1)
27+
28+
def test_processing_time_filter_simple(self):
29+
input_lyr_a = self._make_layer(["POINT(0.0 51.5)"])
30+
input_lyr_b = self._make_layer(
31+
["POINT(0.1 51.5)", "POINT(-0.1 51.5)", "POINT(0.0 51.6)"]
32+
)
33+
results = processing.runAndLoadResults(
34+
"ttp_v4:time_filter_simple",
35+
{
36+
"INPUT_SEARCHES": input_lyr_a,
37+
"INPUT_LOCATIONS": input_lyr_b,
38+
"INPUT_TIME": self._today_at_noon().isoformat(),
39+
"INPUT_TRAVEL_TIME": 15,
40+
"OUTPUT": "memory:",
41+
# TODO: these inputs should work without values
42+
"INPUT_SEARCH_TYPE": 0,
43+
"INPUT_TRNSPT_TYPE": 0,
44+
},
45+
)
46+
output_layer = QgsProject.instance().mapLayer(results["OUTPUT"])
47+
self.assertEqual(output_layer.featureCount(), 3)
48+
49+
def test_processing_routes_simple(self):
50+
input_lyr_a = self._make_layer(["POINT(0.0 51.5)"])
51+
input_lyr_b = self._make_layer(
52+
["POINT(0.1 51.5)", "POINT(-0.1 51.5)", "POINT(0.0 51.6)"]
53+
)
54+
results = processing.runAndLoadResults(
55+
"ttp_v4:routes_simple",
56+
{
57+
"INPUT_SEARCHES": input_lyr_a,
58+
"INPUT_LOCATIONS": input_lyr_b,
59+
"INPUT_TIME": self._today_at_noon().isoformat(),
60+
"OUTPUT": "memory:",
61+
# TODO: these inputs should work without values
62+
"INPUT_SEARCH_TYPE": 0,
63+
"INPUT_TRNSPT_TYPE": 0,
64+
"OUTPUT_RESULT_TYPE": 0,
65+
},
66+
)
67+
output_layer = QgsProject.instance().mapLayer(results["OUTPUT"])
68+
self.assertEqual(output_layer.featureCount(), 3)
69+
70+
def test_processing_time_map(self):
71+
input_lyr_a = self._make_layer(["POINT(0.0 51.5)"])
72+
results = processing.runAndLoadResults(
73+
"ttp_v4:time_map",
74+
{
75+
"INPUT_DEPARTURE_SEARCHES": input_lyr_a,
76+
"INPUT_DEPARTURE_TIME": self._today_at_noon().isoformat(),
77+
"INPUT_DEPARTURE_TRAVEL_TIME": "900",
78+
"INPUT_THROTTLING_STRATEGY": 0,
79+
"OUTPUT": "memory:",
80+
},
81+
)
82+
output_layer = QgsProject.instance().mapLayer(results["OUTPUT"])
83+
self.assertEqual(output_layer.featureCount(), 1)
84+
85+
def test_processing_time_filter(self):
86+
input_lyr_a = self._make_layer(["POINT(0.0 51.5)"])
87+
input_lyr_b = self._make_layer(
88+
["POINT(0.1 51.5)", "POINT(0.2 51.5)", "POINT(0.3 51.5)"]
89+
)
90+
results = processing.runAndLoadResults(
91+
"ttp_v4:time_filter",
92+
{
93+
"INPUT_DEPARTURE_SEARCHES": input_lyr_a,
94+
"INPUT_DEPARTURE_TIME": self._today_at_noon().isoformat(),
95+
"INPUT_DEPARTURE_TRAVEL_TIME": "900",
96+
"INPUT_LOCATIONS": input_lyr_b,
97+
"INPUT_THROTTLING_STRATEGY": 0,
98+
"OUTPUT": "memory:",
99+
},
100+
)
101+
output_layer = QgsProject.instance().mapLayer(results["OUTPUT"])
102+
self.assertEqual(output_layer.featureCount(), 3)
103+
104+
def test_processing_routes(self):
105+
input_lyr_a = self._make_layer(["POINT(0.0 51.5)"])
106+
input_lyr_b = self._make_layer(
107+
["POINT(0.1 51.5)", "POINT(-0.1 51.5)", "POINT(0.0 51.6)"]
108+
)
109+
results = processing.runAndLoadResults(
110+
"ttp_v4:routes",
111+
{
112+
"INPUT_DEPARTURE_SEARCHES": input_lyr_a,
113+
"INPUT_DEPARTURE_TIME": self._today_at_noon().isoformat(),
114+
"INPUT_DEPARTURE_TRAVEL_TIME": "900",
115+
"INPUT_LOCATIONS": input_lyr_b,
116+
"INPUT_THROTTLING_STRATEGY": 0,
117+
"OUTPUT": "memory:",
118+
},
119+
)
120+
output_layer = QgsProject.instance().mapLayer(results["OUTPUT"])
121+
self.assertEqual(output_layer.featureCount(), 3)
122+
123+
def test_processing_geocoding(self):
124+
input_lyr = self._make_layer(
125+
[],
126+
layer_type="NoGeometry?crs=EPSG:4326&field=place:string(255,0)",
127+
)
128+
for place in ["London", "Birmingham Palace", "Newcastle"]:
129+
feat = QgsFeature()
130+
feat.setFields(input_lyr.fields())
131+
feat.setAttribute("place", place)
132+
input_lyr.dataProvider().addFeature(feat)
133+
134+
results = processing.runAndLoadResults(
135+
"ttp_v4:geocoding",
136+
{
137+
"INPUT_DATA": input_lyr,
138+
"INPUT_THROTTLING_STRATEGY": 0,
139+
"INPUT_QUERY_FIELD": '"place"',
140+
"OUTPUT": "memory:",
141+
"OUTPUT_RESULT_TYPE": GeocodingAlgorithmBase.RESULT_TYPE.index(
142+
"BEST_MATCH"
143+
),
144+
},
145+
)
146+
output_layer = QgsProject.instance().mapLayer(results["OUTPUT"])
147+
self.assertEqual(output_layer.featureCount(), 3)
148+
149+
def test_processing_reverse_geocoding(self):
150+
input_lyr = self._make_layer(
151+
["POINT(0.1 51.5)", "POINT(-0.1 51.5)", "POINT(0.0 51.6)"]
152+
)
153+
results = processing.runAndLoadResults(
154+
"ttp_v4:reverse_geocoding",
155+
{
156+
"INPUT_DATA": input_lyr,
157+
"INPUT_THROTTLING_STRATEGY": 0,
158+
"OUTPUT": "memory:",
159+
"OUTPUT_RESULT_TYPE": GeocodingAlgorithmBase.RESULT_TYPE.index(
160+
"BEST_MATCH"
161+
),
162+
},
163+
)
164+
output_layer = QgsProject.instance().mapLayer(results["OUTPUT"])
165+
self.assertEqual(output_layer.featureCount(), 3)

travel_time_platform_plugin/ui/ConfigDialog.ui

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,6 @@
168168
</property>
169169
</widget>
170170
</item>
171-
<item>
172-
<widget class="QCheckBox" name="showRunTestsButton">
173-
<property name="text">
174-
<string>Show a button to run software tests</string>
175-
</property>
176-
</widget>
177-
</item>
178171
<item>
179172
<widget class="QCheckBox" name="disableHttpsCheckBox">
180173
<property name="text">
@@ -216,6 +209,13 @@
216209
</item>
217210
</layout>
218211
</item>
212+
<item>
213+
<widget class="QCheckBox" name="showRunTestsButton">
214+
<property name="text">
215+
<string>Show a button to run software tests (for developpers only)</string>
216+
</property>
217+
</widget>
218+
</item>
219219
</layout>
220220
</widget>
221221
</item>

travel_time_platform_plugin/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def clone_feature(request, source_layer, output_fields=None):
5353
return new_feature
5454

5555

56-
def log(msg):
57-
QgsMessageLog.logMessage(str(msg), "TimeTravelPlatform")
56+
def log(msg, suffix=""):
57+
QgsMessageLog.logMessage(str(msg), f"TimeTravelPlatform{suffix}")
5858

5959

6060
def tr(string):

0 commit comments

Comments
 (0)