Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17.0][FIX] hr_expense_*: Fix + improve tests #279

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 25 additions & 53 deletions hr_expense_cancel/tests/test_hr_expense_cancel.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,45 @@
# Copyright 2019 Tecnativa - Ernesto Tejeda
# Copyright 2024 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.exceptions import UserError
from odoo.tests.common import Form, TransactionCase
from odoo.tests.common import Form, tagged

from odoo.addons.hr_expense.tests.common import TestExpenseCommon

class TestHrExpenseCancel(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env["res.partner"].create({"name": "Test partner"})
self.payment_obj = self.env["account.payment"]
self.account_payment_register = self.env["account.payment.register"]
self.payment_journal = self.env["account.journal"].search(
[("type", "in", ["cash", "bank"])], limit=1
)

self.main_company = company = self.env.ref("base.main_company")
self.expense_journal = self.env["account.journal"].create(
{
"name": "Purchase Journal - Test",
"code": "HRTPJ",
"type": "purchase",
"company_id": company.id,
}
)

self.expense = self.env["hr.expense"].create(
{
"name": "Expense test",
"employee_id": self.ref("hr.employee_admin"),
"product_id": self.ref(
"hr_expense.expense_product_travel_accommodation"
),
"total_amount": 10,
}
)
self.expense.action_submit_expenses()

self.expense_sheet = self.expense.sheet_id
self.expense_sheet.journal_id = self.expense_journal
self.expense_sheet.action_approve_expense_sheets()
@tagged("-at_install", "post_install")
class TestHrExpenseCancel(TestExpenseCommon):
@classmethod
def setUpClass(cls, chart_template_ref=None):
super().setUpClass(chart_template_ref=chart_template_ref)
# Create expense + sheet + approve
cls.expense = cls.create_expense(cls)
res = cls.expense.action_submit_expenses()
cls.expense_sheet = cls.env[res["res_model"]].browse(res["res_id"])
cls.expense_sheet.action_approve_expense_sheets()

def _get_payment_wizard(self, expense_sheet):
action = expense_sheet.action_register_payment()
ctx = action.get("context")
with Form(
self.account_payment_register.with_context(**ctx),
view="account.view_account_payment_register_form",
) as f:
f.amount = expense_sheet.total_amount
register_payment = f.save()
return register_payment
def _get_payment_wizard(self):
res = self.expense_sheet.action_register_payment()
register_form = Form(self.env[res["res_model"]].with_context(**res["context"]))
register_form.journal_id = self.company_data["default_journal_bank"]
register_form.amount = self.expense_sheet.total_amount
return register_form.save()

def test_action_cancel_posted(self):
self.expense_sheet.action_sheet_move_create()

self.assertFalse(len(self.expense_sheet.payment_ids), 1)
self.assertTrue(self.expense_sheet.account_move_ids)

self.expense_sheet.action_cancel()

self.assertFalse(self.expense_sheet.payment_ids)
self.assertFalse(self.expense_sheet.account_move_ids)

def test_action_cancel_no_update_posted(self):
journals = self.payment_journal | self.expense_journal
journals = (
self.company_data["default_journal_bank"]
| self.company_data["default_journal_purchase"]
)
journals.write({"restrict_mode_hash_table": True})
with self.assertRaises(UserError):
self.test_action_cancel_company_account()
Expand All @@ -73,19 +48,16 @@ def test_action_cancel_no_update_posted(self):

def test_action_cancel_company_account(self):
self.expense.payment_mode = "company_account"
self.expense_sheet.journal_id = self.payment_journal
self.expense_sheet.journal_id = self.company_data["default_journal_bank"]
self.expense_sheet.action_sheet_move_create()
self.assertTrue(self.expense_sheet.account_move_ids)
self.expense_sheet.action_cancel()
self.assertFalse(self.expense_sheet.account_move_ids)

def test_action_cancel_own_account(self):
self.expense_sheet.action_sheet_move_create()

payment_wizard = self._get_payment_wizard(self.expense_sheet)
payment_wizard = self._get_payment_wizard()
payment_wizard.action_create_payments()

self.assertTrue(self.expense_sheet.payment_ids)

self.expense_sheet.action_cancel() # assertFalse(payment.exist)
self.assertFalse(self.expense_sheet.payment_ids.state != "cancel")
7 changes: 1 addition & 6 deletions hr_expense_invoice/tests/test_hr_expense_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import base64

from odoo import Command, fields
from odoo import fields
from odoo.exceptions import UserError, ValidationError
from odoo.tests import tagged
from odoo.tests.common import Form
Expand Down Expand Up @@ -35,11 +35,6 @@ def setUpClass(cls, chart_template_ref=None):
"invoice_date": fields.Date.today(),
}
)
cls.expense_employee.bank_account_id = Command.create(
{
"acc_number": "FR20 1242 1242 1242 1242 1242 124",
}
)
cls.expense = cls.env["hr.expense"].create(
{
"name": "Expense test",
Expand Down
74 changes: 20 additions & 54 deletions hr_expense_payment/tests/test_hr_expense_payment.py
Original file line number Diff line number Diff line change
@@ -1,85 +1,51 @@
# Copyright 2019 Tecnativa - Ernesto Tejeda
# Copyright 2021 Ecosoft Co., Ltd (http://ecosoft.co.th/)
# Copyright 2024 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.tests.common import Form, TransactionCase
from odoo.tests import Form, tagged

from odoo.addons.hr_expense.tests.common import TestExpenseCommon

from ..hooks import post_init_hook


class TestHrExpensePayment(TransactionCase):
@tagged("-at_install", "post_install")
class TestHrExpensePayment(TestExpenseCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.account_payment_register = cls.env["account.payment.register"]
cls.payment_journal = cls.env["account.journal"].search(
[("type", "in", ["cash", "bank"])], limit=1
)

company = cls.env.ref("base.main_company")
cls.expense_journal = cls.env["account.journal"].create(
{
"name": "Purchase Journal - Test",
"code": "HRTPJ",
"type": "purchase",
"company_id": company.id,
}
)

cls.expense_sheet = cls.env["hr.expense.sheet"].create(
{
"employee_id": cls.env.ref("hr.employee_admin").id,
"name": "Expense test",
"journal_id": cls.expense_journal.id,
}
)
def setUpClass(cls, chart_template_ref=None):
super().setUpClass(chart_template_ref=chart_template_ref)
# Create expense + sheet + approve
cls.expense = cls.create_expense(cls)
res = cls.expense.action_submit_expenses()
cls.expense_sheet = cls.env[res["res_model"]].browse(res["res_id"])
cls.expense_sheet.action_approve_expense_sheets()

cls.expense = cls.env["hr.expense"].create(
{
"name": "Expense test",
"employee_id": cls.env.ref("hr.employee_admin").id,
"product_id": cls.env.ref("hr_expense.expense_product_meal").id,
"total_amount": 1000,
"sheet_id": cls.expense_sheet.id,
}
)

def _get_payment_wizard(self, expense_sheet):
action = expense_sheet.action_register_payment()
ctx = action.get("context")
with Form(
self.account_payment_register.with_context(**ctx),
view="account.view_account_payment_register_form",
) as f:
f.journal_id = self.payment_journal
f.amount = self.expense_sheet.total_amount
register_payment = f.save()
return register_payment
def _get_payment_wizard(self):
res = self.expense_sheet.action_register_payment()
register_form = Form(self.env[res["res_model"]].with_context(**res["context"]))
register_form.journal_id = self.company_data["default_journal_bank"]
register_form.amount = self.expense_sheet.total_amount
return register_form.save()

def test_post_init_hook(self):
self.expense_sheet.action_sheet_move_create()
payment_wizard = self._get_payment_wizard(self.expense_sheet)
payment_wizard = self._get_payment_wizard()
payment_wizard.action_create_payments()

payment = self.expense_sheet.payment_ids

self.assertEqual(len(payment), 1)
self.assertEqual(len(payment.expense_sheet_ids), 1)

payment.expense_sheet_ids = False
# Recompute many2one
payment = self.expense_sheet.payment_ids

self.assertFalse(payment)
self.assertFalse(payment.expense_sheet_ids)
post_init_hook(self.env)

self.assertEqual(len(self.expense_sheet.payment_ids), 1)

def test_get_payment_vals(self):
self.expense_sheet.action_sheet_move_create()
payment_wizard = self._get_payment_wizard(self.expense_sheet)
payment_wizard = self._get_payment_wizard()
self.assertFalse(self.expense_sheet.payment_ids)
payment_wizard.action_create_payments()
self.assertEqual(len(self.expense_sheet.payment_ids), 1)
Loading