# -*- coding: utf-8 -*- from odoo.tests.common import TransactionCase from datetime import date from ast import literal_eval class TestPresenceStateComputation(TransactionCase): """Test cases for hr_presence_state computation in hr.employee.base""" def setUp(self): """Setup test data - موظف، مستخدم، وإعدادات النظام""" super(TestPresenceStateComputation, self).setUp() # إنشاء مستخدم للموظف self.test_user = self.env['res.users'].create({ 'name': 'Test Employee User', 'login': 'test_employee@test.com', 'email': 'test_employee@test.com', }) # إنشاء موظف مرتبط بالمستخدم self.test_employee = self.env['hr.employee'].create({ 'name': 'Test Employee', 'user_id': self.test_user.id, }) # إنشاء موظف بدون مستخدم (للاختبارات) self.employee_without_user = self.env['hr.employee'].create({ 'name': 'Employee Without User', 'user_id': False, }) # إعداد تاريخ اليوم self.today = date.today() # الحصول على config parameter self.config_param = self.env['ir.config_parameter'].sudo() def test_presence_state_to_define_when_login_check_disabled(self): # Arrange self.config_param.set_param('hr.hr_presence_control_login', 'False') # Act self.test_employee._compute_presence_state() # Assert self.assertEqual( self.test_employee.hr_presence_state, 'to_define', "Presence state should be 'present' when attendance record exists for today" ) def test_presence_state_present_when_attendance_exists(self): # Arrange self.config_param.set_param('hr.hr_presence_control_login', 'True') self.env['attendance.attendance'].sudo().create({ 'employee_id': self.test_employee.id, 'action_date': self.today, }) # Act self.test_employee._compute_presence_state() # Assert self.assertEqual( self.test_employee.hr_presence_state, 'present', "Presence state should be 'to_define' when no attendance record exists" ) def test_presence_state_to_define_when_no_attendance_exists(self): # Arrange self.config_param.set_param('hr.hr_presence_control_login', 'True') existing_attendance = self.env['attendance.attendance'].sudo().search([ ('employee_id', '=', self.test_employee.id), ('action_date', '=', self.today) ]) existing_attendance.unlink() # Act self.test_employee._compute_presence_state() # Assert self.assertEqual( self.test_employee.hr_presence_state, 'to_define', "حالة الحضور يجب أن تكون 'to_define' عند عدم وجود سجل حضور" ) def test_presence_state_multiple_employees(self): # Arrange self.config_param.set_param('hr.hr_presence_control_login', 'True') user2 = self.env['res.users'].create({ 'name': 'Test Employee 2', 'login': 'test_employee2@test.com', }) employee2 = self.env['hr.employee'].create({ 'name': 'Test Employee 2', 'user_id': user2.id, }) self.env['attendance.attendance'].sudo().create({ 'employee_id': employee2.id, 'action_date': self.today, }) user3 = self.env['res.users'].create({ 'name': 'Test Employee 3', 'login': 'test_employee3@test.com', }) employee3 = self.env['hr.employee'].create({ 'name': 'Test Employee 3', 'user_id': user3.id, }) employees = self.test_employee | employee2 | employee3 # Act employees._compute_presence_state() # Assert self.assertEqual(employee2.hr_presence_state, 'present', "Second employee should have state 'present'") self.assertEqual(employee3.hr_presence_state, 'to_define', "Third employee should have state 'to_define'") def test_presence_state_employee_without_user(self): # Arrange self.config_param.set_param('hr.hr_presence_control_login', 'True') # Act self.employee_without_user._compute_presence_state() # Assert self.assertEqual( self.employee_without_user.hr_presence_state, 'to_define', "the employee with not user must be'to_define'" ) def test_presence_state_updates_correctly_on_multiple_calls(self): # Arrange self.config_param.set_param('hr.hr_presence_control_login', 'True') self.test_employee._compute_presence_state() state_before = self.test_employee.hr_presence_state self.env['attendance.attendance'].sudo().create({ 'employee_id': self.test_employee.id, 'action_date': self.today, }) self.test_employee._compute_presence_state() state_after = self.test_employee.hr_presence_state # Assert self.assertEqual(state_before, 'to_define', "The one case must be 'to_define'") self.assertEqual(state_after, 'present', "the one case after present must be 'present'")