Skip to content

Commit 41e34f3

Browse files
Changing oracle.py into a class
Altering file and function names closeing the page at the end of compartment 1
1 parent a8f5741 commit 41e34f3

File tree

6 files changed

+102
-120
lines changed

6 files changed

+102
-120
lines changed

tests/Smokescreen/test_compartment_1.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
from playwright.sync_api import Page
33
from my_pages import *
44
from utils.batch_processing import batch_processing
5-
from utils.oracle import exec_bcss_timed_events
5+
from utils.oracle import OracleDB
66

77
# To Do:
8-
# Create a generic click() function -> this aims to solve an issue where sometimes it thinks it has clicked the element but the page does not change
8+
# Create a common click() function -> this aims to solve an issue where sometimes it thinks it has clicked the element but the page does not change
9+
# playwright._impl._errors.Error: Dialog.accept: Cannot accept dialog which is already handled! - Have a look at removing this error (probably from line 67 of batch_processing)
910

1011
@pytest.mark.wip
1112
def test_example(page: Page) -> None:
@@ -35,16 +36,17 @@ def test_example(page: Page) -> None:
3536

3637
# Print the batch of Pre-Invitation Letters
3738
s1_nhs_no = batch_processing(page, "S1", "Pre-invitation (FIT)", "S9 - Pre-invitation Sent")
38-
exec_bcss_timed_events(s1_nhs_no)
3939
batch_processing(page, "S1", "Pre-invitation (FIT) (digital leaflet)", "S9 - Pre-invitation Sent")
40+
OracleDB().exec_bcss_timed_events(s1_nhs_no)
4041

4142
# Print the batch of Invitation & Test Kit Letters
4243
s9_nhs_no = batch_processing(page, "S9", "Invitation & Test Kit (FIT)", "S10 - Invitation & Test Kit Sent")
43-
exec_bcss_timed_events(s9_nhs_no)
44+
OracleDB().exec_bcss_timed_events(s9_nhs_no)
4445

4546
# Print a set of reminder letters
4647
batch_processing(page, "S10", "Test Kit Reminder", "S19 - Reminder of Initial Test Sent")
4748

4849
# Log out
4950
NavigationBar(page).click_log_out_link()
5051
Logout(page).verify_log_out_page()
52+
page.close()

