Skip to content

Commit 6843d60

Browse files
committed
Add test_search_templates unit test
1 parent 2dd8889 commit 6843d60

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

tests/trgm_search_tests.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,50 @@ def test_search(self):
147147
self.assertEqual(len(data["result_counts"]), 1)
148148
self.assertEqual(data["result_counts"][0]["count"], -1)
149149
self.assertEqual(data["result_counts"][0]["filterword"], "Test")
150+
151+
def test_search_templates(self):
152+
# Test API with Trigram backend and dummy templated SQL queries
153+
os.environ["SEARCH_BACKEND"] = "trgm"
154+
os.environ["TRGM_FEATURE_QUERY_TEMPLATE"] = "UNION ALL".join(list(map(lambda x: """
155+
SELECT
156+
'{{searchtext}}' AS display,
157+
%d AS feature_id,
158+
'test_dataset' AS facet_id,
159+
'test_dataset_id' AS id_field_name,
160+
TRUE AS id_in_quotes,
161+
'[-180,-90,180,90]' AS bbox,
162+
'EPSG:4326' AS srid
163+
""" % x[0], enumerate([None] * 20))))
164+
165+
os.environ["TRGM_LAYER_QUERY_TEMPLATE"] = """
166+
SELECT
167+
'{{searchtext}}' AS display,
168+
'test_dataproduct' AS dataproduct_id,
169+
True AS dset_info,
170+
'foreground' AS stacktype,
171+
'[{"dataproduct_id": "test_dataproduct_sublayer", "display": "Test sublayer", "dset_info": true}]' AS sublayers
172+
"""
173+
os.environ["SEARCH_RESULT_LIMIT"] = "10"
174+
175+
# Test result returned for matching filter
176+
status_code, json_data = self.get(
177+
'/fts/?filter=test_dataset,foreground&searchtext=searchstring\'$')
178+
data = json.loads(json_data)
179+
self.assertEqual(200, status_code, "Status code is not OK")
180+
self.assertEqual(len(data["results"]), 11)
181+
182+
self.assertEqual(self.feature_result_count(data), 20)
183+
self.assertEqual(self.layer_result_count(data), 1)
184+
self.assertEqual(len(data["result_counts"]), 2)
185+
self.assertEqual(len(list(filter(lambda rc: rc["filterword"] == "Test", data["result_counts"]))), 1)
186+
self.assertEqual(len(list(filter(lambda rc: rc["filterword"] == "Map", data["result_counts"]))), 1)
187+
188+
features = list(filter(lambda result: result.get("feature", None), data["results"]))
189+
self.assertEqual(len(features), 10)
190+
feature = features[0]["feature"]
191+
self.assertEqual(feature['display'], 'searchstring\'$')
192+
193+
dataproducts = list(filter(lambda result: result.get("dataproduct", None), data["results"]))
194+
self.assertEqual(len(dataproducts), 1)
195+
dataproduct = dataproducts[0]["dataproduct"]
196+
self.assertEqual(dataproduct['display'], 'searchstring\'$')

0 commit comments

Comments
 (0)