Skip to content

Commit

Permalink
refactor(dialogs): move execution dialogs to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
aarthy-dk committed Sep 30, 2024
1 parent 15b3941 commit 2048bba
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 163 deletions.
File renamed without changes.
80 changes: 80 additions & 0 deletions testgen/ui/views/dialogs/generate_tests_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import time
import pandas as pd
import streamlit as st

import testgen.ui.services.test_suite_service as test_suite_service
from testgen.commands.run_generate_tests import run_test_gen_queries
from testgen.ui.components import widgets as testgen

ALL_TYPES_LABEL = "All Test Types"


@st.dialog(title="Generate Tests")
def generate_tests_dialog(test_suite: pd.Series) -> None:
test_suite_id = test_suite["id"]
test_suite_name = test_suite["test_suite"]
table_group_id = test_suite["table_groups_id"]

selected_set = ""
generation_sets = test_suite_service.get_generation_set_choices()

if generation_sets:
generation_sets.insert(0, ALL_TYPES_LABEL)

with st.container():
selected_set = st.selectbox("Generation Set", generation_sets)
if selected_set == ALL_TYPES_LABEL:
selected_set = ""

test_ct, unlocked_test_ct, unlocked_edits_ct = test_suite_service.get_test_suite_refresh_warning(test_suite_id)
if test_ct:
unlocked_message = ""
if unlocked_edits_ct > 0:
unlocked_message = "Manual changes have been made to auto-generated tests in this test suite that have not been locked. "
elif unlocked_test_ct > 0:
unlocked_message = "Auto-generated tests are present in this test suite that have not been locked. "

warning_message = f"""
{unlocked_message}
Generating tests now will overwrite unlocked tests subject to auto-generation based on the latest profiling.
\n\n_Auto-generated Tests: {test_ct}, Unlocked: {unlocked_test_ct}, Edited Unlocked: {unlocked_edits_ct}_
"""

with st.container():
st.warning(warning_message, icon=":material/warning:")
if unlocked_edits_ct > 0:
if st.button("Lock Edited Tests"):
if test_suite_service.lock_edited_tests(test_suite_id):
st.info("Edited tests have been successfully locked.")

with st.container():
st.markdown(f"Execute test generation for the test suite **{test_suite_name}**?")

if testgen.expander_toggle(expand_label="Show CLI command", key="test_suite:keys:generate-tests-show-cli"):
st.code(
f"testgen run-test-generation --table-group-id {table_group_id} --test-suite-key {test_suite_name}",
language="shellSession",
)

button_container = st.empty()
status_container = st.empty()

test_generation_button = None
with button_container:
_, button_column = st.columns([.75, .25])
with button_column:
test_generation_button = st.button("Generate Tests", use_container_width=True)

if test_generation_button:
button_container.empty()
status_container.info("Starting test generation ...")

try:
run_test_gen_queries(table_group_id, test_suite_name, selected_set)
except Exception as e:
status_container.error(f"Test generation encountered errors: {e!s}.")

status_container.success(f"Test generation completed for test suite **{test_suite_name}**.")
time.sleep(1)
st.cache_data.clear()
st.rerun()
File renamed without changes.
84 changes: 84 additions & 0 deletions testgen/ui/views/dialogs/run_profiling_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import time

import pandas as pd
import streamlit as st

import testgen.ui.services.query_service as dq
from testgen.commands.run_profiling_bridge import run_profiling_in_background
from testgen.ui.components import widgets as testgen
from testgen.ui.session import session

LINK_KEY = "run_profiling_dialog:keys:go-to-runs"
LINK_HREF = "profiling-runs"


@st.dialog(title="Run Profiling")
def run_profiling_dialog(project_code: str, table_group: pd.Series | None = None, default_table_group_id: str | None = None) -> None:
if table_group is not None and not table_group.empty:
table_group_id: str = table_group["id"]
table_group_name: str = table_group["table_groups_name"]
else:
table_groups_df = get_table_group_options(project_code)
table_group_id: str = testgen.select(
label="Table Group",
options=table_groups_df,
value_column="id",
display_column="table_groups_name",
default_value=default_table_group_id,
required=True,
)
table_group_name: str = table_groups_df.loc[table_groups_df["id"] == table_group_id, "table_groups_name"].iloc[0]
testgen.whitespace(1)

with st.container():
st.markdown(f"Execute profiling for the table group **{table_group_name}**?")
st.markdown(":material/info: _Profiling will be performed in a background process._")

if testgen.expander_toggle(expand_label="Show CLI command", key="test_suite:keys:run-tests-show-cli"):
st.code(f"testgen run-profile --table-group-id {table_group_id}", language="shellSession")

button_container = st.empty()
status_container = st.empty()

with button_container:
_, button_column = st.columns([.85, .15])
with button_column:
profile_button = st.button("Run Profiling", use_container_width=True, disabled=not table_group_id)

if profile_button:
button_container.empty()
status_container.info("Starting profiling run ...")

try:
run_profiling_in_background(table_group_id)
except Exception as e:
status_container.error(f"Profiling run encountered errors: {e!s}.")

# The second condition is needed for the link to work
if profile_button or st.session_state.get(LINK_KEY):
with status_container.container():
st.success(
f"Profiling run started for table group **{table_group_name}**."
)

if session.current_page != LINK_HREF:
testgen.link(
label="Go to Profiling Runs",
href=LINK_HREF,
params={ "table_group": table_group_id },
right_icon="chevron_right",
underline=False,
height=40,
key=LINK_KEY,
style="margin-left: auto; border-radius: 4px; border: var(--button-stroked-border); padding: 8px 8px 8px 16px; color: var(--primary-color)",
)
else:
time.sleep(1)
st.cache_data.clear()
st.rerun()


@st.cache_data(show_spinner=False)
def get_table_group_options(project_code: str) -> pd.DataFrame:
schema: str = st.session_state["dbschema"]
return dq.run_table_groups_lookup_query(schema, project_code)
94 changes: 94 additions & 0 deletions testgen/ui/views/dialogs/run_tests_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import time
import pandas as pd
import streamlit as st

import testgen.ui.services.database_service as db
from testgen.commands.run_execute_tests import run_execution_steps_in_background
from testgen.ui.components import widgets as testgen
from testgen.ui.session import session

LINK_KEY = "run_tests_dialog:keys:go-to-runs"
LINK_HREF = "test-runs"


@st.dialog(title="Run Tests")
def run_tests_dialog(project_code: str, test_suite: pd.Series | None = None, default_test_suite_id: str | None = None) -> None:
if test_suite is not None and not test_suite.empty:
test_suite_id: str = test_suite["id"]
test_suite_name: str = test_suite["test_suite"]
else:
test_suites_df = get_test_suite_options(project_code)
test_suite_id: str = testgen.select(
label="Test Suite",
options=test_suites_df,
value_column="id",
display_column="test_suite",
default_value=default_test_suite_id,
required=True,
)
test_suite_name: str = test_suites_df.loc[test_suites_df["id"] == test_suite_id, "test_suite"].iloc[0]
testgen.whitespace(1)

with st.container():
st.markdown(f"Run tests for the test suite **{test_suite_name}**?")
st.markdown(":material/info: _Test execution will be performed in a background process._")

if testgen.expander_toggle(expand_label="Show CLI command", key="run_tests_dialog:keys:show-cli"):
st.code(
f"testgen run-tests --project-key {project_code} --test-suite-key {test_suite['test_suite']}",
language="shellSession"
)

button_container = st.empty()
status_container = st.empty()

run_test_button = None
with button_container:
_, button_column = st.columns([.8, .2])
with button_column:
run_test_button = st.button("Run Tests", use_container_width=True)

if run_test_button:
button_container.empty()
status_container.info("Starting test run ...")

try:
run_execution_steps_in_background(project_code, test_suite_name)
except Exception as e:
status_container.error(f"Test run encountered errors: {e!s}.")

# The second condition is needed for the link to work
if run_test_button or st.session_state.get(LINK_KEY):
with status_container.container():
st.success(
f"Test run started for test suite **{test_suite_name}**."
)

if session.current_page != LINK_HREF:
testgen.link(
label="Go to Test Runs",
href=LINK_HREF,
params={ "test_suite": test_suite_id },
right_icon="chevron_right",
underline=False,
height=40,
key=LINK_KEY,
style="margin-left: auto; border-radius: 4px; border: var(--button-stroked-border); padding: 8px 8px 8px 16px; color: var(--primary-color)",
)
else:
time.sleep(1)
st.cache_data.clear()
st.rerun()


@st.cache_data(show_spinner=False)
def get_test_suite_options(project_code: str) -> pd.DataFrame:
schema: str = st.session_state["dbschema"]
sql = f"""
SELECT test_suites.id::VARCHAR(50),
test_suites.test_suite
FROM {schema}.test_suites
WHERE test_suites.project_code = '{project_code}'
ORDER BY test_suites.test_suite
"""
return db.retrieve_data(sql)
2 changes: 1 addition & 1 deletion testgen/ui/views/profiling_anomalies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from testgen.ui.navigation.page import Page
from testgen.ui.services import project_service
from testgen.ui.session import session
from testgen.ui.views.profiling_modal import view_profiling_button
from testgen.ui.views.dialogs.profiling_results_dialog import view_profiling_button


class ProfilingAnomaliesPage(Page):
Expand Down
2 changes: 1 addition & 1 deletion testgen/ui/views/project_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from testgen.ui.navigation.page import Page
from testgen.ui.services import form_service, project_service
from testgen.ui.session import session
from testgen.ui.views.app_log_modal import view_log_file
from testgen.ui.views.dialogs.application_logs_dialog import view_log_file


class ProjectSettingsPage(Page):
Expand Down
42 changes: 2 additions & 40 deletions testgen/ui/views/table_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import testgen.ui.services.connection_service as connection_service
import testgen.ui.services.form_service as fm
import testgen.ui.services.table_group_service as table_group_service
from testgen.commands.run_profiling_bridge import run_profiling_in_background
from testgen.ui.components import widgets as testgen
from testgen.ui.navigation.page import Page
from testgen.ui.services import project_service
from testgen.ui.services.string_service import empty_if_null
from testgen.ui.session import session
from testgen.ui.views.dialogs.run_profiling_dialog import run_profiling_dialog


class TableGroupsPage(Page):
Expand Down Expand Up @@ -114,7 +114,7 @@ def render(self, connection_id: str, **_kwargs) -> None:
testgen.button(
type_="stroked",
label="Run Profiling",
on_click=partial(run_profiling_dialog, table_group),
on_click=partial(run_profiling_dialog, project_code, table_group),
key=f"tablegroups:keys:runprofiling:{table_group['id']}",
)

Expand Down Expand Up @@ -172,44 +172,6 @@ def delete_table_group_dialog(self, table_group: pd.Series):
st.rerun()


@st.dialog(title="Run Profiling")
def run_profiling_dialog(table_group: pd.Series) -> None:
table_group_id = table_group["id"]

with st.container():
st.markdown(
f"Execute profiling for the Table Group :green[{table_group['table_groups_name']}]?"
" Profiling will be performed in a background process"
)

if testgen.expander_toggle(expand_label="Show CLI command", key="test_suite:keys:run-tests-show-cli"):
st.code(f"testgen run-profile --table-group-id {table_group_id}", language="shellSession")

button_container = st.empty()
status_container = st.empty()

with button_container:
_, button_column = st.columns([.85, .15])
with button_column:
profile_button = st.button("Start", use_container_width=True)

if profile_button:
button_container.empty()

status_container.info("Executing Profiling...")

try:
run_profiling_in_background(table_group_id)
except Exception as e:
status_container.empty()
status_container.error(f"Process started with errors: {e!s}.")

status_container.empty()
status_container.success(
"Process has successfully started. Check 'Data Profiling' item in the menu to see the progress."
)


def show_table_group_form(mode, project_code: str, connection: dict, table_group: pd.Series | None = None):
connection_id = connection["connection_id"]
table_groups_settings_tab, table_groups_preview_tab = st.tabs(["Table Group Settings", "Test"])
Expand Down
2 changes: 1 addition & 1 deletion testgen/ui/views/test_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from testgen.ui.services import authentication_service, project_service
from testgen.ui.services.string_service import empty_if_null, snake_case_to_title_case
from testgen.ui.session import session
from testgen.ui.views.profiling_modal import view_profiling_button
from testgen.ui.views.dialogs.profiling_results_dialog import view_profiling_button

LOG = logging.getLogger("testgen")

Expand Down
2 changes: 1 addition & 1 deletion testgen/ui/views/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from testgen.ui.services import authentication_service, project_service
from testgen.ui.services.string_service import empty_if_null
from testgen.ui.session import session
from testgen.ui.views.profiling_modal import view_profiling_button
from testgen.ui.views.dialogs.profiling_results_dialog import view_profiling_button
from testgen.ui.views.test_definitions import show_test_form_by_id

ALWAYS_SPIN = False
Expand Down
Loading

0 comments on commit 2048bba

Please sign in to comment.