odex30_standard/exp_official_mission/tests/test_official_mission.py

155 lines
5.3 KiB
Python

# -*- coding: utf-8 -*-
from odoo.tests.common import TransactionCase
from odoo.exceptions import UserError, ValidationError
from odoo.tests import tagged
from datetime import date, timedelta
@tagged('post_install', '-at_install')
class TestOfficialMission(TransactionCase):
def setUp(self):
super(TestOfficialMission, self).setUp()
self.company = self.env.company
self.account = self.env['account.account'].create({
'name': 'Mission Expense',
'code': '600000',
'account_type': 'expense',
'reconcile': False,
})
self.journal = self.env['account.journal'].create({
'name': 'Mission Journal',
'type': 'general',
'code': 'MISS',
'default_account_id': self.account.id,
})
self.emp_type = self.env['hr.contract.type'].create({'name': 'Permanent'})
self.employee = self.env['hr.employee'].create({
'name': 'Test Employee',
'work_email': 'test@example.com',
'employee_type_id': self.emp_type.id,
})
self.contract = self.env['hr.contract'].create({
'name': 'Test Contract',
'employee_id': self.employee.id,
'wage': 5000,
'state': 'open',
})
self.employee.contract_id = self.contract
self.mission_type = self.env['hr.official.mission.type'].create({
'name': 'External Mission',
'duration_type': 'days',
'related_with_financial': True,
'type_of_payment': 'fixed',
'day_price': 100.0,
'journal_id': self.journal.id,
'account_id': self.account.id,
'transfer_by_emp_type': False,
'total_months': 12,
'max_request_number': 5,
})
def test_01_duration_calculation(self):
date_from = date.today()
date_to = date.today() + timedelta(days=4)
mission = self.env['hr.official.mission'].create({
'mission_type': self.mission_type.id,
'date_from': date_from,
'date_to': date_to,
})
mission._get_mission_no()
expected_days = 5
self.assertEqual(mission.date_duration, expected_days, "Duration in days calculated incorrectly")
def test_02_workflow_and_financials(self):
mission = self.env['hr.official.mission'].create({
'mission_type': self.mission_type.id,
'date_from': date.today(),
'date_to': date.today() + timedelta(days=2),
'move_type': 'accounting',
})
mission_line = self.env['hr.official.mission.employee'].create({
'official_mission_id': mission.id,
'employee_id': self.employee.id,
'date_from': mission.date_from,
'date_to': mission.date_to,
'hour_from': 8.0,
'hour_to': 16.0,
})
mission_line.days = 3
mission_line.amount = 300.0
self.assertEqual(mission_line.days, 3, "Employee line days incorrect")
self.assertEqual(mission_line.amount, 300.0, "Employee amount calculation incorrect")
mission.send()
self.assertEqual(mission.state, 'send')
mission.accounting_manager()
mission.depart_manager()
mission.approve()
self.assertEqual(mission.state, 'approve', "Mission should be approved")
self.assertTrue(mission_line.account_move_id, "Journal Entry should be created")
self.assertEqual(mission_line.account_move_id.state, 'draft', "Journal Entry should be draft initially")
move_lines = mission_line.account_move_id.line_ids
debit_line = move_lines.filtered(lambda l: l.debit > 0)
self.assertEqual(debit_line.debit, 300.0, "Journal Entry amount mismatch")
def test_03_overlap_constraint(self):
mission1 = self.env['hr.official.mission'].create({
'mission_type': self.mission_type.id,
'date_from': date.today(),
'date_to': date.today() + timedelta(days=5),
})
line1 = self.env['hr.official.mission.employee'].create({
'official_mission_id': mission1.id,
'employee_id': self.employee.id,
'date_from': date.today(),
'date_to': date.today() + timedelta(days=5),
'hour_from': 8,
'hour_to': 16,
})
mission1.state = 'approve'
mission2 = self.env['hr.official.mission'].create({
'mission_type': self.mission_type.id,
'date_from': date.today() + timedelta(days=2),
'date_to': date.today() + timedelta(days=6),
})
with self.assertRaises(ValidationError):
self.env['hr.official.mission.employee'].create({
'official_mission_id': mission2.id,
'employee_id': self.employee.id,
'date_from': date.today() + timedelta(days=2),
'date_to': date.today() + timedelta(days=6),
'hour_from': 8,
'hour_to': 16,
})
def test_04_employees_required(self):
mission = self.env['hr.official.mission'].create({
'mission_type': self.mission_type.id,
'date_from': date.today(),
'date_to': date.today(),
})
with self.assertRaises(UserError):
mission.send()