-
Notifications
You must be signed in to change notification settings - Fork 0
🛠️ Refactor: SubjectsEndpoint - DRY filtering logic #623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fderuiter
merged 3 commits into
main
from
refactor-subject-filtering-dry-8831546442608404248
Feb 4, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| from unittest.mock import AsyncMock, Mock | ||
|
|
||
| import pytest | ||
|
|
||
| from imednet.endpoints.subjects import SubjectsEndpoint | ||
| from imednet.models.subjects import Subject | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def subject_list(): | ||
| return [ | ||
| Subject(studyKey="sk", subjectId=1, siteId=101, subjectKey="s1"), | ||
| Subject(studyKey="sk", subjectId=2, siteId=102, subjectKey="s2"), | ||
| Subject(studyKey="sk", subjectId=3, siteId=101, subjectKey="s3"), | ||
| Subject(studyKey="sk", subjectId=4, siteId="101", subjectKey="s4"), # String siteId | ||
| Subject(studyKey="sk", subjectId=5, siteId="103", subjectKey="s5"), | ||
| ] | ||
|
|
||
|
|
||
| def test_list_by_site_filtering(subject_list): | ||
| # Mock client and context as they are required by __init__ but not used if we mock list | ||
| mock_client = Mock() | ||
| mock_ctx = Mock() | ||
|
|
||
| endpoint = SubjectsEndpoint(mock_client, mock_ctx) | ||
| endpoint.list = Mock(return_value=subject_list) | ||
|
|
||
| # Act | ||
| filtered_int = endpoint.list_by_site("sk", 101) | ||
| filtered_str = endpoint.list_by_site("sk", "101") | ||
| filtered_mismatch = endpoint.list_by_site("sk", 999) | ||
|
|
||
| # Assert | ||
| assert len(filtered_int) == 3 | ||
| assert {s.subject_id for s in filtered_int} == {1, 3, 4} | ||
|
|
||
| assert len(filtered_str) == 3 | ||
| assert {s.subject_id for s in filtered_str} == {1, 3, 4} | ||
|
|
||
| assert len(filtered_mismatch) == 0 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_list_by_site_filtering(subject_list): | ||
| # Mock client and context | ||
| mock_client = Mock() | ||
| mock_ctx = Mock() | ||
|
|
||
| endpoint = SubjectsEndpoint(mock_client, mock_ctx) | ||
| endpoint.async_list = AsyncMock(return_value=subject_list) | ||
|
|
||
| # Act | ||
| filtered_int = await endpoint.async_list_by_site("sk", 101) | ||
| filtered_str = await endpoint.async_list_by_site("sk", "101") | ||
| filtered_mismatch = await endpoint.async_list_by_site("sk", 999) | ||
|
|
||
| # Assert | ||
| assert len(filtered_int) == 3 | ||
| assert {s.subject_id for s in filtered_int} == {1, 3, 4} | ||
|
|
||
| assert len(filtered_str) == 3 | ||
| assert {s.subject_id for s in filtered_str} == {1, 3, 4} | ||
|
|
||
| assert len(filtered_mismatch) == 0 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment "String siteId" is slightly misleading. While the Subject is constructed with a string siteId value, Pydantic's normalization will convert it to an integer (via parse_int_or_default). The actual test coverage is for filtering when the site_id parameter is passed as either int or str, which is tested on lines 29-30 and 53-54. Consider updating the comment to clarify this, or remove it if it's not necessary.