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.

388 lines
15 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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.exceptions import UserError
  6. from odoo.tests.common import TransactionCase
  7. class TestBeesdooShift(TransactionCase):
  8. def setUp(self):
  9. super(TestBeesdooShift, self).setUp()
  10. self.shift_model = self.env["beesdoo.shift.shift"]
  11. self.shift_template_model = self.env["beesdoo.shift.template"]
  12. self.attendance_sheet_model = self.env["beesdoo.shift.sheet"]
  13. self.attendance_sheet_shift_model = self.env[
  14. "beesdoo.shift.sheet.shift"
  15. ]
  16. self.shift_expected_model = self.env["beesdoo.shift.sheet.expected"]
  17. self.shift_added_model = self.env["beesdoo.shift.sheet.added"]
  18. self.pre_filled_task_type_id = (
  19. self.env["ir.config_parameter"]
  20. .sudo()
  21. .get_param("beesdoo_shift.pre_filled_task_type_id")
  22. )
  23. self.current_time = datetime.now()
  24. self.user_admin = self.env.ref("base.user_root")
  25. self.user_generic = self.env.ref(
  26. "beesdoo_shift_attendance.beesdoo_shift_user_1_demo"
  27. )
  28. self.user_permanent = self.env.ref(
  29. "beesdoo_shift_attendance.beesdoo_shift_user_2_demo"
  30. )
  31. self.setting_wizard = self.env["res.config.settings"].sudo(
  32. self.user_admin
  33. )
  34. self.worker_regular_1 = self.env.ref(
  35. "beesdoo_base.res_partner_cooperator_6_demo"
  36. )
  37. self.worker_regular_2 = self.env.ref(
  38. "beesdoo_base.res_partner_cooperator_5_demo"
  39. )
  40. self.worker_regular_3 = self.env.ref(
  41. "beesdoo_base.res_partner_cooperator_3_demo"
  42. )
  43. self.worker_regular_super_1 = self.env.ref(
  44. "beesdoo_base.res_partner_cooperator_1_demo"
  45. )
  46. self.worker_irregular_1 = self.env.ref(
  47. "beesdoo_base.res_partner_cooperator_2_demo"
  48. )
  49. self.worker_irregular_2 = self.env.ref(
  50. "beesdoo_base.res_partner_cooperator_4_demo"
  51. )
  52. self.task_type_1 = self.env.ref(
  53. "beesdoo_shift.beesdoo_shift_task_type_1_demo"
  54. )
  55. self.task_type_2 = self.env.ref(
  56. "beesdoo_shift.beesdoo_shift_task_type_2_demo"
  57. )
  58. self.task_type_3 = self.env.ref(
  59. "beesdoo_shift.beesdoo_shift_task_type_3_demo"
  60. )
  61. self.task_template_1 = self.env.ref(
  62. "beesdoo_worker_status" ".beesdoo_shift_task_template_1_demo"
  63. )
  64. self.task_template_2 = self.env.ref(
  65. "beesdoo_worker_status" ".beesdoo_shift_task_template_2_demo"
  66. )
  67. # Set time in and out of generation interval parameter
  68. self.start_in_1 = self.current_time + timedelta(seconds=2)
  69. self.end_in_1 = self.current_time + timedelta(minutes=10)
  70. self.start_in_2 = self.current_time + timedelta(minutes=9)
  71. self.end_in_2 = self.current_time + timedelta(minutes=21)
  72. self.start_out_1 = self.current_time - timedelta(minutes=50)
  73. self.end_out_1 = self.current_time - timedelta(minutes=20)
  74. self.start_out_2 = self.current_time + timedelta(minutes=40)
  75. self.end_out_2 = self.current_time + timedelta(minutes=50)
  76. self.shift_regular_regular_1 = self.shift_model.create(
  77. {
  78. "task_template_id": self.task_template_1.id,
  79. "task_type_id": self.task_type_1.id,
  80. "worker_id": self.worker_regular_1.id,
  81. "start_time": self.start_in_1,
  82. "end_time": self.end_in_1,
  83. "is_regular": True,
  84. "is_compensation": False,
  85. }
  86. )
  87. self.shift_regular_regular_2 = self.shift_model.create(
  88. {
  89. "task_type_id": self.task_type_2.id,
  90. "worker_id": self.worker_regular_2.id,
  91. "start_time": self.start_out_1,
  92. "end_time": self.end_out_1,
  93. "is_regular": True,
  94. "is_compensation": False,
  95. }
  96. )
  97. self.shift_regular_regular_replaced_1 = self.shift_model.create(
  98. {
  99. "task_template_id": self.task_template_1.id,
  100. "task_type_id": self.task_type_3.id,
  101. "worker_id": self.worker_regular_3.id,
  102. "start_time": self.start_in_1,
  103. "end_time": self.end_in_1,
  104. "is_regular": True,
  105. "is_compensation": False,
  106. "replaced_id": self.worker_regular_2.id,
  107. }
  108. )
  109. self.future_shift_regular = self.shift_model.create(
  110. {
  111. "task_template_id": self.task_template_2.id,
  112. "task_type_id": self.task_type_1.id,
  113. "worker_id": self.worker_regular_super_1.id,
  114. "start_time": self.start_in_2,
  115. "end_time": self.end_in_2,
  116. "is_regular": False,
  117. "is_compensation": True,
  118. }
  119. )
  120. self.shift_irregular_1 = self.shift_model.create(
  121. {
  122. "task_template_id": self.task_template_1.id,
  123. "task_type_id": self.task_type_2.id,
  124. "worker_id": self.worker_irregular_1.id,
  125. "start_time": self.start_in_1,
  126. "end_time": self.end_in_1,
  127. }
  128. )
  129. self.shift_irregular_2 = self.shift_model.create(
  130. {
  131. "task_type_id": self.task_type_3.id,
  132. "worker_id": self.worker_irregular_2.id,
  133. "start_time": self.start_out_2,
  134. "end_time": self.end_out_2,
  135. }
  136. )
  137. self.shift_empty_1 = self.shift_model.create(
  138. {
  139. "task_template_id": self.task_template_1.id,
  140. "task_type_id": self.task_type_1.id,
  141. "start_time": self.start_in_1,
  142. "end_time": self.end_in_1,
  143. }
  144. )
  145. def search_sheets(self, start_time, end_time):
  146. if (type(start_time) and type(end_time)) == datetime:
  147. return self.attendance_sheet_model.search(
  148. [("start_time", "=", start_time), ("end_time", "=", end_time)]
  149. )
  150. def test_attendance_sheet_creation(self):
  151. "Test creation of an attendance sheet with all its expected shifts"
  152. # Set generation interval setting
  153. setting_wizard_1 = self.setting_wizard.create(
  154. {
  155. "pre_filled_task_type_id": self.task_type_1.id,
  156. "attendance_sheet_generation_interval": 15,
  157. }
  158. )
  159. setting_wizard_1.execute()
  160. # Test attendance sheets creation
  161. self.attendance_sheet_model._generate_attendance_sheet()
  162. self.assertEqual(
  163. len(self.search_sheets(self.start_in_1, self.end_in_1)), 1
  164. )
  165. self.assertEqual(
  166. len(self.search_sheets(self.start_in_2, self.end_in_2)), 1
  167. )
  168. self.assertEqual(
  169. len(self.search_sheets(self.start_out_1, self.end_out_1)), 0
  170. )
  171. self.assertEqual(
  172. len(self.search_sheets(self.start_out_2, self.end_out_2)), 0
  173. )
  174. # Test expected shifts creation
  175. # Sheet 1 starts at current time + 2 secs, ends at current time + 10 min
  176. # Sheet 2 starts at current time + 9 min, ends at current time + 21 min
  177. sheet_1 = self.search_sheets(self.start_in_1, self.end_in_1)
  178. sheet_2 = self.search_sheets(self.start_in_2, self.end_in_2)
  179. self.assertTrue(sheet_1.start_time)
  180. self.assertTrue(sheet_1.end_time)
  181. # Empty shift should not be added
  182. self.assertEqual(len(sheet_1.expected_shift_ids), 3)
  183. self.assertEqual(len(sheet_1.added_shift_ids), 0)
  184. self.assertEqual(len(sheet_2.expected_shift_ids), 1)
  185. # Test consistency with actual shift for sheet 1
  186. for shift in sheet_1.expected_shift_ids:
  187. self.assertEquals(shift.worker_id, shift.task_id.worker_id)
  188. self.assertEquals(shift.replaced_id, shift.task_id.replaced_id)
  189. self.assertEqual(shift.task_type_id, shift.task_id.task_type_id)
  190. self.assertEqual(shift.super_coop_id, shift.task_id.super_coop_id)
  191. self.assertEqual(shift.working_mode, shift.task_id.working_mode)
  192. # Status should be "absent" for all shifts
  193. self.assertEquals(shift.state, "absent_2")
  194. # Empty shift should be considered in max worker number calculation
  195. self.assertEqual(sheet_1.max_worker_no, 4)
  196. # Test default values creation
  197. self.assertTrue(sheet_1.time_slot)
  198. self.assertEqual(sheet_1.day, self.start_in_1.date())
  199. self.assertEqual(sheet_1.day_abbrevation, "Lundi")
  200. self.assertEqual(sheet_1.week, "Semaine A")
  201. self.assertTrue(sheet_1.name)
  202. self.assertFalse(sheet_1.notes)
  203. self.assertFalse(sheet_1.is_annotated)
  204. def test_attendance_sheet_barcode_scan(self):
  205. """
  206. Edition of an attendance sheet
  207. with barcode scanner, as a generic user"
  208. """
  209. # Attendance sheet generation
  210. self.attendance_sheet_model.sudo(
  211. self.user_generic
  212. )._generate_attendance_sheet()
  213. sheet_1 = self.search_sheets(self.start_in_1, self.end_in_1)
  214. sheet_1 = sheet_1.sudo(self.user_generic)
  215. # Expected workers are :
  216. # worker_regular_1 (barcode : 421457731745)
  217. # worker_regular_3 replaced by worker_regular_2 (barcode : 421457731744))
  218. # worker_irregular_1 (barcode : 429919251493)
  219. # Scan barcode for expected workers
  220. for barcode in [421457731745, 421457731744, 429919251493]:
  221. sheet_1.on_barcode_scanned(barcode)
  222. # Check expected shifts update
  223. for id_ in sheet_1.expected_shift_ids.ids:
  224. shift = sheet_1.expected_shift_ids.browse(id_)
  225. self.assertEqual(shift.state, "done")
  226. # Added workers are :
  227. # worker_regular_super_1 (barcode : 421457731741)
  228. # worker_irregular_2 (barcode : 421457731743)
  229. # Workararound for _onchange method
  230. # (not applying on temporary object in tests)
  231. sheet_1._origin = sheet_1
  232. # Scan barcode for added workers
  233. sheet_1.on_barcode_scanned(421457731741)
  234. self.assertEqual(len(sheet_1.added_shift_ids), 1)
  235. sheet_1.on_barcode_scanned(421457731743)
  236. # Scan an already added worker should not change anything
  237. sheet_1.on_barcode_scanned(421457731743)
  238. self.assertEqual(len(sheet_1.added_shift_ids), 2)
  239. # Check added shifts fields
  240. for id_ in sheet_1.added_shift_ids.ids:
  241. shift = sheet_1.added_shift_ids.browse(id_)
  242. self.assertEqual(sheet_1, shift.attendance_sheet_id)
  243. self.assertEqual(shift.state, "done")
  244. self.assertEqual(
  245. shift.task_type_id,
  246. self.attendance_sheet_shift_model.pre_filled_task_type_id(),
  247. )
  248. if shift.working_mode == "regular":
  249. self.assertTrue(shift.is_compensation)
  250. else:
  251. self.assertFalse(shift.is_compensation)
  252. # Add a worker that should be replaced
  253. with self.assertRaises(UserError):
  254. sheet_1.on_barcode_scanned(421457731742)
  255. # Wrong barcode
  256. with self.assertRaises(UserError):
  257. sheet_1.on_barcode_scanned(101010)
  258. # Add an unsubscribed worker
  259. self.worker_regular_1.cooperative_status_ids.sr = -2
  260. self.worker_regular_1.cooperative_status_ids.sc = -2
  261. with self.assertRaises(UserError):
  262. sheet_1.on_barcode_scanned(421457731745)
  263. def test_attendance_sheet_edition(self):
  264. # Attendance sheet generation
  265. self.attendance_sheet_model.sudo(
  266. self.user_generic
  267. )._generate_attendance_sheet()
  268. sheet_1 = self.search_sheets(self.start_in_1, self.end_in_1)
  269. # Expected shifts edition
  270. sheet_1.expected_shift_ids[1].state = "done"
  271. sheet_1.expected_shift_ids[2].state = "absent_1"
  272. # Added shits addition
  273. sheet_1.added_shift_ids |= sheet_1.added_shift_ids.new(
  274. {
  275. "task_type_id": self.task_type_2.id,
  276. "state": "done",
  277. "attendance_sheet_id": sheet_1.id,
  278. "worker_id": self.worker_irregular_2.id,
  279. "is_compensation": False,
  280. "is_regular": False,
  281. }
  282. )
  283. # Same task type as empty shift (should edit it on validation)
  284. sheet_1.added_shift_ids |= sheet_1.added_shift_ids.new(
  285. {
  286. "task_type_id": self.task_type_1.id,
  287. "state": "done",
  288. "attendance_sheet_id": sheet_1.id,
  289. "worker_id": self.worker_regular_super_1.id,
  290. "is_compensation": True,
  291. "is_regular": False,
  292. }
  293. )
  294. # TODO: test validation with wizard (as generic user)
  295. # class odoo.tests.common.Form(recordp, view=None)
  296. # is only available from version 12
  297. # sheet_1 = sheet_1.sudo(self.user_generic)
  298. # Validation without wizard (as admin user)
  299. sheet_1 = sheet_1.sudo(self.user_admin)
  300. # Wait necessary time for shifts to begin
  301. waiting_time = (self.start_in_1 - datetime.now()).total_seconds()
  302. if waiting_time > 0:
  303. with self.assertRaises(UserError) as econtext:
  304. sheet_1.validate_with_checks()
  305. self.assertIn(
  306. "once the shifts have started", str(econtext.exception)
  307. )
  308. time.sleep(waiting_time)
  309. sheet_1.worker_nb_feedback = "enough"
  310. sheet_1.feedback = "Great session."
  311. sheet_1.notes = "Important information."
  312. sheet_1.validate_with_checks()
  313. with self.assertRaises(UserError) as econtext:
  314. sheet_1.validate_with_checks()
  315. self.assertIn("already been validated", str(econtext.exception))
  316. self.assertEqual(sheet_1.state, "validated")
  317. self.assertEqual(sheet_1.validated_by, self.user_admin.partner_id)
  318. self.assertTrue(sheet_1.is_annotated)
  319. self.assertFalse(sheet_1.is_read)
  320. # Check actual shifts update
  321. workers = sheet_1.expected_shift_ids.mapped(
  322. "worker_id"
  323. ) | sheet_1.added_shift_ids.mapped("worker_id")
  324. self.assertEqual(len(workers), 5)
  325. self.assertEqual(
  326. sheet_1.expected_shift_ids[0].task_id.state, "absent_2"
  327. )
  328. self.assertEqual(sheet_1.expected_shift_ids[1].task_id.state, "done")
  329. self.assertEqual(
  330. sheet_1.expected_shift_ids[2].task_id.state, "absent_1"
  331. )
  332. self.assertEqual(sheet_1.added_shift_ids[0].task_id.state, "done")
  333. self.assertEqual(sheet_1.added_shift_ids[1].task_id.state, "done")
  334. # Empty shift should have been updated
  335. self.assertEqual(
  336. sheet_1.added_shift_ids[0].task_id, self.shift_empty_1
  337. )
  338. # sheet_1.expected_shift_ids[0].worker_id