861 lines
34 KiB
Python
861 lines
34 KiB
Python
"""Tests for aco.express.core — narwhalify staging transformation functions.
|
|
|
|
Each function in aco.express.core is a @nw.narwhalify transformation
|
|
that selects and optionally enriches input_layer DataFrames into the
|
|
core staging schema. Tests verify:
|
|
|
|
- Return type is a DataFrame
|
|
- Row count is preserved
|
|
- All declared output columns are present
|
|
- Literal/computed columns have the correct constant values
|
|
- Passthrough columns carry through original values intact
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import polars as pl
|
|
import pytest
|
|
|
|
from aco.express.core import (
|
|
stg_claims_member_months,
|
|
stg_clinical_appointment,
|
|
stg_clinical_condition,
|
|
stg_clinical_encounter,
|
|
stg_clinical_immunization,
|
|
stg_clinical_lab_result,
|
|
stg_clinical_location,
|
|
stg_clinical_medication,
|
|
stg_clinical_observation,
|
|
stg_clinical_patient,
|
|
stg_clinical_practitioner,
|
|
stg_clinical_procedure,
|
|
)
|
|
|
|
# ── helpers ───────────────────────────────────────────────────────────────────
|
|
|
|
|
|
def _assert_columns(result: pl.DataFrame, *expected: str) -> None:
|
|
"""Assert that every expected column name is present in result."""
|
|
missing = [col for col in expected if col not in result.columns]
|
|
assert not missing, f"Missing columns: {missing}\nGot: {sorted(result.columns)}"
|
|
|
|
|
|
def _assert_row_count(result: pl.DataFrame, source: pl.DataFrame) -> None:
|
|
assert len(result) == len(source), (
|
|
f"Row count changed: source={len(source)}, result={len(result)}"
|
|
)
|
|
|
|
|
|
def _assert_literal(result: pl.DataFrame, col: str, expected_value) -> None:
|
|
"""Assert a column contains the expected constant value in every row."""
|
|
vals = result[col].to_list()
|
|
for i, v in enumerate(vals):
|
|
assert v == expected_value, (
|
|
f"Row {i}: column {col!r} = {v!r}, expected {expected_value!r}"
|
|
)
|
|
|
|
|
|
def _assert_null_column(result: pl.DataFrame, col: str) -> None:
|
|
"""Assert a column exists and every value is null."""
|
|
assert col in result.columns, f"Column {col!r} missing from result"
|
|
assert result[col].is_null().all(), (
|
|
f"Column {col!r} should be all-null but has non-null values: "
|
|
f"{result[col].drop_nulls().to_list()}"
|
|
)
|
|
|
|
|
|
# ── stg_claims_member_months ──────────────────────────────────────────────────
|
|
|
|
|
|
class TestStgClaimsMemberMonths:
|
|
"""stg_claims_member_months joins member_months with provider attribution."""
|
|
|
|
def test_returns_dataframe(self, member_months_df, provider_attribution_df) -> None:
|
|
result = stg_claims_member_months(member_months_df, provider_attribution_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
def test_row_count_preserved(
|
|
self, member_months_df, provider_attribution_df
|
|
) -> None:
|
|
result = stg_claims_member_months(member_months_df, provider_attribution_df)
|
|
# Result should have at least as many rows as member_months (left join)
|
|
assert len(result) >= 0
|
|
|
|
def test_member_months_columns_carried(
|
|
self, member_months_df, provider_attribution_df
|
|
) -> None:
|
|
result = stg_claims_member_months(member_months_df, provider_attribution_df)
|
|
for col in ("person_id", "member_id", "year", "month"):
|
|
assert col in result.columns, f"Column {col!r} missing"
|
|
|
|
def test_person_id_value_preserved(
|
|
self, member_months_df, provider_attribution_df
|
|
) -> None:
|
|
result = stg_claims_member_months(member_months_df, provider_attribution_df)
|
|
if len(result) > 0:
|
|
assert result["person_id"][0] == "P001"
|
|
|
|
def test_accepts_empty_member_months(self, provider_attribution_df) -> None:
|
|
empty_mm = pl.DataFrame(
|
|
{
|
|
"person_id": pl.Series([], dtype=pl.Utf8),
|
|
"member_id": pl.Series([], dtype=pl.Utf8),
|
|
"year": pl.Series([], dtype=pl.Int64),
|
|
"month": pl.Series([], dtype=pl.Int64),
|
|
"payer": pl.Series([], dtype=pl.Utf8),
|
|
"plan": pl.Series([], dtype=pl.Utf8),
|
|
"data_source": pl.Series([], dtype=pl.Utf8),
|
|
}
|
|
)
|
|
result = stg_claims_member_months(empty_mm, provider_attribution_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
assert len(result) == 0
|
|
|
|
def test_multiple_rows_preserved(self, provider_attribution_df) -> None:
|
|
mm = pl.DataFrame(
|
|
{
|
|
"person_id": ["P001", "P002", "P003"],
|
|
"member_id": ["M001", "M002", "M003"],
|
|
"year": [2024, 2024, 2024],
|
|
"month": [1, 2, 3],
|
|
"payer": ["Medicare", "Medicare", "Medicare"],
|
|
"plan": [None, None, None],
|
|
"data_source": ["test", "test", "test"],
|
|
}
|
|
)
|
|
result = stg_claims_member_months(mm, provider_attribution_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
|
|
# ── stg_clinical_encounter ────────────────────────────────────────────────────
|
|
|
|
|
|
class TestStgClinicalEncounter:
|
|
"""stg_clinical_encounter selects and enriches encounter records."""
|
|
|
|
# Expected output columns from the source code
|
|
EXPECTED_COLUMNS = [
|
|
"encounter_id",
|
|
"person_id",
|
|
"encounter_type",
|
|
"encounter_group",
|
|
"encounter_start_date",
|
|
"encounter_end_date",
|
|
"length_of_stay",
|
|
"admit_source_code",
|
|
"admit_source_description",
|
|
"admit_type_code",
|
|
"admit_type_description",
|
|
"discharge_disposition_code",
|
|
"discharge_disposition_description",
|
|
"attending_provider_id",
|
|
"attending_provider_name",
|
|
"facility_id",
|
|
"facility_name",
|
|
"facility_type",
|
|
"observation_flag",
|
|
"lab_flag",
|
|
"dme_flag",
|
|
"ambulance_flag",
|
|
"pharmacy_flag",
|
|
"ed_flag",
|
|
"delivery_flag",
|
|
"delivery_type",
|
|
"newborn_flag",
|
|
"nicu_flag",
|
|
"snf_part_b_flag",
|
|
"primary_diagnosis_code_type",
|
|
"primary_diagnosis_code",
|
|
"primary_diagnosis_description",
|
|
"drg_code_type",
|
|
"drg_code",
|
|
"drg_description",
|
|
"paid_amount",
|
|
"allowed_amount",
|
|
"charge_amount",
|
|
"claim_count",
|
|
"inst_claim_count",
|
|
"prof_claim_count",
|
|
"source_model",
|
|
"data_source",
|
|
"encounter_source_type",
|
|
]
|
|
|
|
def test_returns_dataframe(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
def test_row_count_preserved(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_row_count(result, encounter_df)
|
|
|
|
def test_all_expected_columns_present(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_columns(result, *self.EXPECTED_COLUMNS)
|
|
|
|
# ── literal columns ───────────────────────────────────────────────────────
|
|
|
|
def test_encounter_group_is_clinical(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_literal(result, "encounter_group", "clinical")
|
|
|
|
def test_encounter_source_type_is_clinical(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_literal(result, "encounter_source_type", "clinical")
|
|
|
|
def test_facility_type_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "facility_type")
|
|
|
|
def test_observation_flag_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "observation_flag")
|
|
|
|
def test_lab_flag_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "lab_flag")
|
|
|
|
def test_dme_flag_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "dme_flag")
|
|
|
|
def test_ambulance_flag_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "ambulance_flag")
|
|
|
|
def test_pharmacy_flag_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "pharmacy_flag")
|
|
|
|
def test_ed_flag_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "ed_flag")
|
|
|
|
def test_delivery_flag_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "delivery_flag")
|
|
|
|
def test_delivery_type_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "delivery_type")
|
|
|
|
def test_newborn_flag_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "newborn_flag")
|
|
|
|
def test_nicu_flag_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "nicu_flag")
|
|
|
|
def test_snf_part_b_flag_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "snf_part_b_flag")
|
|
|
|
def test_claim_count_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "claim_count")
|
|
|
|
def test_inst_claim_count_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "inst_claim_count")
|
|
|
|
def test_prof_claim_count_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "prof_claim_count")
|
|
|
|
def test_source_model_is_null(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
_assert_null_column(result, "source_model")
|
|
|
|
# ── passthrough columns ───────────────────────────────────────────────────
|
|
|
|
def test_encounter_id_passed_through(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
assert result["encounter_id"][0] == "ENC001"
|
|
|
|
def test_person_id_passed_through(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
assert result["person_id"][0] == "P001"
|
|
|
|
def test_encounter_type_passed_through(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
assert result["encounter_type"][0] == "acute inpatient"
|
|
|
|
def test_paid_amount_passed_through(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
assert result["paid_amount"][0] == pytest.approx(5000.0)
|
|
|
|
def test_primary_diagnosis_code_passed_through(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
assert result["primary_diagnosis_code"][0] == "E11.9"
|
|
|
|
def test_discharge_disposition_passed_through(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
assert result["discharge_disposition_code"][0] == "01"
|
|
|
|
def test_data_source_passed_through(self, encounter_df) -> None:
|
|
result = stg_clinical_encounter(encounter_df)
|
|
assert result["data_source"][0] == "test"
|
|
|
|
# ── edge cases ────────────────────────────────────────────────────────────
|
|
|
|
def test_multiple_rows(self) -> None:
|
|
df = pl.DataFrame(
|
|
{
|
|
"encounter_id": ["ENC001", "ENC002"],
|
|
"person_id": ["P001", "P002"],
|
|
"encounter_type": ["acute inpatient", "ed"],
|
|
"encounter_start_date": [None, None],
|
|
"encounter_end_date": [None, None],
|
|
"length_of_stay": [3, 1],
|
|
"admit_source_code": [None, None],
|
|
"admit_source_description": [None, None],
|
|
"admit_type_code": [None, None],
|
|
"admit_type_description": [None, None],
|
|
"discharge_disposition_code": ["01", "01"],
|
|
"discharge_disposition_description": ["home", "home"],
|
|
"attending_provider_id": [None, None],
|
|
"attending_provider_name": [None, None],
|
|
"facility_id": [None, None],
|
|
"facility_name": [None, None],
|
|
"primary_diagnosis_code_type": ["icd-10-cm", "icd-10-cm"],
|
|
"primary_diagnosis_code": ["E11.9", "I10"],
|
|
"primary_diagnosis_description": [None, None],
|
|
"drg_code_type": [None, None],
|
|
"drg_code": [None, None],
|
|
"drg_description": [None, None],
|
|
"paid_amount": [5000.0, 2000.0],
|
|
"allowed_amount": [6000.0, 2500.0],
|
|
"charge_amount": [12000.0, 5000.0],
|
|
"data_source": ["test", "test"],
|
|
}
|
|
)
|
|
result = stg_clinical_encounter(df)
|
|
assert len(result) == 2
|
|
assert result["encounter_group"].to_list() == ["clinical", "clinical"]
|
|
assert result["encounter_source_type"].to_list() == ["clinical", "clinical"]
|
|
|
|
def test_empty_input_returns_empty_dataframe(self) -> None:
|
|
empty = pl.DataFrame(
|
|
{
|
|
"encounter_id": pl.Series([], dtype=pl.Utf8),
|
|
"person_id": pl.Series([], dtype=pl.Utf8),
|
|
"encounter_type": pl.Series([], dtype=pl.Utf8),
|
|
"encounter_start_date": pl.Series([], dtype=pl.Utf8),
|
|
"encounter_end_date": pl.Series([], dtype=pl.Utf8),
|
|
"length_of_stay": pl.Series([], dtype=pl.Int64),
|
|
"admit_source_code": pl.Series([], dtype=pl.Utf8),
|
|
"admit_source_description": pl.Series([], dtype=pl.Utf8),
|
|
"admit_type_code": pl.Series([], dtype=pl.Utf8),
|
|
"admit_type_description": pl.Series([], dtype=pl.Utf8),
|
|
"discharge_disposition_code": pl.Series([], dtype=pl.Utf8),
|
|
"discharge_disposition_description": pl.Series([], dtype=pl.Utf8),
|
|
"attending_provider_id": pl.Series([], dtype=pl.Utf8),
|
|
"attending_provider_name": pl.Series([], dtype=pl.Utf8),
|
|
"facility_id": pl.Series([], dtype=pl.Utf8),
|
|
"facility_name": pl.Series([], dtype=pl.Utf8),
|
|
"primary_diagnosis_code_type": pl.Series([], dtype=pl.Utf8),
|
|
"primary_diagnosis_code": pl.Series([], dtype=pl.Utf8),
|
|
"primary_diagnosis_description": pl.Series([], dtype=pl.Utf8),
|
|
"drg_code_type": pl.Series([], dtype=pl.Utf8),
|
|
"drg_code": pl.Series([], dtype=pl.Utf8),
|
|
"drg_description": pl.Series([], dtype=pl.Utf8),
|
|
"paid_amount": pl.Series([], dtype=pl.Float64),
|
|
"allowed_amount": pl.Series([], dtype=pl.Float64),
|
|
"charge_amount": pl.Series([], dtype=pl.Float64),
|
|
"data_source": pl.Series([], dtype=pl.Utf8),
|
|
}
|
|
)
|
|
result = stg_clinical_encounter(empty)
|
|
assert isinstance(result, pl.DataFrame)
|
|
assert len(result) == 0
|
|
_assert_columns(result, "encounter_group", "encounter_source_type")
|
|
|
|
|
|
# ── stg_clinical_condition ────────────────────────────────────────────────────
|
|
|
|
|
|
class TestStgClinicalCondition:
|
|
"""stg_clinical_condition selects condition columns and adds member_id=null."""
|
|
|
|
EXPECTED_COLUMNS = [
|
|
"condition_id",
|
|
"person_id",
|
|
"member_id",
|
|
"patient_id",
|
|
"encounter_id",
|
|
"claim_id",
|
|
"recorded_date",
|
|
"onset_date",
|
|
"resolved_date",
|
|
"status",
|
|
"condition_type",
|
|
"source_code_type",
|
|
"source_code",
|
|
"source_description",
|
|
"normalized_code_type",
|
|
"normalized_code",
|
|
"normalized_description",
|
|
"condition_rank",
|
|
"present_on_admit_code",
|
|
"present_on_admit_description",
|
|
"data_source",
|
|
]
|
|
|
|
def test_returns_dataframe(self, condition_df) -> None:
|
|
result = stg_clinical_condition(condition_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
def test_row_count_preserved(self, condition_df) -> None:
|
|
result = stg_clinical_condition(condition_df)
|
|
_assert_row_count(result, condition_df)
|
|
|
|
def test_all_expected_columns_present(self, condition_df) -> None:
|
|
result = stg_clinical_condition(condition_df)
|
|
_assert_columns(result, *self.EXPECTED_COLUMNS)
|
|
|
|
def test_member_id_is_null(self, condition_df) -> None:
|
|
"""member_id is injected as a null literal — not in the source schema."""
|
|
result = stg_clinical_condition(condition_df)
|
|
_assert_null_column(result, "member_id")
|
|
|
|
def test_condition_id_passed_through(self, condition_df) -> None:
|
|
result = stg_clinical_condition(condition_df)
|
|
assert result["condition_id"][0] == "COND001"
|
|
|
|
def test_person_id_passed_through(self, condition_df) -> None:
|
|
result = stg_clinical_condition(condition_df)
|
|
assert result["person_id"][0] == "P001"
|
|
|
|
def test_normalized_code_passed_through(self, condition_df) -> None:
|
|
result = stg_clinical_condition(condition_df)
|
|
assert result["normalized_code"][0] == "E11.9"
|
|
|
|
def test_source_code_type_passed_through(self, condition_df) -> None:
|
|
result = stg_clinical_condition(condition_df)
|
|
assert result["source_code_type"][0] == "icd-10-cm"
|
|
|
|
def test_condition_rank_passed_through(self, condition_df) -> None:
|
|
result = stg_clinical_condition(condition_df)
|
|
assert result["condition_rank"][0] == 1
|
|
|
|
def test_status_passed_through(self, condition_df) -> None:
|
|
result = stg_clinical_condition(condition_df)
|
|
assert result["status"][0] == "active"
|
|
|
|
def test_data_source_passed_through(self, condition_df) -> None:
|
|
result = stg_clinical_condition(condition_df)
|
|
assert result["data_source"][0] == "test"
|
|
|
|
|
|
# ── stg_clinical_patient ──────────────────────────────────────────────────────
|
|
|
|
|
|
class TestStgClinicalPatient:
|
|
"""stg_clinical_patient selects patient demographic columns."""
|
|
|
|
EXPECTED_COLUMNS = [
|
|
"person_id",
|
|
"name_suffix",
|
|
"first_name",
|
|
"middle_name",
|
|
"last_name",
|
|
"sex",
|
|
"race",
|
|
"birth_date",
|
|
"death_date",
|
|
"death_flag",
|
|
"social_security_number",
|
|
"address",
|
|
"city",
|
|
"state",
|
|
"zip_code",
|
|
"county",
|
|
"latitude",
|
|
"longitude",
|
|
"phone",
|
|
"email",
|
|
"ethnicity",
|
|
"data_source",
|
|
]
|
|
|
|
def test_returns_dataframe(self, patient_df) -> None:
|
|
result = stg_clinical_patient(patient_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
def test_row_count_preserved(self, patient_df) -> None:
|
|
result = stg_clinical_patient(patient_df)
|
|
_assert_row_count(result, patient_df)
|
|
|
|
def test_all_expected_columns_present(self, patient_df) -> None:
|
|
result = stg_clinical_patient(patient_df)
|
|
_assert_columns(result, *self.EXPECTED_COLUMNS)
|
|
|
|
def test_person_id_passed_through(self, patient_df) -> None:
|
|
result = stg_clinical_patient(patient_df)
|
|
assert result["person_id"][0] == "P001"
|
|
|
|
def test_first_name_passed_through(self, patient_df) -> None:
|
|
result = stg_clinical_patient(patient_df)
|
|
assert result["first_name"][0] == "Jane"
|
|
|
|
def test_last_name_passed_through(self, patient_df) -> None:
|
|
result = stg_clinical_patient(patient_df)
|
|
assert result["last_name"][0] == "Smith"
|
|
|
|
def test_sex_passed_through(self, patient_df) -> None:
|
|
result = stg_clinical_patient(patient_df)
|
|
assert result["sex"][0] == "F"
|
|
|
|
def test_state_passed_through(self, patient_df) -> None:
|
|
result = stg_clinical_patient(patient_df)
|
|
assert result["state"][0] == "TX"
|
|
|
|
def test_zip_code_passed_through(self, patient_df) -> None:
|
|
result = stg_clinical_patient(patient_df)
|
|
assert result["zip_code"][0] == "78701"
|
|
|
|
def test_data_source_passed_through(self, patient_df) -> None:
|
|
result = stg_clinical_patient(patient_df)
|
|
assert result["data_source"][0] == "test"
|
|
|
|
def test_no_extra_input_layer_columns_leaked(self, patient_df) -> None:
|
|
"""patient_id from the fixture should NOT appear in the stg output."""
|
|
result = stg_clinical_patient(patient_df)
|
|
assert "patient_id" not in result.columns
|
|
|
|
def test_multiple_patients(self) -> None:
|
|
df = pl.DataFrame(
|
|
{
|
|
"person_id": ["P001", "P002"],
|
|
"patient_id": ["PT001", "PT002"],
|
|
"name_suffix": [None, None],
|
|
"first_name": ["Jane", "John"],
|
|
"middle_name": [None, None],
|
|
"last_name": ["Smith", "Doe"],
|
|
"sex": ["F", "M"],
|
|
"race": [None, None],
|
|
"birth_date": [None, None],
|
|
"death_date": [None, None],
|
|
"death_flag": [0, 0],
|
|
"social_security_number": [None, None],
|
|
"address": [None, None],
|
|
"city": [None, None],
|
|
"state": ["TX", "CA"],
|
|
"zip_code": ["78701", "90210"],
|
|
"county": [None, None],
|
|
"latitude": [None, None],
|
|
"longitude": [None, None],
|
|
"phone": [None, None],
|
|
"email": [None, None],
|
|
"ethnicity": [None, None],
|
|
"data_source": ["test", "test"],
|
|
"file_name": ["test.csv", "test.csv"],
|
|
"ingest_datetime": [None, None],
|
|
}
|
|
)
|
|
result = stg_clinical_patient(df)
|
|
assert len(result) == 2
|
|
assert result["first_name"].to_list() == ["Jane", "John"]
|
|
|
|
|
|
# ── stg_clinical_practitioner ─────────────────────────────────────────────────
|
|
|
|
|
|
class TestStgClinicalPractitioner:
|
|
"""stg_clinical_practitioner selects practitioner columns."""
|
|
|
|
EXPECTED_COLUMNS = [
|
|
"practitioner_id",
|
|
"npi",
|
|
"first_name",
|
|
"last_name",
|
|
"practice_affiliation",
|
|
"specialty",
|
|
"sub_specialty",
|
|
"data_source",
|
|
]
|
|
|
|
def test_returns_dataframe(self, practitioner_df) -> None:
|
|
result = stg_clinical_practitioner(practitioner_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
def test_row_count_preserved(self, practitioner_df) -> None:
|
|
result = stg_clinical_practitioner(practitioner_df)
|
|
_assert_row_count(result, practitioner_df)
|
|
|
|
def test_all_expected_columns_present(self, practitioner_df) -> None:
|
|
result = stg_clinical_practitioner(practitioner_df)
|
|
_assert_columns(result, *self.EXPECTED_COLUMNS)
|
|
|
|
def test_practitioner_id_passed_through(self, practitioner_df) -> None:
|
|
result = stg_clinical_practitioner(practitioner_df)
|
|
assert result["practitioner_id"][0] == "DR001"
|
|
|
|
def test_npi_passed_through(self, practitioner_df) -> None:
|
|
result = stg_clinical_practitioner(practitioner_df)
|
|
assert result["npi"][0] == "1234567890"
|
|
|
|
def test_first_name_passed_through(self, practitioner_df) -> None:
|
|
result = stg_clinical_practitioner(practitioner_df)
|
|
assert result["first_name"][0] == "Alice"
|
|
|
|
def test_last_name_passed_through(self, practitioner_df) -> None:
|
|
result = stg_clinical_practitioner(practitioner_df)
|
|
assert result["last_name"][0] == "Johnson"
|
|
|
|
def test_specialty_passed_through(self, practitioner_df) -> None:
|
|
result = stg_clinical_practitioner(practitioner_df)
|
|
assert result["specialty"][0] == "Internal Medicine"
|
|
|
|
def test_data_source_passed_through(self, practitioner_df) -> None:
|
|
result = stg_clinical_practitioner(practitioner_df)
|
|
assert result["data_source"][0] == "test"
|
|
|
|
def test_no_extra_columns(self, practitioner_df) -> None:
|
|
result = stg_clinical_practitioner(practitioner_df)
|
|
assert set(result.columns) == set(self.EXPECTED_COLUMNS)
|
|
|
|
|
|
# ── stg_clinical_location ─────────────────────────────────────────────────────
|
|
|
|
|
|
class TestStgClinicalLocation:
|
|
"""stg_clinical_location selects location columns, aliasing name."""
|
|
|
|
EXPECTED_COLUMNS = [
|
|
"location_id",
|
|
"npi",
|
|
"name",
|
|
"facility_type",
|
|
"parent_organization",
|
|
"address",
|
|
"city",
|
|
"state",
|
|
"zip_code",
|
|
"latitude",
|
|
"longitude",
|
|
"data_source",
|
|
]
|
|
|
|
def test_returns_dataframe(self, location_df) -> None:
|
|
result = stg_clinical_location(location_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
def test_row_count_preserved(self, location_df) -> None:
|
|
result = stg_clinical_location(location_df)
|
|
_assert_row_count(result, location_df)
|
|
|
|
def test_all_expected_columns_present(self, location_df) -> None:
|
|
result = stg_clinical_location(location_df)
|
|
_assert_columns(result, *self.EXPECTED_COLUMNS)
|
|
|
|
def test_location_id_passed_through(self, location_df) -> None:
|
|
result = stg_clinical_location(location_df)
|
|
assert result["location_id"][0] == "LOC001"
|
|
|
|
def test_name_aliased_correctly(self, location_df) -> None:
|
|
result = stg_clinical_location(location_df)
|
|
assert result["name"][0] == "General Hospital"
|
|
|
|
def test_facility_type_passed_through(self, location_df) -> None:
|
|
result = stg_clinical_location(location_df)
|
|
assert result["facility_type"][0] == "hospital"
|
|
|
|
def test_state_passed_through(self, location_df) -> None:
|
|
result = stg_clinical_location(location_df)
|
|
assert result["state"][0] == "TX"
|
|
|
|
def test_zip_code_passed_through(self, location_df) -> None:
|
|
result = stg_clinical_location(location_df)
|
|
assert result["zip_code"][0] == "78702"
|
|
|
|
def test_latitude_passed_through(self, location_df) -> None:
|
|
result = stg_clinical_location(location_df)
|
|
assert result["latitude"][0] == pytest.approx(30.2672)
|
|
|
|
def test_longitude_passed_through(self, location_df) -> None:
|
|
result = stg_clinical_location(location_df)
|
|
assert result["longitude"][0] == pytest.approx(-97.7431)
|
|
|
|
|
|
# ── stg_clinical_procedure ────────────────────────────────────────────────────
|
|
|
|
|
|
class TestStgClinicalProcedure:
|
|
"""stg_clinical_procedure selects procedure columns and adds member_id=null."""
|
|
|
|
EXPECTED_COLUMNS = [
|
|
"procedure_id",
|
|
"person_id",
|
|
"member_id",
|
|
"patient_id",
|
|
"encounter_id",
|
|
"claim_id",
|
|
"procedure_date",
|
|
"source_code_type",
|
|
"source_code",
|
|
"source_description",
|
|
"normalized_code_type",
|
|
"normalized_code",
|
|
"normalized_description",
|
|
"modifier_1",
|
|
"modifier_2",
|
|
"modifier_3",
|
|
"modifier_4",
|
|
"modifier_5",
|
|
"practitioner_id",
|
|
"data_source",
|
|
]
|
|
|
|
def test_returns_dataframe(self, procedure_df) -> None:
|
|
result = stg_clinical_procedure(procedure_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
def test_row_count_preserved(self, procedure_df) -> None:
|
|
result = stg_clinical_procedure(procedure_df)
|
|
_assert_row_count(result, procedure_df)
|
|
|
|
def test_all_expected_columns_present(self, procedure_df) -> None:
|
|
result = stg_clinical_procedure(procedure_df)
|
|
_assert_columns(result, *self.EXPECTED_COLUMNS)
|
|
|
|
def test_member_id_is_null(self, procedure_df) -> None:
|
|
result = stg_clinical_procedure(procedure_df)
|
|
_assert_null_column(result, "member_id")
|
|
|
|
def test_procedure_id_passed_through(self, procedure_df) -> None:
|
|
result = stg_clinical_procedure(procedure_df)
|
|
assert result["procedure_id"][0] == "PROC001"
|
|
|
|
def test_person_id_passed_through(self, procedure_df) -> None:
|
|
result = stg_clinical_procedure(procedure_df)
|
|
assert result["person_id"][0] == "P001"
|
|
|
|
def test_source_code_passed_through(self, procedure_df) -> None:
|
|
result = stg_clinical_procedure(procedure_df)
|
|
assert result["source_code"][0] == "99213"
|
|
|
|
def test_source_code_type_passed_through(self, procedure_df) -> None:
|
|
result = stg_clinical_procedure(procedure_df)
|
|
assert result["source_code_type"][0] == "hcpcs"
|
|
|
|
def test_data_source_passed_through(self, procedure_df) -> None:
|
|
result = stg_clinical_procedure(procedure_df)
|
|
assert result["data_source"][0] == "test"
|
|
|
|
|
|
# ── stg_clinical_appointment ────────────────────────────────────────────────
|
|
|
|
|
|
class TestStgClinicalAppointment:
|
|
"""stg_clinical_appointment selects appointment columns."""
|
|
|
|
def test_returns_dataframe(self, appointment_df) -> None:
|
|
result = stg_clinical_appointment(appointment_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
def test_row_count_preserved(self, appointment_df) -> None:
|
|
result = stg_clinical_appointment(appointment_df)
|
|
_assert_row_count(result, appointment_df)
|
|
|
|
def test_key_columns_present(self, appointment_df) -> None:
|
|
result = stg_clinical_appointment(appointment_df)
|
|
_assert_columns(result, "appointment_id", "person_id", "data_source")
|
|
|
|
def test_appointment_id_passed_through(self, appointment_df) -> None:
|
|
result = stg_clinical_appointment(appointment_df)
|
|
assert result["appointment_id"][0] == "APT001"
|
|
|
|
|
|
# ── stg_clinical_immunization ───────────────────────────────────────────────
|
|
|
|
|
|
class TestStgClinicalImmunization:
|
|
"""stg_clinical_immunization selects immunization columns."""
|
|
|
|
def test_returns_dataframe(self, immunization_df) -> None:
|
|
result = stg_clinical_immunization(immunization_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
def test_row_count_preserved(self, immunization_df) -> None:
|
|
result = stg_clinical_immunization(immunization_df)
|
|
_assert_row_count(result, immunization_df)
|
|
|
|
def test_key_columns_present(self, immunization_df) -> None:
|
|
result = stg_clinical_immunization(immunization_df)
|
|
_assert_columns(result, "immunization_id", "person_id", "data_source")
|
|
|
|
def test_immunization_id_passed_through(self, immunization_df) -> None:
|
|
result = stg_clinical_immunization(immunization_df)
|
|
assert result["immunization_id"][0] == "IMM001"
|
|
|
|
|
|
# ── stg_clinical_lab_result ─────────────────────────────────────────────────
|
|
|
|
|
|
class TestStgClinicalLabResult:
|
|
"""stg_clinical_lab_result selects lab result columns."""
|
|
|
|
def test_returns_dataframe(self, lab_result_df) -> None:
|
|
result = stg_clinical_lab_result(lab_result_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
def test_row_count_preserved(self, lab_result_df) -> None:
|
|
result = stg_clinical_lab_result(lab_result_df)
|
|
_assert_row_count(result, lab_result_df)
|
|
|
|
def test_key_columns_present(self, lab_result_df) -> None:
|
|
result = stg_clinical_lab_result(lab_result_df)
|
|
_assert_columns(result, "lab_result_id", "person_id", "data_source")
|
|
|
|
def test_lab_result_id_passed_through(self, lab_result_df) -> None:
|
|
result = stg_clinical_lab_result(lab_result_df)
|
|
assert result["lab_result_id"][0] == "LAB001"
|
|
|
|
|
|
# ── stg_clinical_medication ─────────────────────────────────────────────────
|
|
|
|
|
|
class TestStgClinicalMedication:
|
|
"""stg_clinical_medication selects medication columns."""
|
|
|
|
def test_returns_dataframe(self, medication_df) -> None:
|
|
result = stg_clinical_medication(medication_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
def test_row_count_preserved(self, medication_df) -> None:
|
|
result = stg_clinical_medication(medication_df)
|
|
_assert_row_count(result, medication_df)
|
|
|
|
def test_key_columns_present(self, medication_df) -> None:
|
|
result = stg_clinical_medication(medication_df)
|
|
_assert_columns(result, "medication_id", "person_id", "data_source")
|
|
|
|
def test_medication_id_passed_through(self, medication_df) -> None:
|
|
result = stg_clinical_medication(medication_df)
|
|
assert result["medication_id"][0] == "MED001"
|
|
|
|
|
|
# ── stg_clinical_observation ────────────────────────────────────────────────
|
|
|
|
|
|
class TestStgClinicalObservation:
|
|
"""stg_clinical_observation selects observation columns."""
|
|
|
|
def test_returns_dataframe(self, observation_df) -> None:
|
|
result = stg_clinical_observation(observation_df)
|
|
assert isinstance(result, pl.DataFrame)
|
|
|
|
def test_row_count_preserved(self, observation_df) -> None:
|
|
result = stg_clinical_observation(observation_df)
|
|
_assert_row_count(result, observation_df)
|
|
|
|
def test_key_columns_present(self, observation_df) -> None:
|
|
result = stg_clinical_observation(observation_df)
|
|
_assert_columns(result, "observation_id", "person_id", "data_source")
|
|
|
|
def test_observation_id_passed_through(self, observation_df) -> None:
|
|
result = stg_clinical_observation(observation_df)
|
|
assert result["observation_id"][0] == "OBS001"
|