Skip to content

Commit 825766f

Browse files
Refactoring Code and adding queries
1 parent ce9b15b commit 825766f

File tree

4 files changed

+130
-77
lines changed

4 files changed

+130
-77
lines changed

pages/contacts_list/maintain_contacts_page.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,18 @@ def click_search_button(self) -> None:
5050
"""Click the search button to perform a search"""
5151
self.click(self.search_button)
5252

53-
def click_person_link_from_suranme(self, surname: str) -> None:
53+
def click_person_link_from_surname(self, surname: str) -> None:
5454
"""
5555
Clicks on the link containing the person's surname to go to the edit contact page
5656
Args:
5757
surname (str): The surname of the subject
5858
"""
59-
self.click(self.page.get_by_role("link", name=surname))
59+
self.click(self.page.get_by_role("link", name=surname).last)
6060

6161
def click_person_link_from_forename(self, forename: str) -> None:
6262
"""
6363
Clicks on the link containing the person's surname to go to the edit contact page
6464
Args:
6565
forename (str): The forename of the subject
6666
"""
67-
self.click(self.page.get_by_role("link", name=forename))
68-
69-
def click_first_correct_link(self) -> None:
70-
"""Clicks on the first link in the search results that is a BCSS user and has a code"""
71-
self.search_table.click_surname_if_bcss_user_and_has_code()
67+
self.click(self.page.get_by_role("link", name=forename).last)

tests/regression/regression_tests/test_regression_test_setup_steps.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
from utils.oracle.oracle_specific_functions import (
1616
set_org_parameter_value,
1717
check_parameter,
18+
build_accredited_screening_colonoscopist_query,
19+
get_accredited_screening_colonoscopist_in_bcs001,
1820
)
1921

20-
from utils.oracle.oracle import OracleDB
21-
2222

