Skip to content

Commit ebbe3df

Browse files
Addressed feeback given in the Jira ticket
1 parent 0d689cd commit ebbe3df

File tree

3 files changed

+111
-37
lines changed

3 files changed

+111
-37
lines changed
Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,85 @@
11
# Utility Guide: Subject Demographics
22

3-
The Subject Demographics utility allows for different updates to subjects to be made.<br>
4-
This includes the following:
3+
The Subject Demographics utility provides helper methods for interacting with and updating subject demographic data within the BCSS Playwright automation framework.<br>
4+
This includes:
55

6-
1. Updating a subjects DOB to the following age ranges:
7-
1. 50-70
8-
2. 75-100
6+
1. Updating a subject's date of birth (DOB) to a random value within specific age ranges.
7+
2. Navigating to the subject demographic page and updating fields such as postcode and DOB.
98

109
## Table of Contents
1110

1211
- [Utility Guide: Subject Demographics](#utility-guide-subject-demographics)
1312
- [Table of Contents](#table-of-contents)
14-
- [Using the Subject Demographics class](#using-the-subject-demographics-class)
13+
- [Using the SubjectDemographicUtil class](#using-the-subjectdemographicutil-class)
1514
- [Updating DOB](#updating-dob)
16-
- [Required Args](#required-args)
15+
- [Arguments](#arguments)
1716
- [How to use this method](#how-to-use-this-method)
17+
- [Other Utility Methods](#other-utility-methods)
1818

19-
## Using the Subject Demographics class
19+
## Using the SubjectDemographicUtil class
2020

21-
You can initialise the Subject Demographics class by using the following code in your test file:
21+
You can initialise the SubjectDemographicUtil class by using the following code in your test file:
2222

23-
from utils.subject_demographics import SubjectDemographicUtil
23+
```python
24+
from utils.subject_demographics import SubjectDemographicUtil
25+
```
2426

2527
## Updating DOB
2628

27-
Inside of the SubjectDemographicUtil class there is a method called `update_subject_dob`.<br>
28-
This is used to update the date of birth of a subject to a random age between 50-70 and 75-100 depending on if the argument `younger_subject` is set to True or False.<br>
29-
This method will navigate to the subject demographic page automatically and can be called from any page.
29+
The `update_subject_dob` method allows you to update a subject's date of birth, either to a specific value or to a random value within a chosen age range. The method will automatically navigate to the subject demographic page, fill in required fields (such as postcode if missing), and update the DOB.
3030

31-
### Required Args
31+
### Arguments
3232

33-
- nhs_no:
33+
- `nhs_no`:
3434
- Type: `str`
35-
- This is the NHS number of the subject you want to update
36-
- younger_subject:
35+
- The NHS number of the subject you want to update.
36+
- `random_dob`:
3737
- Type: `bool`
38-
- Whether you want the subject to be younger (50-70) or older (75-100).
38+
- If `True`, the DOB will be set to a random value within the specified age range.
39+
- If `False`, the DOB will be set to the value provided in `new_dob`.
40+
- `younger_subject`:
41+
- Type: `bool | None`
42+
- Determines the age range for the random DOB update (only used if `random_dob` is `True`):
43+
- `True`: Random age between 50-70 years old.
44+
- `False`: Random age between 75-100 years old.
45+
- `None`: Defaults to `False` (75-100 years old).
46+
- `new_dob`:
47+
- Type: `datetime | None`
48+
- The new date of birth to set (only used if `random_dob` is `False`).
3949

4050
### How to use this method
4151

42-
To use this method simply import the SubjectDemographicUtil class and call this method, providing the two arguments:
52+
To update a subject's DOB to a random value between 50-70:
4353

44-
nhs_no = "9468743977"
45-
SubjectDemographicUtil(page).update_subject_dob(nhs_no, False)
54+
```python
55+
nhs_no = "9468743977"
56+
SubjectDemographicUtil(page).update_subject_dob(nhs_no, random_dob=True, younger_subject=True)
57+
```
58+
59+
To update a subject's DOB to a random value between 75-100:
60+
61+
```python
62+
nhs_no = "9468743977"
63+
SubjectDemographicUtil(page).update_subject_dob(nhs_no, random_dob=True, younger_subject=False)
64+
```
65+
66+
To update a subject's DOB to a specific date:
67+
68+
```python
69+
from datetime import datetime
70+
nhs_no = "9468743977"
71+
new_dob = datetime(1960, 5, 15)
72+
SubjectDemographicUtil(page).update_subject_dob(nhs_no, random_dob=False, new_dob=new_dob)
73+
```
74+
75+
## Other Utility Methods
76+
77+
- `random_dob_within_range(younger: bool) -> datetime`
78+
- Generates a random date of birth within the specified age range:
79+
- If `younger` is `True`, returns a DOB for age 50-70.
80+
- If `younger` is `False`, returns a DOB for age 75-100.
81+
82+
- `random_datetime(start: datetime, end: datetime) -> datetime`
83+
Generates a random datetime between two datetime objects.
84+
85+
Refer to the source code in `utils/subject_demographics.py` for more details on available methods and their usage.

tests/smokescreen/test_compartment_6.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_compartment_6(page: Page, smokescreen_properties: dict) -> None:
4444
logging.info("Fetched subjects for investigation dataset updates.")
4545
nhs_no = subjects_df["subject_nhs_number"].iloc[0]
4646
logging.info(f"Selected NHS number for older subject: {nhs_no}")
47-
SubjectDemographicUtil(page).update_subject_dob(nhs_no, False)
47+
SubjectDemographicUtil(page).update_subject_dob(nhs_no, True, False)
4848
logging.info(
4949
f"Updated date of birth for NHS number {nhs_no} to indicate an older subject."
5050
)
@@ -65,7 +65,7 @@ def test_compartment_6(page: Page, smokescreen_properties: dict) -> None:
6565
logging.info("High-risk result for a younger subject")
6666
nhs_no = subjects_df["subject_nhs_number"].iloc[1]
6767
logging.info(f"Selected NHS number for younger subject: {nhs_no}")
68-
SubjectDemographicUtil(page).update_subject_dob(nhs_no, True)
68+
SubjectDemographicUtil(page).update_subject_dob(nhs_no, True, True)
6969
logging.info(
7070
f"Updated date of birth for NHS number {nhs_no} to indicate a younger subject."
7171
)
@@ -86,7 +86,7 @@ def test_compartment_6(page: Page, smokescreen_properties: dict) -> None:
8686
logging.info("LNPCP result for an older subject")
8787
nhs_no = subjects_df["subject_nhs_number"].iloc[2]
8888
logging.info(f"Selected NHS number for older subject: {nhs_no}")
89-
SubjectDemographicUtil(page).update_subject_dob(nhs_no, False)
89+
SubjectDemographicUtil(page).update_subject_dob(nhs_no, True, False)
9090
logging.info(
9191
f"Updated date of birth for NHS number {nhs_no} to indicate an older subject."
9292
)
@@ -105,12 +105,16 @@ def test_compartment_6(page: Page, smokescreen_properties: dict) -> None:
105105
logging.info("LNPCP result for a younger subject")
106106
nhs_no = subjects_df["subject_nhs_number"].iloc[3]
107107
logging.info(f"Selected NHS number for younger subject: {nhs_no}")
108-
SubjectDemographicUtil(page).update_subject_dob(nhs_no, True)
109-
logging.info(f"Updated date of birth for NHS number {nhs_no} to indicate a younger subject.")
108+
SubjectDemographicUtil(page).update_subject_dob(nhs_no, True, True)
109+
logging.info(
110+
f"Updated date of birth for NHS number {nhs_no} to indicate a younger subject."
111+
)
110112
InvestigationDatasetCompletion(page).complete_with_result(
111113
nhs_no, InvestigationDatasetResults.LNPCP
112114
)
113-
logging.info(f"Completed investigation dataset for NHS number {nhs_no} with result: LNPCP.")
115+
logging.info(
116+
f"Completed investigation dataset for NHS number {nhs_no} with result: LNPCP."
117+
)
114118
AfterInvestigationDatasetComplete(page).progress_episode_based_on_result(
115119
InvestigationDatasetResults.LNPCP, True
116120
)
@@ -129,7 +133,9 @@ def test_compartment_6(page: Page, smokescreen_properties: dict) -> None:
129133
AfterInvestigationDatasetComplete(page).progress_episode_based_on_result(
130134
InvestigationDatasetResults.NORMAL, True
131135
)
132-
logging.info(f"Progressed episode for NHS number {nhs_no_normal} based on result: NORMAL.")
136+
logging.info(
137+
f"Progressed episode for NHS number {nhs_no_normal} based on result: NORMAL."
138+
)
133139
# Batch processing for result letters
134140
logging.info("Starting batch processing for result letters.")
135141
batch_processing(

utils/subject_demographics.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,26 @@ class SubjectDemographicUtil:
1919
def __init__(self, page: Page):
2020
self.page = page
2121

22-
def update_subject_dob(self, nhs_no: str, younger_subject: bool) -> None:
22+
def update_subject_dob(
23+
self,
24+
nhs_no: str,
25+
random_dob: bool = False,
26+
younger_subject: bool | None = None,
27+
new_dob: datetime | None = None,
28+
) -> None:
2329
"""
2430
Navigates to the subject demographics page and updates a subject's date of birth.
2531
2632
Args:
2733
nhs_no (str): The NHS number of the subject you want to update.
2834
younger_subject (bool): Whether you want the subject to be younger (50-70) or older (75-100).
2935
"""
30-
if younger_subject:
31-
end_date = datetime.today() - relativedelta(years=50)
32-
start_date = datetime.today() - relativedelta(years=70)
33-
date = self.random_datetime(start_date, end_date)
36+
if random_dob:
37+
date = self.random_dob_within_range(
38+
younger_subject if younger_subject is not None else False
39+
)
3440
else:
35-
end_date = datetime.today() - relativedelta(years=75)
36-
start_date = datetime.today() - relativedelta(years=100)
37-
date = self.random_datetime(start_date, end_date)
41+
date = new_dob
3842

3943
logging.info(f"Navigating to subject demographic page for: {nhs_no}")
4044
BasePage(self.page).click_main_menu_link()
@@ -55,11 +59,35 @@ def update_subject_dob(self, nhs_no: str, younger_subject: bool) -> None:
5559

5660
current_dob = SubjectDemographicPage(self.page).get_dob_field_value()
5761
logging.info(f"Current DOB: {current_dob}")
58-
SubjectDemographicPage(self.page).fill_dob_input(date)
62+
if date is not None:
63+
SubjectDemographicPage(self.page).fill_dob_input(date)
64+
else:
65+
raise ValueError("Date of birth is None. Cannot fill DOB input.")
5966
SubjectDemographicPage(self.page).click_update_subject_data_button()
6067
updated_dob = SubjectDemographicPage(self.page).get_dob_field_value()
6168
logging.info(f"Updated DOB: {updated_dob}")
6269

70+
def random_dob_within_range(self, younger: bool) -> datetime:
71+
"""
72+
Generate a random date of birth within a specified range.
73+
74+
Args:
75+
younger (bool): If True, generate a date of birth for a subject aged between 50 and 70.
76+
If False, generate a date of birth for a subject aged between 75 and 100.
77+
78+
Returns:
79+
datetime: the newly generated date of birth
80+
"""
81+
if younger:
82+
end_date = datetime.today() - relativedelta(years=50)
83+
start_date = datetime.today() - relativedelta(years=70)
84+
date = self.random_datetime(start_date, end_date)
85+
else:
86+
end_date = datetime.today() - relativedelta(years=75)
87+
start_date = datetime.today() - relativedelta(years=100)
88+
date = self.random_datetime(start_date, end_date)
89+
return date
90+
6391
def random_datetime(self, start: datetime, end: datetime) -> datetime:
6492
"""
6593
Generate a random datetime between two datetime objects.

0 commit comments

Comments
 (0)