Esempio n. 1
0
def test_get_states_daily_full(app, headers):
    test_data = daily_push_ny_wa_two_days()  # two days each of NY and WA
    client = app.test_client()
    write_and_publish_data(client, headers, json.dumps(test_data))

    resp = client.get("/api/v2/public/states/daily")
    assert resp.status_code == 200
    assert len(
        resp.json['data']) == 4  # should have 2 days of data for 2 states

    # should come back in reverse chronological order
    first_data = resp.json['data'][0]
    assert first_data['date'] == '2020-05-25'
    assert first_data['state'] == 'NY'
    assert first_data['tests']['pcr']['people']['positive']['value'] == 20
    assert first_data['tests']['pcr']['people']['negative']['value'] == 5

    # make sure calculated values are correct
    assert first_data['tests']['pcr']['people']['negative']['calculated'] == {
        'population_percent': 0,
        'change_from_prior_day': 1,
        'seven_day_average': 4,
        'seven_day_change_percent': None,
    }

    second_data = resp.json['data'][1]
    assert second_data['date'] == '2020-05-25'
    assert second_data['state'] == 'WA'
    assert second_data['tests']['pcr']['people']['positive']['value'] == 10
    assert second_data['tests']['pcr']['people']['negative']['value'] == 10
def test_get_us_daily(app, headers):
    test_data = daily_push_ny_wa_two_days()
    client = app.test_client()

    # Write a batch containing the above data, two days each of NY and WA
    resp = client.post("/api/v1/batches",
                       data=json.dumps(test_data),
                       content_type='application/json',
                       headers=headers)
    assert resp.status_code == 201
    batch_id = resp.json['batch']['batchId']

    # We haven't published the batch yet, so we shouldn't have any data
    resp = client.get("/api/v1/public/us/daily")
    assert resp.status_code == 200
    assert len(resp.json) == 0

    # but it should appear if we ask for preview data
    resp = client.get("/api/v1/public/us/daily?preview=true")
    assert resp.status_code == 200
    assert len(resp.json) == 2

    # Publish the new batch
    resp = client.post("/api/v1/batches/{}/publish".format(batch_id),
                       headers=headers)
    assert resp.status_code == 201

    resp = client.get("/api/v1/public/us/daily")
    assert resp.status_code == 200
    assert len(resp.json) == 2

    # should come back in reverse chronological order
    assert resp.json[0]['date'] == '2020-05-25'
    assert resp.json[0]['positive'] == 30
    assert resp.json[0]['negative'] == 15
    # check that totalTestResult aggregation works as expected
    assert resp.json[0]['totalTestResults'] == 45

    assert resp.json[1]['date'] == '2020-05-24'
    assert resp.json[1]['positive'] == 24
    assert resp.json[1]['negative'] == 12
    assert resp.json[1]['totalTestResults'] == 36
Esempio n. 3
0
def test_get_states_daily_simple(app, headers):
    test_data = daily_push_ny_wa_two_days()  # two days each of NY and WA
    client = app.test_client()
    write_and_publish_data(client, headers, json.dumps(test_data))

    resp = client.get("/api/v2/public/states/daily/simple")
    assert resp.status_code == 200
    assert len(
        resp.json['data']) == 4  # should have 2 days of data for 2 states

    # should come back in reverse chronological order
    first_data = resp.json['data'][0]
    assert first_data['date'] == '2020-05-25'
    assert first_data['state'] == 'NY'
    assert first_data['tests']['pcr']['people']['positive'] == 20
    assert first_data['tests']['pcr']['people']['negative'] == 5

    second_data = resp.json['data'][1]
    assert second_data['date'] == '2020-05-25'
    assert second_data['state'] == 'WA'
    assert second_data['tests']['pcr']['people']['positive'] == 10
    assert second_data['tests']['pcr']['people']['negative'] == 10
def test_get_states_daily_for_state(app, headers):
    test_data = daily_push_ny_wa_two_days()
    client = app.test_client()

    # Write a batch containing the above data, two days each of NY and WA
    resp = client.post("/api/v1/batches",
                       data=json.dumps(test_data),
                       content_type='application/json',
                       headers=headers)
    assert resp.status_code == 201
    batch_id = resp.json['batch']['batchId']

    resp = client.get("/api/v1/public/states/ny/daily?preview=true")
    assert len(resp.json) == 2

    # publish batch
    resp = client.post("/api/v1/batches/{}/publish".format(batch_id),
                       headers=headers)

    # shouldn't work for "ZZ", but should work for both "ny" and "NY"
    resp = client.get("/api/v1/public/states/ZZ/daily")
    assert resp.status_code == 404

    resp = client.get("/api/v1/public/states/ny/daily")
    assert len(resp.json) == 2
    resp = client.get("/api/v1/public/states/NY/daily")
    assert len(resp.json) == 2

    # should come back in reverse chronological order
    assert resp.json[0]['date'] == '2020-05-25'
    assert resp.json[0]['positive'] == 20
    assert resp.json[0]['negative'] == 5

    assert resp.json[1]['date'] == '2020-05-24'
    assert resp.json[1]['positive'] == 15
    assert resp.json[1]['negative'] == 4
