|
1
|
|
|
""" |
|
2
|
|
|
:mod:`tests.unit.test_util` -- Unit Tests |
|
3
|
|
|
========================================= |
|
4
|
|
|
|
|
5
|
|
|
.. module:: tests.unit.test_util |
|
6
|
|
|
:synopsis: Unit tests for the lib.util package. |
|
7
|
|
|
|
|
8
|
|
|
.. moduleauthor:: Bill Maroney <[email protected]> |
|
9
|
|
|
""" |
|
10
|
|
|
|
|
11
|
|
|
import pytest |
|
12
|
|
|
|
|
13
|
|
|
from lib.util import wrap, load_dataset |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
def test_wrap_correctness(): |
|
17
|
|
|
""" Test the correctness of :func:`lib.util.wrap` by examining outputs for expected properties """ |
|
|
|
|
|
|
18
|
|
|
long_line = "I'm a long line, but how long, really, is a line?" |
|
19
|
|
|
long_para = 400 * long_line |
|
20
|
|
|
m, n = 4, 120 |
|
|
|
|
|
|
21
|
|
|
wrapped_para = wrap(long_para, m, n) |
|
22
|
|
|
assert isinstance(wrapped_para, str), "wrong type" |
|
23
|
|
|
for line in wrapped_para.split("\n"): |
|
24
|
|
|
assert isinstance(line, str), "wrong type" |
|
25
|
|
|
assert line.startswith(m * " "), "incorrect white-space padding at front of line" |
|
26
|
|
|
assert len(line) <= n, "line is too long" |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def test_wrap_bad_para_type_float(): |
|
30
|
|
|
""" Test that :func:`lib.util.wrap` raises a ``TypeError`` for a non-``int`` value for `para` (``float``) """ |
|
|
|
|
|
|
31
|
|
|
with pytest.raises(TypeError): |
|
32
|
|
|
wrap(3.14, 4, 80) |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
def test_wrap_bad_m_type_str(): |
|
36
|
|
|
""" Test that :func:`lib.util.wrap` raises a ``TypeError`` for a non-``int`` value for `m` (``str``) """ |
|
|
|
|
|
|
37
|
|
|
with pytest.raises(TypeError): |
|
38
|
|
|
wrap("paragraph I be...", "bad", 80) |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
def test_wrap_bad_m_type_float(): |
|
42
|
|
|
""" Test that :func:`lib.util.wrap` raises a ``TypeError`` for a non-``int`` value for `m` (``float``) """ |
|
|
|
|
|
|
43
|
|
|
with pytest.raises(TypeError): |
|
44
|
|
|
wrap("paragraph I be...", 3.14, 80) |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
def test_wrap_bad_m_negative(): |
|
48
|
|
|
""" Test that :func:`lib.util.wrap` raises a ``ValueError`` for a :math:`m \\lt 0` """ |
|
49
|
|
|
with pytest.raises(ValueError): |
|
50
|
|
|
wrap("paragraph I be...", -14, 80) |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def test_wrap_bad_n_type_str(): |
|
54
|
|
|
""" Test that :func:`lib.util.wrap` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) """ |
|
|
|
|
|
|
55
|
|
|
with pytest.raises(TypeError): |
|
56
|
|
|
wrap("paragraph I be...", 4, "bad") |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def test_wrap_bad_n_type_float(): |
|
60
|
|
|
""" Test that :func:`lib.util.wrap` raises a ``TypeError`` for a non-``int`` value for `n` (``float``) """ |
|
|
|
|
|
|
61
|
|
|
with pytest.raises(TypeError): |
|
62
|
|
|
wrap("paragraph I be...", 4, 3.14) |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
def test_wrap_bad_n_zero(): |
|
66
|
|
|
""" Test that :func:`lib.util.wrap` raises a ``ValueError`` for :math:`n = 0` """ |
|
67
|
|
|
with pytest.raises(ValueError): |
|
68
|
|
|
wrap("paragraph I be...", 4, 0) |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
def test_load_dataset_correctness(): |
|
72
|
|
|
""" Test the correctness of :func:`lib.util.load_dataset` by examining datatypes of outputs """ |
|
73
|
|
|
for elt in load_dataset("general", "primes", data_type=int): |
|
74
|
|
|
assert isinstance(elt, int), "wrong type" |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
def test_load_dataset_bad_root_type_float(): |
|
|
|
|
|
|
78
|
|
|
""" Test that :func:`lib.util.load_dataset` raises a ``TypeError`` for a non-``str`` value for `root` (``float``) |
|
|
|
|
|
|
79
|
|
|
""" |
|
80
|
|
|
with pytest.raises(TypeError): |
|
81
|
|
|
load_dataset(3.14, "primes") |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
def test_load_dataset_bad_root_missing(): |
|
|
|
|
|
|
85
|
|
|
""" Test that :func:`lib.util.load_dataset` raises a ``FileNotFoundError`` for a non-existent directory `root` """ |
|
|
|
|
|
|
86
|
|
|
with pytest.raises(FileNotFoundError): |
|
87
|
|
|
load_dataset("i_dont_exist", "primes") |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
def test_load_dataset_bad_dataset_type_float(): |
|
|
|
|
|
|
91
|
|
|
""" Test that :func:`lib.util.load_dataset` raises a ``TypeError`` for a non-``str`` value for `dataset` (``float``) |
|
|
|
|
|
|
92
|
|
|
""" |
|
93
|
|
|
with pytest.raises(TypeError): |
|
94
|
|
|
load_dataset("general", 3.14) |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
def test_load_dataset_bad_dataset_missing(): |
|
|
|
|
|
|
98
|
|
|
""" Test that :func:`lib.util.load_dataset` raises a ``FileNotFoundError`` for a non-existent file `dataset` """ |
|
|
|
|
|
|
99
|
|
|
with pytest.raises(FileNotFoundError): |
|
100
|
|
|
load_dataset("general", "3.14") |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
def test_load_dataset_bad_separator_type_float(): |
|
|
|
|
|
|
104
|
|
|
""" Test that :func:`lib.util.load_dataset` raises a ``TypeError`` for a non-``str`` value for `separator` |
|
|
|
|
|
|
105
|
|
|
(``float``) |
|
106
|
|
|
""" |
|
107
|
|
|
with pytest.raises(TypeError): |
|
108
|
|
|
load_dataset("general", "primes", separator=12.34) |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
def test_load_dataset_bad_data_type(): |
|
112
|
|
|
""" Test that :func:`lib.util.load_dataset` raises a ``TypeError`` for a non-``type`` value for `data_type` |
|
|
|
|
|
|
113
|
|
|
(``int``) |
|
114
|
|
|
""" |
|
115
|
|
|
with pytest.raises(TypeError): |
|
116
|
|
|
load_dataset("general", "primes", data_type=7) |
|
117
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.