test_cycle.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. from taipy.common.config.common.frequency import Frequency
  14. from taipy.core import CycleId
  15. from taipy.core.cycle._cycle_manager import _CycleManager
  16. from taipy.core.cycle._cycle_manager_factory import _CycleManagerFactory
  17. from taipy.core.cycle.cycle import Cycle
  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._set(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. assert cycle.get_label() == cycle.name
  81. assert cycle.get_simple_label() == cycle.name
  82. cycle._properties["label"] = "label"
  83. assert cycle.get_label() == "label"
  84. assert cycle.get_simple_label() == "label"
  85. def test_add_property_to_scenario(current_datetime):
  86. cycle = Cycle(
  87. Frequency.WEEKLY,
  88. {"key": "value"},
  89. current_datetime,
  90. current_datetime,
  91. current_datetime,
  92. name="foo",
  93. )
  94. assert cycle.properties == {"key": "value"}
  95. assert cycle.properties["key"] == "value"
  96. cycle.properties["new_key"] = "new_value"
  97. assert cycle.properties == {"key": "value", "new_key": "new_value"}
  98. assert cycle.properties["key"] == "value"
  99. assert cycle.properties["new_key"] == "new_value"
  100. def test_auto_set_and_reload(current_datetime):
  101. cycle_1 = Cycle(
  102. Frequency.WEEKLY,
  103. {"key": "value"},
  104. current_datetime,
  105. current_datetime,
  106. current_datetime,
  107. name="foo",
  108. )
  109. _CycleManager._set(cycle_1)
  110. cycle_2 = _CycleManager._get(cycle_1)
  111. # auto set & reload on frequency attribute
  112. assert cycle_1.frequency == Frequency.WEEKLY
  113. cycle_1.frequency = Frequency.YEARLY
  114. assert cycle_1.frequency == Frequency.YEARLY
  115. assert cycle_2.frequency == Frequency.YEARLY
  116. cycle_2.frequency = Frequency.MONTHLY
  117. assert cycle_1.frequency == Frequency.MONTHLY
  118. assert cycle_2.frequency == Frequency.MONTHLY
  119. new_datetime_1 = current_datetime + timedelta(1)
  120. new_datetime_2 = current_datetime + timedelta(2)
  121. # auto set & reload on creation_date attribute
  122. assert cycle_1.creation_date == current_datetime
  123. assert cycle_2.creation_date == current_datetime
  124. cycle_1.creation_date = new_datetime_1
  125. assert cycle_1.creation_date == new_datetime_1
  126. assert cycle_2.creation_date == new_datetime_1
  127. cycle_2.creation_date = new_datetime_2
  128. assert cycle_1.creation_date == new_datetime_2
  129. assert cycle_2.creation_date == new_datetime_2
  130. # auto set & reload on start_date attribute
  131. assert cycle_1.start_date == current_datetime
  132. assert cycle_2.start_date == current_datetime
  133. cycle_1.start_date = new_datetime_1
  134. assert cycle_1.start_date == new_datetime_1
  135. assert cycle_2.start_date == new_datetime_1
  136. cycle_2.start_date = new_datetime_2
  137. assert cycle_1.start_date == new_datetime_2
  138. assert cycle_2.start_date == new_datetime_2
  139. # auto set & reload on end_date attribute
  140. assert cycle_1.end_date == current_datetime
  141. assert cycle_2.end_date == current_datetime
  142. cycle_1.end_date = new_datetime_1
  143. assert cycle_1.end_date == new_datetime_1
  144. assert cycle_2.end_date == new_datetime_1
  145. cycle_2.end_date = new_datetime_2
  146. assert cycle_1.end_date == new_datetime_2
  147. assert cycle_2.end_date == new_datetime_2
  148. # auto set & reload on names attribute
  149. assert cycle_1.name == "foo"
  150. assert cycle_2.name == "foo"
  151. cycle_1.name = "fed"
  152. assert cycle_1.name == "fed"
  153. assert cycle_2.name == "fed"
  154. cycle_2.name = "def"
  155. assert cycle_1.name == "def"
  156. assert cycle_2.name == "def"
  157. # auto set & reload on properties attribute
  158. assert cycle_1.properties == {"key": "value"}
  159. assert cycle_2.properties == {"key": "value"}
  160. cycle_1._properties["qux"] = 4
  161. assert cycle_1.properties["qux"] == 4
  162. assert cycle_2.properties["qux"] == 4
  163. assert cycle_1.properties == {"key": "value", "qux": 4}
  164. assert cycle_2.properties == {"key": "value", "qux": 4}
  165. cycle_2._properties["qux"] = 5
  166. assert cycle_1.properties["qux"] == 5
  167. assert cycle_2.properties["qux"] == 5
  168. cycle_1.properties["temp_key_1"] = "temp_value_1"
  169. cycle_1.properties["temp_key_2"] = "temp_value_2"
  170. assert cycle_1.properties == {
  171. "qux": 5,
  172. "key": "value",
  173. "temp_key_1": "temp_value_1",
  174. "temp_key_2": "temp_value_2",
  175. }
  176. assert cycle_2.properties == {
  177. "qux": 5,
  178. "key": "value",
  179. "temp_key_1": "temp_value_1",
  180. "temp_key_2": "temp_value_2",
  181. }
  182. cycle_1.properties.pop("temp_key_1")
  183. assert "temp_key_1" not in cycle_1.properties.keys()
  184. assert "temp_key_1" not in cycle_1.properties.keys()
  185. assert cycle_1.properties == {
  186. "key": "value",
  187. "qux": 5,
  188. "temp_key_2": "temp_value_2",
  189. }
  190. assert cycle_2.properties == {
  191. "key": "value",
  192. "qux": 5,
  193. "temp_key_2": "temp_value_2",
  194. }
  195. cycle_2.properties.pop("temp_key_2")
  196. assert cycle_1.properties == {"key": "value", "qux": 5}
  197. assert cycle_2.properties == {"key": "value", "qux": 5}
  198. assert "temp_key_2" not in cycle_1.properties.keys()
  199. assert "temp_key_2" not in cycle_2.properties.keys()
  200. cycle_1.properties["temp_key_3"] = 0
  201. assert cycle_1.properties == {"key": "value", "qux": 5, "temp_key_3": 0}
  202. assert cycle_2.properties == {"key": "value", "qux": 5, "temp_key_3": 0}
  203. cycle_1.properties.update({"temp_key_3": 1})
  204. assert cycle_1.properties == {"key": "value", "qux": 5, "temp_key_3": 1}
  205. assert cycle_2.properties == {"key": "value", "qux": 5, "temp_key_3": 1}
  206. cycle_1.properties.update({})
  207. assert cycle_1.properties == {"key": "value", "qux": 5, "temp_key_3": 1}
  208. assert cycle_2.properties == {"key": "value", "qux": 5, "temp_key_3": 1}
  209. cycle_1.properties.pop("key")
  210. cycle_1.properties["temp_key_4"] = 0
  211. cycle_1.properties["temp_key_5"] = 0
  212. new_datetime_3 = new_datetime_1 + timedelta(5)
  213. with cycle_1 as cycle:
  214. assert cycle.frequency == Frequency.MONTHLY
  215. assert cycle.creation_date == new_datetime_2
  216. assert cycle.start_date == new_datetime_2
  217. assert cycle.end_date == new_datetime_2
  218. assert cycle.name == "def"
  219. assert cycle._is_in_context
  220. assert cycle.properties["qux"] == 5
  221. assert cycle.properties["temp_key_3"] == 1
  222. assert cycle.properties["temp_key_4"] == 0
  223. assert cycle.properties["temp_key_5"] == 0
  224. cycle.frequency = Frequency.YEARLY
  225. cycle.creation_date = new_datetime_3
  226. cycle.start_date = new_datetime_3
  227. cycle.end_date = new_datetime_3
  228. cycle.name = "abc"
  229. assert cycle.name == "def"
  230. assert cycle._name == "abc"
  231. cycle.properties["qux"] = 9
  232. cycle.properties.pop("temp_key_3")
  233. cycle.properties.pop("temp_key_4")
  234. cycle.properties.update({"temp_key_4": 1})
  235. cycle.properties.update({"temp_key_5": 2})
  236. cycle.properties.pop("temp_key_5")
  237. cycle.properties.update({})
  238. assert cycle.frequency == Frequency.MONTHLY
  239. assert cycle.creation_date == new_datetime_2
  240. assert cycle.start_date == new_datetime_2
  241. assert cycle.end_date == new_datetime_2
  242. assert cycle._is_in_context
  243. assert cycle.properties["qux"] == 5
  244. assert cycle.name == "def"
  245. assert cycle.properties["temp_key_3"] == 1
  246. assert cycle.properties["temp_key_4"] == 0
  247. assert cycle.properties["temp_key_5"] == 0
  248. assert cycle_1.frequency == Frequency.YEARLY
  249. assert cycle_1.creation_date == new_datetime_3
  250. assert cycle_1.start_date == new_datetime_3
  251. assert cycle_1.end_date == new_datetime_3
  252. assert cycle_1.name == "abc"
  253. assert cycle_1.properties["qux"] == 9
  254. assert "temp_key_3" not in cycle_1.properties.keys()
  255. assert cycle_1.properties["temp_key_4"] == 1
  256. assert "temp_key_5" not in cycle_1.properties.keys()