forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_assert_numpy_array_equal.py
177 lines (122 loc) · 5.48 KB
/
test_assert_numpy_array_equal.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# -*- coding: utf-8 -*-
import numpy as np
import pytest
from pandas import Timestamp
from pandas.util.testing import assert_numpy_array_equal
def test_assert_numpy_array_equal_shape_mismatch():
msg = """numpy array are different
numpy array shapes are different
\\[left\\]: \\(2,\\)
\\[right\\]: \\(3,\\)"""
with pytest.raises(AssertionError, match=msg):
assert_numpy_array_equal(np.array([1, 2]), np.array([3, 4, 5]))
def test_assert_numpy_array_equal_bad_type():
expected = "Expected type"
with pytest.raises(AssertionError, match=expected):
assert_numpy_array_equal(1, 2)
@pytest.mark.parametrize("a,b,klass1,klass2", [
(np.array([1]), 1, "ndarray", "int"),
(1, np.array([1]), "int", "ndarray"),
])
def test_assert_numpy_array_equal_class_mismatch(a, b, klass1, klass2):
msg = """numpy array are different
numpy array classes are different
\\[left\\]: {klass1}
\\[right\\]: {klass2}""".format(klass1=klass1, klass2=klass2)
with pytest.raises(AssertionError, match=msg):
assert_numpy_array_equal(a, b)
def test_assert_numpy_array_equal_value_mismatch1():
msg = """numpy array are different
numpy array values are different \\(66\\.66667 %\\)
\\[left\\]: \\[nan, 2\\.0, 3\\.0\\]
\\[right\\]: \\[1\\.0, nan, 3\\.0\\]"""
with pytest.raises(AssertionError, match=msg):
assert_numpy_array_equal(np.array([np.nan, 2, 3]),
np.array([1, np.nan, 3]))
def test_assert_numpy_array_equal_value_mismatch2():
msg = """numpy array are different
numpy array values are different \\(50\\.0 %\\)
\\[left\\]: \\[1, 2\\]
\\[right\\]: \\[1, 3\\]"""
with pytest.raises(AssertionError, match=msg):
assert_numpy_array_equal(np.array([1, 2]), np.array([1, 3]))
def test_assert_numpy_array_equal_value_mismatch3():
msg = """numpy array are different
numpy array values are different \\(16\\.66667 %\\)
\\[left\\]: \\[\\[1, 2\\], \\[3, 4\\], \\[5, 6\\]\\]
\\[right\\]: \\[\\[1, 3\\], \\[3, 4\\], \\[5, 6\\]\\]"""
with pytest.raises(AssertionError, match=msg):
assert_numpy_array_equal(np.array([[1, 2], [3, 4], [5, 6]]),
np.array([[1, 3], [3, 4], [5, 6]]))
def test_assert_numpy_array_equal_value_mismatch4():
msg = """numpy array are different
numpy array values are different \\(50\\.0 %\\)
\\[left\\]: \\[1\\.1, 2\\.000001\\]
\\[right\\]: \\[1\\.1, 2.0\\]"""
with pytest.raises(AssertionError, match=msg):
assert_numpy_array_equal(np.array([1.1, 2.000001]),
np.array([1.1, 2.0]))
def test_assert_numpy_array_equal_value_mismatch5():
msg = """numpy array are different
numpy array values are different \\(16\\.66667 %\\)
\\[left\\]: \\[\\[1, 2\\], \\[3, 4\\], \\[5, 6\\]\\]
\\[right\\]: \\[\\[1, 3\\], \\[3, 4\\], \\[5, 6\\]\\]"""
with pytest.raises(AssertionError, match=msg):
assert_numpy_array_equal(np.array([[1, 2], [3, 4], [5, 6]]),
np.array([[1, 3], [3, 4], [5, 6]]))
def test_assert_numpy_array_equal_value_mismatch6():
msg = """numpy array are different
numpy array values are different \\(25\\.0 %\\)
\\[left\\]: \\[\\[1, 2\\], \\[3, 4\\]\\]
\\[right\\]: \\[\\[1, 3\\], \\[3, 4\\]\\]"""
with pytest.raises(AssertionError, match=msg):
assert_numpy_array_equal(np.array([[1, 2], [3, 4]]),
np.array([[1, 3], [3, 4]]))
def test_assert_almost_equal_shape_mismatch_override():
msg = """Index are different
Index shapes are different
\\[left\\]: \\(2,\\)
\\[right\\]: \\(3,\\)"""
with pytest.raises(AssertionError, match=msg):
assert_numpy_array_equal(np.array([1, 2]),
np.array([3, 4, 5]),
obj="Index")
def test_numpy_array_equal_unicode():
# see gh-20503
#
# Test ensures that `assert_numpy_array_equals` raises the right
# exception when comparing np.arrays containing differing unicode objects.
msg = """numpy array are different
numpy array values are different \\(33\\.33333 %\\)
\\[left\\]: \\[á, à, ä\\]
\\[right\\]: \\[á, à, å\\]"""
with pytest.raises(AssertionError, match=msg):
assert_numpy_array_equal(np.array([u"á", u"à", u"ä"]),
np.array([u"á", u"à", u"å"]))
def test_numpy_array_equal_object():
a = np.array([Timestamp("2011-01-01"), Timestamp("2011-01-01")])
b = np.array([Timestamp("2011-01-01"), Timestamp("2011-01-02")])
msg = """numpy array are different
numpy array values are different \\(50\\.0 %\\)
\\[left\\]: \\[2011-01-01 00:00:00, 2011-01-01 00:00:00\\]
\\[right\\]: \\[2011-01-01 00:00:00, 2011-01-02 00:00:00\\]"""
with pytest.raises(AssertionError, match=msg):
assert_numpy_array_equal(a, b)
@pytest.mark.parametrize("other_type", ["same", "copy"])
@pytest.mark.parametrize("check_same", ["same", "copy"])
def test_numpy_array_equal_copy_flag(other_type, check_same):
a = np.array([1, 2, 3])
msg = None
if other_type == "same":
other = a.view()
else:
other = a.copy()
if check_same != other_type:
msg = (r"array\(\[1, 2, 3\]\) is not array\(\[1, 2, 3\]\)"
if check_same == "same"
else r"array\(\[1, 2, 3\]\) is array\(\[1, 2, 3\]\)")
if msg is not None:
with pytest.raises(AssertionError, match=msg):
assert_numpy_array_equal(a, other, check_same=check_same)
else:
assert_numpy_array_equal(a, other, check_same=check_same)