Skip to content

Commit

Permalink
Fix #1413: Update and split dash_duo tests (#1417)
Browse files Browse the repository at this point in the history
* update and split dash_duo tests

* separate app setup into a function.

* start tests for favourite addresses autoload.

* adds more tests to selectors.

* changed tables height

---------

Co-authored-by: Calina Cenan <[email protected]>
Co-authored-by: Norbert <[email protected]>
  • Loading branch information
3 people authored Jul 19, 2024
1 parent e8a50a2 commit 28fe75b
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def get_table(table_id, table_name, searchable_field, columns, data, default_sor
sort_action="native", # Enables data to be sorted
style_cell={"textAlign": "left"},
style_table={
"height": "34vh",
"height": "30vh",
"width": "100%",
"overflow": "auto",
"paddingTop": "5px",
Expand Down
31 changes: 19 additions & 12 deletions pdr_backend/analytics/predictoor_dashboard/predictoor_dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,37 @@
)
from pdr_backend.ppss.ppss import PPSS

app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.config["suppress_callback_exceptions"] = True
app.layout = get_layout()
get_callbacks(app)


@enforce_types
def predictoor_dash(ppss: PPSS, debug_mode: bool):
port = 8050
if not debug_mode:
webbrowser.open(f"http://127.0.0.1:{port}/")
folder = ppss.lake_ss.lake_dir
app.lake_dir = folder

app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.config["suppress_callback_exceptions"] = True
app.layout = get_layout()

try:
app.feeds_data = get_feeds_data_from_db(folder)
app.predictoors_data = get_predictoors_data_from_payouts(
get_user_payouts_stats_from_db(folder)
)
setup_app(app, ppss)
except Exception as e:
print(
f"""ERROR: Please make sure there is data in the lake.
Full error: {e}"""
)
return

app.favourite_addresses = ppss.predictoor_ss.my_addresses
get_callbacks(app)
app.run(debug=debug_mode, port=port)


@enforce_types
def setup_app(app, ppss: PPSS):
app.lake_dir = ppss.lake_ss.lake_dir
app.feeds_data = get_feeds_data_from_db(ppss.lake_ss.lake_dir)
app.predictoors_data = get_predictoors_data_from_payouts(
get_user_payouts_stats_from_db(ppss.lake_ss.lake_dir)
)
app.favourite_addresses = ppss.predictoor_ss.my_addresses

return app
112 changes: 112 additions & 0 deletions pdr_backend/analytics/predictoor_dashboard/test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
import pytest
import dash_bootstrap_components as dbc

from dash import Dash
from selenium.webdriver.chrome.options import Options

from pdr_backend.lake.payout import mock_payouts, mock_payouts_related_with_predictions
from pdr_backend.lake.prediction import mock_daily_predictions, mock_first_predictions

from pdr_backend.analytics.predictoor_dashboard.dash_components.callbacks import (
get_callbacks,
)
from pdr_backend.analytics.predictoor_dashboard.dash_components.view_elements import (
get_layout,
)
from pdr_backend.analytics.predictoor_dashboard.test.resources import (
_prepare_test_db,
_clear_test_db,
)
from pdr_backend.analytics.predictoor_dashboard.predictoor_dash import (
setup_app as setup_app_main,
)
from pdr_backend.lake.payout import Payout
from pdr_backend.lake.prediction import Prediction


@pytest.fixture()
def _sample_first_predictions():
Expand Down Expand Up @@ -41,4 +60,97 @@ def pytest_setup_options():
options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")

return options


def _add_css(app):
style_css = ""
# read the styles.css file and add it to the assets folder
# Read the styles.css file
with open("pdr_backend/analytics/predictoor_dashboard/assets/styles.css", "r") as f:
style_css = f.read()

# Manually link the CSS file by embedding its contents in a <style> tag
app.index_string = f"""
<!DOCTYPE html>
<html>
<head>
{{%metas%}}
<title>{{%title%}}</title>
{{%favicon%}}
{{%css%}}
<style>{style_css}</style>
</head>
<body>
{{%app_entry%}}
<footer>
{{%config%}}
{{%scripts%}}
{{%renderer%}}
</footer>
</body>
</html>
"""

return app


@pytest.fixture
def setup_app(
tmpdir, _sample_daily_predictions, _sample_payouts_related_with_predictions
):
_clear_test_db(str(tmpdir))

_prepare_test_db(
tmpdir,
_sample_payouts_related_with_predictions,
table_name=Payout.get_lake_table_name(),
)

ppss, _ = _prepare_test_db(
tmpdir,
_sample_daily_predictions,
table_name=Prediction.get_lake_table_name(),
)

app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.config["suppress_callback_exceptions"] = True

app = _add_css(app)
app.layout = get_layout()

setup_app_main(app, ppss)
get_callbacks(app)

return app


@pytest.fixture
def setup_app_with_favourite_addresses(
tmpdir, _sample_daily_predictions, _sample_payouts_related_with_predictions
):
_clear_test_db(str(tmpdir))

_prepare_test_db(
tmpdir,
_sample_payouts_related_with_predictions,
table_name=Payout.get_lake_table_name(),
)

ppss, _ = _prepare_test_db(
tmpdir,
_sample_daily_predictions,
table_name=Prediction.get_lake_table_name(),
my_addresses=["0x7149ceca72c61991018ed80788bea3f3f4540c3c"],
)

app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.config["suppress_callback_exceptions"] = True

app = _add_css(app)
app.layout = get_layout()
setup_app_main(app, ppss)
get_callbacks(app)

return app
32 changes: 31 additions & 1 deletion pdr_backend/analytics/predictoor_dashboard/test/resources.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import time

from enforce_typing import enforce_types
from selenium.webdriver.common.keys import Keys

from pdr_backend.lake.duckdb_data_store import DuckDBDataStore
from pdr_backend.ppss.ppss import mock_ppss
Expand All @@ -7,11 +10,12 @@
from pdr_backend.lake.payout import Payout


def _prepare_test_db(tmpdir, sample_data, table_name):
def _prepare_test_db(tmpdir, sample_data, table_name, my_addresses=None):
ppss = mock_ppss(
[{"predict": "binance BTC/USDT c 5m", "train_on": "binance BTC/USDT c 5m"}],
"sapphire-mainnet",
str(tmpdir),
my_addresses=my_addresses if my_addresses else [],
)

db = DuckDBDataStore(str(ppss.lake_ss.lake_dir))
Expand All @@ -33,3 +37,29 @@ def _clear_test_db(directory: str):
db.drop_table(Payout.get_lake_table_name())
db.drop_table(Prediction.get_lake_table_name())
db.duckdb_conn.close()


def _input_action(dash_duo, input_id, table_id, input_value, expected_rows):
"""
Helper function to test the search input in the tables
It sends the input_value to the search input and checks
if the number of rows in the table is as expected
"""

search_input = dash_duo.find_element(input_id)
search_input.clear()
search_input.send_keys(input_value + Keys.ENTER)
time.sleep(2)
assert len(dash_duo.find_elements(f"{table_id} tbody tr")) == expected_rows


def start_server_and_wait(dash_duo, app):
"""
Start the server and wait for the elements to be rendered.
"""

dash_duo.start_server(app)
dash_duo.wait_for_element("#feeds_table")
dash_duo.wait_for_element("#predictoors_table")
dash_duo.wait_for_element("#feeds_table tbody tr")
dash_duo.wait_for_element("#predictoors_table tbody tr")
Loading

0 comments on commit 28fe75b

Please sign in to comment.