Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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
Example #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
    ]
Example #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)
Example #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)
Example #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)
Example #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
Example #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)
Example #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))
Example #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)
Example #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']
Example #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)
Example #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)
Example #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')
Example #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)
Example #20
0
def test_parse_docstring(code, docstring):
    source = NotebookSource(code, ext_in='py')
    assert source.doc == docstring
Example #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')
Example #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)
Example #23
0
def test_repr_from_str():
    source = NotebookSource(notebook_ab, ext_in='py')
    assert repr(source) == 'NotebookSource(loaded from string)'
Example #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
Example #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'})
Example #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'
Example #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)