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
|
"""Tests for nbformat validation"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import annotations
import os
import pytest
from nbformat.v4.nbbase import nbformat, new_code_cell, new_markdown_cell, new_raw_cell
from nbformat.v4.nbjson import reads
from nbformat.validator import ValidationError, validate
def validate4(obj, ref=None):
return validate(obj, ref, version=nbformat)
def test_valid_code_cell():
cell = new_code_cell()
validate4(cell, "code_cell")
def test_invalid_code_cell():
cell = new_code_cell()
cell["source"] = 5
with pytest.raises(ValidationError):
validate4(cell, "code_cell")
cell = new_code_cell()
del cell["metadata"]
with pytest.raises(ValidationError):
validate4(cell, "code_cell")
cell = new_code_cell()
del cell["source"]
with pytest.raises(ValidationError):
validate4(cell, "code_cell")
cell = new_code_cell()
del cell["cell_type"]
with pytest.raises(ValidationError):
validate4(cell, "code_cell")
def test_invalid_markdown_cell():
cell = new_markdown_cell()
cell["source"] = 5
with pytest.raises(ValidationError):
validate4(cell, "markdown_cell")
cell = new_markdown_cell()
del cell["metadata"]
with pytest.raises(ValidationError):
validate4(cell, "markdown_cell")
cell = new_markdown_cell()
del cell["source"]
with pytest.raises(ValidationError):
validate4(cell, "markdown_cell")
cell = new_markdown_cell()
del cell["cell_type"]
with pytest.raises(ValidationError):
validate4(cell, "markdown_cell")
def test_invalid_raw_cell():
cell = new_raw_cell()
cell["source"] = 5
with pytest.raises(ValidationError):
validate4(cell, "raw_cell")
cell = new_raw_cell()
del cell["metadata"]
with pytest.raises(ValidationError):
validate4(cell, "raw_cell")
cell = new_raw_cell()
del cell["source"]
with pytest.raises(ValidationError):
validate4(cell, "raw_cell")
cell = new_raw_cell()
del cell["cell_type"]
with pytest.raises(ValidationError):
validate4(cell, "raw_cell")
def test_sample_notebook():
here = os.path.dirname(__file__)
with open(
os.path.join(here, os.pardir, os.pardir, "tests", "test4.ipynb"),
encoding="utf-8",
) as f:
nb = reads(f.read())
validate4(nb)
|