Skip to content

Commit adcdeb0

Browse files
committed
feat: enhance test setup with location and measuring point creation for wells
1 parent aa19373 commit adcdeb0

7 files changed

Lines changed: 196 additions & 111 deletions

File tree

db/thing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ def measuring_point_height(self) -> int | None:
423423
Since measuring_point_history is eagerly loaded, this should not introduce N+1 query issues.
424424
"""
425425
if self.thing_type == "water well":
426+
if not self.measuring_points:
427+
return None
426428
sorted_measuring_point_history = sorted(
427429
self.measuring_points, key=lambda x: x.start_date, reverse=True
428430
)
@@ -439,6 +441,8 @@ def measuring_point_description(self) -> str | None:
439441
Since measuring_point_history is eagerly loaded, this should not introduce N+1 query issues.
440442
"""
441443
if self.thing_type == "water well":
444+
if not self.measuring_points:
445+
return None
442446
sorted_measuring_point_history = sorted(
443447
self.measuring_points, key=lambda x: x.start_date, reverse=True
444448
)

tests/conftest.py

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
import os
2-
import uuid
32

43
import pytest
54
from alembic import command
65
from alembic.config import Config
7-
from dotenv import load_dotenv
8-
from sqlalchemy import text
9-
from sqlalchemy_utils import TSVectorType
10-
from sqlalchemy_searchable import sync_trigger
11-
126
from core.initializers import init_lexicon, init_parameter
137
from db import *
148
from db.engine import session_ctx
9+
from dotenv import load_dotenv
10+
from sqlalchemy_searchable import sync_trigger
1511
from tests import get_parameter_id
1612

1713

1814
def pytest_configure():
1915
load_dotenv(override=True)
20-
os.environ.setdefault("POSTGRES_PORT", "54321")
16+
os.environ["POSTGRES_PORT"] = "54321"
2117

2218

2319
def _alembic_config() -> Config:
@@ -72,9 +68,54 @@ def _setup_test_db():
7268
_sync_search_vectors()
7369
init_lexicon()
7470
init_parameter()
71+
_ensure_locations_for_things()
72+
_ensure_measuring_points_for_wells()
7573
yield
7674

7775

76+
def _ensure_locations_for_things() -> None:
77+
with session_ctx() as session:
78+
things = session.query(Thing).all()
79+
for thing in things:
80+
if thing.location_associations:
81+
continue
82+
location = Location(
83+
point="POINT(-107.0 33.0)",
84+
elevation=0,
85+
release_status=thing.release_status,
86+
)
87+
session.add(location)
88+
session.flush()
89+
session.add(
90+
LocationThingAssociation(
91+
location_id=location.id,
92+
thing_id=thing.id,
93+
effective_start=date.today().isoformat(),
94+
)
95+
)
96+
session.commit()
97+
98+
99+
def _ensure_measuring_points_for_wells() -> None:
100+
with session_ctx() as session:
101+
wells = session.query(Thing).filter(Thing.thing_type == "water well").all()
102+
missing = [well for well in wells if not well.measuring_points]
103+
for well in missing:
104+
session.add(
105+
MeasuringPointHistory(
106+
thing_id=well.id,
107+
measuring_point_height=well.well_casing_depth
108+
or well.well_depth
109+
or 0.0,
110+
measuring_point_description="autogenerated for tests",
111+
start_date=date.today(),
112+
release_status=well.release_status,
113+
)
114+
)
115+
if missing:
116+
session.commit()
117+
118+
78119
@pytest.fixture()
79120
def location():
80121
with session_ctx() as session:

tests/test_geospatial.py

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
# ===============================================================================
16+
from datetime import date
1617
from pathlib import Path
1718

1819
import pytest
19-
from geoalchemy2 import functions as geofunc
20-
2120
from core.constants import SRID_WGS84
2221
from core.dependencies import (
2322
admin_function,
@@ -29,6 +28,7 @@
2928
)
3029
from db import Thing, Location, LocationThingAssociation, Group, MeasuringPointHistory
3130
from db.engine import session_ctx
31+
from geoalchemy2 import functions as geofunc
3232
from main import app
3333
from tests import client, override_authentication
3434

