Пример #1
0
def test_no_rmd_on_not_notebook(tmpdir):
    tmp_ipynb = "notebook.ipynb"
    tmp_rmd = "notebook.Rmd"

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.formats = "ipynb,Rmd"

    with pytest.raises(HTTPError):
        cm.save(model=dict(type="not notebook", content=new_notebook()), path=tmp_ipynb)
    assert not os.path.isfile(str(tmpdir.join(tmp_rmd)))
Пример #2
0
def test_no_rmd_on_not_v4(tmpdir):
    tmp_ipynb = "notebook.ipynb"
    tmp_rmd = "notebook.Rmd"

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.formats = "ipynb,Rmd"

    with pytest.raises(NotebookValidationError):
        cm.save(model=notebook_model(new_notebook(nbformat=3)), path=tmp_rmd)

    assert not os.path.isfile(str(tmpdir.join(tmp_ipynb)))
Пример #3
0
def test_ipynb_is_ok(nb_file, tmpdir):
    nb = jupytext.read(nb_file)
    tmp_ipynb = "notebook.ipynb"
    tmp_rmd = "notebook.Rmd"

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.formats = "ipynb,Rmd"

    cm.save(model=notebook_model(nb), path=tmp_rmd)

    nb2 = jupytext.read(str(tmpdir.join(tmp_ipynb)))
    compare_notebooks(nb2, nb)
Пример #4
0
def test_no_files_created_on_no_format(tmpdir):
    tmp_ipynb = "notebook.ipynb"
    tmp_rmd = "notebook.Rmd"
    tmp_py = "notebook.py"

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.formats = ""

    cm.save(
        model=notebook_model(new_notebook(nbformat=4, metadata=dict())),
        path=tmp_ipynb,
    )

    assert not os.path.isfile(str(tmpdir.join(tmp_py)))
    assert not os.path.isfile(str(tmpdir.join(tmp_rmd)))
Пример #5
0
def test_ipynb_notebooks_can_be_trusted(nb_file, tmpdir,
                                        no_jupytext_version_number):
    cm = TextFileContentsManager()
    root, file = os.path.split(nb_file)
    tmp_ipynb = str(tmpdir.join(file))
    py_file = file.replace(".ipynb", ".py")
    tmp_py = str(tmpdir.join(py_file))
    shutil.copy(nb_file, tmp_ipynb)

    cm.formats = "ipynb,py"
    cm.root_dir = str(tmpdir)
    model = cm.get(file)
    cm.save(model, py_file)

    # Unsign and test notebook
    nb = model["content"]
    for cell in nb.cells:
        cell.metadata.pop("trusted", True)

    cm.notary.unsign(nb)

    model = cm.get(file)
    for cell in model["content"].cells:
        assert ("trusted" not in cell.metadata or not cell.metadata["trusted"]
                or not cell.outputs)

    # Trust and reload
    cm.trust_notebook(py_file)

    model = cm.get(file)
    for cell in model["content"].cells:
        assert cell.metadata.get("trusted", True)

    # Remove py file, content should be the same
    os.remove(tmp_py)
    nb2 = cm.get(file)
    for cell in nb2["content"].cells:
        assert cell.metadata.get("trusted", True)

    compare_notebooks(nb2["content"], model["content"])

    # Just for coverage
    cm.trust_notebook(file)