Exemplo n.º 1
0
def test_ignores_static_analysis_if_non_python_file():
    source = NotebookSource(new_nb(fmt='r:light'),
                            ext_in='R',
                            static_analysis=True)
    params = Params._from_dict({'product': File('output.ipynb')})

    source.render(params)
Exemplo n.º 2
0
def test_error_if_using_undeclared_variable():
    notebook_w_warning = """
# + tags=['parameters']
a = 1
b = 2

# +
# variable "c" is used but never declared!
a + b + c
"""
    source = NotebookSource(notebook_w_warning,
                            ext_in='py',
                            kernelspec_name='python3',
                            static_analysis=True)

    params = Params._from_dict({
        'product': File('output.ipynb'),
        'a': 1,
        'b': 2
    })

    with pytest.raises(RenderError) as excinfo:
        source.render(params)

    assert "undefined name 'c'" in str(excinfo.value)
Exemplo n.º 3
0
def test_error_if_static_analysis_on_a_non_python_nb():
    source = NotebookSource(new_nb(fmt='r:light'),
                            ext_in='R',
                            static_analysis=True)
    params = Params._from_dict({'product': File('output.ipynb')})

    with pytest.raises(NotImplementedError):
        source.render(params)
Exemplo n.º 4
0
def test_removes_papermill_metadata():
    source = NotebookSource(new_nb(fmt='ipynb'),
                            ext_in='ipynb',
                            kernelspec_name='python3')

    params = Params._from_dict({'product': File('file.ipynb')})
    source.render(params)
    nb = source.nb_obj_rendered

    assert all('papermill' not in cell['metadata'] for cell in nb.cells)
Exemplo n.º 5
0
def test_error_if_missing_params():
    source = NotebookSource(notebook_ab,
                            ext_in='py',
                            kernelspec_name='python3',
                            static_analysis=True)

    params = Params._from_dict({'product': File('output.ipynb'), 'a': 1})

    with pytest.raises(TypeError) as excinfo:
        source.render(params)

    assert "Missing params: 'b'" in str(excinfo.value)
Exemplo n.º 6
0
def test_error_message_when_initialized_from_str(tmp_nbs, method, kwargs):
    source = NotebookSource("""
# + tags=["parameters"]
""", ext_in='py')

    source.render(Params._from_dict({'product': File('file.ipynb')}))

    with pytest.raises(ValueError) as excinfo:
        getattr(source, method)(**kwargs)

    expected = (f"Cannot use '{method}' if notebook was not "
                "initialized from a file")
    assert str(excinfo.value) == expected
Exemplo n.º 7
0
def test_warn_if_using_default_value():
    source = NotebookSource(notebook_ab,
                            ext_in='py',
                            kernelspec_name='python3',
                            static_analysis=True)

    params = Params._from_dict({'product': File('output.ipynb'), 'a': 1})

    with pytest.warns(UserWarning) as record:
        source.render(params)

    assert "Missing parameters: {'b'}, will use default value" in [
        str(warning.message) for warning in record
    ]
Exemplo n.º 8
0
def test_static_analysis(hot_reload, tmp_directory):
    nb = jupytext.reads(notebook_ab, fmt='py:light')
    path = Path('nb.ipynb')
    path.write_text(jupytext.writes(nb, fmt='ipynb'))

    source = NotebookSource(path, static_analysis=True, hot_reload=hot_reload)

    params = Params._from_dict({
        'product': File('output.ipynb'),
        'a': 1,
        'b': 2
    })

    source.render(params)
Exemplo n.º 9
0
def test_no_error_if_missing_product_or_upstream():
    code = """
# + tags=["parameters"]

# +
"""
    source = NotebookSource(code,
                            ext_in='py',
                            kernelspec_name='python3',
                            static_analysis=True)

    params = Params._from_dict({'product': File('output.ipynb')})

    source.render(params)
Exemplo n.º 10
0
def test_error_if_passing_undeclared_parameter():
    source = NotebookSource(notebook_ab,
                            ext_in='py',
                            kernelspec_name='python3',
                            static_analysis=True)

    params = Params._from_dict({
        'product': File('output.ipynb'),
        'a': 1,
        'b': 2,
        'c': 3
    })

    with pytest.raises(TypeError) as excinfo:
        source.render(params)

    assert "Unexpected params: 'c'" in str(excinfo.value)
Exemplo n.º 11
0
def test_rmd(code, tmp_directory):
    # check we can initialize from Rmd files with no kernelspec metadata,
    # we need this because the new_nb utility function always creates nbs
    # with metadata (because it uses jupytext.writes)
    path = Path('notebook.Rmd')
    path.write_text(code)
    source = NotebookSource(path)
    assert len(source._nb_obj_unrendered.cells) == 2
Exemplo n.º 12
0
def test_error_if_undefined_name():
    notebook_w_error = """
# + tags=['parameters']

# +
df.head()
"""
    source = NotebookSource(notebook_w_error,
                            ext_in='py',
                            kernelspec_name='python3',
                            static_analysis=True)

    params = Params._from_dict({'product': File('output.ipynb')})

    with pytest.raises(RenderError) as excinfo:
        source.render(params)

    assert "undefined name 'df'" in str(excinfo.value)
Exemplo n.º 13
0
def test_error_missing_params_cell_shows_path_if_available(tmp_directory):
    path = Path('nb.ipynb')
    path.write_text(new_nb(fmt='ipynb', add_tag=False))

    with pytest.raises(SourceInitializationError) as excinfo:
        NotebookSource(path)

    assert ('Notebook "nb.ipynb" does not have a cell tagged "parameters"'
            in str(excinfo.value))
Exemplo n.º 14
0
def test_str_ignores_injected_cell(tmp_directory):
    path = Path('nb.py')
    path.write_text(notebook_ab)
    source = NotebookSource(path)
    source.render(Params._from_dict(dict(a=42, product=File('file.txt'))))
    source.save_injected_cell()

    source = NotebookSource(path)

    # injected cell should not be considered part of the source code
    assert 'a = 42' not in str(source)
Exemplo n.º 15
0
def test_injects_parameters_on_render(nb_str, ext):
    s = NotebookSource(nb_str, ext_in=ext)
    params = Params._from_dict({
        'some_param': 1,
        'product': File('output.ipynb')
    })
    s.render(params)

    nb = nbformat.reads(s.nb_str_rendered, as_version=nbformat.NO_CONVERT)

    # cell 0: parameters
    # cell 1: injected-parameters

    injected = nb.cells[1]
    tags = injected['metadata']['tags']

    assert len(tags) == 1
    assert tags[0] == 'injected-parameters'

    # py 3.5 does not gurantee order, so we check them separately
    assert 'some_param = 1' in injected['source']
    assert 'product = "output.ipynb"' in injected['source']
Exemplo n.º 16
0
def test_error_if_syntax_error():
    notebook_w_error = """
# + tags=['parameters']
a = 1
b = 2

# +
if
"""
    source = NotebookSource(notebook_w_error,
                            ext_in='py',
                            kernelspec_name='python3',
                            static_analysis=True)

    params = Params._from_dict({
        'product': File('output.ipynb'),
        'a': 1,
        'b': 2
    })

    with pytest.raises(SyntaxError) as excinfo:
        source.render(params)

    assert 'invalid syntax\n\nif\n\n  ^\n' in str(excinfo.value)
Exemplo n.º 17
0
def test_read_file(nb_str, ext, expected, tmp_directory):
    path = Path('nb.' + ext)
    path.write_text(nb_str)
    source = NotebookSource(path)

    assert source._ext_in == ext
    assert source.language == expected

    # jupyter sets as "R" (and not "r") as the language for R notebooks
    if expected == 'r':
        expected_lang = expected.upper()
    else:
        expected_lang = expected

    # most common kernels
    lang2kernel = {'python': 'python3', 'r': 'ir'}
    expected_kernel = lang2kernel[expected]

    assert len(source._nb_obj_unrendered.cells) == 2

    assert (source._nb_obj_unrendered.metadata.kernelspec.language ==
            expected_lang)
    assert (
        source._nb_obj_unrendered.metadata.kernelspec.name == expected_kernel)
Exemplo n.º 18
0
def test_error_if_kernelspec_name_is_invalid():
    with pytest.raises(NoSuchKernel):
        NotebookSource(new_nb(fmt='ipynb'),
                       ext_in='ipynb',
                       kernelspec_name='invalid_kernelspec')
Exemplo n.º 19
0
def test_repr_from_path(tmp_directory):
    path = Path(tmp_directory, 'nb.py')
    Path('nb.py').write_text(notebook_ab)
    source = NotebookSource(path)
    assert repr(source) == "NotebookSource('{}')".format(path)
Exemplo n.º 20
0
def test_parse_docstring(code, docstring):
    source = NotebookSource(code, ext_in='py')
    assert source.doc == docstring
Exemplo n.º 21
0
def test_str():
    source = NotebookSource(notebook_ab, ext_in='py')
    source.render(Params._from_dict({'product':
                                     File('path/to/file/data.csv')}))
    assert str(source) == ('\na = 1\nb = 2\nproduct = None\na + b')
Exemplo n.º 22
0
def test_error_if_parameters_cell_doesnt_exist():
    with pytest.raises(SourceInitializationError) as excinfo:
        NotebookSource(new_nb(fmt='ipynb', add_tag=False), ext_in='ipynb')

    assert 'Notebook does not have a cell tagged "parameters"' in str(
        excinfo.value)
Exemplo n.º 23
0
def test_repr_from_str():
    source = NotebookSource(notebook_ab, ext_in='py')
    assert repr(source) == 'NotebookSource(loaded from string)'
Exemplo n.º 24
0
def test_extract_upstream_from_parameters(code, ext, expected_up,
                                          expected_prod):
    source = NotebookSource(code, ext_in=ext)
    upstream = source.extract_upstream()
    assert sorted(upstream) == sorted(expected_up)
    assert source.extract_product() == expected_prod
Exemplo n.º 25
0
def test_nb_str_contains_kernel_info():
    source = NotebookSource(new_nb(fmt='ipynb'), ext_in='ipynb')
    nb = nbformat.reads(source._nb_str_unrendered,
                        as_version=nbformat.NO_CONVERT)
    assert (set(
        nb.metadata.kernelspec.keys()) == {'display_name', 'language', 'name'})
Exemplo n.º 26
0
def test_kernelspec_overrides_nb_kernel_info():
    source = NotebookSource(new_nb(fmt='ipynb'),
                            ext_in='ipynb',
                            kernelspec_name='ir')
    assert source._nb_obj_unrendered.metadata.kernelspec.name == 'ir'
    assert source._nb_obj_unrendered.metadata.kernelspec.language == 'R'
Exemplo n.º 27
0
def test_skip_kernelspec_install_check():
    NotebookSource(new_nb(fmt='ipynb'),
                   ext_in='ipynb',
                   kernelspec_name='unknown_kernelspec',
                   check_if_kernel_installed=False)