Skip to content

Add behavior tests for Sensors API and replace brittle source-string assertion #170

@bhudevbhanpuriya

Description

@bhudevbhanpuriya

Description

The current test coverage gives false confidence around API behavior.

There are no direct tests for the Sensors endpoints in api/tests, so regressions in GET /Sensors, POST /Sensors, PATCH /Sensors(id), or DELETE /Sensors(id) may not be caught.

Additionally, api/tests/test_update_user_error_message.py checks the contents of api/app/v1/endpoints/update/user.py as plain text instead of testing runtime behavior. For example, it asserts that certain source strings exist:

self.assertIn('except UndefinedObjectError as e:', content)
self.assertIn('content={"message": "User not found"}', content)
This is brittle because formatting or refactoring can break the test even if behavior is correct. It can also pass even if the endpoint behavior is wrong.


Understanding the problem

Image

Expected Behavior

Tests should verify actual API/endpoint behavior, not source-code strings.

Proposed Fix

  • Add endpoint-level tests for Sensors API behavior:

    • GET /Sensors returns a streaming response when data exists.
    • GET /Sensors returns 404 when no rows are found.
    • POST /Sensors validates JSON content type.
    • POST /Sensors returns a Location header after creation.
    • PATCH /Sensors(id) returns 404 for a missing sensor.
    • DELETE /Sensors(id) returns 404 for a missing sensor.
  • Rewrite test_update_user_error_message.py to call update_user() directly and assert the actual response.

  • Ensure api/tests is included in default pytest collection.

Why This Matters

Behavior tests protect the public API contract. Source-string tests only protect implementation details and can miss real regressions.

Acceptance Criteria

  • Sensors endpoints have direct behavior coverage in api/tests.
  • test_update_user_error_message.py no longer reads endpoint source files.
  • Default pytest collection includes api/tests.
  • Tests can run without requiring a live database by mocking DB pools and endpoint collaborators.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions