Skip to content

Commit f5d6bdd

Browse files
committed
Add tests for bare functions
1 parent 5122738 commit f5d6bdd

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

tests/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Unit test package for variants."""

tests/_division_data.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
class DivisionDataClass(object):
3+
def __init__(self):
4+
self.MODE_VALS = self.__get_mode_vals()
5+
6+
ALL_VALS = [(0, 1, 0)]
7+
8+
DIV_VALS = ALL_VALS + [
9+
(12, 8, 1.50),
10+
(11, 4, 2.75),
11+
(21, 4, 5.25)
12+
]
13+
14+
ROUND_VALS = ALL_VALS + [
15+
(12, 8, 2.00),
16+
(11, 4, 3.00),
17+
(21, 4, 5.00)
18+
]
19+
20+
FLOOR_VALS = ALL_VALS + [
21+
(12, 8, 1.0),
22+
(11, 4, 2.0),
23+
(21, 4, 5.0)
24+
]
25+
26+
CEIL_VALS = ALL_VALS + [
27+
(12, 8, 2.0),
28+
(11, 4, 3.0),
29+
(21, 4, 6.0)
30+
]
31+
32+
def __get_mode_vals(self):
33+
modes_vals = [
34+
(None, self.DIV_VALS),
35+
('round', self.ROUND_VALS),
36+
('floor', self.FLOOR_VALS),
37+
('ceil', self.CEIL_VALS)
38+
]
39+
40+
out = [(vals + (mode,))
41+
for mode, all_vals in modes_vals
42+
for vals in all_vals]
43+
44+
return out
45+
46+
DivisionData = DivisionDataClass()

tests/test_functions.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from __future__ import division
2+
3+
import math
4+
5+
from variants import variants
6+
7+
from ._division_data import DivisionData
8+
9+
import pytest
10+
11+
###
12+
# Example implementation - division function
13+
@variants
14+
def divide(x, y):
15+
"""A function that divides x by y."""
16+
return x / y
17+
18+
19+
@divide.variant('round')
20+
def divide(x, y):
21+
return round(x / y)
22+
23+
24+
@divide.variant('round_callmain')
25+
def divide(x, y):
26+
return round(divide(x, y))
27+
28+
29+
@divide.variant('floor')
30+
def divide(x, y):
31+
return math.floor(divide(x, y))
32+
33+
34+
@divide.variant('ceil')
35+
def divide(x, y):
36+
return math.ceil(divide(x, y))
37+
38+
39+
@divide.variant('mode')
40+
def divide(x, y, mode=None):
41+
funcs = {
42+
None: divide,
43+
'round': divide.round,
44+
'floor': divide.floor,
45+
'ceil': divide.ceil
46+
}
47+
48+
return funcs[mode](x, y)
49+
50+
###
51+
# Division Function Tests
52+
@pytest.mark.parametrize('x, y, expected', DivisionData.DIV_VALS)
53+
def test_main(x, y, expected):
54+
assert divide(x, y) == expected
55+
56+
57+
@pytest.mark.parametrize('x,y,expected', DivisionData.ROUND_VALS)
58+
def test_round(x, y, expected):
59+
assert divide.round(x, y) == expected
60+
61+
62+
@pytest.mark.parametrize('x,y,expected', DivisionData.ROUND_VALS)
63+
def test_round_callmain(x, y, expected):
64+
assert divide.round_callmain(x, y) == expected
65+
66+
67+
@pytest.mark.parametrize('x,y,expected', DivisionData.FLOOR_VALS)
68+
def test_round_callmain(x, y, expected):
69+
assert divide.floor(x, y) == expected
70+
71+
@pytest.mark.parametrize('x,y,expected', DivisionData.CEIL_VALS)
72+
def test_round_callmain(x, y, expected):
73+
assert divide.ceil(x, y) == expected
74+
75+
76+
@pytest.mark.parametrize('x,y,expected,mode', DivisionData.MODE_VALS)
77+
def test_mode(x, y, expected, mode):
78+
assert divide.mode(x, y, mode) == expected
79+
80+
81+
###
82+
# Division function metadata tests
83+
def test_name():
84+
assert divide.__name__ == 'divide'
85+
86+
87+
def test_docstring():
88+
assert divide.__doc__ == """A function that divides x by y."""
89+
90+
91+
def test_repr():
92+
assert repr(divide) == '<VariantFunction divide>'

0 commit comments

Comments
 (0)