Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor kpi calc tests to use singular constant fixtures for eligibility criteria shared across tests #283

Closed
anchit-chandran opened this issue Sep 22, 2024 · 0 comments
Assignees
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@anchit-chandran
Copy link
Contributor

anchit-chandran commented Sep 22, 2024

All of the tests first start with setting eligibility criteria which are then passed into PatientFactory to create eligible patients.

E.g. in test_kpis_33_40.py:

@pytest.mark.django_db
def test_kpi_calculation_33(AUDIT_START_DATE):
   
...

    # Create  Patients and Visits that should be eligible (KPI5)
    eligible_criteria = {
        # KPI5 base criteria
        "visit__visit_date": AUDIT_START_DATE + relativedelta(days=2),
        "date_of_birth": AUDIT_START_DATE - relativedelta(days=365 * 10),
        # KPI 5 specific eligibility are any of the following:
        # Date of diagnosis NOT within the audit period
        "diagnosis_date": AUDIT_START_DATE - relativedelta(days=2),
        # Date of leaving service NOT within the audit period
        # transfer date only not None if they have left
        "transfer__date_leaving_service": None,
        # Date of death NOT within the audit period"
        "death_date": None,
    }

Often, these eligibility criteria are based on the same measures, usually KPI 1, 5, 6 so far. I think the eligibility criteria may require tweaking so rather than defining them individually in each test as this will be a nightmare to tweak, refactor all universal ones into conftest.py (so all tests can globally access them) as fixtures. An example already exists in conftest.py with AUDIT_START_DATE:

@pytest.fixture
def AUDIT_START_DATE():
    """AUDIT_START_DATE is Day 2 of the first audit period"""
    return date(year=2024, month=4, day=1)

Which can then be used in any test (passing into the test function as an argument) without import e.g in test_kpis_33_40.py:

@pytest.mark.django_db
def test_kpi_calculation_33(AUDIT_START_DATE):
    """Tests that KPI33 is calculated correctly.

    Calculates KPI 32: HbA1c 4+ (%)

    Numerator: Number of eligible patients with at least four entries for HbA1c value (item 17) with an observation date (item 19) within the audit period

    Denominator: Number of patients with Type 1 diabetes with a complete year of care in the audit period (measure 5)
    """

    # Ensure starting with clean pts in test db
    Patient.objects.all().delete()

    # Create  Patients and Visits that should be eligible (KPI5)
    eligible_criteria = {
        # KPI5 base criteria
        "visit__visit_date": AUDIT_START_DATE + relativedelta(days=2),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant