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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
"""Test nbformat.validator"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import annotations
import json
import os
import re
from copy import deepcopy
import pytest
from jsonschema import ValidationError
import nbformat
from nbformat import read
from nbformat.json_compat import VALIDATORS
from nbformat.validator import isvalid, iter_validate, validate
from nbformat.warnings import DuplicateCellId, MissingIDFieldWarning
from .base import TestsBase
nb4 = ("test4.ipynb", "test4.5.ipynb")
# Fixtures
@pytest.fixture(autouse=True)
def clean_env_before_and_after_tests():
"""Fixture to clean up env variables before and after tests."""
os.environ.pop("NBFORMAT_VALIDATOR", None)
yield
os.environ.pop("NBFORMAT_VALIDATOR", None)
# Helpers
def set_validator(validator_name):
os.environ["NBFORMAT_VALIDATOR"] = validator_name
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_should_warn(validator_name):
"""Test that a v4 notebook without id emit a warning"""
set_validator(validator_name)
with TestsBase.fopen("test4.5.ipynb", "r") as f:
nb = read(f, as_version=4)
del nb.cells[3]["id"]
assert nb.cells[3]["cell_type"] == "code"
nb_copy = deepcopy(nb)
with pytest.warns(MissingIDFieldWarning):
validate(nb)
assert isvalid(nb) is True
@pytest.mark.skip(reason="Does not work in all architectures")
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_should_not_mutate(validator_name):
"""Test that a v4 notebook without id raise an error and does/not mutate
Probably should be 2 distinct tests. To enable in the future.
"""
set_validator(validator_name)
with TestsBase.fopen("test4.5.ipynb", "r") as f:
nb = read(f, as_version=4)
del nb.cells[3]["id"]
assert nb.cells[3]["cell_type"] == "code"
nb_deep_copy = deepcopy(nb)
with pytest.raises(MissingIDFieldWarning):
validate(nb)
assert nb == nb_deep_copy
assert isvalid(nb) is False
def _invalidator_1(nb):
del nb.cells[3]["id"]
def _invalidator_3(nb):
nb.cells[3]["id"] = "hey"
nb.cells[2]["id"] = "hey"
def _invalidator_2(nb):
nb.cells[3]["id"] = nb.cells[2]["id"]
@pytest.mark.parametrize("validator_name", VALIDATORS)
@pytest.mark.parametrize("invalidator", [_invalidator_1, _invalidator_2])
def test_is_valid_should_not_mutate(validator_name, invalidator):
"""Test that a v4 notebook does not mutate in is_valid, and does note autofix."""
set_validator(validator_name)
with TestsBase.fopen("test4.5.ipynb", "r") as f:
nb = read(f, as_version=4)
invalidator(nb)
assert nb.cells[3]["cell_type"] == "code"
nb_deep_copy = deepcopy(nb)
assert isvalid(nb) is False
assert nb == nb_deep_copy
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_nb2(validator_name):
"""Test that a v2 notebook converted to current passes validation"""
set_validator(validator_name)
with TestsBase.fopen("test2.ipynb", "r") as f:
nb = read(f, as_version=4)
validate(nb)
assert isvalid(nb)
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_nb3(validator_name):
"""Test that a v3 notebook passes validation"""
set_validator(validator_name)
with TestsBase.fopen("test3.ipynb", "r") as f:
nb = read(f, as_version=4)
validate(nb)
assert isvalid(nb)
@pytest.mark.parametrize("validator_name", VALIDATORS)
@pytest.mark.parametrize("nbfile", nb4)
def test_nb4(validator_name, nbfile):
"""Test that a v4 notebook passes validation"""
set_validator(validator_name)
with TestsBase.fopen(nbfile, "r") as f:
nb = read(f, as_version=4)
validate(nb)
assert isvalid(nb)
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_nb4_document_info(validator_name):
"""Test that a notebook with document_info passes validation"""
set_validator(validator_name)
with TestsBase.fopen("test4docinfo.ipynb", "r") as f:
nb = read(f, as_version=4)
validate(nb)
assert isvalid(nb)
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_nb4custom(validator_name):
"""Test that a notebook with a custom JSON mimetype passes validation"""
set_validator(validator_name)
with TestsBase.fopen("test4custom.ipynb", "r") as f:
nb = read(f, as_version=4)
validate(nb)
assert isvalid(nb)
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_nb4jupyter_metadata(validator_name):
"""Test that a notebook with a jupyter metadata passes validation"""
set_validator(validator_name)
with TestsBase.fopen("test4jupyter_metadata.ipynb", "r") as f:
nb = read(f, as_version=4)
validate(nb)
assert isvalid(nb)
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_nb4jupyter_metadata_timings(validator_name):
"""Tests that a notebook with "timing" in metadata passes validation"""
set_validator(validator_name)
with TestsBase.fopen("test4jupyter_metadata_timings.ipynb", "r") as f:
nb = read(f, as_version=4)
validate(nb)
assert isvalid(nb)
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_invalid(validator_name):
"""Test than an invalid notebook does not pass validation"""
set_validator(validator_name)
# this notebook has a few different errors:
# - one cell is missing its source
# - invalid cell type
# - invalid output_type
with TestsBase.fopen("invalid.ipynb", "r") as f:
nb = read(f, as_version=4)
with pytest.raises(ValidationError):
validate(nb)
assert not isvalid(nb)
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_validate_empty(validator_name):
"""Test that an empty notebook (invalid) fails validation"""
set_validator(validator_name)
with pytest.raises(ValidationError) as e:
validate({})
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_future(validator_name):
"""Test that a notebook from the future with extra keys passes validation"""
set_validator(validator_name)
with TestsBase.fopen("test4plus.ipynb", "r") as f:
nb = read(f, as_version=4)
with pytest.raises(ValidationError):
validate(nb, version=4, version_minor=3)
assert not isvalid(nb, version=4, version_minor=3)
assert isvalid(nb)
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_validation_error(validator_name):
set_validator(validator_name)
with TestsBase.fopen("invalid.ipynb", "r") as f:
nb = read(f, as_version=4)
with pytest.raises(ValidationError) as exception_info:
validate(nb)
s = str(exception_info.value)
assert re.compile(r"validating .required. in markdown_cell").search(s)
assert re.compile(r"source.* is a required property").search(s)
assert re.compile(r"On instance\[u?['\"].*cells['\"]\]\[0\]").search(s)
assert len(s.splitlines()) < 10
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_iter_validation_error(validator_name):
set_validator(validator_name)
with TestsBase.fopen("invalid.ipynb", "r") as f:
nb = read(f, as_version=4)
errors = list(iter_validate(nb))
assert len(errors) == 3
assert {e.ref for e in errors} == {"markdown_cell", "heading_cell", "bad stream"}
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_iter_validation_empty(validator_name):
"""Test that an empty notebook (invalid) fails validation via iter_validate"""
set_validator(validator_name)
errors = list(iter_validate({}))
assert len(errors)
assert type(errors[0]) == ValidationError
@pytest.mark.parametrize("validator_name", VALIDATORS)
def test_validation_no_version(validator_name):
"""Test that an invalid notebook with no version fails validation"""
set_validator(validator_name)
with pytest.raises(ValidationError) as e:
validate({"invalid": "notebook"})
def test_invalid_validator_raises_value_error():
"""Test that an invalid notebook with no version fails validation"""
set_validator("foobar")
with pytest.raises(ValueError), TestsBase.fopen("test2.ipynb", "r") as f:
nb = read(f, as_version=4)
def test_invalid_validator_raises_value_error_after_read():
"""Test that an invalid notebook with no version fails validation"""
set_validator("jsonschema")
with TestsBase.fopen("test2.ipynb", "r") as f:
nb = read(f, as_version=4)
set_validator("foobar")
with pytest.raises(ValueError):
validate(nb)
def test_fallback_validator_with_iter_errors_using_ref(recwarn):
"""
Test that when creating a standalone object (code_cell etc)
the default validator is used as fallback.
"""
set_validator("fastjsonschema")
nbformat.v4.new_code_cell()
nbformat.v4.new_markdown_cell()
nbformat.v4.new_raw_cell()
assert len(recwarn) == 0
def test_non_unique_cell_ids():
"""Test than a non-unique cell id does not pass validation"""
with TestsBase.fopen("invalid_unique_cell_id.ipynb", "r") as f:
# Avoids validate call from `.read`
nb = nbformat.from_dict(json.load(f))
with pytest.raises(ValidationError), pytest.warns(DeprecationWarning):
validate(nb, repair_duplicate_cell_ids=False)
# try again to verify that we didn't modify the content
with pytest.raises(ValidationError), pytest.warns(DeprecationWarning):
validate(nb, repair_duplicate_cell_ids=False)
def test_repair_non_unique_cell_ids():
"""Test that we will repair non-unique cell ids if asked during validation"""
with TestsBase.fopen("invalid_unique_cell_id.ipynb", "r") as f:
# Avoids validate call from `.read`
nb = nbformat.from_dict(json.load(f))
with pytest.warns(DuplicateCellId):
validate(nb)
assert isvalid(nb)
@pytest.mark.filterwarnings("ignore::nbformat.warnings.MissingIDFieldWarning")
def test_no_cell_ids():
"""Test that a cell without a cell ID does not pass validation"""
with TestsBase.fopen("v4_5_no_cell_id.ipynb", "r") as f:
# Avoids validate call from `.read`
nb = nbformat.from_dict(json.load(f))
with pytest.raises(ValidationError), pytest.warns(DeprecationWarning):
validate(nb, repair_duplicate_cell_ids=False)
# try again to verify that we didn't modify the content
with pytest.raises(ValidationError), pytest.warns(DeprecationWarning):
validate(nb, repair_duplicate_cell_ids=False)
@pytest.mark.filterwarnings("ignore::nbformat.warnings.MissingIDFieldWarning")
def test_repair_no_cell_ids():
"""Test that we will repair cells without ids if asked during validation"""
with TestsBase.fopen("v4_5_no_cell_id.ipynb", "r") as f:
# Avoids validate call from `.read`
nb = nbformat.from_dict(json.load(f))
validate(nb)
assert isvalid(nb)
def test_invalid_cell_id():
"""Test than an invalid cell id does not pass validation"""
with TestsBase.fopen("invalid_cell_id.ipynb", "r") as f:
nb = read(f, as_version=4)
with pytest.raises(ValidationError):
validate(nb)
assert not isvalid(nb)
def test_notebook_invalid_without_min_version():
with TestsBase.fopen("no_min_version.ipynb", "r") as f:
nb = read(f, as_version=4)
with pytest.raises(ValidationError):
validate(nb)
def test_notebook_v3_valid_without_min_version():
with TestsBase.fopen("test3_no_min_version.ipynb", "r") as f:
nb = read(f, as_version=4)
validate(nb)
def test_notebook_invalid_without_main_version():
pass
def test_strip_invalid_metadata():
with TestsBase.fopen("v4_5_invalid_metadata.ipynb", "r") as f:
nb = nbformat.from_dict(json.load(f))
assert not isvalid(nb)
with pytest.warns(DeprecationWarning):
validate(nb, strip_invalid_metadata=True)
assert isvalid(nb)
|