|
1 | | -# Utility Guide: Subject Creation Utility |
2 | | - |
3 | | -This guide explains the purpose and usage of the `CreateSubjectSteps` utility found in [`utils/oracle/subject_creation_util.py`](../../utils/oracle/subject_creation_util.py). |
4 | | -It is designed to automate the creation of custom subjects for test scenarios, supporting a variety of criteria such as age, GP practice, and more. |
5 | | - |
6 | | ---- |
7 | | - |
8 | | -## Table of Contents |
9 | | - |
10 | | -- [Utility Guide: Subject Creation Utility](#utility-guide-subject-creation-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 | | - - [Supported Criteria](#supported-criteria) |
17 | | - - [Best Practices](#best-practices) |
18 | | - - [Reference](#reference) |
19 | | - |
20 | | ---- |
21 | | - |
22 | | -## Overview |
23 | | - |
24 | | -The `CreateSubjectSteps` class provides methods to create, log, and verify subjects in the BCSS Playwright test suite. |
25 | | -It interacts with the Oracle database to insert subjects with specific attributes, making it easy to set up test data for automated scenarios. |
26 | | - |
27 | | ---- |
28 | | - |
29 | | -## Required Arguments |
30 | | - |
31 | | -Each function in this utility requires specific arguments: |
32 | | - |
33 | | -- `subject_requirements` (dict): |
34 | | - A dictionary mapping human-readable field labels to desired values. |
35 | | - Example: `{"age": "30"}` or `{"gp practice": "C81014"}` |
36 | | - |
37 | | ---- |
38 | | - |
39 | | -## How It Works |
40 | | - |
41 | | -- The utility generates a random subject and updates its attributes based on the provided requirements. |
42 | | -- Supported criteria are mapped to subject fields (e.g., age, NHS number, GP practice). |
43 | | -- The subject is inserted into the database using the `SubjectRepository`. |
44 | | -- The utility provides helper methods for safe string/date formatting and subject existence checks. |
45 | | - |
46 | | ---- |
47 | | - |
48 | | -## Example Usage |
49 | | - |
50 | | -Below are examples of how to use `CreateSubjectSteps` in your tests: |
51 | | - |
52 | | -```python |
53 | | -import pytest |
54 | | -from utils.oracle.subject_creation_util import CreateSubjectSteps |
55 | | - |
56 | | -@pytest.fixture |
57 | | -def subject_steps(): |
58 | | - return CreateSubjectSteps() |
59 | | - |
60 | | -def test_create_custom_subject_age(subject_steps): |
61 | | - expected_age = 30 |
62 | | - requirements = {"age": str(expected_age)} |
63 | | - nhs_no = subject_steps.create_custom_subject(requirements) |
64 | | - |
65 | | - |
66 | | -def test_create_custom_subject_age_yd(subject_steps): |
67 | | - expected_years = 65 |
68 | | - expected_days = 25 |
69 | | - requirements = {"age (y/d)": f"{expected_years}/{expected_days}"} |
70 | | - nhs_no = subject_steps.create_custom_subject(requirements) |
71 | | - |
72 | | -def test_create_custom_subject_gp_practice(subject_steps): |
73 | | - requirements = {"gp practice": "C81014"} |
74 | | - nhs_no = subject_steps.create_custom_subject(requirements) |
75 | | - |
76 | | -def test_create_custom_subject_active_gp_practice(subject_steps): |
77 | | - requirements = {"active gp practice in hub/sc": "BCS01/BCS001"} |
78 | | - nhs_no = subject_steps.create_custom_subject(requirements) |
79 | | - |
80 | | -def test_create_custom_subject_inactive_gp_practice(subject_steps): |
81 | | - requirements = {"inactive gp practice": ""} |
82 | | - nhs_no = subject_steps.create_custom_subject(requirements) |
83 | | - |
84 | | -def test_create_custom_subject_invalid_criteria(subject_steps): |
85 | | - requirements = {"invalid_field": "value"} |
86 | | - with pytest.raises(ValueError) as excinfo: |
87 | | - subject_steps.create_custom_subject(requirements) |
88 | | - assert "The criteria provided (invalid_field) is not valid" in str(excinfo.value) |
89 | | -``` |
90 | | - |
91 | | ---- |
92 | | - |
93 | | -See `tests_utils/test_subject_creation_util.py` for more examples. |
94 | | - |
95 | | ---- |
96 | | - |
97 | | -## Supported Criteria |
98 | | - |
99 | | -You can create subjects with the following criteria: |
100 | | - |
101 | | -| Criteria Key | Example Value | Description | |
102 | | -|------------------------------------|----------------------|------------------------------------------------------------------| |
103 | | -| `nhs number` | `"9953536309"` | Sets the NHS number | |
104 | | -| `age` | `"30"` | Sets the subject's age in years | |
105 | | -| `age (y/d)` | `"65/25"` | Sets the subject's age in years and days | |
106 | | -| `gp practice` | `"C81014"` | Sets the GP practice code | |
107 | | -| `active gp practice in hub/sc` | `"BCS01/BCS001"` | Sets an active GP practice linked to both hub and screening centre| |
108 | | -| `inactive gp practice` | `""` | Sets an inactive GP practice | |
109 | | - |
110 | | -If an invalid criteria is provided, a `ValueError` will be raised. |
111 | | - |
112 | | ---- |
113 | | - |
114 | | -## Best Practices |
115 | | - |
116 | | -- Use this utility in test setup steps to ensure subjects have the required attributes before running scenario-specific tests. |
117 | | -- Always check for exceptions when providing custom criteria. |
118 | | -- Use the helper methods (`safe_string`, `safe_date`, `get_subject_details`) for logging and debugging. |
119 | | - |
120 | | ---- |
121 | | - |
122 | | -## Reference |
123 | | - |
124 | | -- [`utils/oracle/subject_creation_util.py`](../../utils/oracle/subject_creation_util.py) |
125 | | -- [`tests_utils/test_subject_creation_util.py`](../../tests_utils/test_subject_creation_util.py) |
126 | | -- [SubjectDemographics Utility Guide](SubjectDemographics.md) |
127 | | -- [DateTimeUtility Guide](DateTimeUtility.md) |
128 | | - |
129 | | ---- |
130 | | - |
131 | | -For more details on each method's implementation, refer to the source code in `utils/oracle/subject_creation_util.py`. |
| 1 | +# Utility Guide: Subject Creation Utility |
| 2 | + |
| 3 | +This guide explains the purpose and usage of the `CreateSubjectSteps` utility found in [`utils/oracle/subject_creation_util.py`](../../utils/oracle/subject_creation_util.py). |
| 4 | +It is designed to automate the creation of custom subjects for test scenarios, supporting a variety of criteria such as age, GP practice, and more. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## Table of Contents |
| 9 | + |
| 10 | +- [Utility Guide: Subject Creation Utility](#utility-guide-subject-creation-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 | + - [Supported Criteria](#supported-criteria) |
| 17 | + - [Best Practices](#best-practices) |
| 18 | + - [Reference](#reference) |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Overview |
| 23 | + |
| 24 | +The `CreateSubjectSteps` class provides methods to create, log, and verify subjects in the BCSS Playwright test suite. It interacts with the Oracle database to insert subjects with specific attributes, making it easy to set up test data for automated scenarios. |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## Required Arguments |
| 29 | + |
| 30 | +Each function in this utility requires specific arguments: |
| 31 | + |
| 32 | +- `subject_requirements` (dict): |
| 33 | + A dictionary mapping human-readable field labels to desired values. |
| 34 | + Example: `{"age": "30"}` or `{"gp practice": "C81014"}` |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## How It Works |
| 39 | + |
| 40 | +- The utility generates a random subject and updates its attributes based on the provided requirements. |
| 41 | +- Supported criteria are mapped to subject fields (e.g., age, NHS number, GP practice). |
| 42 | +- The subject is inserted into the database using the `SubjectRepository`. |
| 43 | +- The utility provides helper methods for safe string/date formatting and subject existence checks. |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## Example Usage |
| 48 | + |
| 49 | +Below are examples of how to use `CreateSubjectSteps` in your tests: |
| 50 | + |
| 51 | +```python |
| 52 | +import pytest |
| 53 | +from utils.oracle.subject_creation_util import CreateSubjectSteps |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def subject_steps(): |
| 57 | + return CreateSubjectSteps() |
| 58 | + |
| 59 | +def test_create_custom_subject_age(subject_steps): |
| 60 | + expected_age = 30 |
| 61 | + requirements = {"age": str(expected_age)} |
| 62 | + nhs_no = subject_steps.create_custom_subject(requirements) |
| 63 | + |
| 64 | + |
| 65 | +def test_create_custom_subject_age_yd(subject_steps): |
| 66 | + expected_years = 65 |
| 67 | + expected_days = 25 |
| 68 | + requirements = {"age (y/d)": f"{expected_years}/{expected_days}"} |
| 69 | + nhs_no = subject_steps.create_custom_subject(requirements) |
| 70 | + |
| 71 | +def test_create_custom_subject_gp_practice(subject_steps): |
| 72 | + requirements = {"gp practice": "C81014"} |
| 73 | + nhs_no = subject_steps.create_custom_subject(requirements) |
| 74 | + |
| 75 | +def test_create_custom_subject_active_gp_practice(subject_steps): |
| 76 | + requirements = {"active gp practice in hub/sc": "BCS01/BCS001"} |
| 77 | + nhs_no = subject_steps.create_custom_subject(requirements) |
| 78 | + |
| 79 | +def test_create_custom_subject_inactive_gp_practice(subject_steps): |
| 80 | + requirements = {"inactive gp practice": ""} |
| 81 | + nhs_no = subject_steps.create_custom_subject(requirements) |
| 82 | + |
| 83 | +def test_create_custom_subject_invalid_criteria(subject_steps): |
| 84 | + requirements = {"invalid_field": "value"} |
| 85 | + with pytest.raises(ValueError) as excinfo: |
| 86 | + subject_steps.create_custom_subject(requirements) |
| 87 | + assert "The criteria provided (invalid_field) is not valid" in str(excinfo.value) |
| 88 | +``` |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +See `tests_utils/test_subject_creation_util.py` for more examples. |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## Supported Criteria |
| 97 | + |
| 98 | +You can create subjects with the following criteria: |
| 99 | + |
| 100 | +| Criteria Key | Example Value | Description | |
| 101 | +|------------------------------------|----------------------|------------------------------------------------------------------| |
| 102 | +| `nhs number` | `"9953536309"` | Sets the NHS number | |
| 103 | +| `age` | `"30"` | Sets the subject's age in years | |
| 104 | +| `age (y/d)` | `"65/25"` | Sets the subject's age in years and days | |
| 105 | +| `gp practice` | `"C81014"` | Sets the GP practice code | |
| 106 | +| `active gp practice in hub/sc` | `"BCS01/BCS001"` | Sets an active GP practice linked to both hub and screening centre| |
| 107 | +| `inactive gp practice` | `""` | Sets an inactive GP practice | |
| 108 | + |
| 109 | +If an invalid criteria is provided, a `ValueError` will be raised. |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +## Best Practices |
| 114 | + |
| 115 | +- Use this utility in test setup steps to ensure subjects have the required attributes before running scenario-specific tests. |
| 116 | +- Always check for exceptions when providing custom criteria. |
| 117 | +- Use the helper methods (`safe_string`, `safe_date`, `get_subject_details`) for logging and debugging. |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## Reference |
| 122 | + |
| 123 | +- [`utils/oracle/subject_creation_util.py`](../../utils/oracle/subject_creation_util.py) |
| 124 | +- [`tests_utils/test_subject_creation_util.py`](../../tests_utils/test_subject_creation_util.py) |
| 125 | +- [SubjectDemographics Utility Guide](SubjectDemographics.md) |
| 126 | +- [DateTimeUtility Guide](DateTimeUtility.md) |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +For more details on each method's implementation, refer to the source code in `utils/oracle/subject_creation_util.py`. |
0 commit comments