def test_basic_script_with_output_before(capsys): def run(dirname): with WorkingDir(dirname): main(["bokeh", "html", "--output", "foo.html", "scatter.py"]) out, err = capsys.readouterr() assert err == "" assert out == "" assert set(["foo.html", "scatter.py"]) == set(os.listdir(dirname)) with_directory_contents({ 'scatter.py' : basic_scatter_script }, run)
def test_basic_script(capsys): def run(dirname): with WorkingDir(dirname): main(["bokeh", "svg", "scatter.py"]) out, err = capsys.readouterr() assert err == "" assert out == "" assert set(["scatter.svg", "scatter.py"]) == set(os.listdir(dirname)) with_directory_contents({ 'scatter.py' : basic_svg_scatter_script }, run)
def test_basic_script_with_output_stdout(capsys): def run(dirname): with WorkingDir(dirname): main(["bokeh", "svg", "--output", "-", "scatter.py"]) out, err = capsys.readouterr() assert len(err) == 0 assert len(out) > 0 assert out.startswith('<svg version=') assert set(["scatter.py"]) == set(os.listdir(dirname)) with_directory_contents({ 'scatter.py' : basic_svg_scatter_script }, run)
def test_show(mock_view, capsys): def run(dirname): with WorkingDir(dirname): main(["bokeh", "html", "--show", "scatter.py"]) out, err = capsys.readouterr() assert err == "" assert out == "" assert set(["scatter.html", "scatter.py"]) == set(os.listdir(dirname)) assert mock_view.called assert mock_view.call_args[0] == ('scatter.html',) with_directory_contents({ 'scatter.py' : basic_scatter_script }, run)
def test_directory_empty_mainpy(self): doc = Document() def load(filename): handler = bahd.DirectoryHandler(filename=filename) handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.py' : "# This script does nothing" }, load) assert not doc.roots
def test_directory_mainpy_adds_roots(self): doc = Document() def load(filename): handler = bahd.DirectoryHandler(filename=filename) handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.py' : script_adds_two_roots('SomeModelInTestDirectory', 'AnotherModelInTestDirectory') }, load) assert len(doc.roots) == 2
def test_basic_script_with_multiple_png_plots(capsys): def run(dirname): with WorkingDir(dirname): main(["bokeh", "png", "scatter1.py", "scatter2.py", "scatter3.py"]) out, err = capsys.readouterr() assert err == "" assert out == "" assert set(["scatter1.png", "scatter2.png", "scatter3.png", "scatter1.py", "scatter2.py", "scatter3.py"]) == set(os.listdir(dirname)) with_directory_contents({ 'scatter1.py' : basic_scatter_script, 'scatter2.py' : basic_scatter_script, 'scatter3.py' : basic_scatter_script, }, run)
def test_safe_to_fork(self): doc = Document() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) assert handler.safe_to_fork result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) assert not handler.safe_to_fork with_directory_contents({ 'main.py' : "# This script does nothing", }, load)
async def test_directory_with_lifecycle_and_app_hooks_errors(self) -> None: def load(filename): with pytest.raises(ValueError): bahd.DirectoryHandler(filename=filename) with_directory_contents( { 'main.py': script_adds_two_roots( 'SomeModelInTestDirectoryWithLifecycle', 'AnotherModelInTestDirectoryWithLifecycle'), 'app_hooks.py': script_has_lifecycle_handlers, 'server_lifecycle.py': script_has_request_handler }, load)
def test_safe_to_fork(self) -> None: doc = Document() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) assert handler.safe_to_fork result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) assert not handler.safe_to_fork with_directory_contents({ 'main.py': "# This script does nothing", }, load)
def test_directory_without_template(self): doc = Document() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.py' : "# This script does nothing", }, load) assert not doc.roots assert doc.template is FILE
def test_directory_without_template(self): doc = Document() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.py' : "# This script does nothing", }, load) assert not doc.roots assert doc.template is FILE
def test_directory_has_theme_file(self) -> None: doc = Document() def load(filename: str): handler = bahd.DirectoryHandler(filename=filename) handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) custom_theme = """ attrs: AnotherModelInTestDirectoryTheme: bar: 42 SomeModelInTestDirectoryTheme: foo: 14 """ with_directory_contents( { 'main.py': script_adds_two_roots('SomeModelInTestDirectoryTheme', 'AnotherModelInTestDirectoryTheme') + """ # we're testing that the script can override the theme some = next(m for m in curdoc().roots if isinstance(m, SomeModelInTestDirectoryTheme)) some.foo = 57 """, 'theme.yaml': custom_theme }, load) assert len(doc.roots) == 2 some_model = next( m for m in doc.roots if m.__class__.__name__ == 'SomeModelInTestDirectoryTheme') another_model = next( m for m in doc.roots if m.__class__.__name__ == 'AnotherModelInTestDirectoryTheme') assert another_model.bar == 42 assert some_model.foo == 57 # test that we use the theme if we delete our explicit-set value del some_model.foo assert some_model.foo == 14 # test that removing the theme gets us back to the base doc.theme = None assert some_model.foo == 2 assert another_model.bar == 1
def test_directory_mainpy_adds_roots(self) -> None: doc = Document() def load(filename: str): handler = bahd.DirectoryHandler(filename=filename) handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) with_directory_contents( { 'main.py': script_adds_two_roots('SomeModelInTestDirectory', 'AnotherModelInTestDirectory') }, load) assert len(doc.roots) == 2
def test_directory_with_template(self): doc = Document() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.py' : "# This script does nothing", 'templates/index.html' : "<div>some HTML</div>" }, load) assert not doc.roots assert isinstance(doc.template, jinja2.Template)
def test_directory_with_template(self): doc = Document() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.py' : "# This script does nothing", 'templates/index.html' : "<div>some HTML</div>" }, load) assert not doc.roots assert isinstance(doc.template, jinja2.Template)
def test_directory_empty_mainipynb(self) -> None: import nbformat doc = Document() source = nbformat.v4.new_notebook() result: Dict[str, Handler] = {} def load(filename: str): handler = bahd.DirectoryHandler(filename=filename) handler.modify_document(doc) result['handler'] = handler result['filename'] = filename if handler.failed: raise RuntimeError(handler.error) with_directory_contents({'main.ipynb': nbformat.writes(source)}, load) assert not doc.roots
def test_directory_empty_mainipynb(self): import nbformat doc = Document() source = nbformat.v4.new_notebook() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) handler.modify_document(doc) result['handler'] = handler result['filename'] = filename if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.ipynb': nbformat.writes(source) }, load) assert not doc.roots
def test_directory_without_static(self) -> None: doc = Document() result: Dict[str, Handler] = {} def load(filename: str): handler = bahd.DirectoryHandler(filename=filename) result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.py': "# This script does nothing", }, load) assert not doc.roots handler = result['handler'] assert handler.static_path() is None
def test_basic_script_with_multiple_png_plots(capsys): def run(dirname): with WorkingDir(dirname): main(["bokeh", "png", "scatter1.py", "scatter2.py", "scatter3.py"]) out, err = capsys.readouterr() assert err == "" assert out == "" assert set([ "scatter1.png", "scatter2.png", "scatter3.png", "scatter1.py", "scatter2.py", "scatter3.py" ]) == set(os.listdir(dirname)) with_directory_contents( { 'scatter1.py': basic_scatter_script, 'scatter2.py': basic_scatter_script, 'scatter3.py': basic_scatter_script, }, run)
def test_url_path(self): doc = Document() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) assert handler.safe_to_fork result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) assert not handler.safe_to_fork with_directory_contents({ 'main.py' : "# This script does nothing", }, load) h = result['handler'] assert h.url_path().startswith("/") h._main_handler._runner._failed = True assert h.url_path() is None
def test_directory_with_static(self): doc = Document() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.py' : "# This script does nothing", 'static/js/foo.js' : "# some JS" }, load) assert not doc.roots handler = result['handler'] assert handler.static_path() is not None assert handler.static_path().endswith("static")
def test_url_path(self): doc = Document() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) assert handler.safe_to_fork result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) assert not handler.safe_to_fork with_directory_contents({ 'main.py' : "# This script does nothing", }, load) h = result['handler'] assert h.url_path().startswith("/") h._main_handler._runner._failed = True assert h.url_path() is None
def test_directory_with_static(self): doc = Document() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.py' : "# This script does nothing", 'static/js/foo.js' : "# some JS" }, load) assert not doc.roots handler = result['handler'] assert handler.static_path() is not None assert handler.static_path().endswith("static")
def test_directory_mainipynb_adds_roots(self) -> None: import nbformat doc = Document() source = nbformat.v4.new_notebook() code = script_adds_two_roots('SomeModelInNbTestDirectory', 'AnotherModelInNbTestDirectory') source.cells.append(nbformat.v4.new_code_cell(code)) result: Dict[str, Handler] = {} def load(filename: str): handler = bahd.DirectoryHandler(filename=filename) handler.modify_document(doc) result['handler'] = handler result['filename'] = filename if handler.failed: raise RuntimeError(handler.error) with_directory_contents({'main.ipynb': nbformat.writes(source)}, load) assert len(doc.roots) == 2
def test_directory_both_mainipynb_and_mainpy(self) -> None: doc = Document() def load(filename: str): handler = bahd.DirectoryHandler(filename=filename) handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) import nbformat source = nbformat.v4.new_notebook() with_directory_contents( { 'main.py': script_adds_two_roots('SomeModelInTestDirectory', 'AnotherModelInTestDirectory'), 'main.ipynb': nbformat.writes(source), }, load) assert len(doc.roots) == 2
def test_directory_mainipynb_adds_roots(self): import nbformat doc = Document() source = nbformat.v4.new_notebook() code = script_adds_two_roots('SomeModelInNbTestDirectory', 'AnotherModelInNbTestDirectory') source.cells.append(nbformat.v4.new_code_cell(code)) result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) handler.modify_document(doc) result['handler'] = handler result['filename'] = filename if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.ipynb': nbformat.writes(source) }, load) assert len(doc.roots) == 2
def test_directory_has_theme_file(self): doc = Document() def load(filename): handler = bahd.DirectoryHandler(filename=filename) handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) custom_theme = """ attrs: AnotherModelInTestDirectoryTheme: bar: 42 SomeModelInTestDirectoryTheme: foo: 14 """ with_directory_contents({ 'main.py' : script_adds_two_roots('SomeModelInTestDirectoryTheme', 'AnotherModelInTestDirectoryTheme') + """ # we're testing that the script can override the theme some = next(m for m in curdoc().roots if isinstance(m, SomeModelInTestDirectoryTheme)) some.foo = 57 """, 'theme.yaml' : custom_theme }, load) assert len(doc.roots) == 2 some_model = next(m for m in doc.roots if m.__class__.__name__ == 'SomeModelInTestDirectoryTheme') another_model = next(m for m in doc.roots if m.__class__.__name__ == 'AnotherModelInTestDirectoryTheme') assert another_model.bar == 42 assert some_model.foo == 57 # test that we use the theme if we delete our explicit-set value del some_model.foo assert some_model.foo == 14 # test that removing the theme gets us back to the base doc.theme = None assert some_model.foo == 2 assert another_model.bar == 1
async def test_directory_with_server_lifecycle(self): doc = Document() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.py' : script_adds_two_roots('SomeModelInTestDirectoryWithLifecycle', 'AnotherModelInTestDirectoryWithLifecycle'), 'server_lifecycle.py' : script_has_lifecycle_handlers }, load) assert len(doc.roots) == 2 handler = result['handler'] assert "on_server_loaded" == handler.on_server_loaded(None) assert "on_server_unloaded" == handler.on_server_unloaded(None) assert "on_session_created" == await handler.on_session_created(None) assert "on_session_destroyed" == await handler.on_session_destroyed(None)
def test_directory_with_server_lifecycle(self): doc = Document() result = {} def load(filename): handler = bahd.DirectoryHandler(filename=filename) result['handler'] = handler handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) with_directory_contents({ 'main.py' : script_adds_two_roots('SomeModelInTestDirectoryWithLifecycle', 'AnotherModelInTestDirectoryWithLifecycle'), 'server_lifecycle.py' : script_has_lifecycle_handlers }, load) assert len(doc.roots) == 2 handler = result['handler'] assert "on_server_loaded" == handler.on_server_loaded(None) assert "on_server_unloaded" == handler.on_server_unloaded(None) assert "on_session_created" == handler.on_session_created(None) assert "on_session_destroyed" == handler.on_session_destroyed(None)