test_consumer__on_datanode_created.py 8.6 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 DataNode, Gui
  14. from taipy.core.config import DataNodeConfig
  15. from taipy.core.notification import Event, EventEntityType, EventOperation
  16. from taipy.event.event_consumer import GuiEventConsumer
  17. def cb_0(event: Event, datanode: DataNode):
  18. ...
  19. def cb_1(event: Event, datanode: DataNode, extra:str):
  20. ...
  21. def cb_for_state(state, event: Event, datanode: DataNode):
  22. ...
  23. def test_on_datanode_created(data_node):
  24. consumer = GuiEventConsumer()
  25. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  26. consumer.on_datanode_created(callback=cb_0)
  27. # test the on_datanode_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.DATA_NODE,
  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.DATA_NODE, operation=EventOperation.CREATION, entity_id=data_node.id)
  37. assert actual_filter is not None
  38. with (mock.patch("taipy.get") as mck_get):
  39. mck_get.return_value = data_node
  40. filter_value = actual_filter(event)
  41. mck_get.assert_called_once_with(data_node.id)
  42. assert filter_value is True # No config provided, so the datanode passes the filter
  43. assert event.metadata["predefined_args"] == [data_node]
  44. def test_on_datanode_created_multiple_configs(data_node):
  45. consumer = GuiEventConsumer()
  46. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  47. consumer.on_datanode_created(callback=cb_0,
  48. datanode_config=[DataNodeConfig("dn0"), "dn1", DataNodeConfig("dn2"), "data_node"])
  49. # test the on_datanode_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.DATA_NODE,
  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.DATA_NODE, operation=EventOperation.CREATION, entity_id=data_node.id)
  59. assert actual_filter is not None
  60. with (mock.patch("taipy.get") as mck_get):
  61. mck_get.return_value = data_node
  62. filter_value = actual_filter(event)
  63. mck_get.assert_called_once_with(data_node.id)
  64. assert filter_value is True # The datanode is from config 'data_node', so the datanode passes the filter
  65. assert event.metadata["predefined_args"] == [data_node]
  66. def test_on_datanode_created_multiple_configs_no_matching(data_node):
  67. consumer = GuiEventConsumer()
  68. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  69. consumer.on_datanode_created(callback=cb_0,
  70. datanode_config=[DataNodeConfig("dn0"), "dn1"])
  71. # test the on_datanode_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.DATA_NODE,
  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.DATA_NODE, operation=EventOperation.CREATION,
  81. entity_id=data_node.id)
  82. assert actual_filter is not None
  83. with (mock.patch("taipy.get") as mck_get):
  84. mck_get.return_value = data_node
  85. f_val = actual_filter(event)
  86. mck_get.assert_called_once_with(data_node.id)
  87. assert not f_val # DataNode 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_datanode_created_with_args_and_matching_config(data_node):
  90. consumer = GuiEventConsumer()
  91. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  92. consumer.on_datanode_created(callback=cb_1, callback_args=["foo"], datanode_config="data_node")
  93. # test the on_datanode_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.DATA_NODE,
  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.DATA_NODE, operation=EventOperation.CREATION, entity_id=data_node.id)
  104. with (mock.patch("taipy.get") as mck_get):
  105. mck_get.return_value = data_node
  106. filter_value = actual_filter(event)
  107. mck_get.assert_called_once_with(data_node.id)
  108. assert filter_value is True # datanode is from config 'data_node', so the datanode passes the filter
  109. assert event.metadata.get("predefined_args") == [data_node]
  110. def test_on_datanode_created_with_args_and_not_matching_config(data_node):
  111. consumer = GuiEventConsumer()
  112. with mock.patch("taipy.event.event_consumer.GuiEventConsumer._GuiEventConsumer__on_event") as mck:
  113. consumer.on_datanode_created(callback=cb_1, callback_args=["foo"], datanode_config="WRONG_CFG")
  114. # test the on_datanode_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.DATA_NODE,
  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.DATA_NODE, operation=EventOperation.CREATION, entity_id=data_node.id)
  125. with (mock.patch("taipy.get") as mck_get):
  126. mck_get.return_value = data_node
  127. filter_value = actual_filter(event)
  128. mck_get.assert_called_once_with(data_node.id)
  129. assert filter_value is False # datanode 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 datanode in the metadata
  131. def test_on_datanode_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_datanode_created(callback=cb_for_state)
  135. mck.assert_called_once_with(callback=cb_for_state,
  136. callback_args=None,
  137. entity_type=EventEntityType.DATA_NODE,
  138. operation=EventOperation.CREATION,
  139. filter=ANY,
  140. broadcast=True)