Skip to content

Commit dbdfea2

Browse files
committed
Stabilize shared-db API tests
1 parent 6f09392 commit dbdfea2

File tree

5 files changed

+88
-83
lines changed

5 files changed

+88
-83
lines changed

tests/test_location.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -161,32 +161,33 @@ def test_get_locations(location):
161161
response = client.get("/location")
162162
assert response.status_code == 200
163163
data = response.json()
164-
assert data["total"] == 1
165-
assert data["items"][0]["id"] == location.id
166-
assert data["items"][0]["created_at"] == location.created_at.astimezone(
167-
timezone.utc
168-
).strftime(DT_FMT)
164+
assert data["total"] >= 1
165+
item = next((item for item in data["items"] if item["id"] == location.id), None)
166+
assert item is not None
167+
assert item["created_at"] == location.created_at.astimezone(timezone.utc).strftime(
168+
DT_FMT
169+
)
169170
# assert data["items"][0]["name"] == location.name
170-
assert isinstance(data["items"][0]["notes"], list)
171+
assert isinstance(item["notes"], list)
171172
# If you know the exact number of notes expected:
172173
# assert len(data["items"][0]["notes"]) == expected_count
173174
# If you want to check content of a specific note:
174175
# if data["items"][0]["notes"]:
175176
# assert data["items"][0]["notes"][0]["content"] == expected_content
176-
assert data["items"][0]["point"] == to_shape(location.point).wkt
177-
assert data["items"][0]["elevation"] == location.elevation
178-
assert data["items"][0]["release_status"] == location.release_status
179-
assert "nma_location_notes" in data["items"][0]
180-
assert data["items"][0]["nma_location_notes"] == location.nma_location_notes
181-
assert "nma_data_reliability" in data["items"][0]
182-
assert data["items"][0]["nma_data_reliability"] == location.nma_data_reliability
177+
assert item["point"] == to_shape(location.point).wkt
178+
assert item["elevation"] == location.elevation
179+
assert item["release_status"] == location.release_status
180+
assert "nma_location_notes" in item
181+
assert item["nma_location_notes"] == location.nma_location_notes
182+
assert "nma_data_reliability" in item
183+
assert item["nma_data_reliability"] == location.nma_data_reliability
183184
# assert data["items"][0]["elevation_accuracy"] == location.elevation_accuracy
184185
# assert data["items"][0]["elevation_method"] == location.elevation_method
185186
# assert data["items"][0]["coordinate_accuracy"] == location.coordinate_accuracy
186187
# assert data["items"][0]["coordinate_method"] == location.coordinate_method
187-
assert data["items"][0]["state"] == location.state
188-
assert data["items"][0]["county"] == location.county
189-
assert data["items"][0]["quad_name"] == location.quad_name
188+
assert item["state"] == location.state
189+
assert item["county"] == location.county
190+
assert item["quad_name"] == location.quad_name
190191

191192

192193
def test_get_location_by_id(location):

tests/test_sample.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,11 @@ def test_get_samples(water_chemistry_sample, groundwater_level_sample):
285285
response = client.get("/sample")
286286
assert response.status_code == 200
287287
data = response.json()
288-
assert len(data["items"]) == 2
288+
assert len(data["items"]) >= 2
289+
290+
item_ids = {item["id"] for item in data["items"]}
291+
assert water_chemistry_sample.id in item_ids
292+
assert groundwater_level_sample.id in item_ids
289293

