Skip to content

test: cover the drug form lookups and the static prescription check - #671

Merged
marceloarocha merged 2 commits into
developfrom
claude/magical-wozniak-056mcs
Sep 5, 2026
Merged

test: cover the drug form lookups and the static prescription check#671
marceloarocha merged 2 commits into
developfrom
claude/magical-wozniak-056mcs

Conversation

@marceloarocha

Copy link
Copy Markdown
Collaborator

Increases test coverage on two features that had none. Tests only — no production code changes.

Drug form lookups — tests/integration/test_drug_form_resources.py (17 tests)

The catalogues the prescription drug form loads before it can be rendered:
GET /drugs/resources/<idDrug>/<idSegment>[/<idHospital>], GET /drugs/frequencies
and GET /lists/routes.

  • the full measure-unit and frequency catalogues, and their ordering by description
  • the counts carried over from the drug's aggregated history, including the fact that a
    previously prescribed id comes back twice (history entry with the count, catalogue
    entry with zero) and that history from another segment is ignored
  • the /<idHospital> variant returning the same payload as the two-segment route
  • an unknown drug returning an empty name with the requested id echoed back
  • routes and intervals staying empty outside the complete mode
  • extraFields / removeFields read from the transcription-fields and
    transcription-remove-fields memories, and the empty case
  • map-routes parsing: configured entries, absent memory, and malformed entries skipped
  • the permission gates (READ_PRESCRIPTION on the drug endpoints,
    READ_BASIC_FEATURES on /lists/routes)

Static prescription check — tests/integration/test_static_prescription_status.py (14 tests)

POST /static/prescriptions/status — the endpoint an external system calls to check a
prescription on behalf of one of its own users, identified by external id. The caller
authenticates as a SERVICE_INTEGRATOR (RUN_AS).

  • happy path: the status is set, attributed to the origin user rather than the
    service integrator, and audited with serviceUser: true
  • the response payload, and undoing a check with status 0
  • rejections: unknown external id, inactive origin user, origin user of another schema,
    origin user without segment authorization, origin user without WRITE_PRESCRIPTION,
    unknown prescription, and a status that does not change the prescription
  • the prescription is left untouched when the origin user is refused
  • the RUN_AS permission gate and the authentication gate

Test hygiene

Both files clean their own rows and are re-runnable: the drug fixture reuses the
reserved >= 90000 id range already wiped by tests/conftest.py::_cleanup, the origin
users use the test%@example.com pattern and drop their own usuario_autorizacao rows,
and the memory helper snapshots and restores whatever the schema already held instead of
deleting seed data.

Verification

Full suite against a local PostgreSQL loaded from noharm-ai/database (the same SQL
files CI uses): 2323 passed (2292 before), and green on a second consecutive run to
confirm re-runnability. ruff check . passes.

Note (no change made)

While covering static_check, one behaviour stood out and was deliberately left
untested rather than pinned down: when idOriginUser is absent from the payload, the
lookup becomes User.external IS NULL, which matches whichever seed user comes back
first — so a caller omitting the field can end up running as an arbitrary user. It is
non-deterministic, so no test asserts it. Worth a look separately if you agree it's a
hole.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HJNm2wnqhhzgxa3bx8NSpS


Generated by Claude Code

Two features had no test coverage at all.

Drug form lookups (GET /drugs/resources/<drug>/<segment>[/<hospital>],
GET /drugs/frequencies, GET /lists/routes): the catalogues the prescription
drug form loads before it can be rendered. Covers the full unit/frequency
catalogue and its ordering, the counts carried over from the drug's
aggregated history (including the segment filter), the hospital route
variant, an unknown drug, the empty routes/intervals of the non-complete
mode, the transcription fields read from memory, the map-routes parsing
(configured, absent and malformed entries) and the permission gates.

Static prescription check (POST /static/prescriptions/status): the endpoint
an external system calls to check a prescription on behalf of one of its own
users, identified by external id. Covers the happy path (status, attribution
to the origin user and the audit flagged as serviceUser), undoing a check,
and every rejection: unknown, inactive, foreign-schema, unauthorized-segment
and read-only origin users, unknown prescription, unchanged status, and the
RUN_AS/authentication gates.

The memory helper snapshots and restores whatever the schema already held,
and both files clean their own rows, so the suite stays re-runnable.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HJNm2wnqhhzgxa3bx8NSpS

@amazon-q-developer amazon-q-developer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This PR adds comprehensive test coverage for two features that previously had none: drug form lookup endpoints and static prescription checking. The test implementations are solid with proper cleanup, isolation, and re-runnability. No defects found that would block merge.


You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.

The ordering assertion compared the response against Python's sorted(),
which is codepoint order and put 'UI'/'Un' before the lowercase units. That
only holds under a C collation: CI initialises PostgreSQL with en_US.utf8,
where the same ORDER BY description returns mcg, mg, mg/ml, ml, UI, Un.

Ordering by description is the database's, so compare the catalogue tail of
the response against what the database itself returns for the same table.
The assertion still pins that the service preserves that order and appends
the catalogue after the previously prescribed entries, and it now holds
under either collation.

Verified against both a C.UTF-8 and an en-US collated database: 2323 passed
on each.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HJNm2wnqhhzgxa3bx8NSpS
@marceloarocha
marceloarocha merged commit b7adab6 into develop Sep 5, 2026
9 checks passed
@marceloarocha
marceloarocha deleted the claude/magical-wozniak-056mcs branch September 5, 2026 10: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.

2 participants