test_consumer__on_scenario_created.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, Scenario
  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: Scenario):
  18. ...
  19. def cb_1(event: Event, scenario: Scenario, extra:str):
  20. ...
  21. def cb_for_state(state, event: Event, scenario: Scenario):
  22. ...
  23. def test_on_scenario_created(scenario):
  24. consumer = GuiEventConsumer()
  25. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  26. consumer.on_scenario_created(callback=cb_0)
  27. # test the on_scenario_created 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.CREATION,
  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.CREATION, entity_id=scenario.id)
  37. assert actual_filter is not None
  38. with (mock.patch("taipy.get") as mck_get):
  39. mck_get.return_value = scenario
  40. filter_value = actual_filter(event)
  41. mck_get.assert_called_once_with(scenario.id)
  42. assert filter_value is True # No config provided, so the scenario passes the filter
  43. assert event.metadata["predefined_args"] == [scenario]
  44. def test_on_scenario_created_multiple_configs(scenario):
  45. consumer = GuiEventConsumer()
  46. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  47. consumer.on_scenario_created(callback=cb_0,
  48. scenario_config=[ScenarioConfig("sc_0"), "sc_1", ScenarioConfig("sc_2"), "sc"])
  49. # test the on_scenario_created method delegates to on_event with the correct parameters
  50. mck.assert_called_once_with(callback=cb_0,
  51. callback_args=None,
  52. entity_type=EventEntityType.SCENARIO,
  53. operation=EventOperation.CREATION,
  54. filter=ANY,
  55. broadcast=False)
  56. # check the filter method is correct
  57. actual_filter = mck.call_args.kwargs["filter"]
  58. event = Event(entity_type=EventEntityType.SCENARIO, operation=EventOperation.CREATION, entity_id=scenario.id)
  59. assert actual_filter is not None
  60. with (mock.patch("taipy.get") as mck_get):
  61. mck_get.return_value = scenario
  62. filter_value = actual_filter(event)
  63. mck_get.assert_called_once_with(scenario.id)
  64. assert filter_value is True # The scenario is from config 'sc', so the scenario passes the filter
  65. assert event.metadata["predefined_args"] == [scenario]
  66. def test_on_scenario_created_multiple_configs_no_matching(scenario):
  67. consumer = GuiEventConsumer()
  68. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  69. consumer.on_scenario_created(callback=cb_0,
  70. scenario_config=[ScenarioConfig("sc_0"), "sc_1"])
  71. # test the on_scenario_created method delegates to on_event with the correct parameters
  72. mck.assert_called_once_with(callback=cb_0,
  73. callback_args=None,
  74. entity_type=EventEntityType.SCENARIO,
  75. operation=EventOperation.CREATION,
  76. filter=ANY,
  77. broadcast=False)
  78. # check the filter method is correct
  79. actual_filter = mck.call_args.kwargs["filter"]
  80. event = Event(entity_type=EventEntityType.SCENARIO, operation=EventOperation.CREATION,
  81. entity_id=scenario.id)
  82. assert actual_filter is not None
  83. with (mock.patch("taipy.get") as mck_get):
  84. mck_get.return_value = scenario
  85. f_val = actual_filter(event)
  86. mck_get.assert_called_once_with(scenario.id)
  87. assert not f_val # Scenario is not from any of the provided configs, so it should not pass the filter
  88. assert event.metadata.get("predefined_args") is None
  89. def test_on_scenario_created_with_args_and_matching_config(scenario):
  90. consumer = GuiEventConsumer()
  91. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  92. consumer.on_scenario_created(callback=cb_1, callback_args=["foo"], scenario_config="sc")
  93. # test the on_scenario_created method delegates to on_event with the correct parameters
  94. mck.assert_called_once_with(callback=cb_1,
  95. callback_args=["foo"],
  96. entity_type=EventEntityType.SCENARIO,
  97. operation=EventOperation.CREATION,
  98. filter=ANY,
  99. broadcast=False)
  100. # check the filter method is correct
  101. actual_filter = mck.call_args.kwargs["filter"]
  102. assert actual_filter is not None
  103. event = Event(entity_type=EventEntityType.SCENARIO, operation=EventOperation.CREATION, entity_id=scenario.id)
  104. with (mock.patch("taipy.get") as mck_get):
  105. mck_get.return_value = scenario
  106. filter_value = actual_filter(event)
  107. mck_get.assert_called_once_with(scenario.id)
  108. assert filter_value is True # scenario is from config 'sc', so the scenario passes the filter
  109. assert event.metadata.get("predefined_args") == [scenario]
  110. def test_on_scenario_created_with_args_and_not_matching_config(scenario):
  111. consumer = GuiEventConsumer()
  112. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  113. consumer.on_scenario_created(callback=cb_1, callback_args=["foo"], scenario_config="WRONG_CFG")
  114. # test the on_scenario_created method delegates to on_event with the correct parameters
  115. mck.assert_called_once_with(callback=cb_1,
  116. callback_args=["foo"],
  117. entity_type=EventEntityType.SCENARIO,
  118. operation=EventOperation.CREATION,
  119. filter=ANY,
  120. broadcast=False)
  121. # check the filter method is correct
  122. actual_filter = mck.call_args.kwargs["filter"]
  123. assert actual_filter is not None
  124. event = Event(entity_type=EventEntityType.SCENARIO, operation=EventOperation.CREATION, entity_id=scenario.id)
  125. with (mock.patch("taipy.get") as mck_get):
  126. mck_get.return_value = scenario
  127. filter_value = actual_filter(event)
  128. mck_get.assert_called_once_with(scenario.id)
  129. assert filter_value is False # scenario is not from WRONG_CFG, so it should not pass the filter
  130. assert event.metadata.get("predefined_args") is None # No need to cache the scenario in the metadata
  131. def test_on_scenario_created_with_broadcast():
  132. consumer = GuiEventConsumer(Gui())
  133. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  134. consumer.broadcast_on_scenario_created(callback=cb_for_state)
  135. mck.assert_called_once_with(callback=cb_for_state,
  136. callback_args=None,
  137. entity_type=EventEntityType.SCENARIO,
  138. operation=EventOperation.CREATION,
  139. filter=ANY,
  140. broadcast=True)