Initial commit: Role-based data visualisation platform for DMH hospital admissions (Phases 1-3)
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
import io
|
||||
import pytest
|
||||
|
||||
|
||||
SAMPLE_CSV = """srNo,mrdNumber,Gender,Age,PatientCategoryName,District,State,Country,Nationality,AdmissionDate,DischargeDate,bedNumber,BedLocation,BuildingName,AdmittingDoctorName,AdmittingDoctorSpecialityName,CorporateCompanyName,PolicyName,AdmissionType
|
||||
1,1001,Male,45,GNL,Pune,Maharashtra,India,Indian,01-01-2025 10:00,03-01-2025 12:00,101,Wing A,DMHRC,Dr. Jane Doe,MEDICINE,,,Emergency
|
||||
2,1002,Female,32,TPA,Mumbai,Maharashtra,India,Indian,02-01-2025 11:30,05-01-2025 16:45,202,Wing B,DMHSS,Dr. John Smith,CARDIOLOGY,Care Health,TPA,Walk-in
|
||||
3,1003,Male,60,TPA,Pune,Maharashtra,India,Indian,03-01-2025 09:15,04-01-2025 14:00,303,Wing C,DMHRC,Dr. Alice Brown,MEDICINE,Star Health,TPA,Emergency
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admission_dataset(client, admin_token):
|
||||
csv_file = io.BytesIO(SAMPLE_CSV.encode("utf-8"))
|
||||
upload_res = client.post(
|
||||
"/api/v1/datasets/upload",
|
||||
data={"name": "Admissions Query Test", "description": "For query tests"},
|
||||
files={"file": ("query_test.csv", csv_file, "text/csv")},
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert upload_res.status_code == 201
|
||||
ds = upload_res.json()["dataset"]
|
||||
|
||||
# Configure role permission for viewer with row rule: BuildingName == 'DMHRC'
|
||||
client.post(
|
||||
"/api/v1/permissions",
|
||||
json={
|
||||
"dataset_id": ds["id"],
|
||||
"role": "viewer",
|
||||
"can_view": True,
|
||||
"hidden_column_ids": [],
|
||||
"row_rules": [
|
||||
{
|
||||
"column_name": "BuildingName",
|
||||
"operator": "eq",
|
||||
"rule_value": "DMHRC"
|
||||
}
|
||||
]
|
||||
},
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
return ds
|
||||
|
||||
|
||||
def test_admin_queries_chart_successfully(client, admin_token, admission_dataset):
|
||||
query_payload = {
|
||||
"dataset_id": admission_dataset["id"],
|
||||
"chart_type": "bar",
|
||||
"dimension": "AdmittingDoctorSpecialityName",
|
||||
"measure": "*",
|
||||
"aggregation": "count"
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/charts/query",
|
||||
json=query_payload,
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
res = response.json()
|
||||
assert res["chart_type"] == "bar"
|
||||
assert "MEDICINE" in res["categories"]
|
||||
assert "CARDIOLOGY" in res["categories"]
|
||||
assert res["total_records"] == 3
|
||||
|
||||
|
||||
def test_viewer_row_rules_enforced(client, viewer_token, admission_dataset):
|
||||
# Viewer is restricted to DMHRC only (rows 1 and 3, total 2 rows)
|
||||
query_payload = {
|
||||
"dataset_id": admission_dataset["id"],
|
||||
"chart_type": "bar",
|
||||
"dimension": "BuildingName",
|
||||
"measure": "*",
|
||||
"aggregation": "count"
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/charts/query",
|
||||
json=query_payload,
|
||||
headers={"Authorization": f"Bearer {viewer_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
res = response.json()
|
||||
assert res["categories"] == ["DMHRC"]
|
||||
assert res["total_records"] == 2
|
||||
assert "DMHSS" not in res["categories"]
|
||||
|
||||
|
||||
def test_querying_restricted_sensitive_column_fails(client, viewer_token, admission_dataset):
|
||||
# mrdNumber is sensitive by default, viewer cannot query it
|
||||
query_payload = {
|
||||
"dataset_id": admission_dataset["id"],
|
||||
"chart_type": "bar",
|
||||
"dimension": "mrdNumber",
|
||||
"measure": "*",
|
||||
"aggregation": "count"
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/charts/query",
|
||||
json=query_payload,
|
||||
headers={"Authorization": f"Bearer {viewer_token}"}
|
||||
)
|
||||
assert response.status_code == 403
|
||||
assert "restricted" in response.json()["detail"].lower()
|
||||
|
||||
|
||||
def test_kpi_query(client, admin_token, admission_dataset):
|
||||
query_payload = {
|
||||
"dataset_id": admission_dataset["id"],
|
||||
"chart_type": "kpi",
|
||||
"dimension": "srNo",
|
||||
"measure": "*",
|
||||
"aggregation": "count"
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/charts/query",
|
||||
json=query_payload,
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
res = response.json()
|
||||
assert res["kpi_value"] == 3
|
||||
|
||||
|
||||
def test_stacked_bar_query(client, admin_token, admission_dataset):
|
||||
query_payload = {
|
||||
"dataset_id": admission_dataset["id"],
|
||||
"chart_type": "stacked_bar",
|
||||
"dimension": "PatientCategoryName",
|
||||
"secondary_dimension": "Gender",
|
||||
"measure": "*",
|
||||
"aggregation": "count"
|
||||
}
|
||||
response = client.post(
|
||||
"/api/v1/charts/query",
|
||||
json=query_payload,
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
res = response.json()
|
||||
assert len(res["series"]) >= 2
|
||||
series_names = [s["name"] for s in res["series"]]
|
||||
assert "Male" in series_names or "Female" in series_names
|
||||
|
||||
|
||||
def test_filter_options_endpoint(client, viewer_token, admission_dataset):
|
||||
response = client.get(
|
||||
f"/api/v1/charts/filter-options/{admission_dataset['id']}",
|
||||
headers={"Authorization": f"Bearer {viewer_token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
opts = response.json()["options"]
|
||||
# Viewer row rule restricts to DMHRC
|
||||
buildings = [b["value"] for b in opts.get("BuildingName", [])]
|
||||
assert "DMHRC" in buildings
|
||||
assert "DMHSS" not in buildings
|
||||
Reference in New Issue
Block a user