odex30_standard/hr_base/tests/test_employee_service_durat...

79 lines
2.7 KiB
Python

from odoo.tests.common import TransactionCase
from datetime import date
from dateutil.relativedelta import relativedelta
from odoo.exceptions import ValidationError, UserError
class TestEmployeeServiceDuration(TransactionCase):
def setUp(self):
super(TestEmployeeServiceDuration, self).setUp()
self.employee = self.env['hr.employee'].create({
'name': 'Test Employee',
})
def test_service_duration_current(self):
self.employee.first_hiring_date = date(2023, 1, 1)
self.employee._compute_service_duration()
today = date.today()
expected = relativedelta(today, date(2023, 1, 1))
self.assertEqual(self.employee.service_year, expected.years)
self.assertEqual(self.employee.service_month, expected.months)
self.assertEqual(self.employee.service_day, expected.days)
def test_service_duration_leaving_date(self):
hire_date = date(2023, 1, 1)
leave_date = date(2024, 6, 15)
self.employee.first_hiring_date = hire_date
self.employee.leaving_date = leave_date
self.employee._compute_service_duration()
expected = relativedelta(leave_date, hire_date)
self.assertEqual(self.employee.service_year, expected.years)
self.assertEqual(self.employee.service_month, expected.months)
self.assertEqual(self.employee.service_day, expected.days)
def test_no_hiring_date(self):
self.employee.first_hiring_date = False
self.employee.leaving_date = False
self.employee._compute_service_duration()
self.assertEqual(self.employee.service_year, 0)
self.assertEqual(self.employee.service_month, 0)
self.assertEqual(self.employee.service_day, 0)
class TestEmployeeUniqueConstraints(TransactionCase):
def setUp(self):
super(TestEmployeeUniqueConstraints, self).setUp()
self.employee = self.env['hr.employee'].create({
'name': 'Test Employee',
'emp_no': 'EMP001',
})
def test_duplicate_emp_no(self):
with self.assertRaises(ValidationError):
self.env['hr.employee'].create({
'name': 'Duplicate Employee',
'emp_no': 'EMP001',
})
def test_future_birthday(self):
with self.assertRaises(UserError):
self.env['hr.employee'].create({
'name': 'Future Birthday',
'birthday': date(2026, 1, 1),
})
def test_valid_birthday(self):
employee = self.env['hr.employee'].create({
'name': 'Valid Birthday',
'birthday': date(1990, 1, 1),
})
self.assertEqual(employee.birthday, date(1990, 1, 1))