test_nbbase.py
101 lines
| 3.2 KiB
| text/x-python
|
PythonLexer
MinRK
|
r18573 | # coding: utf-8 | ||
"""Tests for the Python API for composing notebook elements""" | ||||
MinRK
|
r18568 | |||
MinRK
|
r18573 | import nose.tools as nt | ||
MinRK
|
r18575 | from IPython.nbformat.validator import isvalid, validate, ValidationError | ||
from ..nbbase import ( | ||||
MinRK
|
r18573 | NotebookNode, nbformat, | ||
MinRK
|
r18596 | new_code_cell, new_markdown_cell, new_notebook, | ||
MinRK
|
r18573 | new_output, new_raw_cell, | ||
MinRK
|
r18568 | ) | ||
MinRK
|
r18573 | def test_empty_notebook(): | ||
nb = new_notebook() | ||||
nt.assert_equal(nb.cells, []) | ||||
nt.assert_equal(nb.metadata, NotebookNode()) | ||||
nt.assert_equal(nb.nbformat, nbformat) | ||||
def test_empty_markdown_cell(): | ||||
cell = new_markdown_cell() | ||||
nt.assert_equal(cell.cell_type, 'markdown') | ||||
nt.assert_equal(cell.source, '') | ||||
def test_markdown_cell(): | ||||
cell = new_markdown_cell(u'* Søme markdown') | ||||
nt.assert_equal(cell.source, u'* Søme markdown') | ||||
def test_empty_raw_cell(): | ||||
cell = new_raw_cell() | ||||
nt.assert_equal(cell.cell_type, u'raw') | ||||
nt.assert_equal(cell.source, '') | ||||
def test_raw_cell(): | ||||
cell = new_raw_cell('hi') | ||||
nt.assert_equal(cell.source, u'hi') | ||||
def test_empty_code_cell(): | ||||
cell = new_code_cell('hi') | ||||
nt.assert_equal(cell.cell_type, 'code') | ||||
nt.assert_equal(cell.source, u'hi') | ||||
def test_empty_display_data(): | ||||
output = new_output('display_data') | ||||
nt.assert_equal(output.output_type, 'display_data') | ||||
def test_empty_stream(): | ||||
MinRK
|
r18576 | output = new_output('stream') | ||
MinRK
|
r18573 | nt.assert_equal(output.output_type, 'stream') | ||
MinRK
|
r18576 | nt.assert_equal(output.name, 'stdout') | ||
nt.assert_equal(output.text, '') | ||||
MinRK
|
r18573 | |||
def test_empty_execute_result(): | ||||
MinRK
|
r18587 | output = new_output('execute_result', execution_count=1) | ||
MinRK
|
r18573 | nt.assert_equal(output.output_type, 'execute_result') | ||
mimebundle = { | ||||
'text/plain': "some text", | ||||
"application/json": { | ||||
"key": "value" | ||||
}, | ||||
"image/svg+xml": 'ABCDEF', | ||||
"application/octet-stream": 'ABC-123', | ||||
"application/vnd.foo+bar": "Some other stuff", | ||||
} | ||||
def test_display_data(): | ||||
output = new_output('display_data', mimebundle) | ||||
for key, expected in mimebundle.items(): | ||||
MinRK
|
r18589 | nt.assert_equal(output.data[key], expected) | ||
MinRK
|
r18573 | |||
def test_execute_result(): | ||||
MinRK
|
r18587 | output = new_output('execute_result', mimebundle, execution_count=10) | ||
nt.assert_equal(output.execution_count, 10) | ||||
MinRK
|
r18573 | for key, expected in mimebundle.items(): | ||
MinRK
|
r18589 | nt.assert_equal(output.data[key], expected) | ||
MinRK
|
r18573 | |||
def test_error(): | ||||
o = new_output(output_type=u'error', ename=u'NameError', | ||||
evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2'] | ||||
) | ||||
nt.assert_equal(o.output_type, u'error') | ||||
nt.assert_equal(o.ename, u'NameError') | ||||
nt.assert_equal(o.evalue, u'Name not found') | ||||
nt.assert_equal(o.traceback, [u'frame 0', u'frame 1', u'frame 2']) | ||||
def test_code_cell_with_outputs(): | ||||
MinRK
|
r18587 | cell = new_code_cell(execution_count=10, outputs=[ | ||
MinRK
|
r18573 | new_output('display_data', mimebundle), | ||
new_output('stream', text='hello'), | ||||
MinRK
|
r18587 | new_output('execute_result', mimebundle, execution_count=10), | ||
MinRK
|
r18573 | ]) | ||
MinRK
|
r18587 | nt.assert_equal(cell.execution_count, 10) | ||
MinRK
|
r18573 | nt.assert_equal(len(cell.outputs), 3) | ||
er = cell.outputs[-1] | ||||
MinRK
|
r18587 | nt.assert_equal(er.execution_count, 10) | ||
MinRK
|
r18573 | nt.assert_equal(er['output_type'], 'execute_result') | ||
MinRK
|
r18576 | |||
def test_stream(): | ||||
output = new_output('stream', name='stderr', text='hello there') | ||||
nt.assert_equal(output.name, 'stderr') | ||||
nt.assert_equal(output.text, 'hello there') | ||||