test_consumer__on_scenario_deleted.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 import mock
  12. from unittest.mock import ANY
  13. from taipy import Gui
  14. from taipy.core.config import ScenarioConfig
  15. from taipy.core.notification import Event, EventEntityType, EventOperation
  16. from taipy.event.event_consumer import GuiEventConsumer
  17. def cb_0(event: Event, scenario: str):
  18. ...
  19. def cb_1(event: Event, scenario: str, extra:str):
  20. ...
  21. def cb_for_state(state, event: Event, scenario: str):
  22. ...
  23. def test_on_scenario_deleted(scenario):
  24. consumer = GuiEventConsumer()
  25. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  26. consumer.on_scenario_deleted(callback=cb_0)
  27. # test the on_scenario_deleted method delegates to on_event with the correct parameters
  28. mck.assert_called_once_with(callback=cb_0,
  29. callback_args=None,
  30. entity_type=EventEntityType.SCENARIO,
  31. operation=EventOperation.DELETION,
  32. filter=ANY,
  33. broadcast=False)
  34. # check the filter method is correct
  35. actual_filter = mck.call_args.kwargs["filter"]
  36. event = Event(entity_type=EventEntityType.SCENARIO, operation=EventOperation.DELETION, entity_id=scenario.id)
  37. assert actual_filter is not None
  38. filter_value = actual_filter(event)
  39. assert filter_value is True # No config provided, so the scenario passes the filter
  40. assert event.metadata["predefined_args"] == [scenario.id]
  41. def test_on_scenario_deleted_multiple_configs(scenario):
  42. consumer = GuiEventConsumer()
  43. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  44. consumer.on_scenario_deleted(callback=cb_0,
  45. scenario_config=[ScenarioConfig("sc_0"), "sc_1", ScenarioConfig("sc_2"), "sc"])
  46. # test the on_scenario_deleted method delegates to on_event with the correct parameters
  47. mck.assert_called_once_with(callback=cb_0,
  48. callback_args=None,
  49. entity_type=EventEntityType.SCENARIO,
  50. operation=EventOperation.DELETION,
  51. filter=ANY,
  52. broadcast=False)
  53. # check the filter method is correct
  54. actual_filter = mck.call_args.kwargs["filter"]
  55. event = Event(entity_type=EventEntityType.SCENARIO, operation=EventOperation.DELETION, entity_id=scenario.id)
  56. assert actual_filter is not None
  57. filter_value = actual_filter(event)
  58. assert filter_value is True # The scenario is from config 'sc', so the scenario passes the filter
  59. assert event.metadata["predefined_args"] == [scenario.id]
  60. def test_on_scenario_deleted_multiple_configs_no_matching(scenario):
  61. consumer = GuiEventConsumer()
  62. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  63. consumer.on_scenario_deleted(callback=cb_0,
  64. scenario_config=[ScenarioConfig("sc_0"), "sc_1"])
  65. # test the on_scenario_deleted method delegates to on_event with the correct parameters
  66. mck.assert_called_once_with(callback=cb_0,
  67. callback_args=None,
  68. entity_type=EventEntityType.SCENARIO,
  69. operation=EventOperation.DELETION,
  70. filter=ANY,
  71. broadcast=False)
  72. # check the filter method is correct
  73. actual_filter = mck.call_args.kwargs["filter"]
  74. event = Event(entity_type=EventEntityType.SCENARIO, operation=EventOperation.DELETION,
  75. entity_id=scenario.id)
  76. assert actual_filter is not None
  77. f_val = actual_filter(event)
  78. assert not f_val # Scenario is not from any of the provided configs, so it should not pass the filter
  79. assert event.metadata.get("predefined_args") is None
  80. def test_on_scenario_deleted_with_args_and_matching_config(scenario):
  81. consumer = GuiEventConsumer()
  82. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  83. consumer.on_scenario_deleted(callback=cb_1, callback_args=["foo"], scenario_config="sc")
  84. # test the on_scenario_deleted method delegates to on_event with the correct parameters
  85. mck.assert_called_once_with(callback=cb_1,
  86. callback_args=["foo"],
  87. entity_type=EventEntityType.SCENARIO,
  88. operation=EventOperation.DELETION,
  89. filter=ANY,
  90. broadcast=False)
  91. # check the filter method is correct
  92. actual_filter = mck.call_args.kwargs["filter"]
  93. assert actual_filter is not None
  94. event = Event(entity_type=EventEntityType.SCENARIO, operation=EventOperation.DELETION, entity_id=scenario.id)
  95. filter_value = actual_filter(event)
  96. assert filter_value is True # scenario is from config 'sc', so the scenario passes the filter
  97. assert event.metadata.get("predefined_args") == [scenario.id]
  98. def test_on_scenario_deleted_with_args_and_not_matching_config(scenario):
  99. consumer = GuiEventConsumer()
  100. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  101. consumer.on_scenario_deleted(callback=cb_1, callback_args=["foo"], scenario_config="WRONG_CFG")
  102. # test the on_scenario_deleted method delegates to on_event with the correct parameters
  103. mck.assert_called_once_with(callback=cb_1,
  104. callback_args=["foo"],
  105. entity_type=EventEntityType.SCENARIO,
  106. operation=EventOperation.DELETION,
  107. filter=ANY,
  108. broadcast=False)
  109. # check the filter method is correct
  110. actual_filter = mck.call_args.kwargs["filter"]
  111. assert actual_filter is not None
  112. event = Event(entity_type=EventEntityType.SCENARIO, operation=EventOperation.DELETION, entity_id=scenario.id)
  113. filter_value = actual_filter(event)
  114. assert filter_value is False # scenario is not from WRONG_CFG, so it should not pass the filter
  115. assert event.metadata.get("predefined_args") is None # No need to cache the scenario in the metadata
  116. def test_on_scenario_deleted_with_broadcast(scenario):
  117. consumer = GuiEventConsumer(Gui())
  118. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  119. consumer.broadcast_on_scenario_deleted(callback=cb_for_state)
  120. mck.assert_called_once_with(callback=cb_for_state,
  121. callback_args=None,
  122. entity_type=EventEntityType.SCENARIO,
  123. operation=EventOperation.DELETION,
  124. filter=ANY,
  125. broadcast=True)