Skip to content

Commit

Permalink
Build fake for numpy array that raises ValueError
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescooke committed Jun 24, 2023
1 parent 25d1cb8 commit d317a20
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/fakes.py
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
52 changes: 52 additions & 0 deletions tests/test_fake_numpy_array.py
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

0 comments on commit d317a20

Please sign in to comment.