X Tutup
Skip to content

Commit 80fdd06

Browse files
Migrate tests to pytest from nose.
1 parent 9f7e9fd commit 80fdd06

16 files changed

+276
-276
lines changed

patsy/build.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ def _max_allowed_dim(dim, arr, factor):
4747
raise PatsyError(msg, factor)
4848

4949
def test__max_allowed_dim():
50-
from nose.tools import assert_raises
50+
import pytest
5151
f = _MockFactor()
5252
_max_allowed_dim(1, np.array(1), f)
5353
_max_allowed_dim(1, np.array([1]), f)
54-
assert_raises(PatsyError, _max_allowed_dim, 1, np.array([[1]]), f)
55-
assert_raises(PatsyError, _max_allowed_dim, 1, np.array([[[1]]]), f)
54+
pytest.raises(PatsyError, _max_allowed_dim, 1, np.array([[1]]), f)
55+
pytest.raises(PatsyError, _max_allowed_dim, 1, np.array([[[1]]]), f)
5656
_max_allowed_dim(2, np.array(1), f)
5757
_max_allowed_dim(2, np.array([1]), f)
5858
_max_allowed_dim(2, np.array([[1]]), f)
59-
assert_raises(PatsyError, _max_allowed_dim, 2, np.array([[[1]]]), f)
59+
pytest.raises(PatsyError, _max_allowed_dim, 2, np.array([[[1]]]), f)
6060

6161
def _eval_factor(factor_info, data, NA_action):
6262
factor = factor_info.factor
@@ -87,7 +87,7 @@ def _eval_factor(factor_info, data, NA_action):
8787
return result, np.asarray(result == -1)
8888

8989
def test__eval_factor_numerical():
90-
from nose.tools import assert_raises
90+
import pytest
9191
naa = NAAction()
9292
f = _MockFactor()
9393

