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)
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)
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)
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 ]
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)
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(RenderError) as excinfo: source.render(params) assert str(excinfo.value) == "\nPassed non-declared parameters: {'c'}"
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
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))
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']
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(RenderError) as excinfo: source.render(params) assert 'invalid syntax' in str(excinfo.value)
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)
def test_parse_docstring(code, docstring): source = NotebookSource(code, ext_in='py') assert source.doc == docstring
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\n# Parameters' '\nproduct = "path/to/file/data.csv"\n\na + b')
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)
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
def test_repr_from_str(): source = NotebookSource(notebook_ab, ext_in='py') assert repr(source) == 'NotebookSource(loaded from string)'
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'
def test_error_if_kernelspec_name_is_invalid(): with pytest.raises(NoSuchKernel): NotebookSource(new_nb(fmt='ipynb'), ext_in='ipynb', kernelspec_name='invalid_kernelspec')
def test_skip_kernelspec_install_check(): NotebookSource(new_nb(fmt='ipynb'), ext_in='ipynb', kernelspec_name='unknown_kernelspec', check_if_kernel_installed=False)
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)
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'})