290294
for item in data["items"]:
291295
assert "id" in item
@@ -312,7 +316,7 @@ def test_get_samples_by_thing_id(
312316
response = client.get(f"/sample?thing_id={water_well_thing.id}")
313317
assert response.status_code == 200
314318
data = response.json()
315-
assert data["total"] == 2
319+
assert data["total"] >= 2
316320

317321
data_ids = [d["id"] for d in data["items"]]
318322
sorted_data_ids = sorted(data_ids)

tests/test_sensor.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,21 @@ def test_get_sensors(sensor):
175175
response = client.get("/sensor")
176176
assert response.status_code == 200
177177
data = response.json()
178-
assert data["total"] == 1
179-
assert data["items"][0]["id"] == sensor.id
180-
assert data["items"][0]["created_at"] == sensor.created_at.astimezone(
181-
timezone.utc
182-
).strftime(DT_FMT)
183-
assert data["items"][0]["release_status"] == sensor.release_status
184-
assert data["items"][0]["name"] == sensor.name
185-
assert data["items"][0]["sensor_type"] == sensor.sensor_type
186-
assert data["items"][0]["model"] == sensor.model
187-
assert data["items"][0]["serial_no"] == sensor.serial_no
188-
assert data["items"][0]["pcn_number"] == sensor.pcn_number
189-
assert data["items"][0]["owner_agency"] == sensor.owner_agency
190-
assert data["items"][0]["sensor_status"] == sensor.sensor_status
191-
assert data["items"][0]["notes"] == sensor.notes
178+
assert data["total"] >= 1
179+
item = next((item for item in data["items"] if item["id"] == sensor.id), None)
180+
assert item is not None
181+
assert item["created_at"] == sensor.created_at.astimezone(timezone.utc).strftime(
182+
DT_FMT
183+
)
184+
assert item["release_status"] == sensor.release_status
185+
assert item["name"] == sensor.name
186+
assert item["sensor_type"] == sensor.sensor_type
187+
assert item["model"] == sensor.model
188+
assert item["serial_no"] == sensor.serial_no
189+
assert item["pcn_number"] == sensor.pcn_number
190+
assert item["owner_agency"] == sensor.owner_agency
191+
assert item["sensor_status"] == sensor.sensor_status
192+
assert item["notes"] == sensor.notes
192193

193194

194195
def test_get_sensors_by_thing_id(
@@ -219,20 +220,21 @@ def test_get_sensors_by_parameter_id(sensor, groundwater_level_observation):
219220
response = client.get(f"/sensor?parameter_id={_groundwater_level_parameter_id()}")
220221
assert response.status_code == 200
221222
data = response.json()
222-
assert data["total"] == 1
223-
assert data["items"][0]["id"] == sensor.id
224-
assert data["items"][0]["created_at"] == sensor.created_at.astimezone(
225-
timezone.utc
226-
).strftime(DT_FMT)
227-
assert data["items"][0]["release_status"] == sensor.release_status
228-
assert data["items"][0]["name"] == sensor.name
229-
assert data["items"][0]["sensor_type"] == sensor.sensor_type
230-
assert data["items"][0]["model"] == sensor.model
231-
assert data["items"][0]["serial_no"] == sensor.serial_no
232-
assert data["items"][0]["pcn_number"] == sensor.pcn_number
233-
assert data["items"][0]["owner_agency"] == sensor.owner_agency
234-
assert data["items"][0]["sensor_status"] == sensor.sensor_status
235-
assert data["items"][0]["notes"] == sensor.notes
223+
assert data["total"] >= 1
224+
item = next((item for item in data["items"] if item["id"] == sensor.id), None)
225+
assert item is not None
226+
assert item["created_at"] == sensor.created_at.astimezone(timezone.utc).strftime(
227+
DT_FMT
228+
)
229+
assert item["release_status"] == sensor.release_status
230+
assert item["name"] == sensor.name
231+
assert item["sensor_type"] == sensor.sensor_type
232+
assert item["model"] == sensor.model
233+
assert item["serial_no"] == sensor.serial_no
234+
assert item["pcn_number"] == sensor.pcn_number
235+
assert item["owner_agency"] == sensor.owner_agency
236+
assert item["sensor_status"] == sensor.sensor_status
237+
assert item["notes"] == sensor.notes
236238

237239

238240
def test_get_sensor_by_id(sensor):

tests/test_thing.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -731,19 +731,19 @@ def test_get_thing_id_links(thing_id_link):
731731
response = client.get("/thing/id-link")
732732
assert response.status_code == 200
733733
data = response.json()
734-
assert data["total"] == 1
735-
assert data["items"][0]["id"] == thing_id_link.id
736-
assert data["items"][0]["created_at"] == thing_id_link.created_at.astimezone(
734+
assert data["total"] >= 1
735+
item = next(
736+
(item for item in data["items"] if item["id"] == thing_id_link.id), None
737+
)
738+
assert item is not None
739+
assert item["created_at"] == thing_id_link.created_at.astimezone(
737740
timezone.utc
738741
).strftime(DT_FMT)
739-
assert data["items"][0]["release_status"] == thing_id_link.release_status
740-
assert data["items"][0]["thing_id"] == thing_id_link.thing_id
741-
assert data["items"][0]["relation"] == thing_id_link.relation
742-
assert data["items"][0]["alternate_id"] == thing_id_link.alternate_id
743-
assert (
744-
data["items"][0]["alternate_organization"]
745-
== thing_id_link.alternate_organization
746-
)
742+
assert item["release_status"] == thing_id_link.release_status
743+
assert item["thing_id"] == thing_id_link.thing_id
744+
assert item["relation"] == thing_id_link.relation
745+
assert item["alternate_id"] == thing_id_link.alternate_id
746+
assert item["alternate_organization"] == thing_id_link.alternate_organization
747747

748748

749749
def test_get_thing_id_link_by_id(thing_id_link):
@@ -797,11 +797,11 @@ def test_get_things(water_well_thing, spring_thing, location):
797797
response = client.get("/thing")
798798
assert response.status_code == 200
799799

800-
expected_location = LocationResponse.model_validate(location).model_dump()
801-
# created_at is already serialized to UTC format by UTCAwareDatetime
802-
803800
data = response.json()
804-
assert data["total"] == 2
801+
assert data["total"] >= 2
802+
item_ids = {item["id"] for item in data["items"]}
803+
assert water_well_thing.id in item_ids
804+
assert spring_thing.id in item_ids
805805

806806

807807
@pytest.mark.skip("Needs to be updated per changes made from feature files")

tests/test_well_inventory.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from cli.service_adapter import well_inventory_csv
1616
from core.constants import SRID_UTM_ZONE_13N, SRID_WGS84
1717
from db import (
18+
Base,
1819
Location,
1920
LocationThingAssociation,
2021
Thing,
@@ -29,6 +30,24 @@
2930
from shapely import Point
3031

3132

33+
def _reset_well_inventory_tables() -> None:
34+
with session_ctx() as session:
35+
for table in reversed(Base.metadata.sorted_tables):
36+
if table.name in ("alembic_version", "parameter"):
37+
continue
38+
if table.name.startswith("lexicon"):
39+
continue
40+
session.execute(table.delete())
41+
session.commit()
42+
43+
44+
@pytest.fixture(autouse=True)
45+
def isolate_well_inventory_tables():
46+
_reset_well_inventory_tables()
47+
yield
48+
_reset_well_inventory_tables()
49+
50+
3251
def test_well_inventory_db_contents():
3352
"""
3453
Test that the well inventory upload creates the correct database contents.
@@ -417,17 +436,6 @@ def test_well_inventory_db_contents():
417436
else:
418437
assert participant.participant.name == file_content["field_staff_2"]
419438

420-
# CLEAN UP THE DATABASE AFTER TESTING
421-
session.query(FieldEventParticipant).delete()
422-
session.query(FieldActivity).delete()
423-
session.query(FieldEvent).delete()
424-
session.query(ThingContactAssociation).delete()
425-
session.query(LocationThingAssociation).delete()
426-
session.query(Contact).delete()
427-
session.query(Location).delete()
428-
session.query(Thing).delete()
429-
session.commit()
430-
431439

432440
# =============================================================================
433441
# Error Handling Tests - Cover API error paths
@@ -582,7 +590,7 @@ def test_upload_missing_contact_type(self):
582590
result = well_inventory_csv(file_path)
583591
assert result.exit_code == 1
584592

585-
def test_upload_missing_contact_type(self):
593+
def test_upload_missing_contact_role(self):
586594
"""Upload fails when contact is provided without role."""
587595
file_path = Path("tests/features/data/well-inventory-missing-contact-role.csv")
588596
if file_path.exists():
@@ -966,15 +974,5 @@ def test_upload_valid_with_comma_in_quotes(self):
966974
# Should succeed - commas in quoted fields are valid CSV
967975
assert result.exit_code in (0, 1) # 1 if other validation fails
968976

969-
# Clean up if records were created
970-
if result.exit_code == 0:
971-
with session_ctx() as session:
972-
session.query(Thing).delete()
973-
session.query(Location).delete()
974-
session.query(Contact).delete()
975-
session.query(FieldEvent).delete()
976-
session.query(FieldActivity).delete()
977-
session.commit()
978-
979977

980978
# ============= EOF =============================================

0 commit comments

Comments
 (0)