Skip to content

Commit f482b5a

Browse files
committed
fix(well-inventory): normalize "Complete" monitoring frequency to "Not currently monitored"
- Treat source monitoring_frequency value Complete as no monitoring frequency - Map Complete rows to monitoring_status = Not currently monitored - Add schema and import regression coverage for the normalization - Add unit and BDD coverage for the normalization behavior
1 parent 0ec4da9 commit f482b5a

5 files changed

Lines changed: 84 additions & 0 deletions

File tree

schemas/well_inventory.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,25 @@ class WellInventoryRow(BaseModel):
342342
data_quality: DataQualityField = None
343343
water_level_notes: Optional[str] = None
344344

345+
@model_validator(mode="before")
346+
@classmethod
347+
def normalize_complete_monitoring_frequency(cls, data):
348+
"""Normalize `Complete` monitoring_frequency by clearing monitoring_frequency and setting monitoring_status to `Not currently monitored`."""
349+
if not isinstance(data, dict):
350+
return data
351+
352+
monitoring_frequency = data.get("monitoring_frequency")
353+
if (
354+
isinstance(monitoring_frequency, str)
355+
and monitoring_frequency.strip().lower() == "complete"
356+
):
357+
normalized = dict(data)
358+
normalized["monitoring_frequency"] = None
359+
normalized["monitoring_status"] = "Not currently monitored"
360+
return normalized
361+
362+
return data
363+
345364
@field_validator("date_time", mode="before")
346365
def make_date_time_tz_aware(cls, v):
347366
if isinstance(v, str):

tests/features/steps/well-inventory-csv-given.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,14 @@ def step_given_row_contains_invalid_monitoring_status_value(context: Context):
465465
_set_content_from_df(context, df)
466466

467467

468+
@given('my CSV file contains a row with monitoring_frequency set to "Complete"')
469+
def step_given_row_contains_complete_monitoring_frequency(context: Context):
470+
df = _get_valid_df(context)
471+
df.loc[0, "monitoring_frequency"] = "Complete"
472+
context.complete_monitoring_frequency_well_id = df.loc[0, "well_name_point_id"]
473+
_set_content_from_df(context, df)
474+
475+
468476
@given(
469477
'my CSV file contains a row with a well_pump_type value that is not one of: "Submersible", "Jet", "Line Shaft", "Hand"'
470478
)

tests/features/steps/well-inventory-csv.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from behave import given, when, then
77
from behave.runner import Context
88
from cli.service_adapter import well_inventory_csv
9+
from db import Thing
910
from db.engine import session_ctx
1011
from db.lexicon import LexiconCategory
1112
from services.util import convert_dt_tz_naive_to_tz_aware
@@ -415,3 +416,20 @@ def step_then_all_wells_are_imported_with_system_generated_unique_well_name(
415416
assert len(well_ids) == len(
416417
set(well_ids)
417418
), "Expected unique well_name_point_id values"
419+
420+
421+
@then(
422+
'the imported well with monitoring_frequency "Complete" is marked not currently monitored'
423+
)
424+
def step_then_complete_monitoring_frequency_maps_to_not_currently_monitored(
425+
context: Context,
426+
):
427+
with session_ctx() as session:
428+
thing = session.scalars(
429+
select(Thing).where(
430+
Thing.name == context.complete_monitoring_frequency_well_id
431+
)
432+
).one()
433+
434+
assert thing.monitoring_status == "Not currently monitored"
435+
assert thing.monitoring_frequencies == []

tests/features/well-inventory-csv.feature

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ Feature: Bulk upload well inventory from CSV via CLI
182182
Then the command exits with code 0
183183
And all wells are imported
184184

185+
@positive @validation @BDMS-TBD
186+
Scenario: Upload treats Complete monitoring_frequency as not currently monitored
187+
Given my CSV file contains a row with monitoring_frequency set to "Complete"
188+
When I run the well inventory bulk upload command
189+
Then the command exits with code 0
190+
And all wells are imported
191+
And the imported well with monitoring_frequency "Complete" is marked not currently monitored
192+
185193
@positive @validation @autogenerate_ids @BDMS-TBD
186194
Scenario: Upload succeeds and system auto-generates well_name_point_id for uppercase prefix placeholders and blanks
187195
Given my CSV file contains all valid columns but uses uppercase "-xxxx" placeholders and blank values for well_name_point_id

tests/test_well_inventory.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,28 @@ def test_failed_project_rows_do_not_create_empty_group(tmp_path):
760760
assert group is None
761761

762762

763+
def test_complete_monitoring_frequency_sets_not_currently_monitored_without_frequency(
764+
tmp_path,
765+
):
766+
row = _minimal_valid_well_inventory_row()
767+
row["monitoring_frequency"] = "Complete"
768+
769+
file_path = tmp_path / "well-inventory-complete-monitoring-frequency.csv"
770+
with file_path.open("w", encoding="utf-8", newline="") as f:
771+
writer = csv.DictWriter(f, fieldnames=list(row.keys()))
772+
writer.writeheader()
773+
writer.writerow(row)
774+
775+
result = well_inventory_csv(file_path)
776+
assert result.exit_code == 0, result.stderr
777+
778+
with session_ctx() as session:
779+
thing = session.query(Thing).one()
780+
781+
assert thing.monitoring_status == "Not currently monitored"
782+
assert thing.monitoring_frequencies == []
783+
784+
763785
# =============================================================================
764786
# Error Handling Tests - Cover API error paths
765787
# =============================================================================
@@ -1388,6 +1410,15 @@ def test_blank_well_status_is_treated_as_none(self):
13881410

13891411
assert model.well_status is None
13901412

1413+
def test_complete_monitoring_frequency_is_normalized(self):
1414+
row = _minimal_valid_well_inventory_row()
1415+
row["monitoring_frequency"] = "Complete"
1416+
1417+
model = WellInventoryRow(**row)
1418+
1419+
assert model.monitoring_frequency is None
1420+
assert model.monitoring_status.value == "Not currently monitored"
1421+
13911422
def test_whitespace_only_well_status_is_treated_as_none(self):
13921423
row = _minimal_valid_well_inventory_row()
13931424
row["well_hole_status"] = " "

0 commit comments

Comments
 (0)