-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build fake for numpy array that raises ValueError
- Loading branch information
1 parent
25d1cb8
commit d317a20
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class FakeNumpyArray(): | ||
|
||
def __init__(self, *args, **kwargs): | ||
pass | ||
|
||
def __add__(self, other): | ||
return self | ||
|
||
def __lt__(self, other): | ||
raise ValueError( | ||
'Fake for: The truth value of any array with more than one element is ambiguous.' | ||
) | ||
|
||
def __abs__(self): | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import pytest | ||
|
||
from fakes import FakeNumpyArray | ||
|
||
|
||
def test(): | ||
""" | ||
FakeNumpyArray can be used to trigger a ValueError | ||
""" | ||
left = FakeNumpyArray([1, 2, 3]) | ||
right = 1 | ||
|
||
with pytest.raises(ValueError): | ||
abs(left + right) < 19999 | ||
|
||
|
||
def test_init(): | ||
""" | ||
FakeNumpyArray can init with a list. | ||
""" | ||
result = FakeNumpyArray([1, 2, 3]) | ||
|
||
assert isinstance(result, FakeNumpyArray) | ||
|
||
|
||
def test_add(): | ||
""" | ||
FakeNumpyArray returns instance of itself when `+ 1` is applied to it. | ||
""" | ||
fake = FakeNumpyArray() | ||
|
||
result = fake + 1 | ||
|
||
assert isinstance(result, FakeNumpyArray) | ||
|
||
|
||
def test_abs(): | ||
fake = FakeNumpyArray() | ||
|
||
result = abs(fake) | ||
|
||
assert isinstance(result, FakeNumpyArray) | ||
|
||
|
||
def test_bool(): | ||
""" | ||
Attempting to use a FakeNumpyArray in a boolean expression raises. | ||
""" | ||
fake = FakeNumpyArray() | ||
|
||
with pytest.raises(ValueError): | ||
fake < 19999 |