File: test_nbbase.py

package info (click to toggle)
nbformat 5.10.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,072 kB
  • sloc: python: 4,746; makefile: 167; javascript: 2
file content (173 lines) | stat: -rw-r--r-- 6,041 bytes parent folder | download
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from __future__ import annotations

from unittest import TestCase

import pytest

from nbformat.v3.nbbase import (
    NotebookNode,
    nbformat,
    new_author,
    new_code_cell,
    new_heading_cell,
    new_metadata,
    new_notebook,
    new_output,
    new_text_cell,
    new_worksheet,
)


class TestCell(TestCase):
    def test_empty_code_cell(self):
        cc = new_code_cell()
        self.assertEqual(cc.cell_type, "code")
        self.assertEqual("input" not in cc, True)
        self.assertEqual("prompt_number" not in cc, True)
        self.assertEqual(cc.outputs, [])
        self.assertEqual(cc.collapsed, False)

    def test_code_cell(self):
        cc = new_code_cell(input="a=10", prompt_number=0, collapsed=True)
        cc.outputs = [
            new_output(output_type="pyout", output_svg="foo", output_text="10", prompt_number=0)
        ]
        self.assertEqual(cc.input, "a=10")
        self.assertEqual(cc.prompt_number, 0)
        self.assertEqual(cc.language, "python")
        self.assertEqual(cc.outputs[0].svg, "foo")
        self.assertEqual(cc.outputs[0].text, "10")
        self.assertEqual(cc.outputs[0].prompt_number, 0)
        self.assertEqual(cc.collapsed, True)

    def test_pyerr(self):
        o = new_output(
            output_type="pyerr",
            ename="NameError",
            evalue="Name not found",
            traceback=["frame 0", "frame 1", "frame 2"],
        )
        self.assertEqual(o.output_type, "pyerr")
        self.assertEqual(o.ename, "NameError")
        self.assertEqual(o.evalue, "Name not found")
        self.assertEqual(o.traceback, ["frame 0", "frame 1", "frame 2"])

    def test_empty_html_cell(self):
        tc = new_text_cell("html")
        self.assertEqual(tc.cell_type, "html")
        self.assertEqual("source" not in tc, True)

    def test_html_cell(self):
        tc = new_text_cell("html", "hi")
        self.assertEqual(tc.source, "hi")

    def test_empty_markdown_cell(self):
        tc = new_text_cell("markdown")
        self.assertEqual(tc.cell_type, "markdown")
        self.assertEqual("source" not in tc, True)

    def test_markdown_cell(self):
        tc = new_text_cell("markdown", "hi")
        self.assertEqual(tc.source, "hi")

    def test_empty_raw_cell(self):
        tc = new_text_cell("raw")
        self.assertEqual(tc.cell_type, "raw")
        self.assertEqual("source" not in tc, True)

    def test_raw_cell(self):
        tc = new_text_cell("raw", "hi")
        self.assertEqual(tc.source, "hi")

    def test_empty_heading_cell(self):
        tc = new_heading_cell()
        self.assertEqual(tc.cell_type, "heading")
        self.assertEqual("source" not in tc, True)

    def test_heading_cell(self):
        tc = new_heading_cell("hi", level=2)
        self.assertEqual(tc.source, "hi")
        self.assertEqual(tc.level, 2)


class TestWorksheet(TestCase):
    def test_empty_worksheet(self):
        ws = new_worksheet()
        self.assertEqual(ws.cells, [])
        self.assertEqual("name" not in ws, True)

    def test_worksheet(self):
        cells = [new_code_cell(), new_text_cell("html")]
        ws = new_worksheet(cells=cells)
        self.assertEqual(ws.cells, cells)


class TestNotebook(TestCase):
    def test_empty_notebook(self):
        nb = new_notebook()
        self.assertEqual(nb.worksheets, [])
        self.assertEqual(nb.metadata, NotebookNode())
        self.assertEqual(nb.nbformat, nbformat)

    def test_notebook(self):
        worksheets = [new_worksheet(), new_worksheet()]
        metadata = new_metadata(name="foo")
        nb = new_notebook(metadata=metadata, worksheets=worksheets)
        self.assertEqual(nb.metadata.name, "foo")
        self.assertEqual(nb.worksheets, worksheets)
        self.assertEqual(nb.nbformat, nbformat)

    def test_notebook_name(self):
        worksheets = [new_worksheet(), new_worksheet()]
        nb = new_notebook(name="foo", worksheets=worksheets)
        self.assertEqual(nb.metadata.name, "foo")
        self.assertEqual(nb.worksheets, worksheets)
        self.assertEqual(nb.nbformat, nbformat)


class TestMetadata(TestCase):
    def test_empty_metadata(self):
        md = new_metadata()
        self.assertEqual("name" not in md, True)
        self.assertEqual("authors" not in md, True)
        self.assertEqual("license" not in md, True)
        self.assertEqual("saved" not in md, True)
        self.assertEqual("modified" not in md, True)
        self.assertEqual("gistid" not in md, True)

    def test_metadata(self):
        authors = [new_author(name="Bart Simpson", email="bsimpson@fox.com")]
        md = new_metadata(
            name="foo",
            license="BSD",
            created="today",
            modified="now",
            gistid="21341231",
            authors=authors,
        )
        self.assertEqual(md.name, "foo")
        self.assertEqual(md.license, "BSD")
        self.assertEqual(md.created, "today")
        self.assertEqual(md.modified, "now")
        self.assertEqual(md.gistid, "21341231")
        self.assertEqual(md.authors, authors)


class TestOutputs(TestCase):
    def test_binary_png(self):
        with pytest.warns(UserWarning, match="bytes instead of likely base64"):
            out = new_output(output_png=b"\x89PNG\r\n\x1a\n", output_type="display_data")

    def test_b64b6tes_png(self):
        # really those tests are wrong, this is not b64, if prefixed by b
        with pytest.warns(UserWarning, match="bytes instead of likely base64"):
            out = new_output(output_png=b"iVBORw0KG", output_type="display_data")

    def test_binary_jpeg(self):
        with pytest.warns(UserWarning, match="bytes instead of likely base64"):
            out = new_output(output_jpeg=b"\xff\xd8", output_type="display_data")

    def test_b64b6tes_jpeg(self):
        # really those tests are wrong, this is not b64, if prefixed by b
        with pytest.warns(UserWarning, match="bytes instead of likely base64"):
            out = new_output(output_jpeg=b"/9", output_type="display_data")