test_validate.py
105 lines
| 2.5 KiB
| text/x-python
|
PythonLexer
MinRK
|
r18573 | """Tests for nbformat validation""" | ||
# Copyright (c) IPython Development Team. | ||||
# Distributed under the terms of the Modified BSD License. | ||||
import io | ||||
import os | ||||
import nose.tools as nt | ||||
MinRK
|
r18574 | from IPython.nbformat.validator import validate, ValidationError | ||
MinRK
|
r18573 | from ..nbjson import reads | ||
MinRK
|
r18575 | from ..nbbase import ( | ||
nbformat, | ||||
MinRK
|
r18596 | new_code_cell, new_markdown_cell, new_notebook, | ||
MinRK
|
r18573 | new_output, new_raw_cell, | ||
) | ||||
MinRK
|
r18574 | def validate4(obj, ref=None): | ||
return validate(obj, ref, version=nbformat) | ||||
MinRK
|
r18573 | def test_valid_code_cell(): | ||
cell = new_code_cell() | ||||
MinRK
|
r18574 | validate4(cell, 'code_cell') | ||
MinRK
|
r18573 | |||
def test_invalid_code_cell(): | ||||
cell = new_code_cell() | ||||
cell['source'] = 5 | ||||
with nt.assert_raises(ValidationError): | ||||
MinRK
|
r18574 | validate4(cell, 'code_cell') | ||
MinRK
|
r18573 | |||
cell = new_code_cell() | ||||
del cell['metadata'] | ||||
with nt.assert_raises(ValidationError): | ||||
MinRK
|
r18574 | validate4(cell, 'code_cell') | ||
MinRK
|
r18573 | |||
cell = new_code_cell() | ||||
del cell['source'] | ||||
with nt.assert_raises(ValidationError): | ||||
MinRK
|
r18574 | validate4(cell, 'code_cell') | ||
MinRK
|
r18573 | |||
cell = new_code_cell() | ||||
del cell['cell_type'] | ||||
with nt.assert_raises(ValidationError): | ||||
MinRK
|
r18574 | validate4(cell, 'code_cell') | ||
MinRK
|
r18573 | |||
def test_invalid_markdown_cell(): | ||||
cell = new_markdown_cell() | ||||
cell['source'] = 5 | ||||
with nt.assert_raises(ValidationError): | ||||
MinRK
|
r18574 | validate4(cell, 'markdown_cell') | ||
MinRK
|
r18573 | |||
cell = new_markdown_cell() | ||||
del cell['metadata'] | ||||
with nt.assert_raises(ValidationError): | ||||
MinRK
|
r18574 | validate4(cell, 'markdown_cell') | ||
MinRK
|
r18573 | |||
cell = new_markdown_cell() | ||||
del cell['source'] | ||||
with nt.assert_raises(ValidationError): | ||||
MinRK
|
r18574 | validate4(cell, 'markdown_cell') | ||
MinRK
|
r18573 | |||
cell = new_markdown_cell() | ||||
del cell['cell_type'] | ||||
with nt.assert_raises(ValidationError): | ||||
MinRK
|
r18574 | validate4(cell, 'markdown_cell') | ||
MinRK
|
r18573 | |||
def test_invalid_raw_cell(): | ||||
cell = new_raw_cell() | ||||
cell['source'] = 5 | ||||
with nt.assert_raises(ValidationError): | ||||
MinRK
|
r18574 | validate4(cell, 'raw_cell') | ||
MinRK
|
r18573 | |||
cell = new_raw_cell() | ||||
del cell['metadata'] | ||||
with nt.assert_raises(ValidationError): | ||||
MinRK
|
r18574 | validate4(cell, 'raw_cell') | ||
MinRK
|
r18573 | |||
cell = new_raw_cell() | ||||
del cell['source'] | ||||
with nt.assert_raises(ValidationError): | ||||
MinRK
|
r18574 | validate4(cell, 'raw_cell') | ||
MinRK
|
r18573 | |||
cell = new_raw_cell() | ||||
del cell['cell_type'] | ||||
with nt.assert_raises(ValidationError): | ||||
MinRK
|
r18574 | validate4(cell, 'raw_cell') | ||
MinRK
|
r18573 | |||
def test_sample_notebook(): | ||||
here = os.path.dirname(__file__) | ||||
MinRK
|
r18575 | with io.open(os.path.join(here, os.pardir, os.pardir, 'tests', "test4.ipynb"), encoding='utf-8') as f: | ||
MinRK
|
r18573 | nb = reads(f.read()) | ||
MinRK
|
r18574 | validate4(nb) | ||