Skip to content

Commit

Permalink
Add webviz pytest fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders Fredrik Kiær committed Feb 24, 2020
1 parent 8765458 commit 636b3e2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 66 deletions.
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
"themes/default_assets/*",
]
},
entry_points={"console_scripts": ["webviz=webviz_config.command_line:main"]},
entry_points={
"console_scripts": ["webviz=webviz_config.command_line:main"],
"pytest11": ["webviz = webviz_config.testing.plugin"],
},
install_requires=[
# Pinning dash to the 1.7-series as long as
# https://github.com/plotly/dash-core-components/issues/746 is open
Expand Down
117 changes: 52 additions & 65 deletions tests/test_table_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,108 +3,95 @@

from webviz_config.common_cache import CACHE
from webviz_config.themes import default_theme
from webviz_config.plugins import _table_plotter
from webviz_config.plugins import TablePlotter

CSV_FILE = "./tests/data/example_data.csv"

def test_table_plotter(dash_duo):

app = dash.Dash(__name__)
app.config.suppress_callback_exceptions = True
CACHE.init_app(app.server)
app.webviz_settings = {"theme": default_theme}
csv_file = "./tests/data/example_data.csv"
page = _table_plotter.TablePlotter(app, csv_file)
app.layout = page.layout
dash_duo.start_server(app)
def test_table_plotter(webviz_single_plugin, dash_duo):

app = webviz_single_plugin.app

# Wait for the app to render(there is probably a better way...)
time.sleep(5)
plugin = TablePlotter(app, csv_file=CSV_FILE)
app.layout = plugin.layout
dash_duo.start_server(app)

# Checking that no plot options are defined
assert page.plot_options == {}
assert plugin.plot_options == {}

# Check that filter is not active
assert not page.use_filter
assert not plugin.use_filter

# Checking that the correct plot type is initialized
plot_dd = dash_duo.find_element("#" + page.uuid("plottype"))
plot_dd = dash_duo.find_element("#" + plugin.uuid("plottype"))
assert plot_dd.text == "scatter"

# Checking that only the relevant options are shown
for plot_option in page.plot_args.keys():
plot_option_dd = dash_duo.find_element("#" + page.uuid(f"div-{plot_option}"))
if plot_option not in page.plots["scatter"]:
assert plot_option_dd.get_attribute("style") == "display: none;"
for plot_option in plugin.plot_args.keys():
if plot_option not in plugin.plots["scatter"]:
dash_duo.wait_for_style_to_equal(
"#" + plugin.uuid(f"div-{plot_option}"), style="display", val="none"
)

# Checking that options are initialized correctly
for option in ["x", "y"]:
plot_option_dd = dash_duo.find_element("#" + page.uuid(f"dropdown-{option}"))
plot_option_dd = dash_duo.find_element("#" + plugin.uuid(f"dropdown-{option}"))
assert plot_option_dd.text == "Well"


def test_table_plotter_filter(dash_duo):
def test_table_plotter_filter(webviz_single_plugin, dash_duo):

app = dash.Dash(__name__)
app.config.suppress_callback_exceptions = True
CACHE.init_app(app.server)
app.webviz_settings = {"theme": default_theme}
csv_file = "./tests/data/example_data.csv"
page = _table_plotter.TablePlotter(app, csv_file, filter_cols=["Well"])
app.layout = page.layout
dash_duo.start_server(app)
app = webviz_single_plugin.app

plugin = TablePlotter(app, csv_file=CSV_FILE, filter_cols=["Well"])
app.layout = plugin.layout

# Wait for the app to render(there is probably a better way...)
time.sleep(5)
dash_duo.start_server(app)

# Checking that no plot options are defined
assert page.plot_options == {}
assert plugin.plot_options == {}

# Check that filter is active
assert page.use_filter
assert page.filter_cols == ["Well"]
assert plugin.use_filter
assert plugin.filter_cols == ["Well"]

# Checking that the correct plot type is initialized
plot_dd = dash_duo.find_element("#" + page.uuid("plottype"))
plot_dd = dash_duo.find_element("#" + plugin.uuid("plottype"))
assert plot_dd.text == "scatter"

# Checking that only the relevant options are shown
for plot_option in page.plot_args.keys():
plot_option_dd = dash_duo.find_element("#" + page.uuid(f"div-{plot_option}"))
if plot_option not in page.plots["scatter"]:
assert "display: none;" in plot_option_dd.get_attribute("style")
for plot_option in plugin.plot_args.keys():
if plot_option not in plugin.plots["scatter"]:
dash_duo.wait_for_style_to_equal(
"#" + plugin.uuid(f"div-{plot_option}"), style="display", val="none"
)

# Checking that options are initialized correctly
for option in ["x", "y"]:
plot_option_dd = dash_duo.find_element("#" + page.uuid(f"dropdown-{option}"))
plot_option_dd = dash_duo.find_element("#" + plugin.uuid(f"dropdown-{option}"))
assert plot_option_dd.text == "Well"


def test_initialized_table_plotter(dash_duo):

app = dash.Dash(__name__)
app.css.config.serve_locally = True
app.scripts.config.serve_locally = True
app.config.suppress_callback_exceptions = True
CACHE.init_app(app.server)
app.webviz_settings = {"theme": default_theme}
csv_file = "./tests/data/example_data.csv"
plot_options = dict(
x="Well",
y="Initial reservoir pressure (bar)",
size="Average permeability (D)",
facet_col="Segment",
)

page = _table_plotter.TablePlotter(
app, csv_file, lock=True, plot_options=plot_options
)
app.layout = page.layout
dash_duo.start_server(app)
def test_initialized_table_plotter(webviz_single_plugin, dash_duo):

app = webviz_single_plugin.app

# Wait for the app to render(there is probably a better way...)
plot_options = {
"x": "Well",
"y": "Initial reservoir pressure (bar)",
"size": "Average permeability (D)",
"facet_col": "Segment",
}

plugin = TablePlotter(app, csv_file=CSV_FILE, lock=True, plot_options=plot_options)
app.layout = plugin.layout

dash_duo.start_server(app)

# Checking that plot options are defined
assert page.plot_options == plot_options
assert page.lock
assert plugin.plot_options == plot_options
assert plugin.lock

# Checking that the selectors are hidden
selector_row = dash_duo.find_element("#" + page.uuid("selector-row"))
selector_row = dash_duo.find_element("#" + plugin.uuid("selector-row"))
assert "display: none;" in selector_row.get_attribute("style")
26 changes: 26 additions & 0 deletions webviz_config/testing/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import dash
from dash_html_components import Div
import pytest

from webviz_config.common_cache import CACHE
from webviz_config.themes import default_theme


class WebvizSinglePlugin:
def __init__(self):
self._app = dash.Dash(__name__)
self._configure_app()

def _configure_app(self):
self._app.config.suppress_callback_exceptions = True
CACHE.init_app(self._app.server)
self._app.webviz_settings = {"theme": default_theme}

@property
def app(self):
return self._app


@pytest.fixture
def webviz_single_plugin():
return WebvizSinglePlugin()

0 comments on commit 636b3e2

Please sign in to comment.