|
| 1 | +# Copyright 2021-2025 Avaiga Private Limited |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +# the License. You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +# specific language governing permissions and limitations under the License. |
| 11 | +from unittest.mock import patch |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from taipy import Gui, Sequence, SequenceId |
| 16 | +from taipy.core.notification import Event, EventEntityType, EventOperation |
| 17 | +from taipy.gui_core._context import _GuiCoreContext |
| 18 | + |
| 19 | + |
| 20 | +class TestGuiCoreContextProcessSequenceEvent: |
| 21 | + |
| 22 | + @pytest.mark.parametrize("operation, sequence_is_valid", [(EventOperation.CREATION, True), |
| 23 | + (EventOperation.CREATION, False), |
| 24 | + (EventOperation.UPDATE, True), |
| 25 | + (EventOperation.UPDATE, False), |
| 26 | + (EventOperation.SUBMISSION, True), |
| 27 | + (EventOperation.SUBMISSION, False), |
| 28 | + ]) |
| 29 | + def test_sequence_event(self, operation, sequence_is_valid): |
| 30 | + seq_id = "sequence_id" |
| 31 | + seq_parent_ids = {"a_scenario_id", "s_id"} |
| 32 | + sequence = Sequence({}, [], SequenceId(seq_id), parent_ids=seq_parent_ids) |
| 33 | + event = Event(entity_type=EventEntityType.SEQUENCE, |
| 34 | + operation=operation, |
| 35 | + entity_id=seq_id) |
| 36 | + with patch("taipy.gui.gui.Gui._broadcast") as mock_broadcast: |
| 37 | + with patch("taipy.gui_core._context.is_readable") as mock_is_readable: |
| 38 | + with patch("taipy.gui_core._context.core_get") as mock_core_get: |
| 39 | + with patch("taipy.gui.gui.Gui._get_autorization") as mock_get_auth: |
| 40 | + mock_core_get.return_value = sequence |
| 41 | + mock_is_readable.return_value = sequence_is_valid |
| 42 | + gui_core_context = _GuiCoreContext(Gui()) |
| 43 | + gui_core_context.process_event(event=event) |
| 44 | + |
| 45 | + mock_get_auth.assert_called_once_with(system=True) |
| 46 | + if sequence_is_valid: |
| 47 | + mock_broadcast.assert_called_once_with("core_changed", {"scenario": list(seq_parent_ids)}) |
| 48 | + else: |
| 49 | + mock_broadcast.assert_not_called() |
| 50 | + |
| 51 | + @pytest.mark.parametrize("sequence_is_valid", [True, False]) |
| 52 | + def test_sequence_deletion(self, sequence_is_valid): |
| 53 | + event = Event(entity_type=EventEntityType.SEQUENCE, |
| 54 | + operation=EventOperation.DELETION, |
| 55 | + entity_id="sequence_id") |
| 56 | + |
| 57 | + with patch("taipy.gui.gui.Gui._broadcast") as mock_broadcast: |
| 58 | + with patch("taipy.gui_core._context.is_readable") as mock_is_readable: |
| 59 | + with patch("taipy.gui.gui.Gui._get_autorization") as mock_get_auth: |
| 60 | + mock_is_readable.return_value = sequence_is_valid |
| 61 | + gui_core_context = _GuiCoreContext(Gui()) |
| 62 | + gui_core_context.process_event(event=event) |
| 63 | + |
| 64 | + mock_get_auth.assert_called_once_with(system=True) |
| 65 | + mock_broadcast.assert_not_called() |
| 66 | + |
| 67 | + @pytest.mark.parametrize("operation", [EventOperation.CREATION, EventOperation.UPDATE, EventOperation.SUBMISSION]) |
| 68 | + def test_sequence_without_parent_ids(self, operation): |
| 69 | + seq_id = "sequence_id" |
| 70 | + seq_parent_ids = set() |
| 71 | + sequence = Sequence({}, [], SequenceId(seq_id), parent_ids=seq_parent_ids) |
| 72 | + event = Event(entity_type=EventEntityType.SEQUENCE, |
| 73 | + operation=operation, |
| 74 | + entity_id=seq_id) |
| 75 | + with patch("taipy.gui.gui.Gui._broadcast") as mock_broadcast: |
| 76 | + with patch("taipy.gui_core._context.is_readable") as mock_is_readable: |
| 77 | + with patch("taipy.gui_core._context.core_get") as mock_core_get: |
| 78 | + with patch("taipy.gui.gui.Gui._get_autorization") as mock_get_auth: |
| 79 | + mock_core_get.return_value = sequence |
| 80 | + mock_is_readable.return_value = True |
| 81 | + gui_core_context = _GuiCoreContext(Gui()) |
| 82 | + gui_core_context.process_event(event=event) |
| 83 | + |
| 84 | + mock_get_auth.assert_called_once_with(system=True) |
| 85 | + mock_broadcast.assert_not_called() |
0 commit comments