Skip to content

test: add coverage for created_by filter and tag config org clearing - #3713

Open
KeerthiKumarR wants to merge 3 commits into
ohcnetwork:developfrom
KeerthiKumarR:test/created-by-filter-and-tag-config-clearing-coverage
Open

test: add coverage for created_by filter and tag config org clearing#3713
KeerthiKumarR wants to merge 3 commits into
ohcnetwork:developfrom
KeerthiKumarR:test/created-by-filter-and-tag-config-clearing-coverage

Conversation

@KeerthiKumarR

@KeerthiKumarR KeerthiKumarR commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Proposed Changes

  • Adds test coverage for two recently merged features that shipped
    without tests.

created_by filter on questionnaire responses (ENG-558, #3695):
A created_by UUID filter was added to the questionnaire response list
endpoint but no tests were written for it. The new tests verify that
filtering by a user's external_id returns only that user's responses
and excludes responses created by other users.

Clearing organization/facility_organization on tag config update
(ENG-580, #3690):

The tag config update spec (TagConfigUpdateSpec.perform_extra_deserialization)
was updated to explicitly set organization and facility_organization
to None when those fields are omitted from a PUT request — previously
they would be left unchanged. The new tests verify that omitting either
field in an update actually clears the existing value to None in the
database.

Merge Checklist

  • Tests added
  • Linting Complete

@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins

Summary by CodeRabbit

  • Bug Fixes
    • Questionnaire response listings now support filtering by the user who created each response.
    • Tag configuration updates now correctly clear organization and facility organization settings when those fields are omitted.
    • Improved consistency when updating tag configurations and reviewing questionnaire responses.

@KeerthiKumarR
KeerthiKumarR requested a review from a team as a code owner July 16, 2026 04:56
Copilot AI review requested due to automatic review settings July 16, 2026 04:56
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 1af63341-4d9a-4b58-92e8-f11fd6a13deb

📥 Commits

Reviewing files that changed from the base of the PR and between 27c2f9f and 25ec2cf.

📒 Files selected for processing (2)
  • care/emr/tests/test_questionnaire_response_api.py
  • care/emr/tests/test_tag_config_api.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • care/emr/tests/test_questionnaire_response_api.py
  • care/emr/tests/test_tag_config_api.py

📝 Walkthrough

Walkthrough

Adds API test coverage for filtering questionnaire responses by creator and for clearing omitted organization-scoping fields during tag configuration updates.

Changes

Questionnaire response filtering

Layer / File(s) Summary
Creator-filtered response listing
care/emr/tests/test_questionnaire_response_api.py
Adds permission setup and verifies that created_by filtering returns only responses created by the requested user.

Tag configuration update semantics

Layer / File(s) Summary
Clearing omitted scoping fields
care/emr/tests/test_tag_config_api.py
Adds tests confirming omitted organization and facility_organization fields are cleared to None on PUT updates.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • ohcnetwork/care#3609: Extends questionnaire response API test coverage in the same test base.
  • ohcnetwork/care#3695: Adds the questionnaire response created_by filter implementation validated by this test.

Suggested labels: waiting-for-review, Tests

Suggested reviewers: copilot

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the added test coverage for the created_by filter and tag configuration organization clearing.
Description check ✅ Passed The description explains both test areas, references the associated issues, and includes the required test and lint checklist items.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (2)
care/emr/tests/test_questionnaire_response_api.py (1)

477-495: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Assert exact result counts for tighter tests.

While asserting for specific inclusions and exclusions works fine, it leaves the door open for unexpected records (like the pre-existing response created by self.user in setUp) if the filter misbehaves in weird ways. Checking the exact length of the returned array is slightly more robust, though I'm sure you already knew that.

♻️ Proposed refactor for stricter assertions
         # Filter by user_a
         response = self.client.get(
             self.get_url(), {"created_by": str(user_a.external_id)}
         )
         self.assertEqual(response.status_code, 200)
         results = response.json()["results"]
-        result_ids = [res["id"] for res in results]
-        self.assertIn(response_a["id"], result_ids)
-        self.assertNotIn(response_b["id"], result_ids)
+        self.assertEqual(len(results), 1)
+        self.assertEqual(results[0]["id"], response_a["id"])

         # Filter by user_b
         response = self.client.get(
             self.get_url(), {"created_by": str(user_b.external_id)}
         )
         self.assertEqual(response.status_code, 200)
         results = response.json()["results"]
-        result_ids = [res["id"] for res in results]
-        self.assertIn(response_b["id"], result_ids)
-        self.assertNotIn(response_a["id"], result_ids)
+        self.assertEqual(len(results), 1)
+        self.assertEqual(results[0]["id"], response_b["id"])
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@care/emr/tests/test_questionnaire_response_api.py` around lines 477 - 495,
Update the test method containing the user_a and user_b filter requests to
assert the exact result count for each response before checking included and
excluded IDs. Ensure each filtered result set contains only the expected single
record, while preserving the existing membership assertions.
care/emr/tests/test_tag_config_api.py (1)

438-444: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider preserving the facility in the update payload.

You omitted the facility field in the PUT payload. This likely clears it as well, converting the tag to a global one. While the test still happens to pass for your specific assertion, passing facility=self.facility.external_id would actually isolate the test to just the facility_organization field. I suppose this is fine if you're not overly concerned with test precision.

💡 Proposed change
         response = self.client.put(
             self.get_detail_url(tag_config.external_id),
             self.generate_tag_config_data(
                 resource=TagResource.encounter.value,
+                facility=self.facility.external_id,
             ),
             format="json",
         )
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@care/emr/tests/test_tag_config_api.py` around lines 438 - 444, Update the PUT
payload in the test using generate_tag_config_data to include
facility=self.facility.external_id, preserving the existing facility while
changing only the facility_organization-related value.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@care/emr/tests/test_questionnaire_response_api.py`:
- Around line 477-495: Update the test method containing the user_a and user_b
filter requests to assert the exact result count for each response before
checking included and excluded IDs. Ensure each filtered result set contains
only the expected single record, while preserving the existing membership
assertions.

In `@care/emr/tests/test_tag_config_api.py`:
- Around line 438-444: Update the PUT payload in the test using
generate_tag_config_data to include facility=self.facility.external_id,
preserving the existing facility while changing only the
facility_organization-related value.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: bf934a54-4437-4d9d-b25e-83f69678f5c7

📥 Commits

Reviewing files that changed from the base of the PR and between 6eb0df0 and 27c2f9f.

📒 Files selected for processing (2)
  • care/emr/tests/test_questionnaire_response_api.py
  • care/emr/tests/test_tag_config_api.py

Copilot AI 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.

Pull request overview

Adds missing backend test coverage for two previously merged behaviors: filtering questionnaire responses by creator (created_by) and clearing organization / facility_organization when omitted in tag config PUT updates.

Changes:

  • Add tests validating created_by UUID filter on questionnaire response listing.
  • Add tests validating that omitting organization clears it to None on tag config updates.
  • Add tests validating that omitting facility_organization clears it to None on tag config updates.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
care/emr/tests/test_tag_config_api.py Adds tests for clearing organization and facility_organization when omitted in PUT updates.
care/emr/tests/test_questionnaire_response_api.py Adds a test for created_by filter behavior on questionnaire response list endpoint.

Comment thread care/emr/tests/test_tag_config_api.py
Comment thread care/emr/tests/test_questionnaire_response_api.py
Comment thread care/emr/tests/test_questionnaire_response_api.py
@greptile-apps

greptile-apps Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds test coverage for two features that shipped without tests: the created_by UUID filter on questionnaire responses, and the behavior that clears organization/facility_organization on a tag config PUT when those fields are omitted.

  • Questionnaire filter test: Creates two non-superusers with the correct roles, submits responses as each, then asserts the created_by filter returns exactly the right response per user, using the superuser to run the list query.
  • Tag config clearing tests: Uses baker.make to create a TagConfig with organization fields set, then issues a PUT without those fields and asserts refresh_from_db() shows them as None.

Confidence Score: 5/5

Test-only change adding coverage for two previously untested features; no production code is modified.

Both new test methods are logically sound: the questionnaire filter test correctly isolates per-user submissions and asserts bidirectional filter results, and the tag config tests properly use baker.make + a real PUT + refresh_from_db to verify the clearing behavior. Superuser bypass of role checks is confirmed in the authorization layer, so the list call in the filter test is valid without explicit role attachment.

No files require special attention.

Important Files Changed

Filename Overview
care/emr/tests/test_questionnaire_response_api.py Adds test_list_questionnaire_responses_with_created_by_filter; correctly sets up two users with appropriate roles, submits responses under each identity, and asserts bidirectional filter correctness using the superuser (which bypasses role checks) to call the list endpoint.
care/emr/tests/test_tag_config_api.py Adds two tests verifying that omitting organization/facility_organization from a PUT clears those fields to None; correctly uses baker.make for setup, issues a real API PUT, then asserts the DB value via refresh_from_db().

Reviews (2): Last reviewed commit: "test: incorporate code review feedback o..." | Re-trigger Greptile

Comment thread care/emr/tests/test_questionnaire_response_api.py
@KeerthiKumarR

KeerthiKumarR commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

@vigneshhari
PTAL when you got time!

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings August 3, 2026 13:52

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

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