diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/maintenance/tests | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/maintenance/tests')
| -rw-r--r-- | addons/maintenance/tests/__init__.py | 4 | ||||
| -rw-r--r-- | addons/maintenance/tests/test_maintenance.py | 134 | ||||
| -rw-r--r-- | addons/maintenance/tests/test_maintenance_multicompany.py | 175 |
3 files changed, 313 insertions, 0 deletions
diff --git a/addons/maintenance/tests/__init__.py b/addons/maintenance/tests/__init__.py new file mode 100644 index 00000000..e65a25e0 --- /dev/null +++ b/addons/maintenance/tests/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- + +from . import test_maintenance +from . import test_maintenance_multicompany diff --git a/addons/maintenance/tests/test_maintenance.py b/addons/maintenance/tests/test_maintenance.py new file mode 100644 index 00000000..6cd04bd4 --- /dev/null +++ b/addons/maintenance/tests/test_maintenance.py @@ -0,0 +1,134 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import time + +from odoo.tests.common import TransactionCase +from dateutil import relativedelta +import datetime + +class TestEquipment(TransactionCase): + """ Test used to check that when doing equipment/maintenance_request/equipment_category creation.""" + + def setUp(self): + super(TestEquipment, self).setUp() + self.equipment = self.env['maintenance.equipment'] + self.maintenance_request = self.env['maintenance.request'] + self.res_users = self.env['res.users'] + self.maintenance_team = self.env['maintenance.team'] + self.main_company = self.env.ref('base.main_company') + res_user = self.env.ref('base.group_user') + res_manager = self.env.ref('maintenance.group_equipment_manager') + + self.user = self.res_users.create(dict( + name="Normal User/Employee", + company_id=self.main_company.id, + login="emp", + email="empuser@yourcompany.example.com", + groups_id=[(6, 0, [res_user.id])] + )) + + self.manager = self.res_users.create(dict( + name="Equipment Manager", + company_id=self.main_company.id, + login="hm", + email="eqmanager@yourcompany.example.com", + groups_id=[(6, 0, [res_manager.id])] + )) + + self.equipment_monitor = self.env['maintenance.equipment.category'].create({ + 'name': 'Monitors - Test', + }) + + def test_10_equipment_request_category(self): + + # Create a new equipment + equipment_01 = self.equipment.with_user(self.manager).create({ + 'name': 'Samsung Monitor "15', + 'category_id': self.equipment_monitor.id, + 'technician_user_id': self.ref('base.user_root'), + 'owner_user_id': self.user.id, + 'assign_date': time.strftime('%Y-%m-%d'), + 'serial_no': 'MT/127/18291015', + 'model': 'NP355E5X', + 'color': 3, + }) + + # Check that equipment is created or not + assert equipment_01, "Equipment not created" + + # Create new maintenance request + maintenance_request_01 = self.maintenance_request.with_user(self.user).create({ + 'name': 'Resolution is bad', + 'user_id': self.user.id, + 'owner_user_id': self.user.id, + 'equipment_id': equipment_01.id, + 'color': 7, + 'stage_id': self.ref('maintenance.stage_0'), + 'maintenance_team_id': self.ref('maintenance.equipment_team_maintenance') + }) + + # I check that maintenance_request is created or not + assert maintenance_request_01, "Maintenance Request not created" + + # I check that Initially maintenance request is in the "New Request" stage + self.assertEqual(maintenance_request_01.stage_id.id, self.ref('maintenance.stage_0')) + + # I check that change the maintenance_request stage on click statusbar + maintenance_request_01.with_user(self.user).write({'stage_id': self.ref('maintenance.stage_1')}) + + # I check that maintenance request is in the "In Progress" stage + self.assertEqual(maintenance_request_01.stage_id.id, self.ref('maintenance.stage_1')) + + def test_20_cron(self): + """ Check the cron creates the necessary preventive maintenance requests""" + equipment_cron = self.equipment.create({ + 'name': 'High Maintenance Monitor because of Color Calibration', + 'category_id': self.equipment_monitor.id, + 'technician_user_id': self.ref('base.user_root'), + 'owner_user_id': self.user.id, + 'assign_date': time.strftime('%Y-%m-%d'), + 'period': 7, + 'color': 3, + }) + + maintenance_request_cron = self.maintenance_request.create({ + 'name': 'Need a special calibration', + 'user_id': self.user.id, + 'request_date': (datetime.datetime.now() + relativedelta.relativedelta(days=7)).strftime('%Y-%m-%d'), + 'maintenance_type': 'preventive', + 'owner_user_id': self.user.id, + 'equipment_id': equipment_cron.id, + 'color': 7, + 'stage_id': self.ref('maintenance.stage_0'), + 'maintenance_team_id': self.ref('maintenance.equipment_team_maintenance') + }) + + self.env['maintenance.equipment']._cron_generate_requests() + # As it is generating the requests for one month in advance, we should have 4 requests in total + tot_requests = self.maintenance_request.search([('equipment_id', '=', equipment_cron.id)]) + self.assertEqual(len(tot_requests), 1, 'The cron should have generated just 1 request for the High Maintenance Monitor.') + + def test_21_cron(self): + """ Check the creation of maintenance requests by the cron""" + + team_test = self.maintenance_team.create({ + 'name': 'team_test', + }) + equipment = self.equipment.create({ + 'name': 'High Maintenance Monitor because of Color Calibration', + 'category_id': self.equipment_monitor.id, + 'technician_user_id': self.ref('base.user_root'), + 'owner_user_id': self.user.id, + 'assign_date': time.strftime('%Y-%m-%d'), + 'period': 7, + 'color': 3, + 'maintenance_team_id': team_test.id, + 'maintenance_duration': 3.0, + }) + + self.env['maintenance.equipment']._cron_generate_requests() + tot_requests = self.maintenance_request.search([('equipment_id', '=', equipment.id)]) + self.assertEqual(len(tot_requests), 1, 'The cron should have generated just 1 request for the High Maintenance Monitor.') + self.assertEqual(tot_requests.maintenance_team_id.id, team_test.id, 'The maintenance team should be the same as equipment one') + self.assertEqual(tot_requests.duration, 3.0, 'Equipement maintenance duration is not the same as the request one') diff --git a/addons/maintenance/tests/test_maintenance_multicompany.py b/addons/maintenance/tests/test_maintenance_multicompany.py new file mode 100644 index 00000000..5ec2a6bc --- /dev/null +++ b/addons/maintenance/tests/test_maintenance_multicompany.py @@ -0,0 +1,175 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import time + +from odoo.tests.common import TransactionCase +from odoo.exceptions import AccessError + + +class TestEquipmentMulticompany(TransactionCase): + + def test_00_equipment_multicompany_user(self): + """Test Check maintenance with equipment manager and user in multi company environment""" + + # Use full models + Equipment = self.env['maintenance.equipment'] + MaintenanceRequest = self.env['maintenance.request'] + Category = self.env['maintenance.equipment.category'] + ResUsers = self.env['res.users'] + ResCompany = self.env['res.company'] + MaintenanceTeam = self.env['maintenance.team'] + + # Use full reference. + group_user = self.env.ref('base.group_user') + group_manager = self.env.ref('maintenance.group_equipment_manager') + + # Company A + company_a = ResCompany.create({ + 'name': 'Company A', + 'currency_id': self.env.ref('base.USD').id, + }) + + # Create one child company having parent company is 'Your company' + company_b = ResCompany.create({ + 'name': 'Company B', + 'currency_id': self.env.ref('base.USD').id, + }) + + # Create equipment manager. + cids = [company_a.id, company_b.id] + equipment_manager = ResUsers.create({ + 'name': 'Equipment Manager', + 'company_id': company_a.id, + 'login': 'e_equipment_manager', + 'email': 'eqmanager@yourcompany.example.com', + 'groups_id': [(6, 0, [group_manager.id])], + 'company_ids': [(6, 0, [company_a.id, company_b.id])] + }) + + # Create equipment user + user = ResUsers.create({ + 'name': 'Normal User/Employee', + 'company_id': company_b.id, + 'login': 'emp', + 'email': 'empuser@yourcompany.example.com', + 'groups_id': [(6, 0, [group_user.id])], + 'company_ids': [(6, 0, [company_b.id])] + }) + + # create a maintenance team for company A user + team = MaintenanceTeam.with_user(equipment_manager).create({ + 'name': 'Metrology', + 'company_id': company_a.id, + }) + # create a maintenance team for company B user + teamb = MaintenanceTeam.with_user(equipment_manager).with_context(allowed_company_ids=cids).create({ + 'name': 'Subcontractor', + 'company_id': company_b.id, + }) + + # User should not able to create equipment category. + with self.assertRaises(AccessError): + Category.with_user(user).create({ + 'name': 'Software', + 'company_id': company_b.id, + 'technician_user_id': user.id, + }) + + # create equipment category for equipment manager + category_1 = Category.with_user(equipment_manager).with_context(allowed_company_ids=cids).create({ + 'name': 'Monitors - Test', + 'company_id': company_b.id, + 'technician_user_id': equipment_manager.id, + }) + + # create equipment category for equipment manager + Category.with_user(equipment_manager).with_context(allowed_company_ids=cids).create({ + 'name': 'Computers - Test', + 'company_id': company_b.id, + 'technician_user_id': equipment_manager.id, + }) + + # create equipment category for equipment user + Category.with_user(equipment_manager).create({ + 'name': 'Phones - Test', + 'company_id': company_a.id, + 'technician_user_id': equipment_manager.id, + }) + + # Check category for user equipment_manager and user + self.assertEqual(Category.with_user(equipment_manager).with_context(allowed_company_ids=cids).search_count([]), 3) + self.assertEqual(Category.with_user(user).search_count([]), 2) + + # User should not able to create equipment. + with self.assertRaises(AccessError): + Equipment.with_user(user).create({ + 'name': 'Samsung Monitor 15', + 'category_id': category_1.id, + 'assign_date': time.strftime('%Y-%m-%d'), + 'company_id': company_b.id, + 'owner_user_id': user.id, + }) + + Equipment.with_user(equipment_manager).with_context(allowed_company_ids=cids).create({ + 'name': 'Acer Laptop', + 'category_id': category_1.id, + 'assign_date': time.strftime('%Y-%m-%d'), + 'company_id': company_b.id, + 'owner_user_id': user.id, + }) + + # create an equipment for user + Equipment.with_user(equipment_manager).with_context(allowed_company_ids=cids).create({ + 'name': 'HP Laptop', + 'category_id': category_1.id, + 'assign_date': time.strftime('%Y-%m-%d'), + 'company_id': company_b.id, + 'owner_user_id': equipment_manager.id, + }) + # Now there are total 2 equipments created and can view by equipment_manager user + self.assertEqual(Equipment.with_user(equipment_manager).with_context(allowed_company_ids=cids).search_count([]), 2) + + # And there is total 1 equipment can be view by Normal User ( Which user is followers) + self.assertEqual(Equipment.with_user(user).search_count([]), 1) + + # create an equipment team BY user + with self.assertRaises(AccessError): + MaintenanceTeam.with_user(user).create({ + 'name': 'Subcontractor', + 'company_id': company_b.id, + }) + + # create an equipment category BY user + with self.assertRaises(AccessError): + Category.with_user(user).create({ + 'name': 'Computers', + 'company_id': company_b.id, + 'technician_user_id': user.id, + }) + + # create an maintenance stage BY user + with self.assertRaises(AccessError): + self.env['maintenance.stage'].with_user(user).create({ + 'name': 'identify corrective maintenance requirements', + }) + + # Create an maintenance request for ( User Follower ). + MaintenanceRequest.with_user(user).create({ + 'name': 'Some keys are not working', + 'company_id': company_b.id, + 'user_id': user.id, + 'owner_user_id': user.id, + }) + + # Create an maintenance request for equipment_manager (Admin Follower) + MaintenanceRequest.with_user(equipment_manager).create({ + 'name': 'Battery drains fast', + 'company_id': company_a.id, + 'user_id': equipment_manager.id, + 'owner_user_id': equipment_manager.id, + }) + + # Now here is total 1 maintenance request can be view by Normal User + self.assertEqual(MaintenanceRequest.with_user(equipment_manager).with_context(allowed_company_ids=cids).search_count([]), 2) + self.assertEqual(MaintenanceRequest.with_user(user).search_count([]), 1) |
