-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_batch_query.py
More file actions
437 lines (366 loc) · 15.1 KB
/
Copy pathtest_batch_query.py
File metadata and controls
437 lines (366 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.
"""Tests for batch spatial query service."""
import json
from datetime import date
from odoo.tests.common import TransactionCase
from odoo.tools import mute_logger
# Polygon covering roughly lon 27.9..28.1 / lat -2.1..-1.9 (East Africa).
QUERY_POLYGON = {
"type": "Polygon",
"coordinates": [[[27.9, -2.1], [28.1, -2.1], [28.1, -1.9], [27.9, -1.9], [27.9, -2.1]]],
}
class TestBatchSpatialQueryService(TransactionCase):
"""Test batch spatial query service functionality."""
@classmethod
def setUpClass(cls):
"""Set up test data."""
super().setUpClass()
# Create two test areas with distinct registrants
cls.area_1 = cls.env["spp.area"].create(
{
"draft_name": "Batch Test District 1",
"code": "BATCH-DIST-001",
}
)
cls.area_2 = cls.env["spp.area"].create(
{
"draft_name": "Batch Test District 2",
"code": "BATCH-DIST-002",
}
)
# Registrants in area 1
cls.group_1 = cls.env["res.partner"].create(
{
"name": "Batch Household 1",
"is_registrant": True,
"is_group": True,
"area_id": cls.area_1.id,
}
)
cls.individual_1 = cls.env["res.partner"].create(
{
"name": "Batch Individual 1",
"is_registrant": True,
"is_group": False,
"area_id": cls.area_1.id,
"birthdate": date(1990, 5, 15),
}
)
# Registrants in area 2
cls.group_2 = cls.env["res.partner"].create(
{
"name": "Batch Household 2",
"is_registrant": True,
"is_group": True,
"area_id": cls.area_2.id,
}
)
cls.individual_2 = cls.env["res.partner"].create(
{
"name": "Batch Individual 2",
"is_registrant": True,
"is_group": False,
"area_id": cls.area_2.id,
"birthdate": date(2000, 8, 20),
}
)
def test_batch_query_returns_per_geometry_results(self):
"""Test that batch query returns individual results for each geometry."""
from ..services.spatial_query_service import SpatialQueryService
service = SpatialQueryService(self.env)
# Use the simple geometry format that would be sent from the plugin
geometries = [
{
"id": "zone_1",
"geometry": {
"type": "Polygon",
"coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
},
},
{
"id": "zone_2",
"geometry": {
"type": "Polygon",
"coordinates": [[[2, 2], [3, 2], [3, 3], [2, 3], [2, 2]]],
},
},
]
result = service.query_statistics_batch(
geometries=geometries,
filters=None,
variables=None,
)
# Verify structure
self.assertIn("results", result)
self.assertIn("summary", result)
self.assertEqual(len(result["results"]), 2)
# Each result should have the expected fields including metadata
for item in result["results"]:
self.assertIn("id", item)
self.assertIn("total_count", item)
self.assertIn("query_method", item)
self.assertIn("areas_matched", item)
self.assertIn("statistics", item)
self.assertIn("access_level", item)
self.assertIn("from_cache", item)
self.assertIn("computed_at", item)
# IDs should match request
result_ids = {r["id"] for r in result["results"]}
self.assertEqual(result_ids, {"zone_1", "zone_2"})
def test_batch_query_summary_aggregation(self):
"""Test that batch query summary aggregates results."""
from ..services.spatial_query_service import SpatialQueryService
service = SpatialQueryService(self.env)
geometries = [
{
"id": "area_a",
"geometry": {
"type": "Polygon",
"coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
},
},
]
result = service.query_statistics_batch(geometries=geometries)
summary = result["summary"]
self.assertIn("total_count", summary)
self.assertIn("geometries_queried", summary)
self.assertIn("statistics", summary)
self.assertIn("access_level", summary)
self.assertIn("from_cache", summary)
self.assertIn("computed_at", summary)
self.assertEqual(summary["geometries_queried"], 1)
def test_batch_query_handles_invalid_geometry(self):
"""Test that batch query handles errors for individual geometries."""
from ..services.spatial_query_service import SpatialQueryService
service = SpatialQueryService(self.env)
# Mix valid and invalid geometries
geometries = [
{
"id": "valid",
"geometry": {
"type": "Polygon",
"coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
},
},
{
"id": "invalid",
"geometry": {"type": "InvalidType", "coordinates": []},
},
]
# Should not raise - individual errors are caught
result = service.query_statistics_batch(geometries=geometries)
self.assertEqual(len(result["results"]), 2)
# The invalid geometry should return error/empty result
invalid_result = next(r for r in result["results"] if r["id"] == "invalid")
self.assertEqual(invalid_result["total_count"], 0)
def test_batch_query_with_variables(self):
"""Test batch query passes variables to individual queries."""
from ..services.spatial_query_service import SpatialQueryService
service = SpatialQueryService(self.env)
geometries = [
{
"id": "test",
"geometry": {
"type": "Polygon",
"coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
},
},
]
# Passing nonexistent variables should still return a valid response
result = service.query_statistics_batch(
geometries=geometries,
variables=["nonexistent_var"],
)
self.assertIn("results", result)
self.assertEqual(len(result["results"]), 1)
def test_batch_query_with_filters(self):
"""Test batch query passes filters to individual queries."""
from ..services.spatial_query_service import SpatialQueryService
service = SpatialQueryService(self.env)
geometries = [
{
"id": "filtered",
"geometry": {
"type": "Polygon",
"coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
},
},
]
result = service.query_statistics_batch(
geometries=geometries,
filters={"is_group": True},
)
self.assertIn("results", result)
self.assertEqual(len(result["results"]), 1)
def test_batch_query_empty_geometries_list(self):
"""Test batch query with empty geometries returns empty results."""
from ..services.spatial_query_service import SpatialQueryService
service = SpatialQueryService(self.env)
result = service.query_statistics_batch(geometries=[])
self.assertEqual(len(result["results"]), 0)
self.assertEqual(result["summary"]["total_count"], 0)
self.assertEqual(result["summary"]["geometries_queried"], 0)
class TestBatchQueryDoesNotPoisonLaterGeometries(TransactionCase):
"""A geometry that fails inside PostgreSQL must not abort the rest of the batch.
``query_statistics`` only wraps its coordinate attempt in a savepoint; the
area fallback it calls into runs raw SQL unguarded. If that fallback hits a
genuine database error (not a Python-level ``ValueError``), the whole
transaction is left aborted, and every later geometry in the same batch
call fails too, since even opening a new savepoint requires a live
transaction.
"""
@classmethod
def setUpClass(cls):
"""Create an area covering the query polygon plus a registrant in it."""
super().setUpClass()
cls.area = cls.env["spp.area"].create(
{
"draft_name": "Batch Poison Test Area",
"code": "BATCH-POISON-001",
}
)
cls.env.cr.execute(
"""
UPDATE spp_area
SET geo_polygon = ST_SetSRID(ST_GeomFromGeoJSON(%s), 4326)
WHERE id = %s
""",
[json.dumps(QUERY_POLYGON), cls.area.id],
)
cls.group = cls.env["res.partner"].create(
{
"name": "Batch Poison Test Household",
"is_registrant": True,
"is_group": True,
"area_id": cls.area.id,
}
)
def test_invalid_geometry_does_not_abort_later_geometries(self):
"""A DB-level failure on one geometry must not poison the geometries after it."""
from ..services.spatial_query_service import SpatialQueryService
service = SpatialQueryService(self.env)
geometries = [
{
"id": "invalid",
# Genuinely invalid GeoJSON: ST_GeomFromGeoJSON rejects the type at
# the database level, so this fails the same way a real bad request
# would, rather than being simulated with a mock.
"geometry": {"type": "InvalidType", "coordinates": []},
},
{
"id": "valid",
"geometry": QUERY_POLYGON,
},
]
with mute_logger("odoo.sql_db"):
result = service.query_statistics_batch(geometries=geometries)
self.assertEqual(len(result["results"]), 2)
invalid_result = next(r for r in result["results"] if r["id"] == "invalid")
self.assertEqual(invalid_result["query_method"], "error")
valid_result = next(r for r in result["results"] if r["id"] == "valid")
self.assertEqual(
valid_result["query_method"],
"area_fallback",
"a DB error on an earlier geometry aborted the transaction for the rest of the batch",
)
self.assertEqual(valid_result["total_count"], 1)
self.assertEqual(valid_result["areas_matched"], 1)
class TestBatchSpatialQuerySchemas(TransactionCase):
"""Test batch query Pydantic schemas."""
def test_batch_request_schema(self):
"""Test BatchSpatialQueryRequest accepts valid input."""
from ..schemas.query import BatchSpatialQueryRequest
request = BatchSpatialQueryRequest(
geometries=[
{
"id": "zone_1",
"geometry": {
"type": "Polygon",
"coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
},
}
],
filters={"is_group": True},
variables=["children_under_5"],
)
self.assertEqual(len(request.geometries), 1)
self.assertEqual(request.geometries[0].id, "zone_1")
self.assertEqual(request.filters, {"is_group": True})
self.assertEqual(request.variables, ["children_under_5"])
def test_batch_request_requires_geometries(self):
"""Test that BatchSpatialQueryRequest requires at least one geometry."""
from pydantic import ValidationError
from ..schemas.query import BatchSpatialQueryRequest
with self.assertRaises(ValidationError):
BatchSpatialQueryRequest(geometries=[])
def test_batch_response_schema(self):
"""Test BatchSpatialQueryResponse structure with metadata."""
from ..schemas.query import BatchSpatialQueryResponse
response = BatchSpatialQueryResponse(
results=[
{
"id": "zone_1",
"total_count": 100,
"query_method": "coordinates",
"areas_matched": 2,
"statistics": {"total_households": 50},
"access_level": "aggregate",
"from_cache": False,
"computed_at": "2024-01-01T00:00:00Z",
}
],
summary={
"total_count": 100,
"geometries_queried": 1,
"statistics": {"total_households": 50},
"access_level": "aggregate",
"from_cache": False,
"computed_at": "2024-01-01T00:00:00Z",
},
)
self.assertEqual(len(response.results), 1)
self.assertEqual(response.results[0].id, "zone_1")
self.assertEqual(response.results[0].access_level, "aggregate")
self.assertFalse(response.results[0].from_cache)
self.assertEqual(response.results[0].computed_at, "2024-01-01T00:00:00Z")
self.assertEqual(response.summary.total_count, 100)
self.assertEqual(response.summary.access_level, "aggregate")
def test_geometry_item_schema(self):
"""Test GeometryItem schema."""
from ..schemas.query import GeometryItem
item = GeometryItem(
id="flood_zone_1",
geometry={
"type": "MultiPolygon",
"coordinates": [[[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]],
},
)
self.assertEqual(item.id, "flood_zone_1")
self.assertEqual(item.geometry["type"], "MultiPolygon")
def test_batch_response_backward_compatibility(self):
"""Test that schemas work without metadata fields (backward compatibility)."""
from ..schemas.query import BatchSpatialQueryResponse
# Old-style response without metadata fields (should use defaults)
response = BatchSpatialQueryResponse(
results=[
{
"id": "zone_1",
"total_count": 100,
"query_method": "coordinates",
"areas_matched": 2,
"statistics": {"total_households": 50},
}
],
summary={
"total_count": 100,
"geometries_queried": 1,
"statistics": {"total_households": 50},
},
)
# Metadata fields should have defaults
self.assertIsNone(response.results[0].access_level)
self.assertFalse(response.results[0].from_cache)
self.assertIsNone(response.results[0].computed_at)
self.assertIsNone(response.summary.access_level)
self.assertFalse(response.summary.from_cache)
self.assertIsNone(response.summary.computed_at)