You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
8.6 KiB

  1. # Copyright 2019 - Today Coop IT Easy SCRLfs (<http://www.coopiteasy.be>)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from datetime import datetime, timedelta
  4. from odoo.exceptions import ValidationError
  5. from odoo.tests.common import TransactionCase
  6. class TestBeesdooShift(TransactionCase):
  7. def setUp(self):
  8. super(TestBeesdooShift, self).setUp()
  9. self.shift_model = self.env["beesdoo.shift.shift"]
  10. self.shift_template_model = self.env["beesdoo.shift.template"]
  11. self.current_time = datetime.now()
  12. self.user_admin = self.env.ref("base.user_root")
  13. self.worker_regular_1 = self.env.ref(
  14. "beesdoo_base.res_partner_cooperator_6_demo"
  15. )
  16. self.worker_regular_2 = self.env.ref(
  17. "beesdoo_base.res_partner_cooperator_5_demo"
  18. )
  19. self.worker_regular_3 = self.env.ref(
  20. "beesdoo_base.res_partner_cooperator_3_demo"
  21. )
  22. self.worker_regular_super_1 = self.env.ref(
  23. "beesdoo_base.res_partner_cooperator_1_demo"
  24. )
  25. self.worker_irregular_1 = self.env.ref(
  26. "beesdoo_base.res_partner_cooperator_2_demo"
  27. )
  28. self.worker_irregular_2 = self.env.ref(
  29. "beesdoo_base.res_partner_cooperator_4_demo"
  30. )
  31. self.task_type_1 = self.env.ref(
  32. "beesdoo_shift.beesdoo_shift_task_type_1_demo"
  33. )
  34. self.task_type_2 = self.env.ref(
  35. "beesdoo_shift.beesdoo_shift_task_type_2_demo"
  36. )
  37. self.task_type_3 = self.env.ref(
  38. "beesdoo_shift.beesdoo_shift_task_type_3_demo"
  39. )
  40. self.task_template_1 = self.env.ref(
  41. "beesdoo_worker_status.beesdoo_shift_task_template_1_demo"
  42. )
  43. self.task_template_2 = self.env.ref(
  44. "beesdoo_worker_status.beesdoo_shift_task_template_2_demo"
  45. )
  46. # Set time in and out of generation interval parameter
  47. self.start_in_1 = self.current_time + timedelta(seconds=2)
  48. self.end_in_1 = self.current_time + timedelta(minutes=10)
  49. self.start_in_2 = self.current_time + timedelta(minutes=9)
  50. self.end_in_2 = self.current_time + timedelta(minutes=21)
  51. self.start_out_1 = self.current_time - timedelta(minutes=50)
  52. self.end_out_1 = self.current_time - timedelta(minutes=20)
  53. self.start_out_2 = self.current_time + timedelta(minutes=40)
  54. self.end_out_2 = self.current_time + timedelta(minutes=50)
  55. self.shift_regular_regular_1 = self.shift_model.create(
  56. {
  57. "task_template_id": self.task_template_1.id,
  58. "task_type_id": self.task_type_1.id,
  59. "worker_id": self.worker_regular_1.id,
  60. "start_time": self.start_in_1,
  61. "end_time": self.end_in_1,
  62. "is_regular": True,
  63. "is_compensation": False,
  64. }
  65. )
  66. self.shift_regular_regular_2 = self.shift_model.create(
  67. {
  68. "task_type_id": self.task_type_2.id,
  69. "worker_id": self.worker_regular_2.id,
  70. "start_time": self.start_out_1,
  71. "end_time": self.end_out_1,
  72. "is_regular": True,
  73. "is_compensation": False,
  74. }
  75. )
  76. self.shift_regular_regular_replaced_1 = self.shift_model.create(
  77. {
  78. "task_template_id": self.task_template_1.id,
  79. "task_type_id": self.task_type_3.id,
  80. "worker_id": self.worker_regular_3.id,
  81. "start_time": self.start_in_1,
  82. "end_time": self.end_in_1,
  83. "is_regular": True,
  84. "is_compensation": False,
  85. "replaced_id": self.worker_regular_2.id,
  86. }
  87. )
  88. self.future_shift_regular = self.shift_model.create(
  89. {
  90. "task_template_id": self.task_template_2.id,
  91. "task_type_id": self.task_type_1.id,
  92. "worker_id": self.worker_regular_super_1.id,
  93. "start_time": self.start_in_2,
  94. "end_time": self.end_in_2,
  95. "is_regular": False,
  96. "is_compensation": True,
  97. }
  98. )
  99. self.shift_irregular_1 = self.shift_model.create(
  100. {
  101. "task_template_id": self.task_template_1.id,
  102. "task_type_id": self.task_type_2.id,
  103. "worker_id": self.worker_irregular_1.id,
  104. "start_time": self.start_in_1,
  105. "end_time": self.end_in_1,
  106. }
  107. )
  108. self.shift_irregular_2 = self.shift_model.create(
  109. {
  110. "task_type_id": self.task_type_3.id,
  111. "worker_id": self.worker_irregular_2.id,
  112. "start_time": self.start_out_2,
  113. "end_time": self.end_out_2,
  114. }
  115. )
  116. self.shift_empty_1 = self.shift_model.create(
  117. {
  118. "task_template_id": self.task_template_1.id,
  119. "task_type_id": self.task_type_1.id,
  120. "start_time": self.start_in_1,
  121. "end_time": self.end_in_1,
  122. }
  123. )
  124. def test_shift_counters(self):
  125. "Test shift counters calculation and cooperative status update"
  126. status_1 = self.worker_regular_1.cooperative_status_ids
  127. status_2 = self.worker_regular_3.cooperative_status_ids
  128. status_3 = self.worker_irregular_1.cooperative_status_ids
  129. shift_regular = self.shift_model.create(
  130. {
  131. "task_template_id": self.task_template_1.id,
  132. "task_type_id": self.task_type_1.id,
  133. "worker_id": self.worker_regular_1.id,
  134. "start_time": datetime.now() - timedelta(minutes=50),
  135. "end_time": datetime.now() - timedelta(minutes=40),
  136. "is_regular": True,
  137. "is_compensation": False,
  138. }
  139. )
  140. future_shift_regular = self.shift_model.create(
  141. {
  142. "task_template_id": self.task_template_2.id,
  143. "task_type_id": self.task_type_2.id,
  144. "worker_id": self.worker_regular_1.id,
  145. "start_time": datetime.now() + timedelta(minutes=20),
  146. "end_time": datetime.now() + timedelta(minutes=30),
  147. "is_regular": True,
  148. "is_compensation": False,
  149. }
  150. )
  151. shift_irregular = self.shift_model.create(
  152. {
  153. "task_template_id": self.task_template_2.id,
  154. "task_type_id": self.task_type_3.id,
  155. "worker_id": self.worker_irregular_1.id,
  156. "start_time": datetime.now() - timedelta(minutes=15),
  157. "end_time": datetime.now() - timedelta(minutes=10),
  158. }
  159. )
  160. # For a regular worker
  161. status_1.sr = 0
  162. status_1.sc = 0
  163. self.assertEqual(status_1.status, "ok")
  164. shift_regular.state = "absent_1"
  165. self.assertEqual(status_1.sr, -1)
  166. self.assertEqual(status_1.status, "alert")
  167. shift_regular.state = "done"
  168. self.assertEquals(status_1.sr, 0)
  169. self.assertEquals(status_1.sc, 0)
  170. shift_regular.state = "open"
  171. shift_regular.write({"is_regular": False, "is_compensation": True})
  172. shift_regular.state = "done"
  173. self.assertEquals(status_1.sr, 1)
  174. self.assertEquals(status_1.sc, 0)
  175. # Check unsubscribed status
  176. status_1.sr = -1
  177. status_1.sc = -1
  178. # Subscribe him to another future shift
  179. future_shift_regular.worker_id = self.worker_regular_1
  180. with self.assertRaises(ValidationError) as e:
  181. future_shift_regular.state = "absent_2"
  182. self.assertIn("future", str(e.exception))
  183. status_1.sr = -2
  184. status_1.sc = -2
  185. self.assertEquals(status_1.status, "unsubscribed")
  186. # Should be unsubscribed from future shift
  187. self.assertFalse(future_shift_regular.worker_id)
  188. # With replacement worker (self.worker_regular_3)
  189. shift_regular.state = "open"
  190. status_1.sr = 0
  191. status_1.sc = 0
  192. status_2.sr = 0
  193. status_2.sc = 0
  194. shift_regular.replaced_id = self.worker_regular_3
  195. shift_regular.state = "absent_2"
  196. self.assertEqual(status_1.sr, 0)
  197. self.assertEqual(status_1.sc, 0)
  198. self.assertEqual(status_2.sr, -1)
  199. self.assertEqual(status_2.sc, -1)
  200. # For an irregular worker
  201. status_3.sr = 0
  202. status_3.sc = 0
  203. self.assertEqual(status_3.status, "ok")
  204. shift_irregular.state = "done"
  205. self.assertEqual(status_3.sr, 1)
  206. shift_irregular.state = "absent_2"
  207. self.assertEqual(status_3.sr, -1)