Skip to content

Commit 7157b3a

Browse files
committed
Move models tests to their own module
1 parent b9ce898 commit 7157b3a

File tree

2 files changed

+83
-86
lines changed

2 files changed

+83
-86
lines changed

tests/penn_chime/test_models.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import pytest
2+
import pandas as pd
3+
import numpy as np
4+
5+
from src.penn_chime.models import (
6+
sir,
7+
sim_sir_df,
8+
get_growth_rate,
9+
)
10+
11+
12+
def test_sir():
13+
"""
14+
Someone who is good at testing, help
15+
"""
16+
sir_test = sir(100, 1, 0, 0.2, 0.5, 1)
17+
assert sir_test == (
18+
0.7920792079207921,
19+
0.20297029702970298,
20+
0.0049504950495049506,
21+
), "This contrived example should work"
22+
23+
assert isinstance(sir_test, tuple)
24+
for v in sir_test:
25+
assert isinstance(v, float)
26+
assert v >= 0
27+
28+
# Certain things should *not* work
29+
with pytest.raises(TypeError) as error:
30+
sir("S", 1, 0, 0.2, 0.5, 1)
31+
assert str(error.value) == "can't multiply sequence by non-int of type 'float'"
32+
33+
with pytest.raises(TypeError) as error:
34+
sir(100, "I", 0, 0.2, 0.5, 1)
35+
assert str(error.value) == "can't multiply sequence by non-int of type 'float'"
36+
37+
with pytest.raises(TypeError) as error:
38+
sir(100, 1, "R", 0.2, 0.5, 1)
39+
assert str(error.value) == "unsupported operand type(s) for +: 'float' and 'str'"
40+
41+
with pytest.raises(TypeError) as error:
42+
sir(100, 1, 0, "beta", 0.5, 1)
43+
assert str(error.value) == "bad operand type for unary -: 'str'"
44+
45+
with pytest.raises(TypeError) as error:
46+
sir(100, 1, 0, 0.2, "gamma", 1)
47+
assert str(error.value) == "unsupported operand type(s) for -: 'float' and 'str'"
48+
49+
with pytest.raises(TypeError) as error:
50+
sir(100, 1, 0, 0.2, 0.5, "N")
51+
assert str(error.value) == "unsupported operand type(s) for /: 'str' and 'float'"
52+
53+
# Zeros across the board should fail
54+
with pytest.raises(ZeroDivisionError):
55+
sir(0, 0, 0, 0, 0, 0)
56+
57+
58+
def test_sim_sir():
59+
"""
60+
Rounding to move fast past decimal place issues
61+
"""
62+
raw_df = sim_sir_df(
63+
5, 6, 7, 0.1, 0, 0.1, 40, # s # i # r # gamma # i_day # beta1 # n_days1
64+
)
65+
66+
first = raw_df.iloc[0, :]
67+
last = raw_df.iloc[-1, :]
68+
69+
assert round(first.susceptible, 0) == 5
70+
assert round(first.infected, 2) == 6
71+
assert round(first.recovered, 0) == 7
72+
assert round(last.susceptible, 2) == 0
73+
assert round(last.infected, 2) == 0.18
74+
assert round(last.recovered, 2) == 17.82
75+
76+
assert isinstance(raw_df, pd.DataFrame)
77+
78+
79+
def test_growth_rate():
80+
assert np.round(get_growth_rate(5) * 100.0, decimals=4) == 14.8698
81+
assert np.round(get_growth_rate(0) * 100.0, decimals=4) == 0.0
82+
assert np.round(get_growth_rate(-4) * 100.0, decimals=4) == -15.9104