@@ -68,46 +68,16 @@ def override_authentication_dependency_fixture():
6868
@pytest.fixture(autouse=True, scope="module")
6969
def populate():
7070
with session_ctx() as session:
71-
# Create some sample data
72-
thing1 = Thing(name="Thing 1", thing_type="water well")
73-
thing2 = Thing(name="Thing 2", thing_type="water well")
74-
session.add(thing1)
75-
session.add(thing2)
76-
77-
session.commit()
78-
79-
mp_history_1 = MeasuringPointHistory(
80-
thing_id=thing1.id,
81-
measuring_point_height=5.0,
82-
measuring_point_description="MP for Thing 1",
83-
start_date="2023-01-01",
84-
reason="Initial entry",
85-
)
86-
mp_history_2 = MeasuringPointHistory(
87-
thing_id=thing2.id,
88-
measuring_point_height=10.0,
89-
measuring_point_description="MP for Thing 2",
90-
start_date="2023-01-01",
91-
reason="Initial entry",
92-
)
93-
session.add(mp_history_1)
94-
session.add(mp_history_2)
95-
96-
loc1 = Location(
97-
# name="Test Location 1",
98-
point=geofunc.ST_GeomFromText("POINT(10.1 10.1)", srid=SRID_WGS84),
99-
elevation=0,
71+
thing1 = _create_well_with_location(
72+
session,
73+
name="Thing 1",
74+
point_wkt="POINT(10.1 10.1)",
10075
)
101-
loc2 = Location(
102-
# name="Test Location 2",
103-
point=geofunc.ST_GeomFromText("POINT(20 20)", srid=SRID_WGS84),
104-
elevation=0,
76+
thing2 = _create_well_with_location(
77+
session,
78+
name="Thing 2",
79+
point_wkt="POINT(20 20)",
10580
)
106-
session.add(loc1)
107-
session.add(loc2)
108-
109-
session.add(LocationThingAssociation(location=loc1, thing=thing1))
110-
session.add(LocationThingAssociation(location=loc2, thing=thing2))
11181

11282
group = Group(
11383
name="Test Group Foo",
@@ -117,15 +87,48 @@ def populate():
11787

11888
session.add(group)
11989
session.commit()
120-
yield
121-
122-
# Cleanup
123-
session.delete(loc1)
124-
session.delete(loc2)
125-
session.delete(group)
126-
session.delete(thing1)
127-
session.delete(thing2)
128-
session.commit()
90+
try:
91+
yield
92+
finally:
93+
session.delete(group)
94+
session.delete(thing1)
95+
session.delete(thing2)
96+
session.commit()
97+
98+
99+
def _create_well_with_location(session, name: str, point_wkt: str) -> Thing:
100+
thing = Thing(name=name, thing_type="water well", release_status="draft")
101+
session.add(thing)
102+
session.commit()
103+
session.refresh(thing)
104+
105+
location = Location(
106+
point=geofunc.ST_GeomFromText(point_wkt, srid=SRID_WGS84),
107+
elevation=0,
108+
release_status="draft",
109+
)
110+
session.add(location)
111+
session.flush()
112+
session.add(
113+
LocationThingAssociation(
114+
location_id=location.id,
115+
thing_id=thing.id,
116+
effective_start=date.today().isoformat(),
117+
)
118+
)
119+
session.add(
120+
MeasuringPointHistory(
121+
thing_id=thing.id,
122+
measuring_point_height=5.0,
123+
measuring_point_description=f"MP for {name}",
124+
start_date=date.today(),
125+
release_status="draft",
126+
)
127+
)
128+
session.commit()
129+
return thing
130+
131+
# Cleanup
129132

130133

131134
def test_get_project_area():

tests/test_location.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
from datetime import timezone
1717

1818
import pytest
19-
from geoalchemy2.shape import to_shape
20-
2119
from core.dependencies import admin_function, editor_function, viewer_function
2220
from db import Location
21+
from geoalchemy2.shape import to_shape
2322
from main import app
2423
from schemas import DT_FMT
2524
from tests import (
@@ -159,28 +158,28 @@ def test_get_locations(location):
159158
response = client.get("/location")
160159
assert response.status_code == 200
161160
data = response.json()
162-
assert data["total"] == 1
163-
assert data["items"][0]["id"] == location.id
164-
assert data["items"][0]["created_at"] == location.created_at.astimezone(
165-
timezone.utc
166-
).strftime(DT_FMT)
161+
assert data["total"] >= 1
162+
item = next(item for item in data["items"] if item["id"] == location.id)
163+
assert item["created_at"] == location.created_at.astimezone(timezone.utc).strftime(
164+
DT_FMT
165+
)
167166
# assert data["items"][0]["name"] == location.name
168-
assert isinstance(data["items"][0]["notes"], list)
167+
assert isinstance(item["notes"], list)
169168
# If you know the exact number of notes expected:
170169
# assert len(data["items"][0]["notes"]) == expected_count
171170
# If you want to check content of a specific note:
172171
# if data["items"][0]["notes"]:
173172
# assert data["items"][0]["notes"][0]["content"] == expected_content
174-
assert data["items"][0]["point"] == to_shape(location.point).wkt
175-
assert data["items"][0]["elevation"] == location.elevation
176-
assert data["items"][0]["release_status"] == location.release_status
173+
assert item["point"] == to_shape(location.point).wkt
174+
assert item["elevation"] == location.elevation
175+
assert item["release_status"] == location.release_status
177176
# assert data["items"][0]["elevation_accuracy"] == location.elevation_accuracy
178177
# assert data["items"][0]["elevation_method"] == location.elevation_method
179178
# assert data["items"][0]["coordinate_accuracy"] == location.coordinate_accuracy
180179
# assert data["items"][0]["coordinate_method"] == location.coordinate_method
181-
assert data["items"][0]["state"] == location.state
182-
assert data["items"][0]["county"] == location.county
183-
assert data["items"][0]["quad_name"] == location.quad_name
180+
assert item["state"] == location.state
181+
assert item["county"] == location.county
182+
assert item["quad_name"] == location.quad_name
184183

185184

186185
def test_get_location_by_id(location):

0 commit comments

Comments
 (0)