|
| 1 | +from typing import Any |
| 2 | +from unittest.mock import MagicMock, Mock |
| 3 | + |
| 4 | +import pytest |
| 5 | +from nicegui import ui |
| 6 | +from nicegui.testing import User |
| 7 | + |
| 8 | +from OTAnalytics.adapter_ui.view_model import ViewModel |
| 9 | +from OTAnalytics.application.resources.resource_manager import ResourceManager |
| 10 | +from OTAnalytics.application.state import FlowState |
| 11 | +from OTAnalytics.plugin_ui.nicegui_gui.pages.sections_and_flow_form.flow_form import ( |
| 12 | + MARKER_BUTTON_ADD, |
| 13 | + MARKER_BUTTON_GENERATE, |
| 14 | + MARKER_BUTTON_PROPERTIES, |
| 15 | + MARKER_BUTTON_REMOVE, |
| 16 | + MARKER_FLOW_TABLE, |
| 17 | + FlowForm, |
| 18 | +) |
| 19 | + |
| 20 | +# Constants for testing |
| 21 | +ENDPOINT_NAME = "/test-flow-form" |
| 22 | +FLOW_ID_1 = "flow-1" |
| 23 | +FLOW_ID_2 = "flow-2" |
| 24 | +FLOW_NAME_1 = "Flow 1" |
| 25 | +FLOW_NAME_2 = "Flow 2" |
| 26 | + |
| 27 | + |
| 28 | +class MockFlow: |
| 29 | + def __init__(self, flow_id: str, name: str) -> None: |
| 30 | + self.id = flow_id |
| 31 | + self.name = name |
| 32 | + |
| 33 | + def to_dict(self) -> dict[str, Any]: |
| 34 | + return {"id": self.id, "name": self.name} |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def viewmodel() -> Mock: |
| 39 | + viewmodel = MagicMock(spec=ViewModel) |
| 40 | + |
| 41 | + # Set up mock methods |
| 42 | + viewmodel.get_all_flows.return_value = [ |
| 43 | + MockFlow(FLOW_ID_1, FLOW_NAME_1), |
| 44 | + MockFlow(FLOW_ID_2, FLOW_NAME_2), |
| 45 | + ] |
| 46 | + |
| 47 | + return viewmodel |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +def flow_state() -> Mock: |
| 52 | + flow_state = MagicMock(spec=FlowState) |
| 53 | + |
| 54 | + # Set up selected_flows observable |
| 55 | + selected_flows_mock = MagicMock() |
| 56 | + selected_flows_mock.get.return_value = [] |
| 57 | + flow_state.selected_flows = selected_flows_mock |
| 58 | + |
| 59 | + return flow_state |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def flow_form( |
| 64 | + viewmodel: ViewModel, flow_state: FlowState, resource_manager: ResourceManager |
| 65 | +) -> FlowForm: |
| 66 | + return FlowForm(viewmodel, flow_state, resource_manager) |
| 67 | + |
| 68 | + |
| 69 | +class TestFlowForm: |
| 70 | + |
| 71 | + @pytest.mark.asyncio |
| 72 | + async def test_form_build_up( |
| 73 | + self, |
| 74 | + user: User, |
| 75 | + flow_form: FlowForm, |
| 76 | + resource_manager: ResourceManager, |
| 77 | + ) -> None: |
| 78 | + """Test that the form builds correctly and displays all buttons.""" |
| 79 | + |
| 80 | + @ui.page(ENDPOINT_NAME) |
| 81 | + def page() -> None: |
| 82 | + flow_form.build() |
| 83 | + |
| 84 | + await user.open(ENDPOINT_NAME) |
| 85 | + |
| 86 | + # Check that all buttons are visible |
| 87 | + await user.should_see(marker=MARKER_BUTTON_ADD) |
| 88 | + await user.should_see(marker=MARKER_BUTTON_GENERATE) |
| 89 | + await user.should_see(marker=MARKER_BUTTON_REMOVE) |
| 90 | + await user.should_see(marker=MARKER_BUTTON_PROPERTIES) |
| 91 | + |
| 92 | + # Just check that all buttons are visible, skip checking the table for now |
| 93 | + # The table is built, but we can't easily check its contents in this test |
| 94 | + |
| 95 | + @pytest.mark.asyncio |
| 96 | + async def test_generate_flow_button_calls_viewmodel( |
| 97 | + self, |
| 98 | + user: User, |
| 99 | + flow_form: FlowForm, |
| 100 | + viewmodel: Mock, |
| 101 | + resource_manager: ResourceManager, |
| 102 | + ) -> None: |
| 103 | + """Test that clicking the generate flow button calls the viewmodel method.""" |
| 104 | + |
| 105 | + @ui.page(ENDPOINT_NAME) |
| 106 | + def page() -> None: |
| 107 | + flow_form.build() |
| 108 | + |
| 109 | + await user.open(ENDPOINT_NAME) |
| 110 | + |
| 111 | + # Directly call the generate_flow method |
| 112 | + user.find(marker=MARKER_BUTTON_GENERATE).click() |
| 113 | + |
| 114 | + # Verify that the viewmodel method was called |
| 115 | + viewmodel.generate_flows.assert_called_once() |
| 116 | + |
| 117 | + @pytest.mark.asyncio |
| 118 | + async def test_remove_flow_button_calls_viewmodel( |
| 119 | + self, |
| 120 | + user: User, |
| 121 | + flow_form: FlowForm, |
| 122 | + viewmodel: Mock, |
| 123 | + resource_manager: ResourceManager, |
| 124 | + ) -> None: |
| 125 | + """Test that clicking the remove flow button calls the viewmodel method.""" |
| 126 | + |
| 127 | + @ui.page(ENDPOINT_NAME) |
| 128 | + def page() -> None: |
| 129 | + flow_form.build() |
| 130 | + |
| 131 | + await user.open(ENDPOINT_NAME) |
| 132 | + |
| 133 | + # Directly call the remove_flow method |
| 134 | + user.find(marker=MARKER_BUTTON_REMOVE).click() |
| 135 | + |
| 136 | + # Verify that the viewmodel method was called |
| 137 | + viewmodel.remove_flows.assert_called_once() |
| 138 | + |
| 139 | + @pytest.mark.asyncio |
| 140 | + async def test_update_items_displays_flows( |
| 141 | + self, |
| 142 | + user: User, |
| 143 | + flow_form: FlowForm, |
| 144 | + viewmodel: Mock, |
| 145 | + ) -> None: |
| 146 | + """Test that update_items displays the flows from the viewmodel.""" |
| 147 | + |
| 148 | + @ui.page(ENDPOINT_NAME) |
| 149 | + def page() -> None: |
| 150 | + flow_form.build() |
| 151 | + |
| 152 | + await user.open(ENDPOINT_NAME) |
| 153 | + |
| 154 | + # Verify that the table is displayed |
| 155 | + await user.should_see(marker=MARKER_FLOW_TABLE) |
| 156 | + |
| 157 | + # Verify that the flows are in the table's rows |
| 158 | + names = [row.get("name") for row in flow_form._flow_table._rows] |
| 159 | + assert names == [FLOW_NAME_1, FLOW_NAME_2] |
| 160 | + |
| 161 | + # Change the flows returned by the viewmodel |
| 162 | + new_flow = MockFlow("flow-3", "New Flow") |
| 163 | + viewmodel.get_all_flows.return_value = [new_flow] |
| 164 | + |
| 165 | + # Update the items |
| 166 | + flow_form.update_items() |
| 167 | + |
| 168 | + # Verify that the new flow is in the table's rows |
| 169 | + assert names == [FLOW_NAME_1, FLOW_NAME_2] |
0 commit comments