-
Notifications
You must be signed in to change notification settings - Fork 2
ELI-351: Moves/deletes tests after refactoring #265
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
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3d41d66
ELI-351: Moves/deletes tests after refactoring
shweta-nhs 3cb1481
ELI-351: Extracts EligibilityResultBuilder and adds tests
shweta-nhs dc9de8e
ELI-351: De-extracts EligibilityResultBuilder and moves tests to Elig…
shweta-nhs 8886ff5
ELI-351: Removes duplicated tests
shweta-nhs e3dd187
ELI-351: Removes duplicated tests #2
shweta-nhs 9adf850
ELI-351: Adds validation and audit layer to Readme
shweta-nhs de48adb
Merge branch 'main' into refactor/ELI-351
shweta-nhs 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
48 changes: 48 additions & 0 deletions
48
src/eligibility_signposting_api/services/processors/eligibility_result_builder.py
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,48 @@ | ||
| from collections import defaultdict | ||
|
|
||
| from eligibility_signposting_api.model.eligibility_status import ( | ||
| CohortGroupResult, | ||
| Condition, | ||
| ConditionName, | ||
| IterationResult, | ||
| ) | ||
|
|
||
|
|
||
| class EligibilityResultBuilder: | ||
| @staticmethod | ||
| def build_condition_results(condition_results: dict[ConditionName, IterationResult]) -> list[Condition]: | ||
| conditions: list[Condition] = [] | ||
| # iterate over conditions | ||
| for condition_name, active_iteration_result in condition_results.items(): | ||
| grouped_cohort_results = defaultdict(list) | ||
| # iterate over cohorts and group them by status and cohort_group | ||
| for cohort_result in active_iteration_result.cohort_results: | ||
| if active_iteration_result.status == cohort_result.status: | ||
| grouped_cohort_results[cohort_result.cohort_code].append(cohort_result) | ||
|
|
||
| # deduplicate grouped cohort results by cohort_code | ||
| deduplicated_cohort_results = [ | ||
| CohortGroupResult( | ||
| cohort_code=group_cohort_code, | ||
| status=group[0].status, | ||
| # Flatten all reasons from the group | ||
| reasons=[reason for cohort in group for reason in cohort.reasons], | ||
| # get the first nonempty description | ||
| description=next((c.description for c in group if c.description), group[0].description), | ||
| audit_rules=[], | ||
| ) | ||
| for group_cohort_code, group in grouped_cohort_results.items() | ||
| if group | ||
| ] | ||
|
|
||
| # return condition with cohort results | ||
| conditions.append( | ||
| Condition( | ||
| condition_name=condition_name, | ||
| status=active_iteration_result.status, | ||
| cohort_results=list(deduplicated_cohort_results), | ||
| actions=condition_results[condition_name].actions, | ||
| status_text=active_iteration_result.status.get_status_text(condition_name), | ||
| ) | ||
| ) | ||
| return conditions |
262 changes: 262 additions & 0 deletions
262
tests/unit/services/processors/test_eligibility_result_builder.py
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,262 @@ | ||
| import pytest | ||
| from hamcrest import assert_that, contains_inanyorder, is_ | ||
|
|
||
| from eligibility_signposting_api.model.eligibility_status import ( | ||
| ActionCode, | ||
| ActionDescription, | ||
| ActionType, | ||
| CohortGroupResult, | ||
| ConditionName, | ||
| InternalActionCode, | ||
| IterationResult, | ||
| Reason, | ||
| RuleDescription, | ||
| RuleName, | ||
| RulePriority, | ||
| RuleType, | ||
| Status, | ||
| SuggestedAction, | ||
| ) | ||
| from eligibility_signposting_api.services.processors.eligibility_result_builder import EligibilityResultBuilder | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def result_builder(): | ||
| return EligibilityResultBuilder() | ||
|
|
||
|
|
||
| def test_build_condition_results_empty_input(result_builder): | ||
| condition_results = {} | ||
| result = result_builder.build_condition_results(condition_results) | ||
| assert_that(result, is_([])) | ||
|
|
||
|
|
||
| def test_build_condition_results_single_condition_single_cohort_actionable(result_builder): | ||
| cohort_group_results = [CohortGroupResult("COHORT_A", Status.actionable, [], "Cohort A Description", [])] | ||
| suggested_actions = [ | ||
| SuggestedAction( | ||
| internal_action_code=InternalActionCode("default_action_code"), | ||
| action_type=ActionType("CareCardWithText"), | ||
| action_code=ActionCode("BookLocal"), | ||
| action_description=ActionDescription("You can get an RSV vaccination at your GP surgery"), | ||
| url_link=None, | ||
| url_label=None, | ||
| ) | ||
| ] | ||
| iteration_result = IterationResult(Status.actionable, cohort_group_results, suggested_actions) | ||
|
|
||
| condition_results = {ConditionName("RSV"): iteration_result} | ||
|
|
||
| result = result_builder.build_condition_results(condition_results) | ||
|
|
||
| assert_that(len(result), is_(1)) | ||
| assert_that(result[0].condition_name, is_(ConditionName("RSV"))) | ||
| assert_that(result[0].status, is_(Status.actionable)) | ||
| assert_that(result[0].actions, is_(suggested_actions)) | ||
| assert_that(result[0].status_text, is_(Status.actionable.get_status_text(ConditionName("RSV")))) | ||
|
|
||
| assert_that(len(result[0].cohort_results), is_(1)) | ||
| deduplicated_cohort = result[0].cohort_results[0] | ||
| assert_that(deduplicated_cohort.cohort_code, is_("COHORT_A")) | ||
| assert_that(deduplicated_cohort.status, is_(Status.actionable)) | ||
| assert_that(deduplicated_cohort.reasons, is_([])) | ||
| assert_that(deduplicated_cohort.description, is_("Cohort A Description")) | ||
| assert_that(deduplicated_cohort.audit_rules, is_([])) | ||
|
|
||
|
|
||
| def test_build_condition_results_single_condition_single_cohort_not_eligible_with_reasons(result_builder): | ||
| cohort_group_results = [CohortGroupResult("COHORT_A", Status.not_eligible, [], "Cohort A Description", [])] | ||
| suggested_actions = [ | ||
| SuggestedAction( | ||
| internal_action_code=InternalActionCode("default_action_code"), | ||
| action_type=ActionType("CareCardWithText"), | ||
| action_code=ActionCode("BookLocal"), | ||
| action_description=ActionDescription("You can get an RSV vaccination at your GP surgery"), | ||
| url_link=None, | ||
| url_label=None, | ||
| ) | ||
| ] | ||
| iteration_result = IterationResult(Status.not_eligible, cohort_group_results, suggested_actions) | ||
|
|
||
| condition_results = {ConditionName("RSV"): iteration_result} | ||
|
|
||
| result = result_builder.build_condition_results(condition_results) | ||
|
|
||
| assert_that(len(result), is_(1)) | ||
| assert_that(result[0].condition_name, is_(ConditionName("RSV"))) | ||
| assert_that(result[0].status, is_(Status.not_eligible)) | ||
| assert_that(result[0].actions, is_(suggested_actions)) | ||
| assert_that(result[0].status_text, is_(Status.not_eligible.get_status_text(ConditionName("RSV")))) | ||
|
|
||
| assert_that(len(result[0].cohort_results), is_(1)) | ||
| deduplicated_cohort = result[0].cohort_results[0] | ||
| assert_that(deduplicated_cohort.cohort_code, is_("COHORT_A")) | ||
| assert_that(deduplicated_cohort.status, is_(Status.not_eligible)) | ||
| assert_that(deduplicated_cohort.reasons, is_([])) | ||
| assert_that(deduplicated_cohort.description, is_("Cohort A Description")) | ||
| assert_that(deduplicated_cohort.audit_rules, is_([])) | ||
|
|
||
|
|
||
| def test_build_condition_results_single_condition_multiple_cohorts_same_cohort_code_same_status(result_builder): | ||
| reason_1 = Reason( | ||
| RuleType.filter, | ||
| RuleName("Filter Rule 1"), | ||
| RulePriority("1"), | ||
| RuleDescription("Filter Rule Description 2"), | ||
| matcher_matched=True, | ||
| ) | ||
| reason_2 = Reason( | ||
| RuleType.filter, | ||
| RuleName("Filter Rule 2"), | ||
| RulePriority("2"), | ||
| RuleDescription("Filter Rule Description 2"), | ||
| matcher_matched=True, | ||
| ) | ||
| cohort_group_results = [ | ||
| CohortGroupResult("COHORT_A", Status.not_eligible, [reason_1], "", []), | ||
| CohortGroupResult("COHORT_A", Status.not_eligible, [reason_2], "Cohort A Description 2", []), | ||
| CohortGroupResult("COHORT_A", Status.not_eligible, [], "Cohort A Description 3", []), | ||
| ] | ||
| suggested_actions = [ | ||
| SuggestedAction( | ||
| internal_action_code=InternalActionCode("default_action_code"), | ||
| action_type=ActionType("CareCardWithText"), | ||
| action_code=ActionCode("BookLocal"), | ||
| action_description=ActionDescription("You can get an RSV vaccination at your GP surgery"), | ||
| url_link=None, | ||
| url_label=None, | ||
| ) | ||
| ] | ||
| iteration_result = IterationResult(Status.not_eligible, cohort_group_results, suggested_actions) | ||
|
|
||
| condition_results = {ConditionName("RSV"): iteration_result} | ||
|
|
||
| result = result_builder.build_condition_results(condition_results) | ||
|
|
||
| assert_that(len(result), is_(1)) | ||
| condition = result[0] | ||
| assert_that(len(condition.cohort_results), is_(1)) | ||
|
|
||
| deduplicated_cohort = condition.cohort_results[0] | ||
| assert_that(deduplicated_cohort.cohort_code, is_("COHORT_A")) | ||
| assert_that(deduplicated_cohort.status, is_(Status.not_eligible)) | ||
| assert_that(deduplicated_cohort.reasons, contains_inanyorder(reason_1, reason_2)) | ||
| assert_that(deduplicated_cohort.description, is_("Cohort A Description 2")) | ||
| assert_that(deduplicated_cohort.audit_rules, is_([])) | ||
|
|
||
|
|
||
| def test_build_condition_results_multiple_cohorts_different_cohort_code_same_status(result_builder): | ||
| reason_1 = Reason( | ||
| RuleType.filter, | ||
| RuleName("Filter Rule 1"), | ||
| RulePriority("1"), | ||
| RuleDescription("Filter Rule Description 2"), | ||
| matcher_matched=True, | ||
| ) | ||
| reason_2 = Reason( | ||
| RuleType.filter, | ||
| RuleName("Filter Rule 2"), | ||
| RulePriority("2"), | ||
| RuleDescription("Filter Rule Description 2"), | ||
| matcher_matched=True, | ||
| ) | ||
| cohort_group_results = [ | ||
| CohortGroupResult("COHORT_X", Status.not_eligible, [reason_1], "Cohort X Description", []), | ||
| CohortGroupResult("COHORT_Y", Status.not_eligible, [reason_2], "Cohort Y Description", []), | ||
| ] | ||
| suggested_actions = [ | ||
| SuggestedAction( | ||
| internal_action_code=InternalActionCode("default_action_code"), | ||
| action_type=ActionType("CareCardWithText"), | ||
| action_code=ActionCode("BookLocal"), | ||
| action_description=ActionDescription("You can get an RSV vaccination at your GP surgery"), | ||
| url_link=None, | ||
| url_label=None, | ||
| ) | ||
| ] | ||
| iteration_result = IterationResult(Status.not_eligible, cohort_group_results, suggested_actions) | ||
|
|
||
| condition_results = {ConditionName("RSV"): iteration_result} | ||
|
|
||
| result = result_builder.build_condition_results(condition_results) | ||
|
|
||
| assert_that(len(result), is_(1)) | ||
| condition = result[0] | ||
| assert_that(len(condition.cohort_results), is_(2)) | ||
|
|
||
| expected_deduplicated_cohorts = [ | ||
| CohortGroupResult("COHORT_X", Status.not_eligible, [reason_1], "Cohort X Description", []), | ||
| CohortGroupResult("COHORT_Y", Status.not_eligible, [reason_2], "Cohort Y Description", []), | ||
| ] | ||
| assert_that(condition.cohort_results, contains_inanyorder(*expected_deduplicated_cohorts)) | ||
|
|
||
|
|
||
| def test_build_condition_results_cohorts_status_not_matching_iteration_status(result_builder): | ||
| reason_1 = Reason( | ||
| RuleType.filter, RuleName("Filter Rule 1"), RulePriority("1"), RuleDescription("Matching"), matcher_matched=True | ||
| ) | ||
| reason_2 = Reason( | ||
| RuleType.filter, | ||
| RuleName("Filter Rule 2"), | ||
| RulePriority("2"), | ||
| RuleDescription("Not matching"), | ||
| matcher_matched=True, | ||
| ) | ||
| cohort_group_results = [ | ||
| CohortGroupResult("COHORT_X", Status.not_eligible, [reason_1], "Cohort X Description", []), | ||
| CohortGroupResult("COHORT_Y", Status.not_actionable, [reason_2], "Cohort Y Description", []), | ||
| ] | ||
|
|
||
| iteration_result = IterationResult(Status.not_eligible, cohort_group_results, []) | ||
|
|
||
| condition_results = {ConditionName("RSV"): iteration_result} | ||
|
|
||
| result = result_builder.build_condition_results(condition_results) | ||
|
|
||
| assert_that(len(result), is_(1)) | ||
| condition = result[0] | ||
| assert_that(len(condition.cohort_results), is_(1)) | ||
| assert_that(condition.cohort_results[0].cohort_code, is_("COHORT_X")) | ||
| assert_that(condition.cohort_results[0].status, is_(Status.not_eligible)) | ||
|
|
||
|
|
||
| def test_build_condition_results_multiple_conditions(result_builder): | ||
| reason_1 = Reason( | ||
| RuleType.filter, | ||
| RuleName("Filter Rule 1"), | ||
| RulePriority("1"), | ||
| RuleDescription("Filter Rule Description 2"), | ||
| matcher_matched=True, | ||
| ) | ||
| reason_2 = Reason( | ||
| RuleType.filter, | ||
| RuleName("Filter Rule 2"), | ||
| RulePriority("2"), | ||
| RuleDescription("Filter Rule Description 2"), | ||
| matcher_matched=True, | ||
| ) | ||
| cohort_group_result1 = [CohortGroupResult("RSV_COHORT", Status.not_eligible, [reason_1], "RSV Desc", [])] | ||
| cohort_group_result2 = [CohortGroupResult("COVID_COHORT", Status.not_actionable, [reason_2], "Covid Desc", [])] | ||
|
|
||
| iteration_result1 = IterationResult(Status.not_eligible, cohort_group_result1, []) | ||
|
|
||
| iteration_result2 = IterationResult(Status.not_actionable, cohort_group_result2, []) | ||
|
|
||
| condition_results = { | ||
| ConditionName("RSV"): iteration_result1, | ||
| ConditionName("COVID"): iteration_result2, | ||
| } | ||
|
|
||
| result = result_builder.build_condition_results(condition_results) | ||
|
|
||
| rsv = next((c for c in result if c.condition_name == ConditionName("RSV")), None) | ||
| assert_that(rsv.status, is_(Status.not_eligible)) | ||
| assert_that(len(rsv.cohort_results), is_(1)) | ||
| assert_that(rsv.cohort_results[0].cohort_code, is_("RSV_COHORT")) | ||
| assert_that(rsv.cohort_results[0].reasons, is_([reason_1])) | ||
|
|
||
| covid = next((c for c in result if c.condition_name == ConditionName("COVID")), None) | ||
| assert_that(covid.status, is_(Status.not_actionable)) | ||
| assert_that(len(covid.cohort_results), is_(1)) | ||
| assert_that(covid.cohort_results[0].cohort_code, is_("COVID_COHORT")) | ||
| assert_that(covid.cohort_results[0].reasons, is_([reason_2])) |
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.
Uh oh!
There was an error while loading. Please reload this page.