Skip to content

Commit 7be7537

Browse files
Fixing file format errors
1 parent d780012 commit 7be7537

File tree

3 files changed

+177
-177
lines changed

3 files changed

+177
-177
lines changed
Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,87 @@
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)
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import pytest
2-
from utils.subject_assertion import subject_assertion
3-
4-
pytestmark = [pytest.mark.utils_local]
5-
6-
7-
def test_subject_assertion_true():
8-
nhs_number = "9233639266"
9-
criteria = {"screening status": "Inactive", "subject age": "> 28"}
10-
assert subject_assertion(nhs_number, criteria) is True
11-
12-
13-
def test_subject_assertion_false():
14-
nhs_number = "9233639266"
15-
criteria = {"screening status": "Call", "subject age": "< 28"}
16-
assert subject_assertion(nhs_number, criteria) is False
17-
18-
19-
def test_subject_assertion_false_with_some_true():
20-
nhs_number = "9233639266"
21-
criteria = {"screening status": "Inactive", "subject age": "< 28"}
22-
assert subject_assertion(nhs_number, criteria) is False
1+
import pytest
2+
from utils.subject_assertion import subject_assertion
3+
4+
pytestmark = [pytest.mark.utils_local]
5+
6+
7+
def test_subject_assertion_true():
8+
nhs_number = "9233639266"
9+
criteria = {"screening status": "Inactive", "subject age": "> 28"}
10+
assert subject_assertion(nhs_number, criteria) is True
11+
12+
13+
def test_subject_assertion_false():
14+
nhs_number = "9233639266"
15+
criteria = {"screening status": "Call", "subject age": "< 28"}
16+
assert subject_assertion(nhs_number, criteria) is False
17+
18+
19+
def test_subject_assertion_false_with_some_true():
20+
nhs_number = "9233639266"
21+
criteria = {"screening status": "Inactive", "subject age": "< 28"}
22+
assert subject_assertion(nhs_number, criteria) is False

utils/subject_assertion.py

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
1-
from utils.oracle.subject_selection_query_builder import SubjectSelectionQueryBuilder
2-
from utils.oracle.oracle import OracleDB
3-
from classes.subject import Subject
4-
from classes.user import User
5-
import logging
6-
7-
8-
def subject_assertion(nhs_number: str, criteria: dict) -> bool:
9-
"""
10-
Asserts that a subject with the given NHS number exists and matches the provided criteria.
11-
Args:
12-
nhs_number (str): The NHS number of the subject to find.
13-
criteria (dict): A dictionary of criteria to match against the subject's attributes.
14-
Returns:
15-
bool: True if the subject matches the provided criteria, False if it does not.
16-
"""
17-
nhs_no_criteria = {"nhs number": nhs_number}
18-
subject = Subject()
19-
user = User()
20-
builder = SubjectSelectionQueryBuilder()
21-
22-
query, bind_vars = builder.build_subject_selection_query(
23-
criteria=nhs_no_criteria,
24-
user=user,
25-
subject=subject,
26-
subjects_to_retrieve=1,
27-
)
28-
29-
subject_df = OracleDB().execute_query(query, bind_vars)
30-
subject = Subject.from_dataframe_row(subject_df.iloc[0])
31-
32-
criteria["nhs number"] = nhs_number
33-
query, bind_vars = builder.build_subject_selection_query(
34-
criteria=criteria,
35-
user=user,
36-
subject=subject,
37-
subjects_to_retrieve=1,
38-
)
39-
40-
df = OracleDB().execute_query(query, bind_vars)
41-
42-
if nhs_number in df["subject_nhs_number"].values:
43-
return True
44-
45-
# Try removing criteria one by one (except nhs number)
46-
failed_criteria = []
47-
criteria_keys = [key for key in criteria if key != "nhs number"]
48-
for key in criteria_keys:
49-
reduced_criteria = {key: value for key, value in criteria.items() if key != key}
50-
query, bind_vars = builder.build_subject_selection_query(
51-
criteria=reduced_criteria,
52-
user=user,
53-
subject=subject,
54-
subjects_to_retrieve=1,
55-
)
56-
df = OracleDB().execute_query(query, bind_vars)
57-
if nhs_number in df["subject_nhs_number"].values:
58-
failed_criteria.append((key, criteria[key]))
59-
break
60-
else:
61-
failed_criteria.append((key, criteria[key]))
62-
63-
if failed_criteria:
64-
log_message = "Subject Assertion Failed\nFailed criteria:\n" + "\n".join(
65-
[f"Key: '{key}' - Value: '{value}'" for key, value in failed_criteria]
66-
)
67-
logging.error(log_message)
68-
return False
1+
from utils.oracle.subject_selection_query_builder import SubjectSelectionQueryBuilder
2+
from utils.oracle.oracle import OracleDB
3+
from classes.subject import Subject
4+
from classes.user import User
5+
import logging
6+
7+
8+
def subject_assertion(nhs_number: str, criteria: dict) -> bool:
9+
"""
10+
Asserts that a subject with the given NHS number exists and matches the provided criteria.
11+
Args:
12+
nhs_number (str): The NHS number of the subject to find.
13+
criteria (dict): A dictionary of criteria to match against the subject's attributes.
14+
Returns:
15+
bool: True if the subject matches the provided criteria, False if it does not.
16+
"""
17+
nhs_no_criteria = {"nhs number": nhs_number}
18+
subject = Subject()
19+
user = User()
20+
builder = SubjectSelectionQueryBuilder()
21+
22+
query, bind_vars = builder.build_subject_selection_query(
23+
criteria=nhs_no_criteria,
24+
user=user,
25+
subject=subject,
26+
subjects_to_retrieve=1,
27+
)
28+
29+
subject_df = OracleDB().execute_query(query, bind_vars)
30+
subject = Subject.from_dataframe_row(subject_df.iloc[0])
31+
32+
criteria["nhs number"] = nhs_number
33+
query, bind_vars = builder.build_subject_selection_query(
34+
criteria=criteria,
35+
user=user,
36+
subject=subject,
37+
subjects_to_retrieve=1,
38+
)
39+
40+
df = OracleDB().execute_query(query, bind_vars)
41+
42+
if nhs_number in df["subject_nhs_number"].values:
43+
return True
44+
45+
# Try removing criteria one by one (except nhs number)
46+
failed_criteria = []
47+
criteria_keys = [key for key in criteria if key != "nhs number"]
48+
for key in criteria_keys:
49+
reduced_criteria = {key: value for key, value in criteria.items() if key != key}
50+
query, bind_vars = builder.build_subject_selection_query(
51+
criteria=reduced_criteria,
52+
user=user,
53+
subject=subject,
54+
subjects_to_retrieve=1,
55+
)
56+
df = OracleDB().execute_query(query, bind_vars)
57+
if nhs_number in df["subject_nhs_number"].values:
58+
failed_criteria.append((key, criteria[key]))
59+
break
60+
else:
61+
failed_criteria.append((key, criteria[key]))
62+
63+
if failed_criteria:
64+
log_message = "Subject Assertion Failed\nFailed criteria:\n" + "\n".join(
65+
[f"Key: '{key}' - Value: '{value}'" for key, value in failed_criteria]
66+
)
67+
logging.error(log_message)
68+
return False

0 commit comments

Comments
 (0)