File: test_datamodel.py

package info (click to toggle)
python-testbook 0.4.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 408 kB
  • sloc: python: 1,045; makefile: 11
file content (83 lines) | stat: -rw-r--r-- 1,753 bytes parent folder | download | duplicates (2)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
import pytest

from testbook.testbook import testbook


@pytest.fixture(scope='module')
def notebook():
    with testbook('tests/resources/datamodel.ipynb', execute=True) as tb:
        yield tb


def test_len(notebook):
    mylist = notebook.ref("mylist")

    assert len(mylist) == 5


def test_iter(notebook):
    mylist = notebook.ref("mylist")

    expected = []
    for x in mylist:
        expected.append(x)

    assert mylist == expected


def test_getitem(notebook):
    mylist = notebook.ref("mylist")
    mylist.append(6)

    assert mylist[-1] == 6
    assert mylist.__getitem__(-1) == 6


def test_getitem_raisesIndexError(notebook):
    mylist = notebook.ref("mylist")

    with pytest.raises(IndexError):
        mylist[100]


def test_getitem_raisesTypeError(notebook):
    mylist = notebook.ref("mylist")

    with pytest.raises(TypeError):
        mylist['hello']


def test_setitem(notebook):
    notebook.inject("mydict = {'key1': 'value1', 'key2': 'value1'}")
    mydict = notebook.ref("mydict")

    mydict['key3'] = 'value3'
    assert mydict['key3'] == 'value3'

    mylist = notebook.ref("mylist")
    mylist[2] = 10
    assert mylist[2] == 10


def test_setitem_raisesIndexError(notebook):
    mylist = notebook.ref("mylist")

    with pytest.raises(IndexError):
        mylist.__setitem__(10, 100)


def test_setitem_raisesTypeError(notebook):
    mylist = notebook.ref("mylist")

    with pytest.raises(TypeError):
        mylist.__setitem__('key', 10)


def test_contains(notebook):
    notebook.inject("mydict = {'key1': 'value1', 'key2': 'value1'}")
    mydict = notebook.ref("mydict")

    assert 'key1' in mydict
    assert 'key2' in mydict
    assert mydict.__contains__('key1')
    assert mydict.__contains__('key2')