-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_errors.py
More file actions
115 lines (107 loc) · 5.24 KB
/
Copy pathtest_errors.py
File metadata and controls
115 lines (107 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import pytest
import pandas as pd
from plot_misc.errors import (
is_type, is_df, are_columns_in_df,
is_series_type, number_to_list, same_len, string_to_list,
InputValidationError,
_get_param_name,
)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TestIsType(object):
# -------------------------------------------------------------------------
def test_is_type_passes_on_correct_type(self):
assert is_type(5, int) is True
assert is_type("test", (str, list)) is True
# -------------------------------------------------------------------------
def test_is_type_raises_on_wrong_type(self):
with pytest.raises(InputValidationError) as excinfo:
is_type(5, str)
assert "Expected any of" in str(excinfo.value)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TestIsDF(object):
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_is_df_valid(self):
df = pd.DataFrame({'a': [1, 2]})
assert is_df(df) is True
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_is_df_invalid(self):
with pytest.raises(InputValidationError):
is_df([1, 2, 3])
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TestAreColumnsInDF(object):
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_are_columns_in_df_valid(self):
df = pd.DataFrame({'a': [1], 'b': [2]})
assert are_columns_in_df(df, ['a']) is True
assert are_columns_in_df(df, 'a') is True
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_are_columns_in_df_none(self):
df = pd.DataFrame({'a': [1], 'b': [2]})
assert are_columns_in_df(df, ['a', None]) is True
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_are_columns_in_df_missing_with_error(self):
df = pd.DataFrame({'a': [1]})
with pytest.raises(InputValidationError):
are_columns_in_df(df, ['b'])
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_are_columns_in_df_missing_with_warning(self):
df = pd.DataFrame({'a': [1]})
with pytest.warns(UserWarning):
result = are_columns_in_df(df, ['b'], warning=True)
assert result is False
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TestIsSeriesType(object):
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_is_series_type_valid_series(self):
s = pd.Series(['x', 'y'])
assert is_series_type(s, str)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_is_series_type_valid_df(self):
df = pd.DataFrame({'x': [1, 2], 'y': [3, 4]})
assert is_series_type(df, int)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_is_series_type_invalid_element_type(self):
s = pd.Series(['x', 1])
with pytest.raises(InputValidationError):
is_series_type(s, str)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TestSameLen(object):
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_same_len_valid(self):
assert same_len([1, 2], (3, 4)) is True
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_same_len_invalid_lengths(self):
with pytest.raises(ValueError):
same_len([1, 2], [1])
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TestStringToList(object):
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_string_to_list_wraps_string(self):
assert string_to_list("abc") == ['abc']
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_string_to_list_returns_object(self):
assert string_to_list([1, 2]) == [1, 2]
assert string_to_list(3.14) == 3.14
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TestNumberToList(object):
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_default(self):
assert number_to_list(123) == [123]
assert number_to_list(123.01) == [123.01]
assert number_to_list('123.01') == '123.01'
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TestGetParamName(object):
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_get_param_name_recovers_name(self):
test_value = 42
assert _get_param_name(test_value) is None
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_get_param_name_correct_frame(self):
"""
We need to have an outer function that class a subsequent function
which can be used to infer the passed object name.
"""
test_value = 42
def outer(x):
return _get_param_name(x)
assert outer(test_value) == 'test_value'