Skip to content

test: add unit tests for dataset 26 harmonizer#33

Merged
cmungall merged 1 commit into
mainfrom
claude/issue-19-20260630-2247
Jul 1, 2026
Merged

test: add unit tests for dataset 26 harmonizer#33
cmungall merged 1 commit into
mainfrom
claude/issue-19-20260630-2247

Conversation

@cmungall

Copy link
Copy Markdown
Collaborator

Adds tests/test_harmonize_dataset26.py with 18 synthetic-data tests exercising every transformation in dataset_26.harmonize(): datetime parsing, site_id rename, depth midpoint, VWC unit conversion, location lookup, and non-numeric coercion.

Closes #19

Generated with Claude Code

Adds tests/test_harmonize_dataset26.py with 18 synthetic-data tests
exercising every transformation in dataset_26.harmonize():
- datetime parsing (America/Denver -> UTC)
- site_id rename, depth midpoint + cm->m, VWC /100
- replicate/is_timeseries/interval_min constants
- location lookup, filtering, lat/lon values, source_dataset_id
- non-numeric coercion for depth and VWC

Closes #19

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Chris Mungall <cmungall@users.noreply.github.com>
@claude

claude Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Claude finished @cmungall's task —— View job


PR Review

  • Read CLAUDE.md and repository context
  • Read the test file (tests/test_harmonize_dataset26.py)
  • Read the dataset_26 harmonizer (data/gold/expert_code/harmonize_sm/dataset_26.py)
  • Analyze code quality and provide feedback

Overall this is a well-structured test file with good coverage of the harmonizer's transformations. Here is detailed feedback:

Strengths

  • Good use of @needs_pkg skip decorator to gracefully handle environments without the expert harmonizer package.
  • _make_ctx, _make_payload, and _make_loc are clean helpers that keep individual tests concise.
  • pytest.approx is used correctly for floating-point comparisons (depth_m, vwc, lat/lon).
  • Non-numeric coercion tests (lines 283–305) are valuable edge-case coverage.

Issues and Suggestions

1. sys.path.insert repeated in every test (lines 75, 87, 106, …)

Every single test repeats:

sys.path.insert(0, str(HARMONIZE_SM.resolve()))

This mutates global state and can accumulate duplicate entries with repeated test runs. Move it to a module-level conftest.py fixture or use a session-scoped autouse fixture. Alternatively, call it once at module import time after the needs_pkg guard check.

# At module level, after the @needs_pkg definition:
if HARMONIZE_SM.exists():
    sys.path.insert(0, str(HARMONIZE_SM.resolve()))

2. Repeated imports inside each test function

Each test re-imports from dataset_26 import harmonize and sometimes from common import DatasetResult. Python caches modules after the first import so this isn't a correctness issue, but it's verbose and unusual. Prefer importing at module level (guarded by if HARMONIZE_SM.exists()) or use a @pytest.fixture.

3. _make_ctx uses positional map_json indices, not idx (line 36)

In _make_ctx, read_ds_csv dispatches on i == idx (26) and i == ref_idx (0). But dataset_26.py accesses map_json[idx] directly at line 33:

f26 = as_list(map_json[idx]["data_payload_files"])[0]

map_json in the mock is a 2-element list (mapping[0] and mapping[1]), so map_json[26] will raise an IndexError at runtime. The mock will never reach this if the harmonizer uses ctx.dsid(26) for the identifier but uses map_json[26] for file paths.

This is a latent bug — the tests may pass only if harmonize() doesn't call as_list(map_json[idx][...]) in the path exercised by the mock. Looking at dataset_26.py line 33–34:

f26 = as_list(map_json[idx]["data_payload_files"])[0]
m26 = as_list(map_json[REF_IDX]["location_metadata_files"])[0]

These will index map_json[26] and map_json[0], crashing immediately. The synthetic mapping list in _make_ctx only has 2 entries (index 0 and 1). The tests would fail at this point unless read_ds_csv is called with idx=26 and ref_idx=0 — which it is — but the map_json[26] access would still be IndexError.

Recommendation: Either extend mapping to have 27 entries (with dummies for 1–25), or refactor _make_ctx so map_json is a dict keyed by index:

mapping = {ref_idx: {...}, idx: {...}}

This requires a small change in how harmonize() accesses map_json, or you could patch at a different level.

4. test_harmonize_datetime_correct_value comment is misleading (line 129)

The docstring says "01/15/21 in America/Denver (UTC-7 in January) -> 2021-01-15 07:00 UTC" but the test only asserts on year, month, day — not the hour. Either assert the hour (assert first_dt.hour == 7) or remove the specific UTC time from the comment so it doesn't create a false expectation.

5. test_harmonize_locations_non_empty asserts len(result.locations) == 1 (line 238)

This is a reasonable structural check, but the comment just says "non_empty". Consider renaming to test_harmonize_locations_has_one_frame for clarity.

6. set(["site_id", "latitude", "longitude"]) idiom (line 241)

Minor style: prefer the literal set syntax:

{"site_id", "latitude", "longitude"}.issubset(loc_df.columns)

7. No test for source_dataset_id in result.harmonized

dataset_26.py does not add source_dataset_id to the harmonized DataFrame (only to loc26), so this isn't missing functionality — but if the schema were to change, there's no guard. Low priority.

8. Missing test for add_loc_qc side-effect

dataset_26.py calls add_loc_qc(loc26) which adds a qc_flag column. There's no assertion that qc_flag exists in result.locations[0]. Consider:

assert "qc_flag" in result.locations[0].columns

Summary

The most important issue is #3 (potential IndexError on map_json[26]). If the tests currently pass in CI, it's worth verifying whether harmonize() in fact hits that line or whether there's a different code path in effect. Issues #1 and #2 are code hygiene concerns worth addressing. Issues #4#8 are minor refinements.

@cmungall
cmungall merged commit bec1740 into main Jul 1, 2026
1 check passed
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.

test idx 26

1 participant