def test_patients_registered_with_one_practice_between():
    session = make_session()

    patient_registered_in_2001 = Patient()
    patient_registered_in_2002 = Patient()
    patient_unregistered_in_2002 = Patient()
    patient_registered_in_2001.RegistrationHistory = [
        RegistrationHistory(StartDate="2001-01-01", EndDate="9999-01-01")
    ]
    patient_registered_in_2002.RegistrationHistory = [
        RegistrationHistory(StartDate="2002-01-01", EndDate="9999-01-01")
    ]
    patient_unregistered_in_2002.RegistrationHistory = [
        RegistrationHistory(StartDate="2001-01-01", EndDate="2002-01-01")
    ]

    session.add(patient_registered_in_2001)
    session.add(patient_registered_in_2002)
    session.add(patient_unregistered_in_2002)
    session.commit()

    study = StudyDefinition(
        population=patients.registered_with_one_practice_between(
            "2001-12-01", "2003-01-01"
        )
    )
    results = study.to_dicts()
    assert [x["patient_id"] for x in results] == [
        str(patient_registered_in_2001.Patient_ID)
    ]
def test_patient_registered_as_of():
    session = make_session()

    patient_registered_in_2001 = Patient()
    patient_registered_in_2002 = Patient()
    patient_unregistered_in_2002 = Patient()
    patient_registered_in_2001.RegistrationHistory = [
        RegistrationHistory(StartDate="2001-01-01", EndDate="9999-01-01")
    ]
    patient_registered_in_2002.RegistrationHistory = [
        RegistrationHistory(StartDate="2002-01-01", EndDate="9999-01-01")
    ]
    patient_unregistered_in_2002.RegistrationHistory = [
        RegistrationHistory(StartDate="2001-01-01", EndDate="2002-01-01")
    ]

    session.add(patient_registered_in_2001)
    session.add(patient_registered_in_2002)
    session.add(patient_unregistered_in_2002)
    session.commit()

    # No date criteria
    study = StudyDefinition(population=patients.registered_as_of("2002-03-02"))
    results = study.to_dicts()
    assert [x["patient_id"] for x in results] == [
        str(patient_registered_in_2001.Patient_ID),
        str(patient_registered_in_2002.Patient_ID),
    ]
def set_up_patients(event_data):
    organisations = [Organisation(Organisation_ID=ix) for ix in range(5)]

    # This patient is in the target population, because they're still registered at the
    # practice.
    current_patient = Patient()
    current_patient.RegistrationHistory = [
        RegistrationHistory(
            StartDate="2000-01-01",
            EndDate="2001-01-01",
            Organisation=organisations[0],
        ),
        RegistrationHistory(
            StartDate="2001-01-01",
            EndDate="9999-01-01",
            Organisation=organisations[1],
        ),
    ]
    current_patient.CodedEventsSnomed = [
        CodedEventSnomed(ConsultationDate=date, ConceptID=code)
        for date, code in event_data
    ]

    # This patient is in the target population, because they're still registered at the
    # practice, but they have no events.
    current_patient_no_events = Patient()
    current_patient_no_events.RegistrationHistory = [
        RegistrationHistory(
            StartDate="2001-01-01",
            EndDate="9999-01-01",
            Organisation=organisations[1],
        ),
    ]

    # This patient is in the target population, because they died during the timeframe
    # we're interested in.
    dead_patient = Patient(DateOfDeath="2022-01-15")
    dead_patient.RegistrationHistory = [
        RegistrationHistory(
            StartDate="2001-01-01",
            EndDate="2022-01-15",
            Organisation=organisations[2],
        )
    ]
    dead_patient.CodedEventsSnomed = [
        CodedEventSnomed(ConsultationDate=date, ConceptID=code)
        for date, code in event_data
    ]

    # This patient is not in the target population, because they were not registered by
    # the end of the timeframe we're interested in.
    former_patient = Patient()
    former_patient.RegistrationHistory = [
        RegistrationHistory(
            StartDate="2001-01-01",
            EndDate="2022-01-15",
            Organisation=organisations[3],
        )
    ]
    former_patient.CodedEventsSnomed = [
        CodedEventSnomed(ConsultationDate=date, ConceptID=code)
        for date, code in event_data
    ]

    # This patient is not in the target population, because they died before the
    # timeframe we're interested in.
    long_dead_patient = Patient(DateOfDeath="2011-12-31")
    long_dead_patient.RegistrationHistory = [
        RegistrationHistory(
            StartDate="2001-01-01",
            EndDate="2011-12-31",
            Organisation=organisations[4],
        )
    ]
    long_dead_patient.CodedEventsSnomed = [
        CodedEventSnomed(ConsultationDate=date, ConceptID=code)
        for date, code in event_data
    ]

    session = make_session()
    session.add(current_patient)
    session.add(current_patient_no_events)
    session.add(dead_patient)
    session.add(former_patient)
    session.add(long_dead_patient)
    session.commit()