clairenied15 / heart_rate_sentinel_server

0 stars 0 forks source link

Problems with tests #9

Closed clairenied15 closed 5 years ago

clairenied15 commented 5 years ago

I am still confused as to how we can make assertions for testing our functions. I tried testing my tachycardia() function by:

def test_tach(): r = tachycardia(1) assert r == "no tachycardia"

which was the only thing I could think of. I'm not sure how we can make assertions just based on the inputted patient_id because the functions rely on the posted dictionary info. I am REALLY confused on how we do tests for this project. All of the functions seem untestable.

dward2 commented 5 years ago

Here is how I would approach testing for this project. The functions that are tied to an end point (i.e., have an @app.route decorating them) should only do a minimal amount: take in the data from the GET or POST request, send that data out to another function for data manipulation, and then return the result to the calling function. What we will test is then the other function that you use for data manipulation.

So, taking your tachycardia function as an example, I would take your entire for loop and move it to a completely different function (let's call it calculate_tachycardia). Your tachycardia function would then consist of only the following:

@app.route("/api/status/<patient_id>", methods=["GET"])
def tachycardia(patient_id):
    global datastore
    answer = calculate_tachycardia(datastore, patient_id)
    return answer

The new procedure calculate_tachycardia now does all of the data manipulation:

def calculate_tachycardia(datastore, patient_id):
    for item in datastore:
        if item["patient_id"] == patient_id:
            age=item["age"]
            .
            .
            .

Now, you can more easily test the functionality of your data manipulations. For example:

def test_tach():
    datastore=[{'patient_id': "2", `attending_email`: "test@test.com", `age`: 52,
                            heart_rates=[80, 76, 77],
                            heart_rate_times=[time1, time2, time3]},
                       {'patient_id': "3", `attending_email`: "test@test.com", `age`: 42,
                            heart_rates=[80, 76, 77],
                            heart_rate_times=[time1, time2, time3]}]
    patient_id = "2"
    answer = calculate_tachycardia(datastore, patient_id):
    assert answer == "no tachycardia"

What you have now done is remove the need for the server to be active or have an active datastore variable for the test to work. You create a test database to send to your data manipulation function to test it.

Let me know if you'd like to discuss this approach further.