Example #1
0
    def test_capture_validation_error(self):
        """Test that validation error can be captured on read() and write()"""
        validation_error: Dict[str, Any] = {}
        path = os.path.join(self._get_files_path(), "invalid.ipynb")
        nb = read(path,
                  as_version=4,
                  capture_validation_error=validation_error)
        assert not isvalid(nb)
        assert "ValidationError" in validation_error
        assert isinstance(validation_error["ValidationError"], ValidationError)

        validation_error = {}
        with TemporaryDirectory() as td:
            dest = os.path.join(td, "invalid.ipynb")
            write(nb, dest, capture_validation_error=validation_error)
            assert os.path.isfile(dest)
            assert "ValidationError" in validation_error
            assert isinstance(validation_error["ValidationError"],
                              ValidationError)

        # Repeat with a valid notebook file
        validation_error = {}
        path = os.path.join(self._get_files_path(), "test4.ipynb")
        nb = read(path,
                  as_version=4,
                  capture_validation_error=validation_error)
        assert isvalid(nb)
        assert "ValidationError" not in validation_error

        validation_error = {}
        with TemporaryDirectory() as td:
            dest = os.path.join(td, "test4.ipynb")
            write(nb, dest, capture_validation_error=validation_error)
            assert os.path.isfile(dest)
            assert "ValidationError" not in validation_error
Example #2
0
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)
Example #3
0
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)
Example #4
0
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)
Example #5
0
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)
Example #6
0
def test_nb4(validator_name):
    """Test that a v4 notebook passes validation"""
    set_validator(validator_name)
    with TestsBase.fopen("test4.ipynb", "r") as f:
        nb = read(f, as_version=4)
    validate(nb)
    assert isvalid(nb)
Example #7
0
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)
Example #8
0
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)
Example #9
0
def test_repair_no_cell_ids():
    """Test that we will repair cells without ids if asked during validation"""
    import nbformat

    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)
Example #10
0
 def test_upgrade_downgrade_4_3_4(self):
     """Test that a v4 notebook downgraded to v3 and then upgraded to v4
     passes validation tests"""
     with self.fopen("test4.ipynb", "r") as f:
         nb = read(f)
     validate(nb)
     nb = convert(nb, 3)
     validate(nb)
     nb = convert(nb, 4)
     self.assertEqual(isvalid(nb), True)
Example #11
0
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)
Example #12
0
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)
    validate(nb, strip_invalid_metadata=True)
    assert isvalid(nb)