@@ -99,10 +99,10 @@ def test__eval_factor_numerical():
9999
assert np.all(eval123 == [[1], [2], [3]])
100100
assert is_NA.shape == (3,)
101101
assert np.all(~is_NA)
102-
assert_raises(PatsyError, _eval_factor, fi1, {"mock": [[[1]]]}, naa)
103-
assert_raises(PatsyError, _eval_factor, fi1, {"mock": [[1, 2]]}, naa)
104-
assert_raises(PatsyError, _eval_factor, fi1, {"mock": ["a", "b"]}, naa)
105-
assert_raises(PatsyError, _eval_factor, fi1, {"mock": [True, False]}, naa)
102+
pytest.raises(PatsyError, _eval_factor, fi1, {"mock": [[[1]]]}, naa)
103+
pytest.raises(PatsyError, _eval_factor, fi1, {"mock": [[1, 2]]}, naa)
104+
pytest.raises(PatsyError, _eval_factor, fi1, {"mock": ["a", "b"]}, naa)
105+
pytest.raises(PatsyError, _eval_factor, fi1, {"mock": [True, False]}, naa)
106106
fi2 = FactorInfo(_MockFactor(), "numerical",
107107
{}, num_columns=2, categories=None)
108108
eval123321, is_NA = _eval_factor(fi2,
@@ -112,8 +112,8 @@ def test__eval_factor_numerical():
112112
assert np.all(eval123321 == [[1, 3], [2, 2], [3, 1]])
113113
assert is_NA.shape == (3,)
114114
assert np.all(~is_NA)
115-
assert_raises(PatsyError, _eval_factor, fi2, {"mock": [1, 2, 3]}, naa)
116-
assert_raises(PatsyError, _eval_factor, fi2, {"mock": [[1, 2, 3]]}, naa)
115+
pytest.raises(PatsyError, _eval_factor, fi2, {"mock": [1, 2, 3]}, naa)
116+
pytest.raises(PatsyError, _eval_factor, fi2, {"mock": [[1, 2, 3]]}, naa)
117117

118118
ev_nan, is_NA = _eval_factor(fi1, {"mock": [1, 2, np.nan]},
119119
NAAction(NA_types=["NaN"]))
@@ -148,19 +148,19 @@ def test__eval_factor_numerical():
148148
assert np.array_equal(eval_df2, [[2, 3], [1, 4], [3, -1]])
149149
assert np.array_equal(eval_df2.index, [20, 30, 10])
150150

151-
assert_raises(PatsyError,
151+
pytest.raises(PatsyError,
152152
_eval_factor, fi2,
153153
{"mock": pandas.Series([1, 2, 3], index=[10, 20, 30])},
154154
naa)
155-
assert_raises(PatsyError,
155+
pytest.raises(PatsyError,
156156
_eval_factor, fi1,
157157
{"mock":
158158
pandas.DataFrame([[2, 3], [1, 4], [3, -1]],
159159
index=[20, 30, 10])},
160160
naa)
161161

162162
def test__eval_factor_categorical():
163-
from nose.tools import assert_raises
163+
import pytest
164164
from patsy.categorical import C
165165
naa = NAAction()
166166
f = _MockFactor()
@@ -170,20 +170,20 @@ def test__eval_factor_categorical():
170170
cat1, _ = _eval_factor(fi1, {"mock": ["b", "a", "b"]}, naa)
171171
assert cat1.shape == (3,)
172172
assert np.all(cat1 == [1, 0, 1])
173-
assert_raises(PatsyError, _eval_factor, fi1, {"mock": ["c"]}, naa)
174-
assert_raises(PatsyError, _eval_factor, fi1, {"mock": C(["a", "c"])}, naa)
175-
assert_raises(PatsyError, _eval_factor, fi1,
173+
pytest.raises(PatsyError, _eval_factor, fi1, {"mock": ["c"]}, naa)
174+
pytest.raises(PatsyError, _eval_factor, fi1, {"mock": C(["a", "c"])}, naa)
175+
pytest.raises(PatsyError, _eval_factor, fi1,
176176
{"mock": C(["a", "b"], levels=["b", "a"])}, naa)
177-
assert_raises(PatsyError, _eval_factor, fi1, {"mock": [1, 0, 1]}, naa)
177+
pytest.raises(PatsyError, _eval_factor, fi1, {"mock": [1, 0, 1]}, naa)
178178
bad_cat = np.asarray(["b", "a", "a", "b"])
179179
bad_cat.resize((2, 2))
180-
assert_raises(PatsyError, _eval_factor, fi1, {"mock": bad_cat}, naa)
180+
pytest.raises(PatsyError, _eval_factor, fi1, {"mock": bad_cat}, naa)
181181

182182
cat1_NA, is_NA = _eval_factor(fi1, {"mock": ["a", None, "b"]},
183183
NAAction(NA_types=["None"]))
184184
assert np.array_equal(is_NA, [False, True, False])
185185
assert np.array_equal(cat1_NA, [0, -1, 1])
186-
assert_raises(PatsyError, _eval_factor, fi1,
186+
pytest.raises(PatsyError, _eval_factor, fi1,
187187
{"mock": ["a", None, "b"]}, NAAction(NA_types=[]))
188188

189189
fi2 = FactorInfo(_MockFactor(), "categorical", {},
@@ -276,7 +276,7 @@ def _build_subterm(subterm, factor_infos, factor_values, out):
276276
out[:, i] *= factor_values[factor][:, column_idx]
277277

278278
def test__subterm_column_names_iter_and__build_subterm():
279-
from nose.tools import assert_raises
279+
import pytest
280280
from patsy.contrasts import ContrastMatrix
281281
from patsy.categorical import C
282282
f1 = _MockFactor("f1")
@@ -308,7 +308,7 @@ def test__subterm_column_names_iter_and__build_subterm():
308308
[0, 0.5 * 2 * 2],
309309
[3 * 3 * -12, 0]])
310310
# Check that missing categorical values blow up
311-
assert_raises(PatsyError, _build_subterm, subterm1, factor_infos1,
311+
pytest.raises(PatsyError, _build_subterm, subterm1, factor_infos1,
312312
{f1: atleast_2d_column_default([1, 2, 3]),
313313
f2: np.asarray([0, -1, 1]),
314314
f3: atleast_2d_column_default([7.5, 2, -12])},
@@ -557,7 +557,7 @@ def next(self):
557557
string_3col: ([["a", "b", "c"]], [["b", "c", "a"]]),
558558
object_3col: ([[[object()]]], [[[object()]]]),
559559
}
560-
from nose.tools import assert_raises
560+
import pytest
561561
for illegal_factor in illegal_factor_states:
562562
it = DataIterMaker()
563563
try:

patsy/builtins.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,6 @@ def test_Q():
9292
a = 1
9393
assert Q("a") == 1
9494
assert Q("Q") is Q
95-
from nose.tools import assert_raises
96-
assert_raises(NameError, Q, "asdfsadfdsad")
97-
95+
import pytest
96+
pytest.raises(NameError, Q, "asdfsadfdsad")
97+

patsy/categorical.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def t(NA_types, datas, exp_finish_fast, exp_levels, exp_contrast=None):
231231
else:
232232
assert not exp_finish_fast
233233
assert sniffer.levels_contrast() == (exp_levels, exp_contrast)
234-
234+
235235
if have_pandas_categorical:
236236
# We make sure to test with both boxed and unboxed pandas objects,
237237
# because we used to have a bug where boxed pandas objects would be
@@ -295,14 +295,14 @@ def t(NA_types, datas, exp_finish_fast, exp_levels, exp_contrast=None):
295295
# 0d
296296
t([], ["b"], False, ("b",))
297297

298-
from nose.tools import assert_raises
298+
import pytest
299299

300300
# unhashable level error:
301301
sniffer = CategoricalSniffer(NAAction())
302-
assert_raises(PatsyError, sniffer.sniff, [{}])
302+
pytest.raises(PatsyError, sniffer.sniff, [{}])
303303

304304
# >1d is illegal
305-
assert_raises(PatsyError, sniffer.sniff, np.asarray([["b"]]))
305+
pytest.raises(PatsyError, sniffer.sniff, np.asarray([["b"]]))
306306

307307
# returns either a 1d ndarray or a pandas.Series
308308
def categorical_to_int(data, levels, NA_action, origin=None):
@@ -369,15 +369,15 @@ def categorical_to_int(data, levels, NA_action, origin=None):
369369
return out
370370

371371
def test_categorical_to_int():
372-
from nose.tools import assert_raises
372+
import pytest
373373
from patsy.missing import NAAction
374374
if have_pandas:
375375
s = pandas.Series(["a", "b", "c"], index=[10, 20, 30])
376376
c_pandas = categorical_to_int(s, ("a", "b", "c"), NAAction())
377377
assert np.all(c_pandas == [0, 1, 2])
378378
assert np.all(c_pandas.index == [10, 20, 30])
379379
# Input must be 1-dimensional
380-
assert_raises(PatsyError,
380+
pytest.raises(PatsyError,
381381
categorical_to_int,
382382
pandas.DataFrame({10: s}), ("a", "b", "c"), NAAction())
383383
if have_pandas_categorical:
@@ -397,12 +397,12 @@ def Series_from_codes(codes, categories):
397397
NAAction(NA_types=["None"]))
398398
assert np.all(conv2 == [1, 0, -1])
399399
# But levels must match
400-
assert_raises(PatsyError,
400+
pytest.raises(PatsyError,
401401
categorical_to_int,
402402
con([1, 0], ("a", "b")),
403403
("a", "c"),
404404
NAAction())
405-
assert_raises(PatsyError,
405+
pytest.raises(PatsyError,
406406
categorical_to_int,
407407
con([1, 0], ("a", "b")),
408408
("b", "a"),
@@ -422,14 +422,14 @@ def t(data, levels, expected, NA_action=NAAction()):
422422
t(["a", "b", "a"], ("a", "d", "z", "b"), [0, 3, 0])
423423
t([("a", 1), ("b", 0), ("a", 1)], (("a", 1), ("b", 0)), [0, 1, 0])
424424

425-
assert_raises(PatsyError, categorical_to_int,
425+
pytest.raises(PatsyError, categorical_to_int,
426426
["a", "b", "a"], ("a", "c"), NAAction())
427427

428428
t(C(["a", "b", "a"]), ("a", "b"), [0, 1, 0])
429429
t(C(["a", "b", "a"]), ("b", "a"), [1, 0, 1])
430430
t(C(["a", "b", "a"], levels=["b", "a"]), ("b", "a"), [1, 0, 1])
431431
# Mismatch between C() levels and expected levels
432-
assert_raises(PatsyError, categorical_to_int,
432+
pytest.raises(PatsyError, categorical_to_int,
433433
C(["a", "b", "a"], levels=["a", "b"]),
434434
("b", "a"), NAAction())
435435

@@ -439,14 +439,14 @@ def t(data, levels, expected, NA_action=NAAction()):
439439
t(True, (False, True), [1])
440440

441441
# ndim == 2 is disallowed
442-
assert_raises(PatsyError, categorical_to_int,
442+
pytest.raises(PatsyError, categorical_to_int,
443443
np.asarray([["a", "b"], ["b", "a"]]),
444444
("a", "b"), NAAction())
445445

446446
# levels must be hashable
447-
assert_raises(PatsyError, categorical_to_int,
447+
pytest.raises(PatsyError, categorical_to_int,
448448
["a", "b"], ("a", "b", {}), NAAction())
449-
assert_raises(PatsyError, categorical_to_int,
449+
pytest.raises(PatsyError, categorical_to_int,
450450
["a", "b", {}], ("a", "b"), NAAction())
451451

452452
t(["b", None, np.nan, "a"], ("a", "b"), [1, -1, -1, 0],
@@ -458,7 +458,7 @@ def t(data, levels, expected, NA_action=NAAction()):
458458

459459
# Smoke test for the branch that formats the ellipsized list of levels in
460460
# the error message:
461-
assert_raises(PatsyError, categorical_to_int,
461+
pytest.raises(PatsyError, categorical_to_int,
462462
["a", "b", "q"],
463463
("a", "b", "c", "d", "e", "f", "g", "h"),
464464
NAAction())

patsy/constraint.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ def test_LinearConstraint():
115115
lc = LinearConstraint(["a"], [[0]])
116116
assert_equal(lc.coefs, [[0]])
117117

118-
from nose.tools import assert_raises
119-
assert_raises(ValueError, LinearConstraint, ["a"], [[1, 2]])
120-
assert_raises(ValueError, LinearConstraint, ["a"], [[[1]]])
121-
assert_raises(ValueError, LinearConstraint, ["a"], [[1, 2]], [3, 4])
122-
assert_raises(ValueError, LinearConstraint, ["a", "b"], [[1, 2]], [3, 4])
123-
assert_raises(ValueError, LinearConstraint, ["a"], [[1]], [[]])
124-
assert_raises(ValueError, LinearConstraint, ["a", "b"], [])
125-
assert_raises(ValueError, LinearConstraint, ["a", "b"],
118+
import pytest
119+
pytest.raises(ValueError, LinearConstraint, ["a"], [[1, 2]])
120+
pytest.raises(ValueError, LinearConstraint, ["a"], [[[1]]])
121+
pytest.raises(ValueError, LinearConstraint, ["a"], [[1, 2]], [3, 4])
122+
pytest.raises(ValueError, LinearConstraint, ["a", "b"], [[1, 2]], [3, 4])
123+
pytest.raises(ValueError, LinearConstraint, ["a"], [[1]], [[]])
124+
pytest.raises(ValueError, LinearConstraint, ["a", "b"], [])
125+
pytest.raises(ValueError, LinearConstraint, ["a", "b"],
126126
np.zeros((0, 2)))
127127

128128
assert_no_pickling(lc)
@@ -138,9 +138,9 @@ def test_LinearConstraint_combine():
138138
assert_equal(comb.coefs, [[1, 0], [0, 1]])
139139
assert_equal(comb.constants, [[0], [1]])
140140

141-
from nose.tools import assert_raises
142-
assert_raises(ValueError, LinearConstraint.combine, [])
143-
assert_raises(ValueError, LinearConstraint.combine,
141+
import pytest
142+
pytest.raises(ValueError, LinearConstraint.combine, [])
143+
pytest.raises(ValueError, LinearConstraint.combine,
144144
[LinearConstraint(["a"], [1]), LinearConstraint(["b"], [1])])
145145

146146

@@ -218,8 +218,8 @@ def test__tokenize_constraint():
218218
assert got.origin == Origin(code, expected[1], expected[2])
219219
assert got.extra == expected[3]
220220

221-
from nose.tools import assert_raises
222-
assert_raises(PatsyError, _tokenize_constraint, "1 + @b", ["b"])
221+
import pytest
222+
pytest.raises(PatsyError, _tokenize_constraint, "1 + @b", ["b"])
223223
# Shouldn't raise an error:
224224
_tokenize_constraint("1 + @b", ["@b"])
225225

@@ -436,12 +436,12 @@ def _check_lincon(input, varnames, coefs, constants):
436436

437437

438438
def test_linear_constraint():
439-
from nose.tools import assert_raises
439+
import pytest
440440
from patsy.compat import OrderedDict
441441
t = _check_lincon
442442

443443
t(LinearConstraint(["a", "b"], [2, 3]), ["a", "b"], [[2, 3]], [[0]])
444-
assert_raises(ValueError, linear_constraint,
444+
pytest.raises(ValueError, linear_constraint,
445445
LinearConstraint(["b", "a"], [2, 3]),
446446
["a", "b"])
447447

@@ -457,8 +457,8 @@ def test_linear_constraint():
457457
t(OrderedDict([("a", 2), (1, 3)]),
458458
["a", "b"], [[1, 0], [0, 1]], [[2], [3]])
459459

460-
assert_raises(ValueError, linear_constraint, {"q": 1}, ["a", "b"])
461-
assert_raises(ValueError, linear_constraint, {"a": 1, 0: 2}, ["a", "b"])
460+
pytest.raises(ValueError, linear_constraint, {"q": 1}, ["a", "b"])
461+
pytest.raises(ValueError, linear_constraint, {"a": 1, 0: 2}, ["a", "b"])
462462

463463
t(np.array([2, 3]), ["a", "b"], [[2, 3]], [[0]])
464464
t(np.array([[2, 3], [4, 5]]), ["a", "b"], [[2, 3], [4, 5]], [[0], [0]])
@@ -472,7 +472,7 @@ def test_linear_constraint():
472472

473473
t(["a = 2", "b = 3"], ["a", "b"], [[1, 0], [0, 1]], [[2], [3]])
474474

475-
assert_raises(ValueError, linear_constraint, ["a", {"b": 0}], ["a", "b"])
475+
pytest.raises(ValueError, linear_constraint, ["a", {"b": 0}], ["a", "b"])
476476

477477
# Actual evaluator tests
478478
t("2 * (a + b/3) + b + 2*3/4 = 1 + 2*3", ["a", "b"],
@@ -497,9 +497,9 @@ def test_linear_constraint():
497497
t(([[10, 20], [20, 40]], [[30], [35]]), ["a", "b"],
498498
[[10, 20], [20, 40]], [[30], [35]])
499499
# wrong-length tuple
500-
assert_raises(ValueError, linear_constraint,
500+
pytest.raises(ValueError, linear_constraint,
501501
([1, 0], [0], [0]), ["a", "b"])
502-
assert_raises(ValueError, linear_constraint, ([1, 0],), ["a", "b"])
502+
pytest.raises(ValueError, linear_constraint, ([1, 0],), ["a", "b"])
503503

504504
t([10, 20], ["a", "b"], [[10, 20]], [[0]])
505505
t([[10, 20], [20, 40]], ["a", "b"], [[10, 20], [20, 40]], [[0], [0]])
@@ -508,7 +508,7 @@ def test_linear_constraint():
508508
[[10, 20], [20, 40]], [[0], [0]])
509509

510510
# unknown object type
511-
assert_raises(ValueError, linear_constraint, None, ["a", "b"])
511+
pytest.raises(ValueError, linear_constraint, None, ["a", "b"])
512512

513513

514514
_parse_eval_error_tests = [

patsy/contrasts.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def test_ContrastMatrix():
5656
# smoke test
5757
repr(cm)
5858

59-
from nose.tools import assert_raises
60-
assert_raises(PatsyError, ContrastMatrix, [[1], [0]], ["a", "b"])
59+
import pytest
60+
pytest.raises(PatsyError, ContrastMatrix, [[1], [0]], ["a", "b"])
6161

6262
assert_no_pickling(cm)
6363

@@ -128,10 +128,10 @@ def test__get_level():
128128
assert _get_level(["a", "b", "c"], "b") == 1
129129
# For integer levels, we check identity before treating it as an index
130130
assert _get_level([2, 1, 0], 0) == 2
131-
from nose.tools import assert_raises
132-
assert_raises(PatsyError, _get_level, ["a", "b"], 2)
133-
assert_raises(PatsyError, _get_level, ["a", "b"], -3)
134-
assert_raises(PatsyError, _get_level, ["a", "b"], "c")
131+
import pytest
132+
pytest.raises(PatsyError, _get_level, ["a", "b"], 2)
133+
pytest.raises(PatsyError, _get_level, ["a", "b"], -3)
134+
pytest.raises(PatsyError, _get_level, ["a", "b"], "c")
135135

136136
if not six.PY3:
137137
assert _get_level(["a", "b", "c"], long(0)) == 0
@@ -326,8 +326,8 @@ def test_Poly():
326326
[1, 0.293294230042706, -0.762000762001143],
327327
[1, 0.513264902574736, 0.635000635000952]])
328328

329-
from nose.tools import assert_raises
330-
assert_raises(PatsyError,
329+
import pytest
330+
pytest.raises(PatsyError,
331331
Poly(scores=[0, 1]).code_with_intercept,
332332
["a", "b", "c"])
333333

0 commit comments

Comments
 (0)
X Tutup