Skip to content

Commit 3b77808

Browse files
authored
Merge pull request #41 from pganssle/improve_coverage
Improve coverage
2 parents 26e62ea + 78ede19 commit 3b77808

File tree

6 files changed

+98
-8
lines changed

6 files changed

+98
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ __pycache__
99
build/
1010
dist/
1111
dists/
12+
pip-wheel-metadata
1213
.eggs
1314
*.egg-info/
1415

@@ -25,3 +26,4 @@ docs/_build/
2526
.idea
2627
.cache
2728
.mypy_cache
29+
.DS_STORE

src/grid_strategy/_abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_grid(self, n):
3535

3636
@classmethod
3737
@abstractmethod
38-
def get_grid_arrangement(cls, n):
38+
def get_grid_arrangement(cls, n): # pragma: nocover
3939
pass
4040

4141
def get_gridspec(self, grid_arrangement):

src/grid_strategy/strategies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def stripe_even(cls, n_more, more_val, n_less, less_val):
170170
"""
171171
total = n_more + n_less
172172
if total % 2:
173-
msg = ("Expected an even number of values, " + "got {} + {}").format(
173+
msg = "Expected an even number of values, got {} + {}".format(
174174
n_more, n_less
175175
)
176176
raise ValueError(msg)

tests/test_grids.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import pytest
2+
from unittest import mock
3+
4+
from grid_strategy.strategies import SquareStrategy
5+
6+
7+
class SpecValue:
8+
def __init__(self, rows, cols, parent=None):
9+
self.rows = rows
10+
self.cols = cols
11+
self.parent = parent
12+
13+
def __repr__(self): # pragma: nocover
14+
return f"{self.__class__.__name__}({self.rows}, {self.cols})"
15+
16+
def __eq__(self, other):
17+
return self.rows == other.rows and self.cols == other.cols
18+
19+
20+
class GridSpecMock:
21+
def __init__(self, nrows, ncols, *args, **kwargs):
22+
self._nrows_ = nrows
23+
self._ncols_ = ncols
24+
25+
self._args_ = args
26+
self._kwargs_ = kwargs
27+
28+
def __getitem__(self, key_tup):
29+
return SpecValue(*key_tup, self)
30+
31+
32+
@pytest.fixture
33+
def gridspec_mock():
34+
class Figure:
35+
pass
36+
37+
def figure(*args, **kwargs):
38+
return Figure()
39+
40+
with mock.patch(f"grid_strategy._abc.gridspec.GridSpec", new=GridSpecMock) as g:
41+
with mock.patch(f"grid_strategy._abc.plt.figure", new=figure):
42+
yield g
43+
44+
45+
@pytest.mark.parametrize(
46+
"align, n, exp_specs",
47+
[
48+
("center", 1, [(0, slice(0, 1))]),
49+
("center", 2, [(0, slice(0, 1)), (0, slice(1, 2))]),
50+
("center", 3, [(0, slice(0, 2)), (0, slice(2, 4)), (1, slice(1, 3))]),
51+
("left", 3, [(0, slice(0, 2)), (0, slice(2, 4)), (1, slice(0, 2))]),
52+
("right", 3, [(0, slice(0, 2)), (0, slice(2, 4)), (1, slice(2, 4))]),
53+
("justified", 3, [(0, slice(0, 1)), (0, slice(1, 2)), (1, slice(0, 2))]),
54+
(
55+
"center",
56+
8,
57+
[
58+
(0, slice(0, 2)),
59+
(0, slice(2, 4)),
60+
(0, slice(4, 6)),
61+
(1, slice(1, 3)),
62+
(1, slice(3, 5)),
63+
(2, slice(0, 2)),
64+
(2, slice(2, 4)),
65+
(2, slice(4, 6)),
66+
],
67+
),
68+
("left", 2, [(0, slice(0, 1)), (0, slice(1, 2))]),
69+
],
70+
)
71+
def test_square_spec(gridspec_mock, align, n, exp_specs):
72+
ss = SquareStrategy(align)
73+
74+
act = ss.get_grid(n)
75+
exp = [SpecValue(*spec) for spec in exp_specs]
76+
77+
assert act == exp

tests/test_strategies.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,27 @@ def test_rectangular_strategy(rectangular_strategy, num_plots, grid_arrangement)
4646
(9, (3, 3, 3)),
4747
(10, (3, 4, 3)),
4848
(12, (4, 4, 4)),
49+
(14, (3, 4, 4, 3)),
50+
(17, (3, 4, 3, 4, 3)),
4951
(20, (5, 5, 5, 5)),
52+
(31, (6, 6, 7, 6, 6)),
53+
(34, (6, 5, 6, 6, 5, 6)),
54+
(58, (7, 8, 7, 7, 7, 7, 8, 7)),
55+
(94, (9, 10, 9, 10, 9, 9, 10, 9, 10, 9)),
5056
],
5157
)
5258
def test_square_strategy(square_strategy, num_plots, grid_arrangement):
5359
assert square_strategy.get_grid_arrangement(num_plots) == grid_arrangement
5460

5561

5662
# Test for bad input
57-
def test_strategy_with_bad_input(rectangular_strategy, square_strategy):
63+
@pytest.mark.parametrize("n", [-1, -1000])
64+
def test_rectangular_strategy_with_bad_input(rectangular_strategy, n):
5865
with pytest.raises(ValueError):
59-
rectangular_strategy.get_grid(-1)
60-
rectangular_strategy.get_grid(-1000)
66+
rectangular_strategy.get_grid(n)
6167

62-
square_strategy.get_grid(-1)
63-
square_strategy.get_grid(-110)
68+
69+
@pytest.mark.parametrize("n", [-1, -1000])
70+
def test_square_strategy_with_bad_input(square_strategy, n):
71+
with pytest.raises(ValueError):
72+
square_strategy.get_grid(n)

tox.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ deps =
1111
pytest-cov >= 2.0.0
1212
coverage
1313
commands = pytest --cov-config="{toxinidir}/tox.ini" \
14-
--cov="{envsitepackagesdir}/grid_strategy"
14+
--cov="{envsitepackagesdir}/grid_strategy" \
15+
--cov="{toxinidir}/tests" \
16+
{posargs}
1517

1618
[testenv:coverage]
1719
description = combine coverage data and create reports

0 commit comments

Comments
 (0)