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.

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