|
14 | 14 | import pytest |
15 | 15 | from cli.service_adapter import well_inventory_csv |
16 | 16 | from core.constants import SRID_UTM_ZONE_13N, SRID_WGS84 |
| 17 | +from core.enums import Role, ContactType |
17 | 18 | from db import ( |
18 | 19 | Base, |
19 | 20 | Location, |
@@ -67,7 +68,7 @@ def isolate_well_inventory_tables(): |
67 | 68 | _reset_well_inventory_tables() |
68 | 69 |
|
69 | 70 |
|
70 | | -def test_well_inventory_db_contents(): |
| 71 | +def test_well_inventory_db_contents_no_waterlevels(): |
71 | 72 | """ |
72 | 73 | Test that the well inventory upload creates the correct database contents. |
73 | 74 |
|
@@ -457,6 +458,77 @@ def test_well_inventory_db_contents(): |
457 | 458 | assert participant.participant.name == file_content["field_staff_2"] |
458 | 459 |
|
459 | 460 |
|
| 461 | +def test_well_inventory_db_contents_with_waterlevels(tmp_path): |
| 462 | + """ |
| 463 | + Tests that the following records are made: |
| 464 | +
|
| 465 | + - field event |
| 466 | + - field activity for well inventory |
| 467 | + - field activity for water level measurement |
| 468 | + - field participants |
| 469 | + - contact |
| 470 | + - location |
| 471 | + - thing |
| 472 | + - sample |
| 473 | + - observation |
| 474 | +
|
| 475 | + """ |
| 476 | + row = _minimal_valid_well_inventory_row() |
| 477 | + row.update( |
| 478 | + { |
| 479 | + "water_level_date_time": "2025-02-15T10:30:00", |
| 480 | + "depth_to_water_ft": "8", |
| 481 | + "sample_method": "Steel-tape measurement", |
| 482 | + "data_quality": "Water level accurate to within two hundreths of a foot", |
| 483 | + "water_level_notes": "Attempted measurement", |
| 484 | + "mp_height_ft": 2.5, |
| 485 | + "level_status": "Water level not affected", |
| 486 | + } |
| 487 | + ) |
| 488 | + file_path = tmp_path / "well-inventory-blank-depth.csv" |
| 489 | + with file_path.open("w", encoding="utf-8", newline="") as f: |
| 490 | + writer = csv.DictWriter(f, fieldnames=list(row.keys())) |
| 491 | + writer.writeheader() |
| 492 | + writer.writerow(row) |
| 493 | + |
| 494 | + result = well_inventory_csv(file_path) |
| 495 | + assert result.exit_code == 0, result.stderr |
| 496 | + |
| 497 | + with session_ctx() as session: |
| 498 | + field_events = session.query(FieldEvent).all() |
| 499 | + field_activities = session.query(FieldActivity).all() |
| 500 | + field_event_participants = session.query(FieldEventParticipant).all() |
| 501 | + contacts = session.query(Contact).all() |
| 502 | + locations = session.query(Location).all() |
| 503 | + things = session.query(Thing).all() |
| 504 | + samples = session.query(Sample).all() |
| 505 | + observations = session.query(Observation).all() |
| 506 | + |
| 507 | + assert len(field_events) == 1 |
| 508 | + assert len(field_activities) == 2 |
| 509 | + activity_types = {fa.activity_type for fa in field_activities} |
| 510 | + assert activity_types == { |
| 511 | + "well inventory", |
| 512 | + "groundwater level", |
| 513 | + }, f"Unexpected activity types: {activity_types}" |
| 514 | + gwl_field_activity = next( |
| 515 | + (fa for fa in field_activities if fa.activity_type == "groundwater level"), |
| 516 | + None, |
| 517 | + ) |
| 518 | + assert gwl_field_activity is not None |
| 519 | + |
| 520 | + assert len(field_event_participants) == 1 |
| 521 | + assert len(contacts) == 1 |
| 522 | + assert len(locations) == 1 |
| 523 | + assert len(things) == 1 |
| 524 | + assert len(samples) == 1 |
| 525 | + sample = samples[0] |
| 526 | + assert sample.field_activity == gwl_field_activity |
| 527 | + assert len(observations) == 1 |
| 528 | + observation = observations[0] |
| 529 | + assert observation.sample == sample |
| 530 | + |
| 531 | + |
460 | 532 | def test_blank_depth_to_water_still_creates_water_level_records(tmp_path): |
461 | 533 | """Blank depth-to-water is treated as missing while preserving the attempted measurement.""" |
462 | 534 | row = _minimal_valid_well_inventory_row() |
@@ -486,9 +558,9 @@ def test_blank_depth_to_water_still_creates_water_level_records(tmp_path): |
486 | 558 |
|
487 | 559 | assert len(samples) == 1 |
488 | 560 | assert len(observations) == 1 |
489 | | - assert samples[0].sample_date == datetime.fromisoformat("2025-02-15T10:30:00") |
| 561 | + assert samples[0].sample_date == datetime.fromisoformat("2025-02-15T10:30:00Z") |
490 | 562 | assert observations[0].observation_datetime == datetime.fromisoformat( |
491 | | - "2025-02-15T10:30:00" |
| 563 | + "2025-02-15T10:30:00Z" |
492 | 564 | ) |
493 | 565 | assert observations[0].value is None |
494 | 566 | assert observations[0].measuring_point_height == 2.5 |
@@ -779,8 +851,8 @@ def test_make_contact_with_full_info(self): |
779 | 851 | model.contact_special_requests_notes = "Call before visiting" |
780 | 852 | model.contact_1_name = "John Doe" |
781 | 853 | model.contact_1_organization = "Test Org" |
782 | | - model.contact_1_role = "Owner" |
783 | | - model.contact_1_type = "Primary" |
| 854 | + model.contact_1_role = Role.Owner |
| 855 | + model.contact_1_type = ContactType.Primary |
784 | 856 | model.contact_1_email_1 = "john@example.com" |
785 | 857 | model.contact_1_email_1_type = "Work" |
786 | 858 | model.contact_1_email_2 = None |
@@ -866,8 +938,8 @@ def test_make_contact_with_organization_only(self): |
866 | 938 | model.contact_special_requests_notes = None |
867 | 939 | model.contact_1_name = None |
868 | 940 | model.contact_1_organization = "Test Org" |
869 | | - model.contact_1_role = None |
870 | | - model.contact_1_type = None |
| 941 | + model.contact_1_role = Role.Owner |
| 942 | + model.contact_1_type = ContactType.Primary |
871 | 943 | model.contact_1_email_1 = None |
872 | 944 | model.contact_1_email_1_type = None |
873 | 945 | model.contact_1_email_2 = None |
@@ -1094,7 +1166,7 @@ def test_water_level_aliases_are_mapped(self): |
1094 | 1166 | "sample_method": "Steel-tape measurement", |
1095 | 1167 | "water_level_date_time": "2025-02-15T10:30:00", |
1096 | 1168 | "mp_height_ft": 2.5, |
1097 | | - "level_status": "Static", |
| 1169 | + "level_status": "Other conditions exist that would affect the level (remarks)", |
1098 | 1170 | "depth_to_water_ft": 11.2, |
1099 | 1171 | "data_quality": "Water level accurate to within two hundreths of a foot", |
1100 | 1172 | "water_level_notes": "Initial reading", |
@@ -1131,6 +1203,8 @@ def test_blank_contact_organization_is_treated_as_none(self): |
1131 | 1203 | row = _minimal_valid_well_inventory_row() |
1132 | 1204 | row["contact_1_name"] = "Test Contact" |
1133 | 1205 | row["contact_1_organization"] = "" |
| 1206 | + row["contact_1_role"] = "Owner" |
| 1207 | + row["contact_1_type"] = "Primary" |
1134 | 1208 |
|
1135 | 1209 | model = WellInventoryRow(**row) |
1136 | 1210 |
|
|
0 commit comments