| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
from freezegun import freeze_time |
| 3 |
|
| 4 |
from odoo.addons.hr_homeworking_calendar.tests.common import TestHrHomeworkingCommon |
| 5 |
|
| 6 |
from odoo.tests import tagged |
| 7 |
from datetime import datetime |
| 8 |
|
| 9 |
@tagged('post_install', '-at_install', "homeworking_tests") |
| 10 |
class TestHrHomeworkingHrEmployeeLocation(TestHrHomeworkingCommon): |
| 11 |
@classmethod |
| 12 |
def setUpClass(cls): |
| 13 |
super().setUpClass() |
| 14 |
cls.HrEmployeeLocation = cls.env['hr.employee.location'] |
| 15 |
|
| 16 |
def test_set_location_with_weekly_option_changes_employee_location(self): |
| 17 |
wizard = self.env['homework.location.wizard'].create({ |
| 18 |
'work_location_id': self.work_home.id, |
| 19 |
'date': datetime(2023, 10, 4), # wednesday |
| 20 |
'employee_id': self.employee_emp.id, |
| 21 |
'weekly': True |
| 22 |
}) |
| 23 |
wizard.set_employee_location() |
| 24 |
self.assertEqual(self.employee_emp.wednesday_location_id.id, self.work_home.id) |
| 25 |
created_worklocations = self.HrEmployeeLocation.search([ |
| 26 |
('employee_id', '=', self.employee_emp.id), |
| 27 |
('date', '=', datetime(2023, 10, 4)), |
| 28 |
]) |
| 29 |
self.assertEqual(len(created_worklocations), 0, 'should have created 0 worklocation records') |
| 30 |
|
| 31 |
def test_set_location_without_weekly_option_should_create_an_exception(self): |
| 32 |
wizard = self.env['homework.location.wizard'].create({ |
| 33 |
'work_location_id': self.work_home.id, |
| 34 |
'date': datetime(2023, 10, 4), # wednesday |
| 35 |
'employee_id': self.employee_emp.id, |
| 36 |
'weekly': False |
| 37 |
}) |
| 38 |
wizard.set_employee_location() |
| 39 |
created_worklocations = self.HrEmployeeLocation.search([ |
| 40 |
('employee_id', '=', self.employee_emp.id), |
| 41 |
('date', '=', datetime(2023, 10, 4)), |
| 42 |
]) |
| 43 |
self.assertEqual(len(created_worklocations), 1, 'should have created 1 worklocation record') |
| 44 |
self.assertEqual(created_worklocations.work_location_id.id, self.work_home.id) |
| 45 |
|
| 46 |
def test_create_exception_on_top_of_exception_keeps_a_single_record(self): |
| 47 |
wizard = self.env['homework.location.wizard'].create({ |
| 48 |
'work_location_id': self.work_home.id, |
| 49 |
'date': datetime(2023, 10, 4), # wednesday |
| 50 |
'employee_id': self.employee_emp.id, |
| 51 |
'weekly': False |
| 52 |
}) |
| 53 |
wizard.set_employee_location() |
| 54 |
created_worklocations = self.HrEmployeeLocation.search([ |
| 55 |
('employee_id', '=', self.employee_emp.id), |
| 56 |
('date', '=', datetime(2023, 10, 4)), |
| 57 |
]) |
| 58 |
self.assertEqual(len(created_worklocations), 1, 'should have created 1 worklocation record') |
| 59 |
wizard.work_location_id = self.work_office_2 |
| 60 |
wizard.set_employee_location() |
| 61 |
created_worklocations = self.HrEmployeeLocation.search([ |
| 62 |
('employee_id', '=', self.employee_emp.id), |
| 63 |
('date', '=', datetime(2023, 10, 4)), |
| 64 |
]) |
| 65 |
self.assertEqual(len(created_worklocations), 1, 'should have created 1 worklocation record') |
| 66 |
self.assertEqual(created_worklocations.work_location_id.id, self.work_office_2.id) |
| 67 |
|
| 68 |
def test_set_same_location_as_default_one_should_not_create_exception(self): |
| 69 |
wizard = self.env['homework.location.wizard'].create({ |
| 70 |
'work_location_id': self.work_office_1.id, |
| 71 |
'date': datetime(2023, 10, 4), # wednesday |
| 72 |
'employee_id': self.employee_emp.id, |
| 73 |
'weekly': False |
| 74 |
}) |
| 75 |
wizard.set_employee_location() |
| 76 |
created_worklocations = self.HrEmployeeLocation.search([ |
| 77 |
('employee_id', '=', self.employee_emp.id), |
| 78 |
('date', '=', datetime(2023, 10, 4)), |
| 79 |
]) |
| 80 |
self.assertEqual(len(created_worklocations), 0, 'should have created 0 worklocation records') |
| 81 |
|
| 82 |
def test_set_default_day_location_as_exception_will_delete_exception(self): |
| 83 |
# create exception for a certain day |
| 84 |
wizard = self.env['homework.location.wizard'].create({ |
| 85 |
'work_location_id': self.work_home.id, |
| 86 |
'date': datetime(2023, 10, 4), # wednesday |
| 87 |
'employee_id': self.employee_emp.id, |
| 88 |
'weekly': False |
| 89 |
}) |
| 90 |
wizard.set_employee_location() |
| 91 |
|
| 92 |
created_worklocations = self.HrEmployeeLocation.search([ |
| 93 |
('employee_id', '=', self.employee_emp.id), |
| 94 |
('date', '=', datetime(2023, 10, 4)), |
| 95 |
]) |
| 96 |
|
| 97 |
self.assertEqual(len(created_worklocations), 1, 'should have created 1 worklocation records') |
| 98 |
|
| 99 |
# set default work location on day where an exception exists |
| 100 |
wizard.work_location_id = self.work_office_1 |
| 101 |
wizard.set_employee_location() |
| 102 |
|
| 103 |
created_worklocations = self.HrEmployeeLocation.search([ |
| 104 |
('employee_id', '=', self.employee_emp.id), |
| 105 |
('date', '=', datetime(2023, 10, 4)), |
| 106 |
]) |
| 107 |
|
| 108 |
# exception should be deleted |
| 109 |
self.assertEqual(len(created_worklocations), 0, 'should have deleted the worklocation record') |
| 110 |
|
| 111 |
def test_get_views_replace_hw_location_by_date(self): |
| 112 |
view = self.env["ir.ui.view"].create({ |
| 113 |
"arch": """<list><field name="work_location_name" /></list>""", |
| 114 |
"model": "hr.employee", |
| 115 |
"type": "list", |
| 116 |
}) |
| 117 |
# Wednesday January 28 2026 |
| 118 |
with freeze_time("2026-01-28"): |
| 119 |
got_view = self.env["hr.employee"].get_views([(view.id, "list")]) |
| 120 |
self.assertTrue("work_location_name" in got_view["models"]["hr.employee"]["fields"]) |
| 121 |
self.assertTrue("wednesday_location_id" in got_view["models"]["hr.employee"]["fields"]) |
| 122 |
self.assertEqual(got_view["views"]["list"]["arch"], """<list><field name="wednesday_location_id"/></list>""") |