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_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.º 6
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.º 7
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.º 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_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.º 11
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.º 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_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.º 14
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.º 15
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')