test_cycle.py 11 KB

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