tests/test_app.py

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@
1212
build_census_chart,
1313
build_descriptions,
1414
)
15-
from src.penn_chime.models import (
16-
SimSirModel as Model,
17-
sir,
18-
sim_sir_df,
19-
build_admits_df,
20-
get_growth_rate,
21-
)
15+
2216
from src.penn_chime.parameters import (
2317
Parameters,
2418
Disposition,
@@ -51,79 +45,6 @@ def test_defaults_repr(DEFAULTS):
5145

5246
# Test the math
5347

54-
55-
def test_sir():
56-
"""
57-
Someone who is good at testing, help
58-
"""
59-
sir_test = sir(100, 1, 0, 0.2, 0.5, 1)
60-
assert sir_test == (
61-
0.7920792079207921,
62-
0.20297029702970298,
63-
0.0049504950495049506,
64-
), "This contrived example should work"
65-
66-
assert isinstance(sir_test, tuple)
67-
for v in sir_test:
68-
assert isinstance(v, float)
69-
assert v >= 0
70-
71-
# Certain things should *not* work
72-
with pytest.raises(TypeError) as error:
73-
sir("S", 1, 0, 0.2, 0.5, 1)
74-
assert str(error.value) == "can't multiply sequence by non-int of type 'float'"
75-
76-
with pytest.raises(TypeError) as error:
77-
sir(100, "I", 0, 0.2, 0.5, 1)
78-
assert str(error.value) == "can't multiply sequence by non-int of type 'float'"
79-
80-
with pytest.raises(TypeError) as error:
81-
sir(100, 1, "R", 0.2, 0.5, 1)
82-
assert str(error.value) == "unsupported operand type(s) for +: 'float' and 'str'"
83-
84-
with pytest.raises(TypeError) as error:
85-
sir(100, 1, 0, "beta", 0.5, 1)
86-
assert str(error.value) == "bad operand type for unary -: 'str'"
87-
88-
with pytest.raises(TypeError) as error:
89-
sir(100, 1, 0, 0.2, "gamma", 1)
90-
assert str(error.value) == "unsupported operand type(s) for -: 'float' and 'str'"
91-
92-
with pytest.raises(TypeError) as error:
93-
sir(100, 1, 0, 0.2, 0.5, "N")
94-
assert str(error.value) == "unsupported operand type(s) for /: 'str' and 'float'"
95-
96-
# Zeros across the board should fail
97-
with pytest.raises(ZeroDivisionError):
98-
sir(0, 0, 0, 0, 0, 0)
99-
100-
101-
def test_sim_sir():
102-
"""
103-
Rounding to move fast past decimal place issues
104-
"""
105-
raw_df = sim_sir_df(
106-
5, # s
107-
6, # i
108-
7, # r
109-
0.1, # gamma
110-
0, # i_day
111-
0.1, # beta1
112-
40, # n_days1
113-
)
114-
115-
first = raw_df.iloc[0, :]
116-
last = raw_df.iloc[-1, :]
117-
118-
assert round(first.susceptible, 0) == 5
119-
assert round(first.infected, 2) == 6
120-
assert round(first.recovered, 0) == 7
121-
assert round(last.susceptible, 2) == 0
122-
assert round(last.infected, 2) == 0.18
123-
assert round(last.recovered, 2) == 17.82
124-
125-
assert isinstance(raw_df, pd.DataFrame)
126-
12748
def test_admits_chart(admits_df):
12849
chart = build_admits_chart(alt=alt, admits_df=admits_df)
12950
assert isinstance(chart, (alt.Chart, alt.LayerChart))
@@ -228,12 +149,6 @@ def test_model_cumulative_census(param, model):
228149
assert (diff.abs() < 0.1).all()
229150

230151

231-
def test_growth_rate():
232-
assert np.round(get_growth_rate(5) * 100.0, decimals=4) == 14.8698
233-
assert np.round(get_growth_rate(0) * 100.0, decimals=4) == 0.0
234-
assert np.round(get_growth_rate(-4) * 100.0, decimals=4) == -15.9104
235-
236-
237152
def test_build_descriptions(admits_df, param):
238153
chart = build_admits_chart(alt=alt, admits_df=admits_df)
239154
description = build_descriptions(chart=chart, labels=param.labels)

0 commit comments

Comments
 (0)