def test_minimal_study_to_csv():
    session = make_session()
    patient_1 = Patient(DateOfBirth="1900-01-01", Sex="M")
    patient_2 = Patient(DateOfBirth="1900-01-01", Sex="F")
    session.add_all([patient_1, patient_2])
    session.commit()
    study = StudyDefinition(population=patients.all(), sex=patients.sex())
    with tempfile.NamedTemporaryFile(mode="w+") as f:
        study.to_csv(f.name)
        results = list(csv.DictReader(f))
        assert results == [
            {"patient_id": str(patient_1.Patient_ID), "sex": "M"},
            {"patient_id": str(patient_2.Patient_ID), "sex": "F"},
        ]
def test_sqlcmd_and_odbc_outputs_match():
    session = make_session()
    patient = Patient(DateOfBirth="1950-01-01")
    patient.CodedEvents.append(
        CodedEvent(CTV3Code="XYZ", NumericValue=50, ConsultationDate="2002-06-01")
    )
    session.add(patient)
    session.commit()

    study = StudyDefinition(
        population=patients.with_these_clinical_events(codelist(["XYZ"], "ctv3"))
    )
    with tempfile.NamedTemporaryFile() as input_csv_odbc, tempfile.NamedTemporaryFile() as input_csv_sqlcmd:
        # windows line endings
        study.to_csv(input_csv_odbc.name, with_sqlcmd=False)
        # unix line endings
        study.to_csv(input_csv_sqlcmd.name, with_sqlcmd=True)
        assert filecmp.cmp(input_csv_odbc.name, input_csv_sqlcmd.name, shallow=False)
def test_duplicate_id_checking():
    study = StudyDefinition(population=patients.all())
    # A bit of a hack: overwrite the queries we're going to run with a query which
    # deliberately returns duplicate values
    study.queries = [
        (
            "dummy_query",
            """
            SELECT * FROM (
              VALUES
                (1,1),
                (2,2),
                (3,3),
                (1,4)
            ) t (patient_id, foo)
            """,
        )
    ]
    with pytest.raises(RuntimeError):
        study.to_dicts()
    with pytest.raises(RuntimeError):
        with tempfile.NamedTemporaryFile(mode="w+") as f:
            study.to_csv(f.name)