test_cycle.py 11 KB

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