|
1 | | -# Utility Guide: Subject Assertion Utility |
2 | | - |
3 | | -This guide explains the purpose and usage of the `subject_assertion` utility found in [`utils/subject_assertion.py`](../../utils/subject_assertion.py). |
4 | | -It is designed to assert that a subject with a given NHS number matches specified criteria in the database, and provides detailed logging when criteria do not match. |
5 | | - |
6 | | ---- |
7 | | - |
8 | | -## Table of Contents |
9 | | - |
10 | | -- [Utility Guide: Subject Assertion Utility](#utility-guide-subject-assertion-utility) |
11 | | - - [Table of Contents](#table-of-contents) |
12 | | - - [Overview](#overview) |
13 | | - - [Required Arguments](#required-arguments) |
14 | | - - [How It Works](#how-it-works) |
15 | | - - [Example Usage](#example-usage) |
16 | | - - [Behaviour Details](#behaviour-details) |
17 | | - - [Best Practices](#best-practices) |
18 | | - - [Reference](#reference) |
19 | | - |
20 | | ---- |
21 | | - |
22 | | -## Overview |
23 | | - |
24 | | -The `subject_assertion` function is used to verify that a subject in the database matches a set of criteria. |
25 | | -If the subject does not match all criteria, the function will iteratively remove criteria (except NHS number) and retry, logging any criteria that caused the assertion to fail. |
26 | | - |
27 | | ---- |
28 | | - |
29 | | -## Required Arguments |
30 | | - |
31 | | -- `nhs_number` (`str`): The NHS number of the subject to check. |
32 | | -- `criteria` (`dict`): A dictionary of criteria to match against the subject's attributes. |
33 | | - |
34 | | ---- |
35 | | - |
36 | | -## How It Works |
37 | | - |
38 | | -1. The function first checks if the subject with the given NHS number matches all provided criteria. |
39 | | -2. If not, it removes one criterion at a time (except NHS number) and retries the assertion. |
40 | | -3. This process continues until either a match is found or all criteria (except NHS number) have been removed. |
41 | | -4. If a match is found only after removing criteria, the failed criteria are logged. |
42 | | -5. The function returns `True` only if all criteria match on the first attempt; otherwise, it returns `False`. |
43 | | - |
44 | | ---- |
45 | | - |
46 | | -## Example Usage |
47 | | - |
48 | | -Below are examples of how to use `subject_assertion` in your tests: |
49 | | - |
50 | | -```python |
51 | | -import pytest |
52 | | -from utils.subject_assertion import subject_assertion |
53 | | - |
54 | | -pytestmark = [pytest.mark.utils_local] |
55 | | - |
56 | | -def test_subject_assertion_true(): |
57 | | - nhs_number = "9233639266" |
58 | | - criteria = {"screening status": "Inactive", "subject age": "> 28"} |
59 | | - assert subject_assertion(nhs_number, criteria) is True |
60 | | -``` |
61 | | - |
62 | | -See `tests_utils/test_subject_assertion_util.py` for more examples. |
63 | | - |
64 | | ---- |
65 | | - |
66 | | -## Behaviour Details |
67 | | - |
68 | | -- The function always keeps the NHS number criterion. |
69 | | -- If a match is found only after removing criteria, the failed criteria are logged in the format: |
70 | | - - Failed criteria: Key: 'key1', Value: 'value1' |
71 | | -- The function will only return `True` if all criteria match on the first attempt. |
72 | | - |
73 | | ---- |
74 | | - |
75 | | -## Best Practices |
76 | | - |
77 | | -- Use this utility to validate subject data in database-driven tests. |
78 | | -- Review logs for failed criteria to diagnose why assertions did not pass. |
79 | | -- Always provide the NHS number as part of your criteria. |
80 | | - |
81 | | ---- |
82 | | - |
83 | | -## Reference |
84 | | - |
85 | | -- [`utils/subject_assertion.py`](../../utils/subject_assertion.py) |
86 | | -- [`tests_utils/test_subject_assertion_util.py`](../../tests_utils/test_subject_assertion_util.py) |
87 | | -- [SubjectSelectionQueryBuilder Utility Guide](SubjectSelectionQueryBuilder.md) |
| 1 | +# Utility Guide: Subject Assertion Utility |
| 2 | + |
| 3 | +This guide explains the purpose and usage of the `subject_assertion` utility found in [`utils/subject_assertion.py`](../../utils/subject_assertion.py). |
| 4 | +It is designed to assert that a subject with a given NHS number matches specified criteria in the database, and provides detailed logging when criteria do not match. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## Table of Contents |
| 9 | + |
| 10 | +- [Utility Guide: Subject Assertion Utility](#utility-guide-subject-assertion-utility) |
| 11 | + - [Table of Contents](#table-of-contents) |
| 12 | + - [Overview](#overview) |
| 13 | + - [Required Arguments](#required-arguments) |
| 14 | + - [How It Works](#how-it-works) |
| 15 | + - [Example Usage](#example-usage) |
| 16 | + - [Behaviour Details](#behaviour-details) |
| 17 | + - [Best Practices](#best-practices) |
| 18 | + - [Reference](#reference) |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Overview |
| 23 | + |
| 24 | +The `subject_assertion` function is used to verify that a subject in the database matches a set of criteria. |
| 25 | +If the subject does not match all criteria, the function will iteratively remove criteria (except NHS number) and retry, logging any criteria that caused the assertion to fail. |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## Required Arguments |
| 30 | + |
| 31 | +- `nhs_number` (`str`): The NHS number of the subject to check. |
| 32 | +- `criteria` (`dict`): A dictionary of criteria to match against the subject's attributes. |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +## How It Works |
| 37 | + |
| 38 | +1. The function first checks if the subject with the given NHS number matches all provided criteria. |
| 39 | +2. If not, it removes one criterion at a time (except NHS number) and retries the assertion. |
| 40 | +3. This process continues until either a match is found or all criteria (except NHS number) have been removed. |
| 41 | +4. If a match is found only after removing criteria, the failed criteria are logged. |
| 42 | +5. The function returns `True` only if all criteria match on the first attempt; otherwise, it returns `False`. |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +## Example Usage |
| 47 | + |
| 48 | +Below are examples of how to use `subject_assertion` in your tests: |
| 49 | + |
| 50 | +```python |
| 51 | +import pytest |
| 52 | +from utils.subject_assertion import subject_assertion |
| 53 | + |
| 54 | +pytestmark = [pytest.mark.utils_local] |
| 55 | + |
| 56 | +def test_subject_assertion_true(): |
| 57 | + nhs_number = "9233639266" |
| 58 | + criteria = {"screening status": "Inactive", "subject age": "> 28"} |
| 59 | + assert subject_assertion(nhs_number, criteria) is True |
| 60 | +``` |
| 61 | + |
| 62 | +See `tests_utils/test_subject_assertion_util.py` for more examples. |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## Behaviour Details |
| 67 | + |
| 68 | +- The function always keeps the NHS number criterion. |
| 69 | +- If a match is found only after removing criteria, the failed criteria are logged in the format: |
| 70 | + - Failed criteria: Key: 'key1', Value: 'value1' |
| 71 | +- The function will only return `True` if all criteria match on the first attempt. |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## Best Practices |
| 76 | + |
| 77 | +- Use this utility to validate subject data in database-driven tests. |
| 78 | +- Review logs for failed criteria to diagnose why assertions did not pass. |
| 79 | +- Always provide the NHS number as part of your criteria. |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## Reference |
| 84 | + |
| 85 | +- [`utils/subject_assertion.py`](../../utils/subject_assertion.py) |
| 86 | +- [`tests_utils/test_subject_assertion_util.py`](../../tests_utils/test_subject_assertion_util.py) |
| 87 | +- [SubjectSelectionQueryBuilder Utility Guide](SubjectSelectionQueryBuilder.md) |
0 commit comments