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 authored and anders-kiaer committed Aug 16, 2021
1 parent 0a9a093 commit d08ddff
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ webviz_config/_docs/static/*.js
webviz_config/_docs/static/*.css
!webviz_config/_docs/static/webviz-doc.js
!webviz_config/_docs/static/webviz-doc.css
geckodriver.log
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def get_long_description() -> str:
"TablePlotter = webviz_config.generic_plugins._table_plotter:TablePlotter",
"PivotTable = webviz_config.generic_plugins._pivot_table:PivotTable",
],
"pytest11": ["webviz = webviz_config.testing.plugin"],
},
install_requires=[
"bleach>=3.1",
Expand Down
66 changes: 41 additions & 25 deletions tests/test_table_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,31 @@ def test_table_plotter(dash_duo: DashComposite) -> None:
app.layout = page.layout
dash_duo.start_server(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.find_element("#" + plugin.uuid(f"div-{plot_option}"))
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"


Expand All @@ -58,28 +62,30 @@ def test_table_plotter_filter(dash_duo: DashComposite) -> None:
app.layout = page.layout
dash_duo.start_server(app)

# 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.find_element("#" + plugin.uuid(f"div-{plot_option}"))
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"


Expand All @@ -105,12 +111,22 @@ def test_initialized_table_plotter(dash_duo: DashComposite) -> None:
app.layout = page.layout
dash_duo.start_server(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")
Empty file.
52 changes: 52 additions & 0 deletions webviz_config/testing/_monitor_builtin_open.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import io
import site
import inspect
import importlib
import warnings
from pathlib import Path

WHITELISTED_FILENAMES = ["geckodriver.log", ".pytest-sugar.conf"]
WHITELISTED_PREFIXES = ["/tmp"] + site.PREFIXES
WHITELISTED_POSTFIXES = [str(Path(".plotly" / Path(".config"))), ".py", ".json"]
WHITELISTED_CONTAINS = [
".pytest_cache",
"__pycache__",
".cache",
".egg-info",
"/dev/null",
]

import builtins

FUNCTIONS = [
("builtins", "open", "filepath"),
("pandas", "read_csv", "filepath"),
("xtgeo", "Well", "self"),
]


class MonitorBuiltinOpen:
def __init__(self):
self._original_functions = [
getattr(importlib.import_module(module), function)
for module, function, _ in FUNCTIONS
]

def stop_monitoring(self):
pass
# builtins.open = self._original_open
# io.open = self._original_open

def start_monitoring(self):
pass
# def wrapped_open(*args, **kwargs):
# path = str(args[0])
# if Path(path).name not in WHITELISTED_FILENAMES and str(Path(path).parent) != "." and all([part not in path for part in WHITELISTED_CONTAINS]) and all([not path.startswith(prefix) for prefix in WHITELISTED_PREFIXES]) and all([not path.endswith(postfix) for postfix in WHITELISTED_POSTFIXES]):
# raise RuntimeError(f"File {path} opened, which is not white-listed as a 'portable' location.")
# return self._original_open(*args, **kwargs)

# builtins.open = wrapped_open
# io.open = wrapped_open


monitor_builtin_open = MonitorBuiltinOpen()
31 changes: 31 additions & 0 deletions webviz_config/testing/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest

import dash
from dash_html_components import Div

from ._monitor_builtin_open import monitor_builtin_open
from ..common_cache import CACHE
from ..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}

def check_portability(self):
monitor_builtin_open.start_monitoring()

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


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

0 comments on commit d08ddff

Please sign in to comment.