Skip to content

Commit a57e05c

Browse files
Create taipy.gui.test
1 parent b7a0fda commit a57e05c

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

taipy/gui/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,4 @@ def __getattr__(self, var_name: str) -> t.Any:
311311
)
312312

313313
def _invoke_on_gui(self, method: t.Callable, *args):
314-
return self.get_gui()._invoke_method(t.cast(str, self._get_placeholder("__state_id")), method, *args)
314+
return self.get_gui()._invoke_method(t.cast(str, self._get_placeholder("__state_id")), method, *args) # type: ignore[reportAttributeAccessIssue]

taipy/gui/mock/__init__.py renamed to taipy/gui/test/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
99
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
1010
# specific language governing permissions and limitations under the License.
11+
12+
from .mock_state import MockState

taipy/gui/mock/mock_state.py renamed to taipy/gui/test/mock_state.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515

1616

1717
class MockState(State):
18-
"""A Mock implementation for `State`.
19-
TODO
20-
example of use:
21-
```py
18+
"""A mock version of the `State^` class that enables isolated testing of state updates.
19+
20+
This class can be used with the *unittest* or *pytest* Python test frameworks to test side
21+
effects of changing the values of a state:
22+
23+
Ex:
24+
```python
2225
def test_callback():
23-
ms = MockState(Gui(""), a = 1)
24-
on_action(ms) # function to test
25-
assert ms.a == 2
26+
mock_state = MockState(Gui(""), a = 1)
27+
on_action(mock_state) # function to test
28+
assert mock_state.a == 2
2629
```
2730
"""
2831

@@ -60,3 +63,6 @@ def __exit__(self, exc_type, exc_value, traceback):
6063

6164
def broadcast(self, name: str, value: t.Any):
6265
pass
66+
67+
def _invoke_on_gui(self, method: t.Callable, *args):
68+
return method(self.get_gui(), *args)

tests/gui/mock/test_mock_state.py renamed to tests/gui/test/test_mock_state.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from unittest.mock import Mock
1313

1414
from taipy.gui import Gui, State
15-
from taipy.gui.mock.mock_state import MockState
15+
from taipy.gui.test.mock_state import MockState
1616
from taipy.gui.utils import _MapDict
1717

1818

@@ -46,6 +46,7 @@ def test_write_attr():
4646
ms.a += 1
4747
assert ms.a == 3
4848

49+
4950
def test_dict():
5051
ms = MockState(Gui(""))
5152
a_dict = {"a": 1}
@@ -61,13 +62,15 @@ def test_write_context():
6162
ms["page"].b = 3
6263
assert ms["page"].b == 3
6364

65+
6466
def test_assign():
6567
ms = MockState(Gui(""), a=1)
6668
ms.assign("a", 2)
6769
assert ms.a == 2
6870
ms.assign("b", 1)
6971
assert ms.b == 1
7072

73+
7174
def test_refresh():
7275
ms = MockState(Gui(""), a=1)
7376
ms.refresh("a")
@@ -76,23 +79,27 @@ def test_refresh():
7679
ms.refresh("a")
7780
assert ms.a == 2
7881

82+
7983
def test_context_manager():
8084
with MockState(Gui(""), a=1) as ms:
8185
assert ms is not None
8286
ms.a = 2
8387
assert ms.a == 2
8488

89+
8590
def test_broadcast():
8691
ms = MockState(Gui(""), a=1)
8792
ms.broadcast("a", 2)
8893

94+
8995
def test_set_favicon():
9096
gui = Gui("")
9197
gui.set_favicon = Mock()
9298
ms = MockState(gui, a=1)
9399
ms.set_favicon("a_path")
94100
gui.set_favicon.assert_called_once()
95101

102+
96103
def test_callback():
97104
def on_action(state: State):
98105
state.assign("a", 2)
@@ -101,6 +108,7 @@ def on_action(state: State):
101108
on_action(ms)
102109
assert ms.a == 2
103110

111+
104112
def test_false():
105113
ms = MockState(Gui(""), a=False)
106114
assert ms.a is False

tests/gui_core/test_context_crud_scenario.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from taipy import Scope
1616
from taipy.core import DataNode, Scenario
1717
from taipy.core.data.pickle import PickleDataNode
18-
from taipy.gui.mock.mock_state import MockState
18+
from taipy.gui.test.mock_state import MockState
1919
from taipy.gui_core._context import _GuiCoreContext
2020

2121
scenario_a = Scenario("scenario_a_config_id", None, {"a_prop": "a"})
@@ -35,9 +35,11 @@ def mock_core_get(entity_id):
3535
return datanode_b
3636
return None
3737

38+
3839
def mock_is_true(entity_id):
3940
return True
4041

42+
4143
class TestGuiCoreContext_crud_scenario:
4244
def test_crud_scenario_delete(self):
4345
gui_core_context = _GuiCoreContext(Mock())
@@ -48,7 +50,7 @@ def test_crud_scenario_delete(self):
4850
with (
4951
patch("taipy.gui_core._context.core_get", side_effect=mock_core_get),
5052
patch("taipy.gui_core._context.is_deletable", side_effect=mock_is_true),
51-
patch("taipy.gui_core._context.core_delete", side_effect=mock_core_delete)
53+
patch("taipy.gui_core._context.core_delete", side_effect=mock_core_delete),
5254
):
5355
gui_core_context.crud_scenario(
5456
state,

0 commit comments

Comments
 (0)