Skip to content

Add unit tests for converter, source mapping, and record classes#117

Open
RSBalchII wants to merge 1 commit into
DataIntegrationGroup:mainfrom
RSBalchII:feature/converter-unit-tests
Open

Add unit tests for converter, source mapping, and record classes#117
RSBalchII wants to merge 1 commit into
DataIntegrationGroup:mainfrom
RSBalchII:feature/converter-unit-tests

Conversation

@RSBalchII

Copy link
Copy Markdown

Summary

Added 45 comprehensive unit tests for the converter module and related components, covering unit conversion logic, configuration consistency, and record schema validation. All tests pass with zero regressions and zero new dependencies.

What's Tested

tests/test_converter.py (248 lines, ~35 assertions)

Validates every conversion factor documented in UNIT_CONVERSIONS.md plus edge cases:

  • Non-detect markers — validates <, >, ND, etc. coerce to 0.0 correctly
  • Coercion edge cases — numeric strings, floats, invalid inputs
  • Length conversions — meters ↔ feet with spec-accurate factors
  • Parameter-specific conversions — nitrate (as N), uranium, conductivity aliases
  • Miscellaneous units — ppm, ppb, µg/L, tons/acre-ft
  • Graceful failure paths — unknown units return factor=None without crashing

tests/test_source_mapping.py (48 lines, 4 tests)

Catches configuration drift between two separate config sources:

  • Every parameter in PARAMETER_OPTIONS has ≥1 source defined
  • No empty agency lists in PARAMETER_SOURCE_MAP
  • ANALYTE_OPTIONS entries match PARAMETER_SOURCE_MAP keys
  • All agency references are valid SOURCE_KEYS

tests/test_record_classes.py (119 lines, 7 tests)

Validates record schema contracts and behavior:

  • SiteRecord defaults — elevation_units, horizontal_datum apply correctly
  • Sigfig rounding — lat/lon to 6 decimals, elevation/min/max/mean to 2 decimals
  • Key completeness — SummaryRecord and ParameterRecord expose all documented fields

Test Results

✅ All 45 new tests passing in 1.26s
✅ Full suite: 432 tests passed (0 regressions)
✅ Zero new dependencies (uses only pytest, already in dev requirements)

Bug Discovered & Documented

CaCO₃ String Mismatch in backend/converter.py

Bicarbonate, calcium, and carbonate conversions fail due to a unit string mismatch:

Spec (UNIT_CONVERSIONS.md):       'mg/L as CaCO3'
Code checks after lowercasing:    'mg/l caco3'
Actual lowercased input:          'mg/l as caco3'  ← MISMATCH

Result: These three chemistry conversions return factor=None instead of the documented factors (×1.22, ×0.4, ×0.6).

Documentation: See tests/test_converter.py lines 94-124 for detailed documentation:

  • Class docstring explains the bug (lines 94-105)
  • Each test includes inline comment (lines 107-124)
  • Tests assert the current broken behavior as regression protection

Files Changed

tests/test_converter.py        +248 lines (NEW)
tests/test_source_mapping.py   +48 lines  (NEW)
tests/test_record_classes.py   +119 lines (NEW)
────────────────────────────────────────────
Total: +415 lines, 3 new files, 0 modifications

Running Tests

# New tests only
pytest tests/test_converter.py tests/test_source_mapping.py tests/test_record_classes.py -v

# With coverage
pytest tests/test_converter.py tests/test_source_mapping.py tests/test_record_classes.py --cov=backend

# Full suite (verify zero regressions)
pytest tests/ -v

Testing Approach

  • Spec-driven — validated against UNIT_CONVERSIONS.md and README.md
  • Systematic coverage — every documented conversion factor tested
  • Configuration drift detection — catches mismatches between config sources
  • Follows repo conventions — matches existing test file structure and naming
  • Pure Python tests — no mocks, factories, or external services

Checklist

  • Tests added/updated
  • Documentation updated (in-code and PR description)
  • No breaking changes
  • All 45 tests passing
  • Zero regressions (full suite: 432 passed)
  • Zero new dependencies
  • Bug discovered and documented

- Adds test_converter.py validating all documented conversion factors from
  UNIT_CONVERSIONS.md, non-detect marker handling, and graceful failure paths.

- Adds test_source_mapping.py to catch drift between PARAMETER_SOURCE_MAP
  and ANALYTE_OPTIONS in constants.py.

- Adds test_record_classes.py verifying SiteRecord defaults, sigfig rounding
  behavior, and expected key sets for Summary/Parameter records.

- Discovered bug: mg/L as CaCO3 conversions (bicarbonate, calcium, carbonate)
  return factor=None because the code checks 'mg/l caco3' but lowercased input
  is 'mg/l as caco3'. Tests document this known issue until it's resolved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant