Skip to content

Commit 566f662

Browse files
committed
Improved testing
1 parent a109eb7 commit 566f662

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

src/tdamapper/app.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -653,40 +653,49 @@ def load_file(self):
653653
logger.warning("No data found. Please upload a file first.")
654654
ui.notify("No data found. Please upload a file first.", type="warning")
655655

656-
async def async_run_mapper(self):
656+
def notification_running_start(self, message):
657657
notification = ui.notification(timeout=None, type="ongoing")
658-
notification.message = "Running Mapper..."
658+
notification.message = message
659659
notification.spinner = True
660+
return notification
661+
662+
def notification_running_stop(self, notification, message, type=None):
663+
if type is not None:
664+
notification.type = type
665+
notification.message = message
666+
notification.timeout = 5.0
667+
notification.spinner = False
668+
669+
async def async_run_mapper(self):
670+
notification = self.notification_running_start("Running Mapper...")
660671
df_X = self.storage.get("df", pd.DataFrame())
661672
if df_X is None or df_X.empty:
662-
logger.warning("No data found. Please upload a file first.")
663-
ui.notify("No data found. Please upload a file first.", type="warning")
664-
notification.message = "No data found. Please upload a file first."
665-
notification.spinner = False
666-
notification.dismiss()
673+
logger.warning("No data found. Please load data first.")
674+
self.notification_running_stop(
675+
notification,
676+
"Running Mapper failed: No data found, please load data first.",
677+
type="warning",
678+
)
667679
return
668680
mapper_config = self.get_mapper_config()
669681
result = await run.cpu_bound(run_mapper, df_X, **asdict(mapper_config))
670682
if result is None:
671-
notification.message = "Something went wrong."
672-
notification.spinner = False
673-
notification.dismiss()
683+
self.notification_running_stop(
684+
notification, "Running Mapper failed.", type="error"
685+
)
674686
return
675687
mapper_graph, df_y = result
676688
if mapper_graph is not None:
677689
self.storage["mapper_graph"] = mapper_graph
678690
if df_y is not None:
679691
self.storage["df_y"] = df_y
680-
notification.message = "Running Mapper Completed!"
681-
notification.spinner = False
682-
notification.dismiss()
683-
692+
self.notification_running_stop(
693+
notification, "Running Mapper completed.", type="positive"
694+
)
684695
await self.async_draw_mapper()
685696

686697
async def async_draw_mapper(self):
687-
notification = ui.notification(timeout=None, type="ongoing")
688-
notification.message = "Drawing Mapper..."
689-
notification.spinner = True
698+
notification = self.notification_running_start("Drawing Mapper...")
690699

691700
mapper_config = self.get_mapper_config()
692701

@@ -697,13 +706,11 @@ async def async_draw_mapper(self):
697706

698707
if df_X.empty or mapper_graph is None:
699708
logger.warning("No data or Mapper graph found. Please run Mapper first.")
700-
ui.notify(
701-
"No data or Mapper graph found. Please run Mapper first.",
709+
self.notification_running_stop(
710+
notification=notification,
711+
message="Drawing Mapper failed: no data or graph found.",
702712
type="warning",
703713
)
704-
notification.message = "No data or Mapper graph found."
705-
notification.spinner = False
706-
notification.dismiss()
707714
return
708715

709716
mapper_fig = await run.cpu_bound(
@@ -714,17 +721,21 @@ async def async_draw_mapper(self):
714721
mapper_graph,
715722
**asdict(mapper_config),
716723
)
724+
if mapper_fig is None:
725+
self.notification_running_stop(
726+
notification, "Drawing Mapper failed.", type="error"
727+
)
728+
return
717729

718730
logger.info("Displaying Mapper plot.")
719731
if self.draw_area is not None:
720732
self.draw_area.clear()
721733
self.plot_container.clear()
722734
with self.plot_container:
723735
self.draw_area = ui.plotly(mapper_fig).classes("w-full h-full")
724-
725-
notification.message = "Drawing Mapper Completed!"
726-
notification.spinner = False
727-
notification.dismiss()
736+
self.notification_running_stop(
737+
notification, "Drawing Mapper completed", type="positive"
738+
)
728739

729740

730741
def startup():

tests/test_unit_app.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
from dataclasses import asdict
23

34
from nicegui.testing import User
@@ -10,7 +11,21 @@
1011
pytest_plugins = ["nicegui.testing.user_plugin"]
1112

1213

13-
async def test_run_app(user: User) -> None:
14+
async def test_run_app_fail(user: User) -> None:
15+
app.startup()
16+
await user.open("/")
17+
await user.should_see("Load Data")
18+
await user.should_see("Lens")
19+
await user.should_see("Cover")
20+
await user.should_see("Clustering")
21+
await user.should_see("Run Mapper")
22+
await user.should_see("Redraw")
23+
user.find("Run Mapper").click()
24+
await user.should_see("Running Mapper failed")
25+
await user.should_not_see("File loaded successfully")
26+
27+
28+
async def test_run_app_success(user: User) -> None:
1429
app.startup()
1530
await user.open("/")
1631
await user.should_see("Load Data")
@@ -25,14 +40,14 @@ async def test_run_app(user: User) -> None:
2540
user.find("Run Mapper").click()
2641
await user.should_see("Running Mapper...")
2742
await user.should_not_see("No data found.")
28-
await user.should_see("Running Mapper Completed!", retries=RETRIES)
43+
await user.should_see("Running Mapper completed", retries=RETRIES)
2944
await user.should_see("Drawing Mapper...")
3045
await user.should_not_see("No data")
31-
await user.should_see("Drawing Mapper Completed!", retries=RETRIES)
46+
await user.should_see("Drawing Mapper completed", retries=RETRIES)
3247
user.find("Redraw").click()
3348
await user.should_see("Drawing Mapper...")
3449
await user.should_not_see("No data")
35-
await user.should_see("Drawing Mapper Completed!", retries=RETRIES)
50+
await user.should_see("Drawing Mapper completed", retries=RETRIES)
3651

3752

3853
def test_run_mapper() -> None:

0 commit comments

Comments
 (0)