Skip to content

Commit 6e7ce75

Browse files
committed
wip
1 parent 3b8db93 commit 6e7ce75

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
from datetime import datetime
3+
4+
import pytest
5+
6+
from manage_breast_screening.notifications.management.commands.create_appointments import (
7+
Command,
8+
)
9+
from manage_breast_screening.notifications.services.blob_storage import BlobStorage
10+
from manage_breast_screening.notifications.tests.integration.helpers import Helpers
11+
from scripts.python.load_test.notifications_load_test import generate_load_test_data
12+
13+
14+
# TODO: DELETE - setup as easy way to validate data locally
15+
@pytest.mark.integration
16+
class TestLoadTest:
17+
@pytest.fixture(autouse=True)
18+
def setup(self, monkeypatch):
19+
monkeypatch.setenv(
20+
"BLOB_STORAGE_CONNECTION_STRING", Helpers().azurite_connection_string()
21+
)
22+
monkeypatch.setenv("BLOB_CONTAINER_NAME", "nbss-appoinments-data")
23+
24+
@pytest.fixture
25+
def helpers(self):
26+
return Helpers()
27+
28+
@pytest.mark.django_db
29+
def test_generate_test_data(self, helpers):
30+
for number in range(10):
31+
with open(
32+
f"{os.path.dirname(os.path.realpath(__file__))}/../fixtures/load_data_{number}.dat",
33+
"w",
34+
) as file:
35+
data_string = generate_load_test_data(number)
36+
file.write(data_string)
37+
38+
for number in range(10):
39+
today_dirname = datetime.today().strftime("%Y-%m-%d")
40+
test_file_name = f"load_data_{number}.dat"
41+
blob_name = f"{today_dirname}/{test_file_name}"
42+
43+
with open(
44+
f"{os.path.dirname(os.path.realpath(__file__))}/../fixtures/load_data_{number}.dat"
45+
) as test_file:
46+
BlobStorage().add(blob_name, test_file.read())
47+
48+
Command().handle(**{"date_str": today_dirname})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"NBSSAPPT_HDR"|"88888888"|"20250101"|"170922"|"000003"
2+
"NBSSAPPT_FLDS"|"Sequence"|"BSO"|"Action"|"Clinic Code"|"Holding Clinic"|"Status"|"Attended Not Scr"|"Appointment ID"|"NHS Num"|"Epsiode Type"|"Episode Start"|"BatchID"|"Screen or Asses"|"Screen Appt num"|"Booked By"|"Cancelled By"|"Appt Date"|"Appt Time"|"Location"|"Clinic Name"|"Clinic Name (Let)"|"Clinic Address 1"|"Clinic Address 2"|"Clinic Address 3"|"Clinic Address 4"|"Clinic Address 5"|"Postcode"|"Action Timestamp"
3+
"NBSSAPPT_DATA"|"000001"|"SM0K3"|"B"|"SM0K3"|"N"|"B"|"N"|"SM0K3-0000000000"|"9449306621"|"F"|"20250101"|"SM0K0000"|"S"|"1"|"H"|""|"20250101"|"1445"|"SMOK"|"SMOKE TEST CLINIC"|"SMOKE TEST CLINIC"|"SMOKE TEST UNIT"|"NOT A REAL HOSPITAL"|"SMOKE TEST LANE"|"LONDON"|"E5 0AB"|"E5 0AB"|"20250101-154004"
4+
"NBSSAPPT_END"|"00000013"|"20250101"|"17:09:22"|"000003"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import logging
2+
import os
3+
import subprocess
4+
import time
5+
from datetime import datetime
6+
7+
from scripts.python.smoke_test.notifications_smoke_test import (
8+
mesh_client,
9+
populate_mesh_env_vars,
10+
)
11+
12+
WORK_DIR = os.path.dirname(os.path.realpath(__file__))
13+
STARTUP_SCRIPT_PATH = f"{WORK_DIR}/../../bash/run_container_app_job.sh"
14+
NUMBER_OF_ROWS = os.getenv("NUMBER_OF_ROWS", "1000")
15+
NUMBER_OF_FILES = os.getenv("NUMBER_OF_ROWS", "1000")
16+
17+
18+
def test_load():
19+
logging.info("Running notifications load test")
20+
21+
environment = os.getenv("ENVIRONMENT")
22+
resource_group_name = f"rg-manbrs-{environment}-container-app-uks"
23+
24+
if environment == "prod":
25+
return
26+
27+
add_load_test_data_to_mesh(environment, resource_group_name)
28+
29+
for job in ["smm", "cap"]:
30+
logging.info(
31+
"Starting notifications container app job manbrs-%s-%s", job, environment
32+
)
33+
34+
job_result = subprocess.run(
35+
[STARTUP_SCRIPT_PATH, environment, job],
36+
check=True,
37+
capture_output=True,
38+
text=True,
39+
)
40+
assert job_result.returncode == 0
41+
42+
logging.info("Finished notifications load test")
43+
44+
45+
def add_load_test_data_to_mesh(environment: str, resource_group_name: str):
46+
populate_mesh_env_vars(environment, resource_group_name)
47+
for number in range(int(NUMBER_OF_FILES)):
48+
mesh_client().send_message(
49+
os.getenv("NBSS_MESH_INBOX_NAME"),
50+
generate_load_test_data(number).encode("ASCII"),
51+
subject="Load test data",
52+
)
53+
54+
55+
def generate_load_test_data(file_sequence_number: int) -> str:
56+
data = open(f"{WORK_DIR}/load_test_data.dat").read()
57+
data = data.replace("20250101", datetime.now().strftime("%Y%m%d"))
58+
data = data.replace("88888888", f"{file_sequence_number:06d}")
59+
data = data.replace("SM0K3-0000000000", f"SM0K3-{time.time_ns()}")
60+
61+
rows = data.split("\n")
62+
_header = rows[0]
63+
_footer = rows[3]
64+
first_data_row = rows[2]
65+
66+
for number in range(2, int(NUMBER_OF_ROWS)):
67+
new_data = first_data_row.replace("000001", f"{number:06d}")
68+
new_data = new_data.replace("SM0K3-0000000000", f"SM0K3-{time.time_ns()}")
69+
rows.insert((number + 1), new_data)
70+
71+
return "\n".join(rows)

0 commit comments

Comments
 (0)