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
|
import pytest
from testbook.testbook import testbook
from testbook.exceptions import TestbookAttributeError, TestbookSerializeError
@pytest.fixture(scope='module')
def notebook():
with testbook('tests/resources/reference.ipynb', execute=True) as tb:
yield tb
def test_create_reference(notebook):
a = notebook.ref("a")
assert repr(a) == "[1, 2, 3]"
def test_create_reference_getitem(notebook):
a = notebook["a"]
assert repr(a) == "[1, 2, 3]"
def test_create_reference_get(notebook):
a = notebook.get("a")
assert repr(a) == "[1, 2, 3]"
def test_eq_in_notebook(notebook):
a = notebook.ref("a")
a.append(4)
assert a == [1, 2, 3, 4]
def test_eq_in_notebook_ref(notebook):
a, b = notebook.ref("a"), notebook.ref("b")
assert a == b
def test_function_call(notebook):
double = notebook.ref("double")
assert double([1, 2, 3]) == [2, 4, 6]
def test_function_call_with_ref_object(notebook):
double, a = notebook.ref("double"), notebook.ref("a")
assert double(a) == [2, 4, 6]
def test_reference(notebook):
Foo = notebook.ref("Foo")
# Check that when a non-serializeable object is returned, it returns
# a reference to that object instead
f = Foo('bar')
assert repr(f) == "\"<Foo value='bar'>\""
# Valid attribute access
assert f.say_hello()
# Invalid attribute access
with pytest.raises(TestbookAttributeError):
f.does_not_exist
assert f.say_hello() == 'Hello bar!'
# non JSON-serializeable output
with pytest.raises(TestbookSerializeError):
f.resolve()
|