-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
40 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
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,13 @@ | ||
from contextlib import contextmanager | ||
|
||
from cubed.core import array | ||
|
||
|
||
@contextmanager | ||
def raise_if_computes(): | ||
"""Returns a context manager for testing that ``compute`` is not called.""" | ||
array.compute_should_raise = True | ||
try: | ||
yield | ||
finally: | ||
array.compute_should_raise = False |
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,21 @@ | ||
import numpy as np | ||
import pytest | ||
from numpy.testing import assert_array_equal | ||
|
||
import cubed.array_api as xp | ||
from cubed.testing import raise_if_computes | ||
|
||
|
||
def test_raise_if_computes(): | ||
# shouldn't raise since compute has not been called | ||
with raise_if_computes(): | ||
a = xp.ones((3, 3), chunks=(2, 2)) | ||
b = xp.negative(a) | ||
|
||
# should raise since compute is called | ||
with pytest.raises(RuntimeError): | ||
with raise_if_computes(): | ||
b.compute() | ||
|
||
# shouldn't raise since we are outside the context manager | ||
assert_array_equal(b.compute(), -np.ones((3, 3))) |