-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_refiner.py
More file actions
218 lines (178 loc) · 7.34 KB
/
test_refiner.py
File metadata and controls
218 lines (178 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import os
import json
import shutil
import pytest
from refiner.refine import Refiner
from refiner.config import settings
@pytest.fixture
def setup_test_environment():
"""Prepare the test environment with sample data."""
# Create test directories
os.makedirs("test_input", exist_ok=True)
os.makedirs("test_output", exist_ok=True)
# Create a sample FHIR patient file
patient_example = {
"resourceType": "Patient",
"id": "example-patient-1",
"name": [
{
"use": "official",
"family": "Smith",
"given": ["John"]
}
],
"telecom": [
{
"system": "phone",
"value": "555-1234"
}
]
}
# Create a sample FHIR medication statement
medication_example = {
"resourceType": "MedicationStatement",
"id": "med-1",
"subject": {
"reference": "Patient/example-patient-1"
},
"status": "active",
"medicationCodeableConcept": {
"coding": [
{
"system": "http://www.nlm.nih.gov/research/umls/rxnorm",
"code": "1049502",
"display": "Acetaminophen 325 MG Oral Tablet"
}
],
"text": "Acetaminophen 325 MG Oral Tablet"
}
}
# Create a FHIR bundle with both resources
bundle_example = {
"resourceType": "Bundle",
"type": "collection",
"entry": [
{"resource": patient_example},
{"resource": medication_example}
]
}
# Write individual files
with open("test_input/patient.json", "w") as f:
json.dump(patient_example, f)
with open("test_input/medication.json", "w") as f:
json.dump(medication_example, f)
# Write bundle file
with open("test_input/bundle.json", "w") as f:
json.dump(bundle_example, f)
# Save original settings
original_input = settings.INPUT_DIR
original_output = settings.OUTPUT_DIR
# Modify configuration for testing
settings.INPUT_DIR = "test_input"
settings.OUTPUT_DIR = "test_output"
yield
# Restore original settings
settings.INPUT_DIR = original_input
settings.OUTPUT_DIR = original_output
# Clean up test directories
shutil.rmtree("test_input", ignore_errors=True)
shutil.rmtree("test_output", ignore_errors=True)
def test_refiner_transform(setup_test_environment):
"""Test the complete transformation flow."""
# Run the refiner
refiner = Refiner()
output = refiner.transform()
# Verify that the expected files were generated
assert os.path.exists(os.path.join("test_output", "schema.json"))
assert os.path.exists(os.path.join("test_output", "db.libsql"))
# Verify that the output.json file exists in the correct directory
output_path = os.path.join("output", "output.json")
assert os.path.exists(output_path), f"Output file not found at {output_path}"
# Verify that the output has the refinement URL
assert output.refinement_url is not None
assert output.refinement_url.startswith(settings.IPFS_GATEWAY_URL)
# Verify that the schema was generated correctly
assert output.schema_content is not None
assert output.schema_content.name == settings.SCHEMA_NAME
assert output.schema_content.version == settings.SCHEMA_VERSION
print(f"Refinement URL: {output.refinement_url}")
print(f"Schema: {output.schema_content}")
# Check the contents of the output.json file
with open(output_path, "r") as f:
output_json = json.load(f)
assert "refinement_url" in output_json
assert "schema" in output_json
def test_refiner_database_content(setup_test_environment):
"""Test that the database contains the expected data."""
# Ejecutar el refinador
refiner = Refiner()
output = refiner.transform()
# Importar SQLite para verificar contenido
import sqlite3
# Conectar a la base de datos
db_path = os.path.join("test_output", "db.libsql")
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Verificar pacientes
cursor.execute("SELECT id, family_name FROM patient")
patients = cursor.fetchall()
# Debería haber 1 paciente (los archivos individuales y el bundle comparten el mismo ID)
assert len(patients) == 1, f"Expected 1 patient, found {len(patients)}"
assert patients[0][0] == "example-patient-1"
assert patients[0][1] == "Smith"
# Verificar medicamentos
cursor.execute("SELECT id, patient_id, code, display FROM medication")
medications = cursor.fetchall()
# Debería haber 1 medicamento (mismo ID en ambos archivos)
assert len(medications) == 1, f"Expected 1 medication, found {len(medications)}"
assert medications[0][0] == "med-1"
assert medications[0][1] == "example-patient-1"
assert medications[0][2] == "1049502"
assert medications[0][3] == "Acetaminophen 325 MG Oral Tablet"
# Cerrar conexión
conn.close()
def test_refiner_bundle_processing(setup_test_environment):
"""Test that the refiner can process a FHIR bundle."""
# Run the refiner
refiner = Refiner()
output = refiner.transform()
# Import SQLite to check database content
import sqlite3
# Connect to the database
db_path = os.path.join("test_output", "db.libsql")
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Check that both resources from the bundle were processed
cursor.execute("SELECT COUNT(*) FROM patient")
patient_count = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM medication")
medication_count = cursor.fetchone()[0]
# We expect at least one of each since we have individual files and a bundle
assert patient_count >= 1, f"Expected at least 1 patient, found {patient_count}"
assert medication_count >= 1, f"Expected at least 1 medication, found {medication_count}"
# Close the connection
conn.close()
def test_refiner_schema_structure(setup_test_environment):
"""Test that the generated schema has the expected structure."""
# Run the refiner
refiner = Refiner()
output = refiner.transform()
# Load the schema.json file
with open(os.path.join("test_output", "schema.json"), "r") as f:
schema = json.load(f)
# Check that the schema has the expected tables
tables = [table["name"] for table in schema["tables"]]
assert "patient" in tables, "Schema should include patient table"
assert "medication" in tables, "Schema should include medication table"
# Check that the schema has the expected relationships
relationships = schema.get("relationships", [])
relationship_names = [rel["name"] for rel in relationships]
assert "patient_medications" in relationship_names, "Schema should include patient_medications relationship"
# Find the medication table and check its columns
medication_table = next((table for table in schema["tables"] if table["name"] == "medication"), None)
assert medication_table is not None
# Check that the medication table has the expected columns
column_names = [col["name"] for col in medication_table["columns"]]
expected_columns = ["id", "patient_id", "resource_type", "code", "display", "system", "text"]
for col in expected_columns:
assert col in column_names, f"Medication table should have {col} column"