test_topic.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. # Copyright 2021-2024 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. import pytest
  12. from taipy.core.exceptions.exceptions import InvalidEventOperation
  13. from taipy.core.notification._topic import _Topic
  14. from taipy.core.notification.event import EventEntityType, EventOperation
  15. def test_general_topic_creation():
  16. topic_1 = _Topic(None, None, None, None)
  17. assert topic_1.entity_type is None
  18. assert topic_1.entity_id is None
  19. assert topic_1.operation is None
  20. assert topic_1.attribute_name is None
  21. topic_2 = _Topic(EventEntityType.SCENARIO, "scenario_id")
  22. assert topic_2.entity_type == EventEntityType.SCENARIO
  23. assert topic_2.entity_id == "scenario_id"
  24. assert topic_2.operation is None
  25. assert topic_2.attribute_name is None
  26. topic_3 = _Topic(None, None, EventOperation.CREATION)
  27. assert topic_3.entity_type is None
  28. assert topic_3.entity_id is None
  29. assert topic_3.operation == EventOperation.CREATION
  30. assert topic_3.attribute_name is None
  31. topic_4 = _Topic(None, None, EventOperation.UPDATE, "properties")
  32. assert topic_4.entity_type is None
  33. assert topic_4.entity_id is None
  34. assert topic_4.operation == EventOperation.UPDATE
  35. assert topic_4.attribute_name == "properties"
  36. topic_5 = _Topic(entity_type=EventEntityType.JOB, operation=EventOperation.DELETION)
  37. assert topic_5.entity_type == EventEntityType.JOB
  38. assert topic_5.entity_id is None
  39. assert topic_5.operation == EventOperation.DELETION
  40. assert topic_5.attribute_name is None
  41. topic_6 = _Topic(entity_type=EventEntityType.SEQUENCE)
  42. assert topic_6.entity_type == EventEntityType.SEQUENCE
  43. assert topic_6.entity_id is None
  44. assert topic_6.operation is None
  45. assert topic_6.attribute_name is None
  46. def test_topic_creation_cycle():
  47. topic_1 = _Topic(EventEntityType.CYCLE, "cycle_id", EventOperation.CREATION)
  48. assert topic_1.entity_type == EventEntityType.CYCLE
  49. assert topic_1.entity_id == "cycle_id"
  50. assert topic_1.operation == EventOperation.CREATION
  51. assert topic_1.attribute_name is None
  52. topic_2 = _Topic(EventEntityType.CYCLE, "cycle_id", EventOperation.UPDATE, "frequency")
  53. assert topic_2.entity_type == EventEntityType.CYCLE
  54. assert topic_2.entity_id == "cycle_id"
  55. assert topic_2.operation == EventOperation.UPDATE
  56. assert topic_2.attribute_name == "frequency"
  57. topic_3 = _Topic(EventEntityType.CYCLE, "cycle_id", EventOperation.DELETION)
  58. assert topic_3.entity_type == EventEntityType.CYCLE
  59. assert topic_3.entity_id == "cycle_id"
  60. assert topic_3.operation == EventOperation.DELETION
  61. assert topic_3.attribute_name is None
  62. topic_4 = _Topic(EventEntityType.CYCLE, "cycle_id", EventOperation.CREATION)
  63. assert topic_4.entity_type == EventEntityType.CYCLE
  64. assert topic_4.entity_id == "cycle_id"
  65. assert topic_4.operation == EventOperation.CREATION
  66. assert topic_4.attribute_name is None
  67. # with pytest.raises(InvalidEventAttributeName):
  68. # _ = Topic(EventEntityType.CYCLE, "cycle_id", EventOperation.CREATION, "frequency")
  69. # with pytest.raises(InvalidEventAttributeName):
  70. # _ = Topic(EventEntityType.CYCLE, "cycle_id", EventOperation.DELETION, "frequency")
  71. # with pytest.raises(InvalidEventAttributeName):
  72. # _ = Topic(EventEntityType.CYCLE, "cycle_id", attribute_name="frequency")
  73. with pytest.raises(InvalidEventOperation):
  74. _ = _Topic(EventEntityType.CYCLE, "cycle_id", EventOperation.SUBMISSION)
  75. with pytest.raises(InvalidEventOperation):
  76. _ = _Topic(EventEntityType.CYCLE, "cycle_id", EventOperation.SUBMISSION, "frequency")
  77. def test_topic_creation_scenario():
  78. topic_1 = _Topic(EventEntityType.SCENARIO, "scenario_id", EventOperation.CREATION)
  79. assert topic_1.entity_type == EventEntityType.SCENARIO
  80. assert topic_1.entity_id == "scenario_id"
  81. assert topic_1.operation == EventOperation.CREATION
  82. assert topic_1.attribute_name is None
  83. topic_2 = _Topic(EventEntityType.SCENARIO, "scenario_id", EventOperation.UPDATE, "is_primary")
  84. assert topic_2.entity_type == EventEntityType.SCENARIO
  85. assert topic_2.entity_id == "scenario_id"
  86. assert topic_2.operation == EventOperation.UPDATE
  87. assert topic_2.attribute_name == "is_primary"
  88. topic_3 = _Topic(EventEntityType.SCENARIO, "scenario_id", EventOperation.DELETION)
  89. assert topic_3.entity_type == EventEntityType.SCENARIO
  90. assert topic_3.entity_id == "scenario_id"
  91. assert topic_3.operation == EventOperation.DELETION
  92. assert topic_3.attribute_name is None
  93. topic_4 = _Topic(EventEntityType.SCENARIO, "scenario_id", EventOperation.SUBMISSION)
  94. assert topic_4.entity_type == EventEntityType.SCENARIO
  95. assert topic_4.entity_id == "scenario_id"
  96. assert topic_4.operation == EventOperation.SUBMISSION
  97. assert topic_4.attribute_name is None
  98. topic_5 = _Topic(EventEntityType.SCENARIO, "scenario_id", EventOperation.UPDATE, "properties")
  99. assert topic_5.entity_type == EventEntityType.SCENARIO
  100. assert topic_5.entity_id == "scenario_id"
  101. assert topic_5.operation == EventOperation.UPDATE
  102. assert topic_5.attribute_name == "properties"
  103. # with pytest.raises(InvalidEventAttributeName):
  104. # _ = Topic(EventEntityType.SCENARIO, "scenario_id", EventOperation.CREATION, "is_primary")
  105. # with pytest.raises(InvalidEventAttributeName):
  106. # _ = Topic(EventEntityType.SCENARIO, "scenario_id", EventOperation.DELETION, "is_primary")
  107. # with pytest.raises(InvalidEventAttributeName):
  108. # _ = Topic(EventEntityType.SCENARIO, "scenario_id", EventOperation.SUBMISSION, "is_primary")
  109. # with pytest.raises(InvalidEventAttributeName):
  110. # _ = Topic(EventEntityType.SCENARIO, "scenario_id", attribute_name="is_primary")
  111. def test_topic_creation_sequence():
  112. topic_1 = _Topic(EventEntityType.SEQUENCE, "sequence_id", EventOperation.CREATION)
  113. assert topic_1.entity_type == EventEntityType.SEQUENCE
  114. assert topic_1.entity_id == "sequence_id"
  115. assert topic_1.operation == EventOperation.CREATION
  116. assert topic_1.attribute_name is None
  117. topic_2 = _Topic(EventEntityType.SEQUENCE, "sequence_id", EventOperation.UPDATE, "subscribers")
  118. assert topic_2.entity_type == EventEntityType.SEQUENCE
  119. assert topic_2.entity_id == "sequence_id"
  120. assert topic_2.operation == EventOperation.UPDATE
  121. assert topic_2.attribute_name == "subscribers"
  122. topic_3 = _Topic(EventEntityType.SEQUENCE, "sequence_id", EventOperation.DELETION)
  123. assert topic_3.entity_type == EventEntityType.SEQUENCE
  124. assert topic_3.entity_id == "sequence_id"
  125. assert topic_3.operation == EventOperation.DELETION
  126. assert topic_3.attribute_name is None
  127. topic_4 = _Topic(EventEntityType.SEQUENCE, "sequence_id", EventOperation.SUBMISSION)
  128. assert topic_4.entity_type == EventEntityType.SEQUENCE
  129. assert topic_4.entity_id == "sequence_id"
  130. assert topic_4.operation == EventOperation.SUBMISSION
  131. assert topic_4.attribute_name is None
  132. topic_5 = _Topic(EventEntityType.SEQUENCE, "sequence_id", EventOperation.DELETION)
  133. assert topic_5.entity_type == EventEntityType.SEQUENCE
  134. assert topic_5.entity_id == "sequence_id"
  135. assert topic_5.operation == EventOperation.DELETION
  136. assert topic_5.attribute_name is None
  137. # with pytest.raises(InvalidEventAttributeName):
  138. # _ = Topic(EventEntityType.SEQUENCE, "sequence_id", EventOperation.CREATION, "subscribers")
  139. # with pytest.raises(InvalidEventAttributeName):
  140. # _ = Topic(EventEntityType.SEQUENCE, "sequence_id", EventOperation.DELETION, "subscribers")
  141. # with pytest.raises(InvalidEventAttributeName):
  142. # _ = Topic(EventEntityType.SEQUENCE, "sequence_id", EventOperation.SUBMISSION, "subscribers")
  143. # with pytest.raises(InvalidEventAttributeName):
  144. # _ = Topic(EventEntityType.SEQUENCE, "sequence_id", attribute_name="subscribers")
  145. def test_topic_creation_task():
  146. topic_1 = _Topic(EventEntityType.TASK, "task_id", EventOperation.CREATION)
  147. assert topic_1.entity_type == EventEntityType.TASK
  148. assert topic_1.entity_id == "task_id"
  149. assert topic_1.operation == EventOperation.CREATION
  150. assert topic_1.attribute_name is None
  151. topic_2 = _Topic(EventEntityType.TASK, "task_id", EventOperation.UPDATE, "function")
  152. assert topic_2.entity_type == EventEntityType.TASK
  153. assert topic_2.entity_id == "task_id"
  154. assert topic_2.operation == EventOperation.UPDATE
  155. assert topic_2.attribute_name == "function"
  156. topic_3 = _Topic(EventEntityType.TASK, "task_id", EventOperation.DELETION)
  157. assert topic_3.entity_type == EventEntityType.TASK
  158. assert topic_3.entity_id == "task_id"
  159. assert topic_3.operation == EventOperation.DELETION
  160. assert topic_3.attribute_name is None
  161. topic_4 = _Topic(EventEntityType.TASK, "task_id", EventOperation.SUBMISSION)
  162. assert topic_4.entity_type == EventEntityType.TASK
  163. assert topic_4.entity_id == "task_id"
  164. assert topic_4.operation == EventOperation.SUBMISSION
  165. assert topic_4.attribute_name is None
  166. topic_5 = _Topic(EventEntityType.TASK, "task_id", EventOperation.SUBMISSION)
  167. assert topic_5.entity_type == EventEntityType.TASK
  168. assert topic_5.entity_id == "task_id"
  169. assert topic_5.operation == EventOperation.SUBMISSION
  170. assert topic_5.attribute_name is None
  171. # with pytest.raises(InvalidEventAttributeName):
  172. # _ = Topic(EventEntityType.TASK, "task_id", EventOperation.CREATION, "function")
  173. # with pytest.raises(InvalidEventAttributeName):
  174. # _ = Topic(EventEntityType.TASK, "task_id", EventOperation.DELETION, "function")
  175. # with pytest.raises(InvalidEventAttributeName):
  176. # _ = Topic(EventEntityType.TASK, "task_id", EventOperation.SUBMISSION, "function")
  177. # with pytest.raises(InvalidEventAttributeName):
  178. # _ = Topic(EventEntityType.TASK, "task_id", attribute_name="function")
  179. def test_topic_creation_datanode():
  180. topic_1 = _Topic(EventEntityType.DATA_NODE, "dn_id", EventOperation.CREATION)
  181. assert topic_1.entity_type == EventEntityType.DATA_NODE
  182. assert topic_1.entity_id == "dn_id"
  183. assert topic_1.operation == EventOperation.CREATION
  184. assert topic_1.attribute_name is None
  185. topic_2 = _Topic(EventEntityType.DATA_NODE, "dn_id", EventOperation.UPDATE, "properties")
  186. assert topic_2.entity_type == EventEntityType.DATA_NODE
  187. assert topic_2.entity_id == "dn_id"
  188. assert topic_2.operation == EventOperation.UPDATE
  189. assert topic_2.attribute_name == "properties"
  190. topic_3 = _Topic(EventEntityType.DATA_NODE, "dn_id", EventOperation.DELETION)
  191. assert topic_3.entity_type == EventEntityType.DATA_NODE
  192. assert topic_3.entity_id == "dn_id"
  193. assert topic_3.operation == EventOperation.DELETION
  194. assert topic_3.attribute_name is None
  195. topic_4 = _Topic(None, "dn_id", EventOperation.UPDATE, "scope")
  196. assert topic_4.entity_type is None
  197. assert topic_4.entity_id == "dn_id"
  198. assert topic_4.operation == EventOperation.UPDATE
  199. assert topic_4.attribute_name == "scope"
  200. # with pytest.raises(InvalidEventAttributeName):
  201. # _ = Topic(EventEntityType.DATA_NODE, "dn_id", EventOperation.CREATION, "properties")
  202. # with pytest.raises(InvalidEventAttributeName):
  203. # _ = Topic(EventEntityType.DATA_NODE, "dn_id", EventOperation.DELETION, "properties")
  204. # with pytest.raises(InvalidEventAttributeName):
  205. # _ = Topic(EventEntityType.DATA_NODE, "dn_id", attribute_name="properties")
  206. with pytest.raises(InvalidEventOperation):
  207. _ = _Topic(EventEntityType.DATA_NODE, "dn_id", EventOperation.SUBMISSION)
  208. # with pytest.raises(InvalidEventOperation):
  209. # _ = Topic(EventEntityType.DATA_NODE, "dn_id", EventOperation.SUBMISSION, "properties")
  210. def test_topic_creation_job():
  211. topic_1 = _Topic(EventEntityType.JOB, "job_id", EventOperation.CREATION)
  212. assert topic_1.entity_type == EventEntityType.JOB
  213. assert topic_1.entity_id == "job_id"
  214. assert topic_1.operation == EventOperation.CREATION
  215. assert topic_1.attribute_name is None
  216. topic_2 = _Topic(EventEntityType.JOB, "job_id", EventOperation.UPDATE, "force")
  217. assert topic_2.entity_type == EventEntityType.JOB
  218. assert topic_2.entity_id == "job_id"
  219. assert topic_2.operation == EventOperation.UPDATE
  220. assert topic_2.attribute_name == "force"
  221. topic_3 = _Topic(EventEntityType.JOB, "job_id", EventOperation.DELETION)
  222. assert topic_3.entity_type == EventEntityType.JOB
  223. assert topic_3.entity_id == "job_id"
  224. assert topic_3.operation == EventOperation.DELETION
  225. assert topic_3.attribute_name is None
  226. topic_4 = _Topic(EventEntityType.JOB, "job_id", EventOperation.CREATION)
  227. assert topic_4.entity_type == EventEntityType.JOB
  228. assert topic_4.entity_id == "job_id"
  229. assert topic_4.operation == EventOperation.CREATION
  230. assert topic_4.attribute_name is None
  231. # with pytest.raises(InvalidEventAttributeName):
  232. # _ = Topic(EventEntityType.JOB, "job_id", EventOperation.CREATION, "force")
  233. # with pytest.raises(InvalidEventAttributeName):
  234. # _ = Topic(EventEntityType.JOB, "job_id", EventOperation.DELETION, "force")
  235. # with pytest.raises(InvalidEventAttributeName):
  236. # _ = Topic(EventEntityType.JOB, "job_id", attribute_name="force")
  237. with pytest.raises(InvalidEventOperation):
  238. _ = _Topic(EventEntityType.JOB, "job_id", EventOperation.SUBMISSION)
  239. # with pytest.raises(InvalidEventOperation):
  240. # _ = Topic(EventEntityType.JOB, "job_id", EventOperation.SUBMISSION, "force")
  241. def test_topic_equal():
  242. assert _Topic() == _Topic()
  243. assert _Topic(EventEntityType.SCENARIO) == _Topic(EventEntityType.SCENARIO)
  244. assert _Topic(entity_id="sequence_id") == _Topic(entity_id="sequence_id")
  245. assert _Topic(operation=EventOperation.SUBMISSION) == _Topic(operation=EventOperation.SUBMISSION)
  246. assert _Topic(EventEntityType.JOB, "JOB_id", EventOperation.UPDATE, "status") == _Topic(
  247. EventEntityType.JOB, "JOB_id", EventOperation.UPDATE, "status"
  248. )
  249. def test_print():
  250. topic = _Topic(EventEntityType.TASK, "task_id", EventOperation.UPDATE, "foo")
  251. assert "TASK" in str(topic)
  252. assert "task_id" in str(topic)
  253. assert "UPDATE" in str(topic)
  254. assert "foo" in str(topic)
  255. topic_2 = _Topic(EventEntityType.SCENARIO, None, EventOperation.CREATION, None)
  256. assert "SCENARIO" in str(topic_2)
  257. assert "CREATION" in str(topic_2)