128 lines
4.4 KiB
Python
128 lines
4.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
from odoo.tests import TransactionCase
|
|
|
|
|
|
class TestShiftTimeCalculation(TransactionCase):
|
|
"""Test cases for shift working hours calculation in resource.calendar"""
|
|
|
|
def setUp(self):
|
|
"""Setup test data - calendar for shift calculations"""
|
|
super(TestShiftTimeCalculation, self).setUp()
|
|
|
|
self.calendar = self.env['resource.calendar'].create({
|
|
'name': 'Shift Test Calendar',
|
|
'is_full_day': False,
|
|
'noke': False,
|
|
})
|
|
|
|
def test_get_shift_working_hour_standard_8_hours(self):
|
|
"""
|
|
Arrange: Standard 8-hour shift (08:00 to 16:00)
|
|
Act: Calculate working hours
|
|
Assert: Should return 8.0 hours
|
|
"""
|
|
# Arrange & Act
|
|
result = self.calendar.get_shift_working_hour(8.0, 16.0)
|
|
|
|
# Assert
|
|
self.assertEqual(result, 8.0,
|
|
"Standard 8-hour shift should return 8.0 hours")
|
|
|
|
def test_get_shift_working_hour_with_decimal_minutes(self):
|
|
"""
|
|
Arrange: Shift with decimal time (08:30 to 17:15)
|
|
Act: Calculate working hours
|
|
Assert: Should return correct hours with decimal precision
|
|
"""
|
|
# Arrange & Act
|
|
result = self.calendar.get_shift_working_hour(8.5, 17.25)
|
|
|
|
# Assert
|
|
self.assertAlmostEqual(result, 8.75, places=2,
|
|
msg="Shift from 08:30 to 17:15 should return 8.75 hours")
|
|
|
|
def test_shift_one_working_hours_auto_compute_on_change(self):
|
|
"""
|
|
Arrange: Set shift one min sign in and out
|
|
Act: Trigger onchange method
|
|
Assert: shift_one_working_hours should be automatically calculated
|
|
"""
|
|
# Arrange
|
|
self.calendar.write({
|
|
'shift_one_min_sign_in': 7.0,
|
|
'shift_one_max_sign_in': 8.0, # Add
|
|
'shift_one_min_sign_out': 15.0,
|
|
'shift_one_max_sign_out': 16.0, # Add
|
|
})
|
|
|
|
# Act
|
|
self.calendar.work_hours()
|
|
|
|
# Assert
|
|
self.assertEqual(self.calendar.shift_one_working_hours, 8.0,
|
|
"Shift one working hours should auto-calculate to 8.0")
|
|
|
|
def test_shift_two_working_hours_auto_compute_on_change(self):
|
|
"""
|
|
Arrange: Set shift two min sign in and out
|
|
Act: Trigger onchange method
|
|
Assert: shift_two_working_hours should be automatically calculated
|
|
"""
|
|
# Arrange - Add ALL required fields
|
|
self.calendar.write({
|
|
'shift_two_min_sign_in': 15.0,
|
|
'shift_two_max_sign_in': 16.0, # Add this
|
|
'shift_two_min_sign_out': 23.0,
|
|
'shift_two_max_sign_out': 24.0, # Add this
|
|
})
|
|
|
|
# Act
|
|
self.calendar.work_hours()
|
|
|
|
# Assert
|
|
self.assertEqual(self.calendar.shift_two_working_hours, 8.0,
|
|
"Shift two working hours should auto-calculate to 8.0")
|
|
|
|
def test_both_shifts_working_hours_computed_together(self):
|
|
"""
|
|
Arrange: Set both shifts' times
|
|
Act: Trigger onchange
|
|
Assert: Both shift working hours should be calculated
|
|
"""
|
|
# Arrange - Add max fields
|
|
self.calendar.write({
|
|
'shift_one_min_sign_in': 6.0,
|
|
'shift_one_max_sign_in': 7.0, # Add
|
|
'shift_one_min_sign_out': 14.0,
|
|
'shift_one_max_sign_out': 15.0, # Add
|
|
'shift_two_min_sign_in': 14.0,
|
|
'shift_two_max_sign_in': 15.0, # Add
|
|
'shift_two_min_sign_out': 22.0,
|
|
'shift_two_max_sign_out': 23.0, # Add
|
|
})
|
|
|
|
# Act
|
|
self.calendar.work_hours()
|
|
|
|
# Assert
|
|
self.assertEqual(self.calendar.shift_one_working_hours, 8.0,
|
|
"Shift one should be 8 hours")
|
|
self.assertEqual(self.calendar.shift_two_working_hours, 8.0,
|
|
"Shift two should be 8 hours")
|
|
|
|
def test_overnight_shift_calculation(self):
|
|
"""
|
|
Arrange: Overnight shift (22:00 to 06:00)
|
|
Act: Calculate working hours
|
|
Assert: Should correctly handle overnight shifts
|
|
"""
|
|
# Arrange & Act
|
|
result = self.calendar.get_shift_working_hour(22.0, 6.0)
|
|
|
|
# Assert
|
|
# Note: This depends on implementation - may need adjustment
|
|
expected_hours = 8.0 if result > 0 else -16.0
|
|
self.assertGreater(result, 0,
|
|
"Overnight shift should return positive hours")
|
|
|