-
Notifications
You must be signed in to change notification settings - Fork 3
NO TICKET fix(importers): prevent duplicate well-name collisions in well inventory and water-level imports #633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -51,7 +51,7 @@ | |||||||||||||||
| from schemas.well_inventory import WellInventoryRow | ||||||||||||||||
| from services.contact_helper import add_contact | ||||||||||||||||
| from services.exceptions_helper import PydanticStyleException | ||||||||||||||||
| from services.thing_helper import add_thing | ||||||||||||||||
| from services.thing_helper import add_thing, find_water_wells_by_name | ||||||||||||||||
| from services.util import transform_srid, convert_ft_to_m | ||||||||||||||||
|
|
||||||||||||||||
| AUTOGEN_DEFAULT_PREFIX = "NM-" | ||||||||||||||||
|
|
@@ -280,7 +280,12 @@ class dialect: | |||||||||||||||
| field = "Database error" | ||||||||||||||||
| else: | ||||||||||||||||
| error_text = str(e) | ||||||||||||||||
| field = _extract_field_from_value_error(error_text) | ||||||||||||||||
| if error_text.startswith( | ||||||||||||||||
| "Well already exists in database for well_name_point_id " | ||||||||||||||||
| ): | ||||||||||||||||
| field = "well_name_point_id" | ||||||||||||||||
| else: | ||||||||||||||||
| field = _extract_field_from_value_error(error_text) | ||||||||||||||||
|
Comment on lines
+283
to
+288
|
||||||||||||||||
| if error_text.startswith( | |
| "Well already exists in database for well_name_point_id " | |
| ): | |
| field = "well_name_point_id" | |
| else: | |
| field = _extract_field_from_value_error(error_text) | |
| field = _extract_field_from_value_error(error_text) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -463,3 +463,65 @@ def test_bulk_upload_water_levels_imports_valid_rows_when_other_rows_fail( | |
| "Unknown well_name_point_id 'Unknown Well'" in message | ||
| for message in result.payload["validation_errors"] | ||
| ) | ||
|
|
||
|
|
||
| def test_bulk_upload_water_levels_reports_duplicate_well_name_matches(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test raises what appears to be an omission in the DB. Shouldn't names be unique? That would prevent this error from even occuring. There's even a
|
||
| with session_ctx() as session: | ||
| well_one = Thing(name="Duplicate Well", thing_type="water well") | ||
| well_two = Thing(name="Duplicate Well", thing_type="water well") | ||
| session.add_all([well_one, well_two]) | ||
| session.commit() | ||
| well_one_id = well_one.id | ||
| well_two_id = well_two.id | ||
|
|
||
| csv_content = "\n".join( | ||
| [ | ||
| ",".join( | ||
| [ | ||
| "field_staff", | ||
| "well_name_point_id", | ||
| "field_event_date_time", | ||
| "measurement_date_time", | ||
| "sampler", | ||
| "sample_method", | ||
| "mp_height", | ||
| "level_status", | ||
| "depth_to_water_ft", | ||
| "data_quality", | ||
| "water_level_notes", | ||
| ] | ||
| ), | ||
| ",".join( | ||
| [ | ||
| "A Lopez", | ||
| "Duplicate Well", | ||
| "2025-02-15T08:00:00-07:00", | ||
| "2025-02-15T10:30:00-07:00", | ||
| "A Lopez", | ||
| "electric tape", | ||
| "1.5", | ||
| "Water level not affected", | ||
| "7.0", | ||
| "Water level accurate to within two hundreths of a foot", | ||
| "Initial measurement", | ||
| ] | ||
| ), | ||
| ] | ||
| ) | ||
|
|
||
| try: | ||
| result = bulk_upload_water_levels(csv_content.encode("utf-8")) | ||
|
|
||
| assert result.exit_code == 1 | ||
| assert result.payload["summary"]["total_rows_processed"] == 1 | ||
| assert result.payload["summary"]["total_rows_imported"] == 0 | ||
| assert result.payload["validation_errors"] == [ | ||
| "Row 1: Multiple wells found for well_name_point_id 'Duplicate Well'" | ||
| ] | ||
| finally: | ||
| with session_ctx() as session: | ||
| for well_id in (well_one_id, well_two_id): | ||
| well = session.get(Thing, well_id) | ||
| if well is not None: | ||
| session.delete(well) | ||
| session.commit() | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -833,6 +833,42 @@ def test_upload_duplicate_well_ids(self): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errors = result.payload.get("validation_errors", []) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert any("Duplicate" in str(e) for e in errors) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_upload_fails_when_well_name_already_exists_in_database(self, tmp_path): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Upload fails when a water well with the same Thing.name already exists.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| row = _minimal_valid_well_inventory_row() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with session_ctx() as session: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| session.add(Thing(name=row["well_name_point_id"], thing_type="water well")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| session.commit() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| file_path = tmp_path / "well-inventory-existing-db-well.csv" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with file_path.open("w", encoding="utf-8", newline="") as f: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writer = csv.DictWriter(f, fieldnames=list(row.keys())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writer.writeheader() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writer.writerow(row) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = well_inventory_csv(file_path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert result.exit_code == 1, result.stderr | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errors = result.payload.get("validation_errors", []) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert errors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert errors[0]["field"] == "well_name_point_id" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errors[0]["error"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| == "Well already exists in database for well_name_point_id 'TEST-0001'" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with session_ctx() as session: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| things = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| session.query(Thing) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Thing.name == row["well_name_point_id"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Thing.thing_type == "water well", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .all() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert len(things) == 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+839
to
+870
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with session_ctx() as session: | |
| session.add(Thing(name=row["well_name_point_id"], thing_type="water well")) | |
| session.commit() | |
| file_path = tmp_path / "well-inventory-existing-db-well.csv" | |
| with file_path.open("w", encoding="utf-8", newline="") as f: | |
| writer = csv.DictWriter(f, fieldnames=list(row.keys())) | |
| writer.writeheader() | |
| writer.writerow(row) | |
| result = well_inventory_csv(file_path) | |
| assert result.exit_code == 1, result.stderr | |
| errors = result.payload.get("validation_errors", []) | |
| assert errors | |
| assert errors[0]["field"] == "well_name_point_id" | |
| assert ( | |
| errors[0]["error"] | |
| == "Well already exists in database for well_name_point_id 'TEST-0001'" | |
| ) | |
| with session_ctx() as session: | |
| things = ( | |
| session.query(Thing) | |
| .filter( | |
| Thing.name == row["well_name_point_id"], | |
| Thing.thing_type == "water well", | |
| ) | |
| .all() | |
| ) | |
| assert len(things) == 1 | |
| thing_name = row["well_name_point_id"] | |
| try: | |
| with session_ctx() as session: | |
| session.add(Thing(name=thing_name, thing_type="water well")) | |
| session.commit() | |
| file_path = tmp_path / "well-inventory-existing-db-well.csv" | |
| with file_path.open("w", encoding="utf-8", newline="") as f: | |
| writer = csv.DictWriter(f, fieldnames=list(row.keys())) | |
| writer.writeheader() | |
| writer.writerow(row) | |
| result = well_inventory_csv(file_path) | |
| assert result.exit_code == 1, result.stderr | |
| errors = result.payload.get("validation_errors", []) | |
| assert errors | |
| assert errors[0]["field"] == "well_name_point_id" | |
| assert ( | |
| errors[0]["error"] | |
| == "Well already exists in database for well_name_point_id 'TEST-0001'" | |
| ) | |
| with session_ctx() as session: | |
| things = ( | |
| session.query(Thing) | |
| .filter( | |
| Thing.name == thing_name, | |
| Thing.thing_type == "water well", | |
| ) | |
| .all() | |
| ) | |
| assert len(things) == 1 | |
| finally: | |
| # Clean up the Thing created for this test to avoid leaking state | |
| with session_ctx() as session: | |
| ( | |
| session.query(Thing) | |
| .filter( | |
| Thing.name == thing_name, | |
| Thing.thing_type == "water well", | |
| ) | |
| .delete(synchronize_session=False) | |
| ) | |
| session.commit() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this string is used in multiple places I would recommend creating a global constant at the beginning and then inserting it into f-strings wherever it is used. This way you don't have to worry about having the text correspond between its different invocations and can easily change it in the future. Something like