136 lines
5.2 KiB
Python
136 lines
5.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
from odoo.tests import TransactionCase
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class TestWorkingHoursConstraints(TransactionCase):
|
|
"""Test cases for working hours validation constraints in resource.calendar"""
|
|
|
|
def setUp(self):
|
|
super(TestWorkingHoursConstraints, self).setUp()
|
|
|
|
# Create base calendar
|
|
self.calendar = self.env['resource.calendar'].create({
|
|
'name': 'Test Calendar',
|
|
'is_full_day': True,
|
|
'noke': False,
|
|
})
|
|
|
|
def test_full_day_working_hours_mismatch_raises_error(self):
|
|
"""
|
|
Arrange: Set full day sign in/out times with mismatched working hours
|
|
Act: Try to save the calendar
|
|
Assert: ValidationError should be raised
|
|
"""
|
|
# Arrange & Act & Assert - Error happens during write()
|
|
with self.assertRaises(ValidationError) as context:
|
|
self.calendar.write({
|
|
'full_min_sign_in': 8.0,
|
|
'full_max_sign_in': 9.0,
|
|
'full_min_sign_out': 16.0,
|
|
'full_max_sign_out': 17.0,
|
|
'working_hours': 7.0, # Wrong value - should be 8.0
|
|
})
|
|
|
|
self.assertIn('8.0', str(context.exception),
|
|
"Error message should mention the correct working hours (8.0)")
|
|
|
|
def test_shift_one_working_hours_match_time_difference(self):
|
|
"""
|
|
Arrange: Set shift one times with matching working hours
|
|
Act: Save the calendar
|
|
Assert: No validation error should be raised
|
|
"""
|
|
# Arrange
|
|
self.calendar.write({
|
|
'is_full_day': False,
|
|
'shift_one_min_sign_in': 7.0,
|
|
'shift_one_max_sign_in': 8.0, # Add max
|
|
'shift_one_min_sign_out': 15.0,
|
|
'shift_one_max_sign_out': 16.0, # Add max
|
|
'shift_one_working_hours': 8.0,
|
|
'shift_two_min_sign_in': 15.0, # Add shift two to avoid errors
|
|
'shift_two_max_sign_in': 16.0,
|
|
'shift_two_min_sign_out': 23.0,
|
|
'shift_two_max_sign_out': 24.0,
|
|
'shift_two_working_hours': 8.0,
|
|
})
|
|
|
|
# Act & Assert
|
|
try:
|
|
self.calendar.result_greed_constrains()
|
|
except ValidationError:
|
|
self.fail("ValidationError should not be raised for valid shift one working hours")
|
|
|
|
def test_shift_one_working_hours_mismatch_raises_error(self):
|
|
"""
|
|
Arrange: Set shift one times with mismatched working hours
|
|
Act: Try to save the calendar
|
|
Assert: ValidationError should be raised
|
|
"""
|
|
# Arrange & Act & Assert
|
|
with self.assertRaises(ValidationError) as context:
|
|
self.calendar.write({
|
|
'is_full_day': False,
|
|
'shift_one_min_sign_in': 7.0,
|
|
'shift_one_max_sign_in': 8.0,
|
|
'shift_one_min_sign_out': 15.0,
|
|
'shift_one_max_sign_out': 16.0,
|
|
'shift_one_working_hours': 9.0, # Wrong - should be 8.0
|
|
'shift_two_min_sign_in': 15.0,
|
|
'shift_two_max_sign_in': 16.0,
|
|
'shift_two_min_sign_out': 23.0,
|
|
'shift_two_max_sign_out': 24.0,
|
|
'shift_two_working_hours': 8.0,
|
|
})
|
|
|
|
self.assertIn('8.0', str(context.exception),
|
|
"Error message should mention the correct shift one working hours")
|
|
|
|
def test_shift_two_working_hours_exceeds_maximum_raises_error(self):
|
|
"""
|
|
Arrange: Set shift two working hours greater than time difference
|
|
Act: Try to save the calendar
|
|
Assert: ValidationError should be raised
|
|
"""
|
|
# Arrange & Act & Assert
|
|
with self.assertRaises(ValidationError) as context:
|
|
self.calendar.write({
|
|
'is_full_day': False,
|
|
'shift_one_min_sign_in': 7.0,
|
|
'shift_one_max_sign_in': 8.0,
|
|
'shift_one_min_sign_out': 15.0,
|
|
'shift_one_max_sign_out': 16.0,
|
|
'shift_one_working_hours': 8.0,
|
|
'shift_two_min_sign_in': 15.0,
|
|
'shift_two_max_sign_in': 16.0,
|
|
'shift_two_min_sign_out': 23.0,
|
|
'shift_two_max_sign_out': 24.0,
|
|
'shift_two_working_hours': 10.0, # Greater than 8 hours
|
|
})
|
|
|
|
self.assertIn('8.0', str(context.exception),
|
|
"Error message should mention the maximum allowed shift two working hours")
|
|
|
|
|
|
def test_full_day_working_hours_match_time_difference(self):
|
|
"""
|
|
Arrange: Set full day sign in/out times with matching working hours
|
|
Act: Save the calendar
|
|
Assert: No validation error should be raised
|
|
"""
|
|
# Arrange - Add ALL required fields to pass other constraints
|
|
self.calendar.write({
|
|
'full_min_sign_in': 8.0,
|
|
'full_max_sign_in': 9.0, # Add this
|
|
'full_min_sign_out': 16.0,
|
|
'full_max_sign_out': 17.0, # Add this
|
|
'working_hours': 8.0,
|
|
})
|
|
|
|
# Act & Assert
|
|
try:
|
|
self.calendar.result_greed_constrains()
|
|
except ValidationError:
|
|
self.fail("ValidationError should not be raised when working hours match time difference")
|