Skip to content

Commit a256e83

Browse files
committed
Move charts tests to their own module
1 parent 7157b3a commit a256e83

File tree

3 files changed

+76
-76
lines changed

3 files changed

+76
-76
lines changed

tests/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import datetime
22

33
import pytest
4+
import pandas as pd
45

56
from src.penn_chime.parameters import (
67
Parameters,
@@ -91,3 +92,13 @@ def model(param):
9192
@pytest.fixture
9293
def halving_model(halving_param):
9394
return SimSirModel(halving_param)
95+
96+
97+
@pytest.fixture
98+
def admits_df():
99+
return pd.read_csv('tests/by_doubling_time/2020-03-28_projected_admits.csv', parse_dates=['date'])
100+
101+
102+
@pytest.fixture
103+
def census_df():
104+
return pd.read_csv('tests/by_doubling_time/2020-03-28_projected_census.csv', parse_dates=['date'])

tests/penn_chime/test_charts.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from math import ceil
2+
from datetime import datetime
3+
4+
import altair as alt
5+
import pytest
6+
7+
from src.penn_chime.charts import (
8+
build_admits_chart,
9+
build_census_chart,
10+
build_descriptions,
11+
)
12+
13+
# TODO add test for asterisk
14+
15+
16+
def test_admits_chart(admits_df):
17+
chart = build_admits_chart(alt=alt, admits_df=admits_df)
18+
assert isinstance(chart, (alt.Chart, alt.LayerChart))
19+
assert round(chart.data.iloc[40].icu, 0) == 39
20+
21+
# test fx call with no params
22+
with pytest.raises(TypeError):
23+
build_admits_chart()
24+
25+
26+
def test_build_descriptions(admits_df, param):
27+
chart = build_admits_chart(alt=alt, admits_df=admits_df)
28+
description = build_descriptions(chart=chart, labels=param.labels)
29+
30+
hosp, icu, vent = description.split("\n\n") # break out the description into lines
31+
32+
max_hosp = chart.data["hospitalized"].max()
33+
assert str(ceil(max_hosp)) in hosp
34+
35+
36+
def test_no_asterisk(admits_df, param):
37+
param.n_days = 600
38+
39+
chart = build_admits_chart(alt=alt, admits_df=admits_df)
40+
description = build_descriptions(chart=chart, labels=param.labels)
41+
assert "*" not in description
42+
43+
44+
def test_census(census_df, param):
45+
chart = build_census_chart(alt=alt, census_df=census_df)
46+
description = build_descriptions(chart=chart, labels=param.labels)
47+
48+
assert str(ceil(chart.data["ventilated"].max())) in description
49+
assert str(chart.data["icu"].idxmax()) not in description
50+
assert (
51+
datetime.strftime(chart.data.iloc[chart.data["icu"].idxmax()].date, "%b %d")
52+
in description
53+
)
54+
55+
56+
def test_census_chart(census_df):
57+
chart = build_census_chart(alt=alt, census_df=census_df)
58+
assert isinstance(chart, (alt.Chart, alt.LayerChart))
59+
assert chart.data.iloc[1].hospitalized == 3
60+
assert chart.data.iloc[49].ventilated == 365
61+
62+
# test fx call with no params
63+
with pytest.raises(TypeError):
64+
build_census_chart()

tests/test_app.py

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
"""Tests."""
2-
3-
from math import ceil # type: ignore
4-
from datetime import date, datetime # type: ignore
5-
import pytest # type: ignore
1+
from datetime import date
62
import pandas as pd # type: ignore
73
import numpy as np # type: ignore
8-
import altair as alt # type: ignore
9-
10-
from src.penn_chime.charts import (
11-
build_admits_chart,
12-
build_census_chart,
13-
build_descriptions,
14-
)
15-
16-
from src.penn_chime.parameters import (
17-
Parameters,
18-
Disposition,
19-
Regions,
20-
)
214

225
EPSILON = 1.e-7
236

@@ -26,14 +9,6 @@
269
# we just want to verify that st _attempted_ to render the right stuff
2710
# so we store the input, and make sure that it matches what we expect
2811

29-
@pytest.fixture
30-
def admits_df():
31-
return pd.read_csv('tests/by_doubling_time/2020-03-28_projected_admits.csv', parse_dates=['date'])
32-
33-
@pytest.fixture
34-
def census_df():
35-
return pd.read_csv('tests/by_doubling_time/2020-03-28_projected_census.csv', parse_dates=['date'])
36-
3712

3813
def test_defaults_repr(DEFAULTS):
3914
"""
@@ -43,28 +18,6 @@ def test_defaults_repr(DEFAULTS):
4318
# TODO: Add assertions here
4419

4520

46-
# Test the math
47-
48-
def test_admits_chart(admits_df):
49-
chart = build_admits_chart(alt=alt, admits_df=admits_df)
50-
assert isinstance(chart, (alt.Chart, alt.LayerChart))
51-
assert round(chart.data.iloc[40].icu, 0) == 39
52-
53-
# test fx call with no params
54-
with pytest.raises(TypeError):
55-
build_admits_chart()
56-
57-
def test_census_chart(census_df):
58-
chart = build_census_chart(alt=alt, census_df=census_df)
59-
assert isinstance(chart, (alt.Chart, alt.LayerChart))
60-
assert chart.data.iloc[1].hospitalized == 3
61-
assert chart.data.iloc[49].ventilated == 365
62-
63-
# test fx call with no params
64-
with pytest.raises(TypeError):
65-
build_census_chart()
66-
67-
6821
def test_model(model, param):
6922
# test the Model
7023

@@ -147,31 +100,3 @@ def test_model_cumulative_census(param, model):
147100
0.05 * 0.05 * (raw_df.infected[1:-1] + raw_df.recovered[1:-1]) - 1.0
148101
)
149102
assert (diff.abs() < 0.1).all()
150-
151-
152-
def test_build_descriptions(admits_df, param):
153-
chart = build_admits_chart(alt=alt, admits_df=admits_df)
154-
description = build_descriptions(chart=chart, labels=param.labels)
155-
156-
hosp, icu, vent = description.split("\n\n") # break out the description into lines
157-
158-
max_hosp = chart.data['hospitalized'].max()
159-
assert str(ceil(max_hosp)) in hosp
160-
161-
# TODO add test for asterisk
162-
163-
def test_no_asterisk(admits_df, param):
164-
param.n_days = 600
165-
166-
chart = build_admits_chart(alt=alt, admits_df=admits_df)
167-
description = build_descriptions(chart=chart, labels=param.labels)
168-
assert "*" not in description
169-
170-
171-
def test_census(census_df, param):
172-
chart = build_census_chart(alt=alt, census_df=census_df)
173-
description = build_descriptions(chart=chart, labels=param.labels)
174-
175-
assert str(ceil(chart.data['ventilated'].max())) in description
176-
assert str(chart.data['icu'].idxmax()) not in description
177-
assert datetime.strftime(chart.data.iloc[chart.data['icu'].idxmax()].date, '%b %d') in description

0 commit comments

Comments
 (0)