Skip to content

Fix: Handle measuring_point fields correctly in Thing creation API#265

Merged
jirhiker merged 2 commits into
stagingfrom
measuring-point-bugfix
Nov 29, 2025
Merged

Fix: Handle measuring_point fields correctly in Thing creation API#265
jirhiker merged 2 commits into
stagingfrom
measuring-point-bugfix

Conversation

@kbighorse

@kbighorse kbighorse commented Nov 27, 2025

Copy link
Copy Markdown
Contributor

Problem

Creating a well via the API with measuring_point_height and measuring_point_description fails with:
AttributeError: property 'measuring_point_height' of 'Thing' object has no setter

This bug was latent in the codebase but only surfaced when API endpoints were called with these fields in the payload.

Root Cause

measuring_point_height and measuring_point_description are not database columns on the Thing model. They are @property methods that retrieve the current value from the MeasuringPointHistory table. Properties without setters
cannot be assigned during object construction.

The transfer scripts (transfers/well_transfer.py) already handle this correctly by:

  1. Excluding these fields when creating Thing objects (lines 286-294)
  2. Creating separate MeasuringPointHistory records after Thing creation (lines 371-378)

However, the API service layer (services/thing_helper.py) was not following this pattern.

Solution

Updated add_thing() in services/thing_helper.py to:

  1. Extract measuring_point_height and measuring_point_description from data dict before Thing creation
  2. Create the Thing without these fields
  3. After flushing Thing, create a separate MeasuringPointHistory record with the extracted values

Changes:

  • Added imports: datetime, ZoneInfo, MeasuringPointHistory
  • Modified add_thing() to handle measuring point fields separately (lines 166-190)

Testing

Added test test_add_water_well_with_measuring_point() that:

  • ✅ Reproduces the bug (fails before fix)
  • ✅ Verifies the fix (passes after fix)
  • ✅ Confirms measuring point data is correctly stored and retrieved

Verified no regressions:

  • test_validate_hole_depth_well_depth
  • test_validate_mp_height_hole_depth
  • test_add_well_screen

Impact

  • Scope: API endpoints for creating wells (springs don't have measuring points)
  • Behavior: Wells can now be created via API with measuring point data
  • Consistency: API service layer now matches transfer script pattern
  • Breaking changes: None - this fixes broken functionality

Related

This bug was discovered during work on [legacy date field migration], (#264) but is independent of that feature and should be fixed on main first.

@kbighorse kbighorse requested a review from Copilot November 27, 2025 02:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where creating wells via the API with measuring_point_height and measuring_point_description fields caused an AttributeError. These fields are read-only properties that retrieve data from the MeasuringPointHistory table rather than direct database columns on the Thing model. The fix aligns the API service layer with the existing pattern used in transfer scripts by extracting these fields before Thing creation and creating a separate MeasuringPointHistory record afterward.

Key changes:

  • Modified add_thing() in services/thing_helper.py to handle measuring point fields separately
  • Added test case to verify wells can be created with measuring point data via the API

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov-commenter

codecov-commenter commented Nov 27, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

Files with missing lines Coverage Δ
services/thing_helper.py 80.85% <100.00%> (-11.02%) ⬇️
tests/test_thing.py 68.31% <100.00%> (-31.69%) ⬇️

... and 21 files with indirect coverage changes

@kbighorse kbighorse requested a review from jirhiker November 27, 2025 02:18
@jirhiker jirhiker merged commit 0259d7e into staging Nov 29, 2025
6 checks passed
@kbighorse kbighorse deleted the measuring-point-bugfix branch December 2, 2025 00:45
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.

4 participants