Lightmatter / welkin-health

A Python wrapper of the Welkin Health API
https://welkin.readthedocs.io
Other
4 stars 3 forks source link

Tests should be stateless and repeatable #76

Open samamorgan opened 1 year ago

samamorgan commented 1 year ago

In its current form, the test suite requires some state to function. Patient IDs for instance live as strings in tests. This makes generating new VCR cassettes difficult. In the unfortunate event of a breaking API change, fixing this library would represent a significant amount of test refactoring.

The key indicator here is one should be able to delete all cassettes, run the entire test suite, and all new cassettes are regenerated without errors.

I propose that several fixtures be introduced to either get or create the necessary state on a Welkin test instance. For example, a Patient fixture could be made that records its own cassette:

conftest.py

# Note: This is not necessarily functional code, just illustrating an idea.
@pytest.fixture
def patient(client, vcr):
    create_kwargs = {
        "firstName": "Foo",
        "lastName": "Bar",
        "externalId": "welkin-health-test",
    }
    with vcr.use_cassette("patient_id"):
        for patient in client.Patients().get(paginate=True, size=2000):
            if patient.externalId == create_kwargs["externalId"]:
                return patient

        return client.Patient(**create_kwargs).create()

@pytest.fixture
def patient_id(patient):
    return patient.id

test_patients.py

# Refactored to use the new "patient_id" fixture
@pytest.mark.vcr
def test_patient_read(client, patient_id, vcr_cassette):
    patient = client.Patient(id=patient_id).get()

    assert isinstance(patient, Patient)
    assert patient.id == patient_id
    assert len(vcr_cassette) == 1
gone commented 1 year ago

I think this is a great idea - it acts as a test against the api as well since if that breaks the fixture would fall apart