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.

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