Skip to content

Commit d317a20

Browse files
committed
Build fake for numpy array that raises ValueError
1 parent 25d1cb8 commit d317a20

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

tests/fakes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class FakeNumpyArray():
2+
3+
def __init__(self, *args, **kwargs):
4+
pass
5+
6+
def __add__(self, other):
7+
return self
8+
9+
def __lt__(self, other):
10+
raise ValueError(
11+
'Fake for: The truth value of any array with more than one element is ambiguous.'
12+
)
13+
14+
def __abs__(self):
15+
return self

tests/test_fake_numpy_array.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pytest
2+
3+
from fakes import FakeNumpyArray
4+
5+
6+
def test():
7+
"""
8+
FakeNumpyArray can be used to trigger a ValueError
9+
"""
10+
left = FakeNumpyArray([1, 2, 3])
11+
right = 1
12+
13+
with pytest.raises(ValueError):
14+
abs(left + right) < 19999
15+
16+
17+
def test_init():
18+
"""
19+
FakeNumpyArray can init with a list.
20+
"""
21+
result = FakeNumpyArray([1, 2, 3])
22+
23+
assert isinstance(result, FakeNumpyArray)
24+
25+
26+
def test_add():
27+
"""
28+
FakeNumpyArray returns instance of itself when `+ 1` is applied to it.
29+
"""
30+
fake = FakeNumpyArray()
31+
32+
result = fake + 1
33+
34+
assert isinstance(result, FakeNumpyArray)
35+
36+
37+
def test_abs():
38+
fake = FakeNumpyArray()
39+
40+
result = abs(fake)
41+
42+
assert isinstance(result, FakeNumpyArray)
43+
44+
45+
def test_bool():
46+
"""
47+
Attempting to use a FakeNumpyArray in a boolean expression raises.
48+
"""
49+
fake = FakeNumpyArray()
50+
51+
with pytest.raises(ValueError):
52+
fake < 19999

0 commit comments

Comments
 (0)