utils/batch_processing.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
from pages.active_batch_list_page import ActiveBatchList
55
from pages.manage_active_batch_page import ManageActiveBatch
66
from pages.archived_batch_list_page import ArchivedBatchList
7-
from utils.extract_nhs_no_from_pdf import pdf_reader
8-
from utils.csv_reader import csv_reader
9-
from utils.subject_search_by_nhs_no import subject_search_by_nhs_no
10-
from utils.oracle import database_connection_exec, database_connection_query
7+
from utils.pdf_reader import extract_nhs_no_from_pdf
8+
from utils.csv_reader import convert_csv_to_df
9+
from utils.screening_subject_page_searcher import verify_subject_event_status_by_nhs_no
1110
import os
1211
import pytest
1312
from playwright.sync_api import Page
@@ -56,10 +55,10 @@ def batch_processing(page: Page, batch_type: str, batch_description: str, latest
5655
# Wait for the download process to complete and save the downloaded file in a temp folder
5756
download_file.save_as(file)
5857
if file.endswith(".pdf"):
59-
nhs_no = pdf_reader(file)
58+
nhs_no = extract_nhs_no_from_pdf(file)
6059
os.remove(file) # Deletes the file after extracting the necessary data
6160
elif file.endswith(".csv"):
62-
csv_df = csv_reader(file) # Currently no use in compartment 1, will be necessary for future compartments
61+
csv_df = convert_csv_to_df(file) # Currently no use in compartment 1, will be necessary for future compartments
6362
os.remove(file) # Deletes the file after extracting the necessary data
6463

6564
# This loops through each Confirm printed button and clicks each one
@@ -75,5 +74,5 @@ def batch_processing(page: Page, batch_type: str, batch_description: str, latest
7574
ArchivedBatchList(page).enter_id_filter(link_text)
7675
ArchivedBatchList(page).verify_table_data(link_text)
7776

78-
subject_search_by_nhs_no(page, nhs_no, latest_event_status)
77+
verify_subject_event_status_by_nhs_no(page, nhs_no, latest_event_status)
7978
return nhs_no

utils/csv_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pandas as pd
22

3-
def csv_reader(file: str):
3+
def convert_csv_to_df(file: str):
44
csv_df = pd.read_csv(file)
55
return csv_df

utils/oracle.py

Lines changed: 87 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -2,113 +2,94 @@
22
import os
33
from dotenv import load_dotenv
44

5-
def database_connection_exec(procedure: str): # To use when "exec xxxx" (stored procedures)
6-
load_dotenv()
7-
un = os.getenv("un")
8-
cs = os.getenv("cs")
9-
pw = os.getenv("pw")
10-
11-
try:
12-
print("Attempting DB connection...")
13-
conn = oracledb.connect(user=un, password=pw, dsn=cs)
14-
print(conn.version, "DB connection successful!")
5+
class OracleDB:
6+
def __init__(self):
7+
load_dotenv()
8+
self.user = os.getenv("un")
9+
self.dns = os.getenv("cs")
10+
self.password = os.getenv("pw")
11+
12+
def exec_bcss_timed_events(self, nhs_no: str): # Executes bcss_timed_events when given an NHS number
1513
try:
16-
print(f"Attempting to execute stored procedure: {procedure}")
17-
cursor = conn.cursor()
18-
cursor.callproc(procedure)
19-
print(conn.version, "stored procedure execution successful!")
20-
except Exception as executionError:
21-
print(f"Failed to execute stored procedure with execution error: {executionError}")
22-
finally:
23-
if conn is not None:
24-
conn.close()
25-
except Exception as connectionError:
26-
print(f"Failed to connect to the DB! with connection error: {connectionError}")
27-
28-
def exec_bcss_timed_events(nhs_no: str): # To use when "exec xxxx" (stored procedures)
29-
load_dotenv()
30-
un = os.getenv("un")
31-
cs = os.getenv("cs")
32-
pw = os.getenv("pw")
33-
34-
try:
35-
print("Attempting DB connection...")
36-
conn = oracledb.connect(user=un, password=pw, dsn=cs)
37-
print(conn.version, "DB connection successful!")
38-
try:
39-
print(f"Attempting to get subject_id from nhs number: {nhs_no}")
40-
cursor = conn.cursor()
41-
cursor.execute(f"SELECT SCREENING_SUBJECT_ID FROM SCREENING_SUBJECT_T WHERE SUBJECT_NHS_NUMBER = {int(nhs_no)}")
42-
result = cursor.fetchall()
43-
subject_id = result[0][0]
44-
print(conn.version, "Able to extract subject ID")
14+
print("Attempting DB connection...")
15+
conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns)
16+
print(conn.version, "DB connection successful!")
4517
try:
46-
print(f"Attempting to execute stored procedure: {f"'bcss_timed_events', [subject_id,'Y']"}")
18+
print(f"Attempting to get subject_id from nhs number: {nhs_no}")
4719
cursor = conn.cursor()
48-
cursor.callproc('bcss_timed_events', [subject_id,'Y'])
49-
print(conn.version, "stored procedure execution successful!")
50-
except Exception as spExecutionError:
51-
print(f"Failed to execute stored procedure with execution error: {spExecutionError}")
52-
except Exception as queryExecutionError:
53-
print(f"Failed to to extract subject ID with error: {queryExecutionError}")
54-
finally:
55-
if conn is not None:
56-
conn.close()
57-
except Exception as connectionError:
58-
print(f"Failed to connect to the DB! with connection error: {connectionError}")
59-
60-
def database_connection_query(query: str,): # To use when "exec xxxx" (stored procedures)
61-
load_dotenv()
62-
un = os.getenv("un")
63-
cs = os.getenv("cs")
64-
pw = os.getenv("pw")
65-
66-
try:
67-
print("Attempting DB connection...")
68-
conn = oracledb.connect(user=un, password=pw, dsn=cs)
69-
print(conn.version, "DB connection successful!")
20+
cursor.execute(f"SELECT SCREENING_SUBJECT_ID FROM SCREENING_SUBJECT_T WHERE SUBJECT_NHS_NUMBER = {int(nhs_no)}")
21+
result = cursor.fetchall()
22+
subject_id = result[0][0]
23+
print(conn.version, "Able to extract subject ID")
24+
try:
25+
print(f"Attempting to execute stored procedure: {f"'bcss_timed_events', [subject_id,'Y']"}")
26+
cursor = conn.cursor()
27+
cursor.callproc('bcss_timed_events', [subject_id,'Y'])
28+
print(conn.version, "stored procedure execution successful!")
29+
except Exception as spExecutionError:
30+
print(f"Failed to execute stored procedure with execution error: {spExecutionError}")
31+
except Exception as queryExecutionError:
32+
print(f"Failed to to extract subject ID with error: {queryExecutionError}")
33+
finally:
34+
if conn is not None:
35+
conn.close()
36+
except Exception as connectionError:
37+
print(f"Failed to connect to the DB! with connection error: {connectionError}")
38+
39+
def populate_ui_approved_users_table(self, user: str): # To add users to the UI_APPROVED_USERS table
7040
try:
71-
print(f"Attempting to execute query: {query}")
72-
cursor = conn.cursor()
73-
cursor.execute(query)
74-
result = cursor.fetchall()
75-
print(conn.version, "query execution successful!")
76-
except Exception as executionError:
77-
print(f"Failed to execute query with execution error {executionError}")
78-
finally:
79-
if conn is not None:
80-
conn.close()
81-
return result
82-
except Exception as connectionError:
83-
print(f"Failed to connect to the DB! with connection error {connectionError}")
84-
85-
def populate_ui_approved_users_table(user: str): # To add users to the UI_APPROVED_USERS table
86-
load_dotenv()
87-
un = os.getenv("un")
88-
cs = os.getenv("cs")
89-
pw = os.getenv("pw")
90-
91-
try:
92-
print("Attempting DB connection...")
93-
conn = oracledb.connect(user=un, password=pw, dsn=cs)
94-
print(conn.version, "DB connection successful!")
95-
try:
96-
print("Attempting to write to the db...")
97-
cursor = conn.cursor()
98-
cursor.execute(f'INSERT INTO "UI_APPROVED_USERSf" (OE_USER_CODE) VALUES ({user})')
99-
print(conn.version, "DB write successful!")
100-
except Exception as dbWriteError:
101-
print(f"Failed to write to the DB! with write error {dbWriteError}")
102-
finally:
103-
if conn is not None:
104-
conn.close()
105-
except Exception as connectionError:
106-
print(f"Failed to connect to the DB! with connection error {connectionError}")
107-
108-
109-
110-
111-
112-
113-
114-
41+
print("Attempting DB connection...")
42+
conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns)
43+
print(conn.version, "DB connection successful!")
44+
try:
45+
print("Attempting to write to the db...")
46+
cursor = conn.cursor()
47+
cursor.execute(f'INSERT INTO "UI_APPROVED_USERSf" (OE_USER_CODE) VALUES ({user})')
48+
print(conn.version, "DB write successful!")
49+
except Exception as dbWriteError:
50+
print(f"Failed to write to the DB! with write error {dbWriteError}")
51+
finally:
52+
if conn is not None:
53+
conn.close()
54+
except Exception as connectionError:
55+
print(f"Failed to connect to the DB! with connection error {connectionError}")
56+
57+
# The following two functions are commented out as they are not used currently, but may be needed in future compartments
58+
59+
# def execute_stored_procedure(self, procedure: str): # To use when "exec xxxx" (stored procedures)
60+
# try:
61+
# print("Attempting DB connection...")
62+
# conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns)
63+
# print(conn.version, "DB connection successful!")
64+
# try:
65+
# print(f"Attempting to execute stored procedure: {procedure}")
66+
# cursor = conn.cursor()
67+
# cursor.callproc(procedure)
68+
# print(conn.version, "stored procedure execution successful!")
69+
# except Exception as executionError:
70+
# print(f"Failed to execute stored procedure with execution error: {executionError}")
71+
# finally:
72+
# if conn is not None:
73+
# conn.close()
74+
# except Exception as connectionError:
75+
# print(f"Failed to connect to the DB! with connection error: {connectionError}")
76+
77+
# def execute_query(self, query: str,): # To use when "select xxxx" (stored procedures)
78+
# try:
79+
# print("Attempting DB connection...")
80+
# conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns)
81+
# print(conn.version, "DB connection successful!")
82+
# try:
83+
# print(f"Attempting to execute query: {query}")
84+
# cursor = conn.cursor()
85+
# cursor.execute(query)
86+
# result = cursor.fetchall()
87+
# print(conn.version, "query execution successful!")
88+
# except Exception as executionError:
89+
# print(f"Failed to execute query with execution error {executionError}")
90+
# finally:
91+
# if conn is not None:
92+
# conn.close()
93+
# return result
94+
# except Exception as connectionError:
95+
# print(f"Failed to connect to the DB! with connection error {connectionError}")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pypdf import PdfReader
22

3-
def pdf_reader(file: str):
3+
def extract_nhs_no_from_pdf(file: str):
44
reader = PdfReader(file)
55

66
# For loop looping through all pages of the file to find the NHS Number
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pages.subject_screening_summary import SubjectScreeningSummary
55
from playwright.sync_api import Page
66

7-
def subject_search_by_nhs_no(page: Page, nhs_no: str, latest_event_status: str):
7+
def verify_subject_event_status_by_nhs_no(page: Page, nhs_no: str, latest_event_status: str):
88
NavigationBar(page).click_main_menu_link()
99
MainMenu(page).go_to_screening_subject_search_page()
1010
SubjectScreeningPage(page).click_nhs_number_filter()

0 commit comments

Comments
 (0)