Skip to content

Commit

Permalink
test that in_valid does note mutate or autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Aug 3, 2022
1 parent 898cd15 commit fc45fed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions nbformat/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def isvalid(nbjson, ref=None, version=None, version_minor=None):
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=MissingIDFieldWarning)
validate(nbjson, ref, version, version_minor, repair_duplicate_cell_ids=False)
except ValidationError:
return False
Expand Down
31 changes: 30 additions & 1 deletion tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def test_should_not_mutate(validator_name):
nb = read(f, as_version=4)

del nb.cells[3]["id"]
assert nb.cells[3].get("id") is None
assert nb.cells[3]["cell_type"] == "code"

nb_deep_copy = deepcopy(nb)
Expand All @@ -77,6 +76,36 @@ def test_should_not_mutate(validator_name):
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"""
Expand Down

0 comments on commit fc45fed

Please sign in to comment.