test_cycle.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 datetime
  12. from datetime import timedelta
  13. import pytest
  14. from taipy.config.common.frequency import Frequency
  15. from taipy.core import CycleId
  16. from taipy.core.cycle._cycle_manager import _CycleManager
  17. from taipy.core.cycle._cycle_manager_factory import _CycleManagerFactory
  18. from taipy.core.cycle.cycle import Cycle
  19. from taipy.core.exceptions import AttributeKeyAlreadyExisted
  20. from taipy.core.task.task import Task
  21. def test_cycle_equals(cycle):
  22. cycle_manager = _CycleManagerFactory()._build_manager()
  23. cycle_id = cycle.id
  24. cycle_manager._set(cycle)
  25. # To test if instance is same type
  26. task = Task("task", {}, print, [], [], cycle_id)
  27. cycle_2 = cycle_manager._get(cycle_id)
  28. assert cycle == cycle_2
  29. assert cycle != cycle_id
  30. assert cycle != task
  31. def test_create_cycle_entity(current_datetime):
  32. cycle_1 = Cycle(
  33. Frequency.DAILY,
  34. {"key": "value"},
  35. creation_date=current_datetime,
  36. start_date=current_datetime,
  37. end_date=current_datetime,
  38. name="foo",
  39. )
  40. assert cycle_1.id is not None
  41. assert cycle_1.name == "foo"
  42. assert cycle_1.properties == {"key": "value"}
  43. assert cycle_1.creation_date == current_datetime
  44. assert cycle_1.start_date == current_datetime
  45. assert cycle_1.end_date == current_datetime
  46. assert cycle_1.key == "value"
  47. assert cycle_1.frequency == Frequency.DAILY
  48. cycle_2 = Cycle(Frequency.YEARLY, {}, current_datetime, current_datetime, current_datetime)
  49. assert cycle_2.name == current_datetime.strftime("%Y")
  50. assert cycle_2.frequency == Frequency.YEARLY
  51. cycle_3 = Cycle(Frequency.MONTHLY, {}, current_datetime, current_datetime, current_datetime)
  52. assert cycle_3.name == current_datetime.strftime("%B %Y")
  53. assert cycle_3.frequency == Frequency.MONTHLY
  54. cycle_4 = Cycle(Frequency.WEEKLY, {}, current_datetime, current_datetime, current_datetime)
  55. assert cycle_4.name == current_datetime.strftime("Week %W %Y, from %d. %B")
  56. assert cycle_4.frequency == Frequency.WEEKLY
  57. cycle_5 = Cycle(Frequency.DAILY, {}, current_datetime, current_datetime, current_datetime)
  58. assert cycle_5.name == current_datetime.strftime("%A, %d. %B %Y")
  59. assert cycle_5.frequency == Frequency.DAILY
  60. def test_cycle_name(current_datetime):
  61. start_date = datetime.datetime(2023, 1, 2)
  62. cycle = Cycle(Frequency.DAILY, {}, current_datetime, start_date, start_date, "name", CycleId("id"))
  63. assert cycle.name == "name"
  64. cycle = Cycle(Frequency.DAILY, {}, current_datetime, start_date, start_date, None, CycleId("id"))
  65. assert cycle.name == "Monday, 02. January 2023"
  66. cycle = Cycle(Frequency.WEEKLY, {}, current_datetime, start_date, start_date, None, CycleId("id"))
  67. assert cycle.name == "Week 01 2023, from 02. January"
  68. cycle = Cycle(Frequency.MONTHLY, {}, current_datetime, start_date, start_date, None, CycleId("id"))
  69. assert cycle.name == "January 2023"
  70. cycle = Cycle(Frequency.QUARTERLY, {}, current_datetime, start_date, start_date, None, CycleId("id"))
  71. assert cycle.name == "2023 Q1"
  72. cycle = Cycle(Frequency.YEARLY, {}, current_datetime, start_date, start_date, None, CycleId("id"))
  73. assert cycle.name == "2023"
  74. def test_cycle_label(current_datetime):
  75. cycle = Cycle(
  76. Frequency.DAILY,
  77. {"key": "value"},
  78. creation_date=current_datetime,
  79. start_date=current_datetime,
  80. end_date=current_datetime,
  81. )
  82. assert cycle.get_label() == cycle.name
  83. assert cycle.get_simple_label() == cycle.name
  84. cycle._properties["label"] = "label"
  85. assert cycle.get_label() == "label"
  86. assert cycle.get_simple_label() == "label"
  87. def test_add_property_to_scenario(current_datetime):
  88. cycle = Cycle(
  89. Frequency.WEEKLY,
  90. {"key": "value"},
  91. current_datetime,
  92. current_datetime,
  93. current_datetime,
  94. name="foo",
  95. )
  96. assert cycle.properties == {"key": "value"}
  97. assert cycle.key == "value"
  98. cycle.properties["new_key"] = "new_value"
  99. assert cycle.properties == {"key": "value", "new_key": "new_value"}
  100. assert cycle.key == "value"
  101. assert cycle.new_key == "new_value"
  102. def test_get_set_property_and_attribute(current_datetime):
  103. cycle_manager = _CycleManagerFactory()._build_manager()
  104. cycle = Cycle(
  105. Frequency.WEEKLY,
  106. {"key": "value"},
  107. current_datetime,
  108. current_datetime,
  109. current_datetime,
  110. name="foo",
  111. )
  112. cycle_manager._set(cycle)
  113. assert cycle.properties == {"key": "value"}
  114. cycle.properties["new_key"] = "new_value"
  115. cycle.another_key = "another_value"
  116. assert cycle.key == "value"
  117. assert cycle.new_key == "new_value"
  118. assert cycle.another_key == "another_value"
  119. assert cycle.properties["new_key"] == "new_value"
  120. with pytest.raises(AttributeKeyAlreadyExisted):
  121. cycle.key = "KeyAlreadyUsed"
  122. def test_auto_set_and_reload(current_datetime):
  123. cycle_1 = Cycle(
  124. Frequency.WEEKLY,
  125. {"key": "value"},
  126. current_datetime,
  127. current_datetime,
  128. current_datetime,
  129. name="foo",
  130. )
  131. _CycleManager._set(cycle_1)
  132. cycle_2 = _CycleManager._get(cycle_1)
  133. # auto set & reload on frequency attribute
  134. assert cycle_1.frequency == Frequency.WEEKLY
  135. cycle_1.frequency = Frequency.YEARLY
  136. assert cycle_1.frequency == Frequency.YEARLY
  137. assert cycle_2.frequency == Frequency.YEARLY
  138. cycle_2.frequency = Frequency.MONTHLY
  139. assert cycle_1.frequency == Frequency.MONTHLY
  140. assert cycle_2.frequency == Frequency.MONTHLY
  141. new_datetime_1 = current_datetime + timedelta(1)
  142. new_datetime_2 = current_datetime + timedelta(2)
  143. # auto set & reload on creation_date attribute
  144. assert cycle_1.creation_date == current_datetime
  145. assert cycle_2.creation_date == current_datetime
  146. cycle_1.creation_date = new_datetime_1
  147. assert cycle_1.creation_date == new_datetime_1
  148. assert cycle_2.creation_date == new_datetime_1
  149. cycle_2.creation_date = new_datetime_2
  150. assert cycle_1.creation_date == new_datetime_2
  151. assert cycle_2.creation_date == new_datetime_2
  152. # auto set & reload on start_date attribute
  153. assert cycle_1.start_date == current_datetime
  154. assert cycle_2.start_date == current_datetime
  155. cycle_1.start_date = new_datetime_1
  156. assert cycle_1.start_date == new_datetime_1
  157. assert cycle_2.start_date == new_datetime_1
  158. cycle_2.start_date = new_datetime_2
  159. assert cycle_1.start_date == new_datetime_2
  160. assert cycle_2.start_date == new_datetime_2
  161. # auto set & reload on end_date attribute
  162. assert cycle_1.end_date == current_datetime
  163. assert cycle_2.end_date == current_datetime
  164. cycle_1.end_date = new_datetime_1
  165. assert cycle_1.end_date == new_datetime_1
  166. assert cycle_2.end_date == new_datetime_1
  167. cycle_2.end_date = new_datetime_2
  168. assert cycle_1.end_date == new_datetime_2
  169. assert cycle_2.end_date == new_datetime_2
  170. # auto set & reload on names attribute
  171. assert cycle_1.name == "foo"
  172. assert cycle_2.name == "foo"
  173. cycle_1.name = "fed"
  174. assert cycle_1.name == "fed"
  175. assert cycle_2.name == "fed"
  176. cycle_2.name = "def"
  177. assert cycle_1.name == "def"
  178. assert cycle_2.name == "def"
  179. # auto set & reload on properties attribute
  180. assert cycle_1.properties == {"key": "value"}
  181. assert cycle_2.properties == {"key": "value"}
  182. cycle_1._properties["qux"] = 4
  183. assert cycle_1.properties["qux"] == 4
  184. assert cycle_2.properties["qux"] == 4
  185. assert cycle_1.properties == {"key": "value", "qux": 4}
  186. assert cycle_2.properties == {"key": "value", "qux": 4}
  187. cycle_2._properties["qux"] = 5
  188. assert cycle_1.properties["qux"] == 5
  189. assert cycle_2.properties["qux"] == 5
  190. cycle_1.properties["temp_key_1"] = "temp_value_1"
  191. cycle_1.properties["temp_key_2"] = "temp_value_2"
  192. assert cycle_1.properties == {
  193. "qux": 5,
  194. "key": "value",
  195. "temp_key_1": "temp_value_1",
  196. "temp_key_2": "temp_value_2",
  197. }
  198. assert cycle_2.properties == {
  199. "qux": 5,
  200. "key": "value",
  201. "temp_key_1": "temp_value_1",
  202. "temp_key_2": "temp_value_2",
  203. }
  204. cycle_1.properties.pop("temp_key_1")
  205. assert "temp_key_1" not in cycle_1.properties.keys()
  206. assert "temp_key_1" not in cycle_1.properties.keys()
  207. assert cycle_1.properties == {
  208. "key": "value",
  209. "qux": 5,
  210. "temp_key_2": "temp_value_2",
  211. }
  212. assert cycle_2.properties == {
  213. "key": "value",
  214. "qux": 5,
  215. "temp_key_2": "temp_value_2",
  216. }
  217. cycle_2.properties.pop("temp_key_2")
  218. assert cycle_1.properties == {"key": "value", "qux": 5}
  219. assert cycle_2.properties == {"key": "value", "qux": 5}
  220. assert "temp_key_2" not in cycle_1.properties.keys()
  221. assert "temp_key_2" not in cycle_2.properties.keys()
  222. cycle_1.properties["temp_key_3"] = 0
  223. assert cycle_1.properties == {"key": "value", "qux": 5, "temp_key_3": 0}
  224. assert cycle_2.properties == {"key": "value", "qux": 5, "temp_key_3": 0}
  225. cycle_1.properties.update({"temp_key_3": 1})
  226. assert cycle_1.properties == {"key": "value", "qux": 5, "temp_key_3": 1}
  227. assert cycle_2.properties == {"key": "value", "qux": 5, "temp_key_3": 1}
  228. cycle_1.properties.update({})
  229. assert cycle_1.properties == {"key": "value", "qux": 5, "temp_key_3": 1}
  230. assert cycle_2.properties == {"key": "value", "qux": 5, "temp_key_3": 1}
  231. cycle_1.properties.pop("key")
  232. cycle_1.properties["temp_key_4"] = 0
  233. cycle_1.properties["temp_key_5"] = 0
  234. new_datetime_3 = new_datetime_1 + timedelta(5)
  235. with cycle_1 as cycle:
  236. assert cycle.frequency == Frequency.MONTHLY
  237. assert cycle.creation_date == new_datetime_2
  238. assert cycle.start_date == new_datetime_2
  239. assert cycle.end_date == new_datetime_2
  240. assert cycle.name == "def"
  241. assert cycle._is_in_context
  242. assert cycle.properties["qux"] == 5
  243. assert cycle.properties["temp_key_3"] == 1
  244. assert cycle.properties["temp_key_4"] == 0
  245. assert cycle.properties["temp_key_5"] == 0
  246. cycle.frequency = Frequency.YEARLY
  247. cycle.creation_date = new_datetime_3
  248. cycle.start_date = new_datetime_3
  249. cycle.end_date = new_datetime_3
  250. cycle.name = "abc"
  251. assert cycle.name == "def"
  252. assert cycle._name == "abc"
  253. cycle.properties["qux"] = 9
  254. cycle.properties.pop("temp_key_3")
  255. cycle.properties.pop("temp_key_4")
  256. cycle.properties.update({"temp_key_4": 1})
  257. cycle.properties.update({"temp_key_5": 2})
  258. cycle.properties.pop("temp_key_5")
  259. cycle.properties.update({})
  260. assert cycle.frequency == Frequency.MONTHLY
  261. assert cycle.creation_date == new_datetime_2
  262. assert cycle.start_date == new_datetime_2
  263. assert cycle.end_date == new_datetime_2
  264. assert cycle._is_in_context
  265. assert cycle.properties["qux"] == 5
  266. assert cycle.name == "def"
  267. assert cycle.properties["temp_key_3"] == 1
  268. assert cycle.properties["temp_key_4"] == 0
  269. assert cycle.properties["temp_key_5"] == 0
  270. assert cycle_1.frequency == Frequency.YEARLY
  271. assert cycle_1.creation_date == new_datetime_3
  272. assert cycle_1.start_date == new_datetime_3
  273. assert cycle_1.end_date == new_datetime_3
  274. assert cycle_1.name == "abc"
  275. assert cycle_1.properties["qux"] == 9
  276. assert "temp_key_3" not in cycle_1.properties.keys()
  277. assert cycle_1.properties["temp_key_4"] == 1
  278. assert "temp_key_5" not in cycle_1.properties.keys()