Skip to content

Commit 03f5581

Browse files
committed
formatter tests more in the pytest style
1 parent 39e3fb3 commit 03f5581

File tree

1 file changed

+49
-85
lines changed

1 file changed

+49
-85
lines changed

probscale/tests/test_formatters.py

Lines changed: 49 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,94 +6,58 @@
66
from probscale import formatters
77

88

9-
class Mixin_Check_Formatter_sig_figs(object):
10-
def teardown(self):
11-
pass
9+
@pytest.mark.parametrize("fmtr", [formatters.PctFormatter, formatters.ProbFormatter])
10+
def test_base_class_of_formatter(fmtr):
11+
assert issubclass(fmtr, formatters._FormatterMixin)
1212

13-
def test_baseline(self):
14-
assert self.fmt._sig_figs(self.x, 3) == self.known_3
15-
assert self.fmt._sig_figs(self.x, 4) == self.known_4
1613

17-
def test_string(self):
18-
assert self.fmt._sig_figs('1.23', 3) == '1.23'
19-
20-
def test_na_inf(self):
21-
assert self.fmt._sig_figs(numpy.nan, 3) == 'NA'
22-
assert self.fmt._sig_figs(numpy.inf, 3) == 'NA'
23-
24-
def test_zero(self):
25-
assert self.fmt._sig_figs(0, 3) == '0'
26-
27-
def test_trailing_zeros(self):
28-
assert self.fmt._sig_figs(self.x, 8) == self.known_8
29-
30-
def test_sigFigs_zero_n(self):
31-
with pytest.raises(ValueError):
32-
self.fmt._sig_figs(self.x, 0)
33-
34-
def test_sigFigs_negative_n(self):
35-
with pytest.raises(ValueError):
36-
self.fmt._sig_figs(self.x, -1)
37-
38-
def test_forceint(self):
39-
assert self.fmt._sig_figs(self.x, 3, forceint=True) == self.known_int
40-
41-
42-
class Mixin_Check_PctFormatter_sig_figs(Mixin_Check_Formatter_sig_figs):
14+
@pytest.mark.parametrize(('pct', 'expected'), [
15+
(0.0301, '0.03'), (0.20, '0.2'), (0.100, '0.1'),
16+
(10.000, '10'), (5.00, '5'), (50.00, '50'),
17+
(99.000, '99'), (99.1, '99.1'), (99.99, '99.99'),
18+
])
19+
def test__call___PctFormatter(pct, expected):
4320
fmt = formatters.PctFormatter()
44-
def test__call__(self):
45-
assert self.fmt(0.0301) == '0.03'
46-
assert self.fmt(0.2) == '0.2'
47-
assert self.fmt(0.1) == '0.1'
48-
assert self.fmt(10) == '10'
49-
assert self.fmt(5) == '5'
50-
assert self.fmt(50) == '50'
51-
assert self.fmt(99) == '99'
52-
assert self.fmt(99.1) == '99.1'
53-
assert self.fmt(99.99) == '99.99'
21+
assert fmt(pct) == expected
5422

5523

56-
class Mixin_Check_ProbFormatter_sig_figs(Mixin_Check_Formatter_sig_figs):
24+
@pytest.mark.parametrize(('prob', 'expected'), [
25+
(0.000301, '0.0003'), (0.001000, '0.001'), (0.100000, '0.10'),
26+
(0.050000, '0.05'), (0.500000, '0.50'), (0.990000, '0.99'),
27+
(0.991000, '0.991'), (0.999900, '0.9999'),
28+
])
29+
def test__call___ProbFormmater(prob, expected):
5730
fmt = formatters.ProbFormatter()
58-
def test__call__(self):
59-
assert self.fmt(0.000301) == '0.0003'
60-
assert self.fmt(0.001) == '0.001'
61-
assert self.fmt(0.10) == '0.10'
62-
assert self.fmt(0.05) == '0.05'
63-
assert self.fmt(0.50) == '0.50'
64-
assert self.fmt(0.99) == '0.99'
65-
assert self.fmt(0.991) == '0.991'
66-
assert self.fmt(0.9999) == '0.9999'
67-
68-
69-
class Test_PctFormatter_sig_figs_gt1(Mixin_Check_PctFormatter_sig_figs):
70-
def setup(self):
71-
self.x = 1234.56
72-
self.known_3 = '1,230'
73-
self.known_4 = '1,235'
74-
self.known_8 = '1,234.5600'
75-
self.known_exp3 = '1.23e+08'
76-
self.known_int = '1,235'
77-
self.factor = 10**5
78-
79-
80-
class Test_PctFormatter_sig_figs_lt1(Mixin_Check_PctFormatter_sig_figs):
81-
def setup(self):
82-
self.x = 0.123456
83-
self.known_3 = '0.123'
84-
self.known_4 = '0.1235'
85-
self.known_8 = '0.12345600'
86-
self.known_exp3 = '1.23e-07'
87-
self.known_int = '0'
88-
self.factor = 10**-6
89-
90-
91-
class Test_ProbFormatter_sig_figs(Mixin_Check_ProbFormatter_sig_figs):
92-
def setup(self):
93-
self.x = 0.123456
94-
self.known_3 = '0.123'
95-
self.known_4 = '0.1235'
96-
self.known_8 = '0.12345600'
97-
self.known_exp3 = '1.23e-07'
98-
self.known_int = '0'
99-
self.factor = 10**-6
31+
assert fmt(prob) == expected
32+
33+
34+
@pytest.mark.parametrize(('value', 'N', 'expected', 'forceint'), [
35+
(1234.56, 3, '1,230', False),
36+
(1234.56, 4, '1,235', False),
37+
('1.23', 3, '1.23', False),
38+
(numpy.nan, 3, 'NA', False),
39+
(numpy.inf, 3, 'NA', False),
40+
(0, 3, '0', False),
41+
(1234.56, 8, '1,234.5600', False),
42+
(1.23456e8, 3, '1.23e+08', False),
43+
(1234.56, 3, '1,235', True),
44+
(0.123456, 3, '0.123', False),
45+
(0.123456, 4, '0.1235', False),
46+
('0.123456', 3, '0.123', False),
47+
(numpy.nan, 3, 'NA', False),
48+
(numpy.inf, 3, 'NA', False),
49+
(0, 3, '0', False),
50+
(0.123456, 8, '0.12345600', False),
51+
(1.23456e-7, 3, '1.23e-07', False),
52+
(0.123456, 3, '0', True),
53+
])
54+
def test__sig_figs(value, N, expected, forceint):
55+
fmt = formatters._FormatterMixin()
56+
assert fmt._sig_figs(value, N, forceint=forceint) == expected
57+
58+
59+
@pytest.mark.parametrize('N', [-1, 0, 0.5])
60+
def test__sig_figs_errors(N):
61+
fmt = formatters._FormatterMixin()
62+
with pytest.raises(ValueError):
63+
fmt._sig_figs(1.23, N)

0 commit comments

Comments
 (0)