Esempio n. 5
0
def test_get_us_daily(app, headers):
    test_data = daily_push_ny_wa_two_days()
    client = app.test_client()

    # Write and publish a batch containing the above data, two days each of NY and WA
    resp = client.post("/api/v1/batches",
                       data=json.dumps(test_data),
                       content_type='application/json',
                       headers=headers)
    assert resp.status_code == 201
    batch_id = resp.json['batch']['batchId']
    resp = client.post("/api/v1/batches/{}/publish".format(batch_id),
                       headers=headers)
    assert resp.status_code == 201

    resp = client.get("/api/v2/public/us/daily")
    assert resp.status_code == 200
    assert len(resp.json['data']) == 2

    # should come back in reverse chronological order
    first_data = resp.json['data'][0]
    assert first_data['date'] == '2020-05-25'
    assert first_data['states'] == 2
    assert first_data['testing']['total']['value'] == 45

    # make sure calculated values are correct
    assert first_data['testing']['total']['calculated'] == {
        'population_percent': 0.0,
        'change_from_prior_day': 9,
        'seven_day_change_percent': None,
    }

    second_data = resp.json['data'][1]
    assert second_data['date'] == '2020-05-24'
    assert second_data['states'] == 2
    assert second_data['testing']['total']['value'] == 36
def test_get_us_states_csv(app, headers):
    test_data = daily_push_ny_wa_two_days()
    client = app.test_client()

    # Write a batch containing the above data, two days each of NY and WA
    resp = client.post(
        "/api/v1/batches",
        data=json.dumps(test_data),
        content_type='application/json',
        headers=headers)
    assert resp.status_code == 201
    batch_id = resp.json['batch']['batchId']

    # Publish the new batch
    resp = client.post("/api/v1/batches/{}/publish".format(batch_id),
                       headers=headers)
    assert resp.status_code == 201

    # test US daily CSV
    resp = client.get("/api/v1/public/us/daily.csv")
    assert resp.status_code == 200
    lines = resp.data.decode("utf-8").splitlines()
    assert len(lines) == 3
    reader = csv.DictReader(lines, delimiter=',')
    data = list(reader)

    assert data[0]["Date"] == '20200525'
    assert data[0]["States"] == "2"
    assert data[0]["Positive"] == "30"
    assert data[0]["Negative"] == "15"
    assert data[0]["Total Test Results"] == "45"

    assert data[1]["Date"] == '20200524'
    assert data[1]["States"] == "2"
    assert data[1]["Positive"] == "24"
    assert data[1]["Negative"] == "12"
    assert data[0]["Total Test Results"] == "45"

    # test states current CSV
    resp = client.get("/api/v1/public/states/current.csv")
    assert resp.status_code == 200
    lines = resp.data.decode("utf-8").splitlines()
    assert len(lines) == 3
    reader = csv.DictReader(lines, delimiter=',')
    data = list(reader)

    # test states current CSV
    resp = client.get("/api/v1/public/states/current.csv")
    assert resp.status_code == 200
    lines = resp.data.decode("utf-8").splitlines()
    assert len(lines) == 3
    reader = csv.DictReader(lines, delimiter=',')
    data = list(reader)

    assert len(data) == 2
    assert "Date" not in data[0]
    assert data[0]["State"] == "NY"
    assert data[0]["Positive"] == "20"
    assert data[0]["Negative"] == "5"
    assert data[1]["State"] == "WA"
    assert data[1]["Positive"] == "10"
    assert data[1]["Negative"] == "10"

    # test US current CSV
    resp = client.get("/api/v1/public/us/current.csv")
    assert resp.status_code == 200
    lines = resp.data.decode("utf-8").splitlines()
    assert len(lines) == 2
    reader = csv.DictReader(lines, delimiter=',')
    data = list(reader)

    # US current shouldn't have date and state columns
    assert "Date" not in data[0]
    assert "State" not in data[0]
    assert "Deaths" in data[0]
    assert data[0]["Positive"] == "30"
    assert data[0]["Negative"] == "15"