test_validator.py
58 lines
| 1.9 KiB
| text/x-python
|
PythonLexer
MinRK
|
r18243 | """Test nbformat.validator""" | ||
Jessica B. Hamrick
|
r16431 | |||
MinRK
|
r18243 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Jessica B. Hamrick
|
r16431 | |||
import os | ||||
from .base import TestsBase | ||||
MinRK
|
r18243 | from jsonschema import ValidationError | ||
MinRK
|
r18603 | from IPython.nbformat import read | ||
MinRK
|
r18243 | from ..validator import isvalid, validate | ||
Jessica B. Hamrick
|
r16431 | |||
class TestValidator(TestsBase): | ||||
def test_nb2(self): | ||||
MinRK
|
r18579 | """Test that a v2 notebook converted to current passes validation""" | ||
Jessica B. Hamrick
|
r16431 | with self.fopen(u'test2.ipynb', u'r') as f: | ||
MinRK
|
r18603 | nb = read(f, as_version=4) | ||
MinRK
|
r18243 | validate(nb) | ||
Jessica B. Hamrick
|
r16431 | self.assertEqual(isvalid(nb), True) | ||
def test_nb3(self): | ||||
"""Test that a v3 notebook passes validation""" | ||||
with self.fopen(u'test3.ipynb', u'r') as f: | ||||
MinRK
|
r18603 | nb = read(f, as_version=4) | ||
MinRK
|
r18243 | validate(nb) | ||
Jessica B. Hamrick
|
r16431 | self.assertEqual(isvalid(nb), True) | ||
MinRK
|
r18579 | def test_nb4(self): | ||
MinRK
|
r18596 | """Test that a v4 notebook passes validation""" | ||
MinRK
|
r18579 | with self.fopen(u'test4.ipynb', u'r') as f: | ||
MinRK
|
r18603 | nb = read(f, as_version=4) | ||
MinRK
|
r18579 | validate(nb) | ||
self.assertEqual(isvalid(nb), True) | ||||
Jessica B. Hamrick
|
r16431 | def test_invalid(self): | ||
"""Test than an invalid notebook does not pass validation""" | ||||
# this notebook has a few different errors: | ||||
Jessica B. Hamrick
|
r16434 | # - one cell is missing its source | ||
MinRK
|
r18596 | # - invalid cell type | ||
# - invalid output_type | ||||
Jessica B. Hamrick
|
r16431 | with self.fopen(u'invalid.ipynb', u'r') as f: | ||
MinRK
|
r18603 | nb = read(f, as_version=4) | ||
MinRK
|
r18243 | with self.assertRaises(ValidationError): | ||
validate(nb) | ||||
Jessica B. Hamrick
|
r16431 | self.assertEqual(isvalid(nb), False) | ||
MinRK
|
r18251 | def test_future(self): | ||
"""Test than a notebook from the future with extra keys passes validation""" | ||||
MinRK
|
r18599 | with self.fopen(u'test4plus.ipynb', u'r') as f: | ||
MinRK
|
r18603 | nb = read(f, as_version=4) | ||
MinRK
|
r18251 | with self.assertRaises(ValidationError): | ||
MinRK
|
r18599 | validate(nb, version=4) | ||
self.assertEqual(isvalid(nb, version=4), False) | ||||
MinRK
|
r18251 | self.assertEqual(isvalid(nb), True) | ||
MinRK
|
r18599 | |||