Skip to content

Commit 28061d2

Browse files
committed
First tests
1 parent eef6e74 commit 28061d2

2 files changed

Lines changed: 114 additions & 2 deletions

File tree

codonPython/age_bands.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ def age_band_5_years(age: int)->str:
3232
return 'Age not known'
3333

3434
if age > 89:
35-
return '90 and over'
35+
if age < 150:
36+
return '90 and over'
37+
else:
38+
raise ValueError
3639

3740
lowerbound = 5 * int(math.floor(age / 5))
3841
upperbound = lowerbound + 4
@@ -70,7 +73,10 @@ def age_band_10_years(age: int)->str:
7073
return 'Age not known'
7174

7275
if age > 89:
73-
return '90 and over'
76+
if age < 150:
77+
return '90 and over'
78+
else:
79+
raise ValueError
7480

7581
lowerbound = 10 * int(math.floor(age / 10))
7682
upperbound = lowerbound + 9
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from codonPython import age_bands
2+
import numpy as np
3+
import math
4+
import pytest
5+
6+
@pytest.mark.parametrize("age, expected", [
7+
(0, '0-4'),
8+
(1, '0-4'),
9+
(12, '10-14'),
10+
(23, '20-24'),
11+
(34, '30-34'),
12+
(35, '35-39'),
13+
(46, '45-49'),
14+
(57, '55-59'),
15+
(68, '65-69'),
16+
(79, '75-79'),
17+
(90, '90 and over'),
18+
])
19+
def test_age_band_5_years_BAU(age, expected):
20+
assert expected == age_bands.age_band_5_years(age)
21+
22+
def test_age_band_5_years_typeErrors():
23+
with pytest.raises(TypeError):
24+
age_bands.age_band_5_years("age")
25+
26+
@pytest.mark.parametrize("age", [
27+
np.nan,
28+
math.inf
29+
])
30+
def test_age_band_5_years_valueErrors(age):
31+
with pytest.raises(ValueError):
32+
age_bands.age_band_5_years(age)
33+
34+
@pytest.mark.parametrize("age, expected", [
35+
(-1, 'Age not known'),
36+
(None, 'Age not known'),
37+
])
38+
def test_age_band_5_years_edgeCases(age, expected):
39+
assert expected == age_bands.age_band_5_years(age)
40+
41+
@pytest.mark.parametrize("age, expected", [
42+
(0.1, '0-4'),
43+
(1.2, '0-4'),
44+
(12.3, '10-14'),
45+
(23.4, '20-24'),
46+
(34.5, '30-34'),
47+
(35.6, '35-39'),
48+
(46.7, '45-49'),
49+
(57.8, '55-59'),
50+
(68.9, '65-69'),
51+
(79.0, '75-79'),
52+
(90.1, '90 and over'),
53+
])
54+
def test_age_band_5_years_BAU_floats(age, expected):
55+
assert expected == age_bands.age_band_5_years(age)
56+
57+
@pytest.mark.parametrize("age, expected", [
58+
(0, '0-9'),
59+
(1, '0-9'),
60+
(12, '10-19'),
61+
(23, '20-29'),
62+
(34, '30-39'),
63+
(35, '30-39'),
64+
(46, '40-49'),
65+
(57, '50-59'),
66+
(68, '60-69'),
67+
(79, '70-79'),
68+
(90, '90 and over'),
69+
])
70+
def test_age_band_10_years_BAU(age, expected):
71+
assert expected == age_bands.age_band_10_years(age)
72+
73+
def test_age_band_10_years_typeErrors():
74+
with pytest.raises(TypeError):
75+
age_bands.age_band_10_years("age")
76+
77+
@pytest.mark.parametrize("age", [
78+
np.nan,
79+
math.inf
80+
])
81+
def test_age_band_10_years_valueErrors(age):
82+
with pytest.raises(ValueError):
83+
age_bands.age_band_10_years(age)
84+
85+
@pytest.mark.parametrize("age, expected", [
86+
(-1, 'Age not known'),
87+
(None, 'Age not known'),
88+
])
89+
def test_age_band_10_years_edgeCases(age, expected):
90+
assert expected == age_bands.age_band_10_years(age)
91+
92+
@pytest.mark.parametrize("age, expected", [
93+
(0.1, '0-9'),
94+
(1.2, '0-9'),
95+
(12.3, '10-19'),
96+
(23.4, '20-29'),
97+
(34.5, '30-39'),
98+
(35.6, '30-39'),
99+
(46.7, '40-49'),
100+
(57.8, '50-59'),
101+
(68.9, '60-69'),
102+
(79.0, '70-79'),
103+
(90.1, '90 and over'),
104+
])
105+
def test_age_band_10_years_BAU_floats(age, expected):
106+
assert expected == age_bands.age_band_10_years(age)

0 commit comments

Comments
 (0)