2323
def test_allow_10_minute_colonsocopy_assessment_appointments(page: Page) -> None:
2424
"""
@@ -50,36 +50,31 @@ def test_ensure_the_is_accredited_screening_colonoscopist_with_current_resect_an
5050
Scenario: 2: Ensure there is an Accredited Screening Colonoscopist with current Resect & Discard accreditation
5151
"""
5252
UserTools.user_login(page, "BCSS Bureau Staff at X26")
53+
54+
current_df = build_accredited_screening_colonoscopist_query("Current")
55+
if current_df.empty:
56+
pytest.skip(
57+
"No Accredited Screening Colonoscopist with current Resect & Discard accreditation found."
58+
)
59+
expired_df = build_accredited_screening_colonoscopist_query("Expiring soon")
60+
if expired_df.empty:
61+
pytest.skip(
62+
"No Accredited Screening Colonoscopist with expiring Resect & Discard accreditation found."
63+
)
64+
person_df = get_accredited_screening_colonoscopist_in_bcs001()
65+
if person_df.empty:
66+
pytest.fail("No Accredited Screening Colonoscopist found in the database.")
67+
5368
BasePage(page).go_to_contacts_list_page()
5469
ContactsListPage(page).go_to_maintain_contacts_page()
55-
query = """
56-
SELECT
57-
prs.prs_id,
58-
prs.person_family_name,
59-
prs.person_given_name,
60-
prs.gmc_code,
61-
pio.pio_id,
62-
pio.role_id,
63-
org.org_code,
64-
org.org_name
65-
FROM person prs
66-
INNER JOIN person_in_org pio ON prs.prs_id = pio.prs_id
67-
INNER JOIN org ON org.org_id = pio.org_id
68-
WHERE 1=1
69-
AND pio.role_id = (SELECT valid_value_id FROM valid_values WHERE description = 'Accredited Screening Colonoscopist')
70-
AND org.org_code = 'BCS001'
71-
AND TRUNC(SYSDATE) BETWEEN TRUNC(pio.start_date) AND NVL(pio.end_date, SYSDATE)
72-
AND pio.is_bcss_user = 1
73-
ORDER BY prs.prs_id
74-
FETCH FIRST 1 ROWS ONLY
75-
"""
76-
person_df = OracleDB().execute_query(query)
70+
7771
surname = person_df.iloc[0]["person_family_name"]
7872
forename = person_df.iloc[0]["person_given_name"]
7973
MaintainContactsPage(page).fill_surname_input_field(surname)
8074
MaintainContactsPage(page).fill_forenames_input_field(forename)
8175
MaintainContactsPage(page).click_search_button()
82-
MaintainContactsPage(page).click_first_correct_link()
76+
MaintainContactsPage(page).click_person_link_from_surname(surname)
77+
8378
EditContactPage(page).click_view_resect_and_discard_link()
8479
ResectAndDiscardAccreditationHistoryPage(page).verify_heading_is_correct()
8580
ResectAndDiscardAccreditationHistoryPage(
@@ -89,6 +84,7 @@ def test_ensure_the_is_accredited_screening_colonoscopist_with_current_resect_an
8984
ResectAndDiscardAccreditationHistoryPage(
9085
page
9186
).add_new_period_of_resect_and_discard_accerditation(date=yesterday)
87+
9288
BasePage(page).click_back_button()
9389
end_date = yesterday + relativedelta(years=2)
9490
EditContactPage(page).assert_value_for_label_in_edit_contact_table(

utils/oracle/oracle_specific_functions.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,112 @@ def get_investigation_dataset_polyp_algorithm_size(
737737
return polyp_algorithm_size
738738

739739

740+
def build_accredited_screening_colonoscopist_query(query_type: str) -> pd.DataFrame:
741+
"""
742+
Builds a SQL query for either 'Current' or 'Expiring soon' accreditations.
743+
744+
Args:
745+
query_type (str): Either "Current" or "Expiring soon".
746+
747+
Returns:
748+
str: The SQL query string.
749+
750+
Raises:
751+
ValueError: If query_type is invalid.
752+
"""
753+
base_query = """
754+
SELECT
755+
prs.prs_id,
756+
prs.person_family_name,
757+
prs.person_given_name,
758+
prs.gmc_code,
759+
pio305730.pio_id
760+
FROM person prs
761+
LEFT OUTER JOIN person_in_org pio305730 ON pio305730.prs_id = prs.prs_id
762+
AND NOT EXISTS (
763+
SELECT 1
764+
FROM person_in_org pio305730_2
765+
WHERE pio305730.prs_id = pio305730_2.prs_id
766+
AND pio305730.role_id = pio305730_2.role_id
767+
AND pio305730.pio_id != pio305730_2.pio_id
768+
)
769+
AND pio305730.role_id = 305730
770+
AND TRUNC(SYSDATE) BETWEEN pio305730.start_date AND NVL(pio305730.end_date, TO_DATE('31/12/3999','DD/MM/YYYY'))
771+
AND pio305730.org_id IN (
772+
SELECT oio305730.org_id
773+
FROM org oio305730
774+
WHERE oio305730.org_code = 'BCS001'
775+
)
776+
INNER JOIN person_accreditation pa ON pa.prs_id = prs.prs_id
777+
AND pa.accreditation_id = (
778+
SELECT pax.accreditation_id
779+
FROM person_accreditation pax
780+
WHERE pax.prs_id = prs.prs_id
781+
AND pax.accreditation_type_id = 305730
782+
ORDER BY pax.start_date DESC
783+
FETCH FIRST 1 ROW ONLY
784+
)
785+
AND pa.accreditation_type_id = 305730
786+
AND pa.start_date <= TRUNC(SYSDATE)
787+
"""
788+
789+
# condition for accreditation end date
790+
if query_type == "Expiring soon":
791+
condition = """ AND pa.end_date > TRUNC(SYSDATE)
792+
AND pa.end_date <= ADD_MONTHS(TRUNC(SYSDATE), 5)"""
793+
elif query_type == "Current":
794+
condition = """ AND pa.end_date > ADD_MONTHS(TRUNC(SYSDATE), 5)"""
795+
else:
796+
raise ValueError("query_type must be either 'Current' or 'Expiring soon'")
797+
798+
full_query = f"""{base_query}
799+
{condition}
800+
WHERE 1=1
801+
AND pio305730.pio_id IS NOT NULL
802+
FETCH FIRST 1 ROWS ONLY"""
803+
804+
df = OracleDB().execute_query(full_query)
805+
return df
806+
807+
808+
def get_accredited_screening_colonoscopist_in_bcs001() -> pd.DataFrame:
809+
query = """
810+
SELECT
811+
prs.prs_id,
812+
prs.person_family_name,
813+
prs.person_given_name,
814+
prs.gmc_code,
815+
pio305730.pio_id
816+
FROM person prs
817+
LEFT OUTER JOIN person_in_org pio305730 ON pio305730.prs_id = prs.prs_id
818+
AND NOT EXISTS (
819+
SELECT 1
820+
FROM person_in_org pio305730_2
821+
WHERE pio305730.prs_id = pio305730_2.prs_id
822+
AND pio305730.role_id = pio305730_2.role_id
823+
AND pio305730.pio_id != pio305730_2.pio_id
824+
)
825+
AND pio305730.role_id = 305730
826+
AND TRUNC(SYSDATE) BETWEEN pio305730.start_date AND NVL(pio305730.end_date, TO_DATE('31/12/3999','DD/MM/YYYY'))
827+
AND pio305730.org_id IN (
828+
SELECT oio305730.org_id
829+
FROM org oio305730
830+
WHERE oio305730.org_code = 'BCS001'
831+
)
832+
INNER JOIN person_accreditation pa ON pa.prs_id = prs.prs_id
833+
AND pa.accreditation_id = (
834+
SELECT pax.accreditation_id
835+
FROM person_accreditation pax
836+
WHERE pax.prs_id = prs.prs_id
837+
AND pax.accreditation_type_id = 305730
838+
ORDER BY pax.start_date DESC
839+
FETCH FIRST 1 ROW ONLY
840+
)
841+
"""
842+
df = OracleDB().execute_query(query)
843+
return df
844+
845+
740846
class SubjectSelector:
741847
"""
742848
Provides helper methods for selecting screening subjects based on preconditions

utils/table_util.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -365,51 +365,6 @@ def get_row_where(self, criteria: dict[str, str]) -> Locator | None:
365365
return self.pick_row(i)
366366
return None
367367

368-
def click_surname_if_bcss_user_and_has_code(self):
369-
"""
370-
Clicks on the surname link of the first row where:
371-
- 'BCSS User?' column is 'Yes'
372-
- 'User Code' is not '-'
373-
"""
374-
surname_col = self.get_column_index("Surname")
375-
user_code_col = self.get_column_index("User Code")
376-
bcss_col = self.get_column_index("BCSS User?")
377-
378-
if -1 in (surname_col, user_code_col, bcss_col):
379-
raise ValueError("One or more required columns not found")
380-
381-
rows = self.table.locator(self.tbody_tr_string)
382-
383-
for i in range(rows.count()):
384-
row = rows.nth(i)
385-
386-
# Skip rows with no <td> cells
387-
cell_count = row.locator("td").count()
388-
if cell_count == 0:
389-
continue
390-
391-
# Skip rows that don’t have enough columns
392-
if cell_count < max(user_code_col, bcss_col, surname_col):
393-
continue
394-
395-
user_code = (
396-
row.locator(f"td:nth-child({user_code_col})").inner_text().strip()
397-
)
398-
bcss_value = row.locator(f"td:nth-child({bcss_col})").inner_text().strip()
399-
400-
if bcss_value.lower() == "yes" and user_code != "-":
401-
surname_link = row.locator(f"td:nth-child({surname_col}) a")
402-
if surname_link.count() > 0:
403-
surname_link.click()
404-
return
405-
else:
406-
logging.error(f"No clickable surname link found in row {i+1}")
407-
return
408-
409-
logging.warning(
410-
"No matching row found where BCSS User? == 'Yes' and User Code != '-'"
411-
)
412-
413368
def verify_value_for_label(self, label_text: str, expected_text: str) -> None:
414369
"""
415370
Verifies that the value cell (2nd <td>) in the row where the first <td>

0 commit comments

Comments
 (0)