-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] account_analytic_line_split: add unit tests
- Loading branch information
1 parent
5af8ce6
commit 83cc187
Showing
10 changed files
with
253 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,5 +16,4 @@ | |
"wizard/analytic_line_split_wizard.xml", | ||
"security/ir.model.access.csv", | ||
], | ||
"demo": [], | ||
} |
15 changes: 4 additions & 11 deletions
15
account_analytic_line_split/models/account_analytic_line.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,25 @@ | ||
# Copyright 2024 - TODAY, Wesley Oliveira <[email protected]> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import api, fields, models | ||
from odoo import fields, models | ||
|
||
|
||
class AccountAnalyticLine(models.Model): | ||
_inherit = "account.analytic.line" | ||
|
||
parent_id = fields.Many2one( | ||
parent_line_id = fields.Many2one( | ||
comodel_name="account.analytic.line", | ||
string="Origin", | ||
ondelete="cascade", | ||
readonly=True, | ||
) | ||
child_ids = fields.One2many( | ||
child_line_ids = fields.One2many( | ||
comodel_name="account.analytic.line", | ||
inverse_name="parent_id", | ||
inverse_name="parent_line_id", | ||
string="Child Lines", | ||
compute="_compute_child_ids", | ||
store=True, | ||
readonly=True, | ||
) | ||
|
||
@api.depends("parent_id") | ||
def _compute_child_ids(self): | ||
for line in self: | ||
line.child_ids = self.search([("parent_id", "=", line.id)]) | ||
|
||
def action_edit_analytic_line(self): | ||
context = dict(self.env.context) | ||
context["form_view_initial_mode"] = "edit" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from . import test_account_analytic_line | ||
from . import test_account_move | ||
from . import test_analytic_line_split_wizard |
34 changes: 34 additions & 0 deletions
34
account_analytic_line_split/tests/test_account_analytic_line.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright 2024 - TODAY, Wesley Oliveira <[email protected]> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from odoo.tests.common import SavepointCase | ||
|
||
|
||
class TestAccountAnalyticLine(SavepointCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
super(TestAccountAnalyticLine, cls).setUpClass() | ||
|
||
cls.analytic_account = cls.env["account.analytic.account"].create( | ||
{ | ||
"name": "Test Analytic Account", | ||
} | ||
) | ||
cls.analytic_line = cls.env["account.analytic.line"].create( | ||
{ | ||
"name": "Test Analytic Line", | ||
"amount": 100, | ||
"account_id": cls.analytic_account.id, | ||
} | ||
) | ||
|
||
def test_action_edit_analytic_line(self): | ||
action = self.analytic_line.action_edit_analytic_line() | ||
self.assertEqual(action["res_id"], self.analytic_line.id) | ||
self.assertEqual(action["views"], [(False, "form")]) | ||
|
||
def test_action_split_analytic_line(self): | ||
action = self.analytic_line.action_split_analytic_line() | ||
self.assertEqual(action["context"]["active_id"], self.analytic_line.id) | ||
self.assertEqual(action["context"]["account_id"], self.analytic_account.id) | ||
self.assertEqual(action["context"]["amount"], self.analytic_line.amount) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright 2024 - TODAY, Wesley Oliveira <[email protected]> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from odoo.tests.common import SavepointCase | ||
|
||
|
||
class TestAccountMove(SavepointCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
super(TestAccountMove, cls).setUpClass() | ||
|
||
cls.account = cls.env["account.account"].create( | ||
{ | ||
"code": "test_account_01", | ||
"name": "test account", | ||
"user_type_id": cls.env.ref("account.data_account_type_receivable").id, | ||
"reconcile": True, | ||
} | ||
) | ||
cls.account_2 = cls.env["account.account"].create( | ||
{ | ||
"code": "test_account_02", | ||
"name": "test account 2", | ||
"user_type_id": cls.env.ref("account.data_account_type_payable").id, | ||
"reconcile": True, | ||
} | ||
) | ||
cls.analytic_account = cls.env["account.analytic.account"].create( | ||
{ | ||
"name": "Test Analytic Account", | ||
} | ||
) | ||
cls.analytic_line = cls.env["account.analytic.line"].create( | ||
{ | ||
"name": "Test Analytic Line", | ||
"amount": 100, | ||
"account_id": cls.analytic_account.id, | ||
} | ||
) | ||
cls.account_move = cls.env["account.move"].create( | ||
{ | ||
"name": "Test Move", | ||
"line_ids": [ | ||
( | ||
0, | ||
0, | ||
{ | ||
"name": "Test Move Line", | ||
"debit": 50, | ||
"credit": 0, | ||
"account_id": cls.account.id, | ||
"analytic_account_id": cls.analytic_account.id, | ||
"analytic_line_ids": [(6, 0, [cls.analytic_line.id])], | ||
}, | ||
), | ||
( | ||
0, | ||
0, | ||
{ | ||
"name": "Test Move Line", | ||
"debit": 0, | ||
"credit": 50, | ||
"account_id": cls.account_2.id, | ||
}, | ||
), | ||
], | ||
} | ||
) | ||
|
||
def test_compute_analytic_line_ids(self): | ||
self.account_move._compute_analytic_line_ids() | ||
self.assertIn(self.analytic_line, self.account_move.analytic_line_ids) |
71 changes: 71 additions & 0 deletions
71
account_analytic_line_split/tests/test_analytic_line_split_wizard.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Copyright 2024 - TODAY, Wesley Oliveira <[email protected]> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from odoo.tests.common import SavepointCase | ||
|
||
|
||
class TestAnalyticLineSplitWizard(SavepointCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
super(TestAnalyticLineSplitWizard, cls).setUpClass() | ||
|
||
cls.wizard_obj = cls.env["analytic.line.split.wizard"] | ||
cls.split_obj = cls.env["analytic.line.split"] | ||
cls.analytic_account = cls.env["account.analytic.account"].create( | ||
{ | ||
"name": "Test Analytic Account", | ||
} | ||
) | ||
cls.analytic_line = cls.env["account.analytic.line"].create( | ||
{ | ||
"name": "Test Analytic Line", | ||
"amount": 100, | ||
"account_id": cls.analytic_account.id, | ||
} | ||
) | ||
cls.wizard = cls.wizard_obj.create( | ||
{ | ||
"line_id": cls.analytic_line.id, | ||
"amount_total": 100, | ||
} | ||
) | ||
|
||
def test_compute_percentage(self): | ||
line_split = self.split_obj.create( | ||
{ | ||
"wizard_id": self.wizard.id, | ||
"account_id": self.analytic_account.id, | ||
"percentage": 30, | ||
} | ||
) | ||
self.wizard._compute_percentage() | ||
self.assertEqual(self.wizard.percentage, 70) | ||
self.assertEqual(line_split.percentage, 30) | ||
|
||
def test_compute_amount(self): | ||
line_split = self.split_obj.create( | ||
{ | ||
"wizard_id": self.wizard.id, | ||
"account_id": self.analytic_account.id, | ||
"percentage": 30, | ||
} | ||
) | ||
self.wizard._compute_amount() | ||
self.assertEqual(self.wizard.amount, 70) | ||
self.assertEqual(line_split.amount, 30) | ||
|
||
def test_action_split_line(self): | ||
line_split = self.split_obj.create( | ||
{ | ||
"wizard_id": self.wizard.id, | ||
"account_id": self.analytic_account.id, | ||
"percentage": 30, | ||
} | ||
) | ||
self.wizard.action_split_line() | ||
self.assertEqual(self.analytic_line.amount, 70) | ||
self.assertEqual(line_split.amount, 30) | ||
new_line = self.env["account.analytic.line"].search( | ||
[("parent_line_id", "=", self.analytic_line.id)] | ||
) | ||
self.assertIn(new_line, self.analytic_line.child_line_ids) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from . import analytic_line_split_wizard | ||
from . import analytic_line_split |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2024 - TODAY, Wesley Oliveira <[email protected]> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import _, api, fields, models | ||
from odoo.exceptions import ValidationError | ||
|
||
|
||
class AnalyticLineSplit(models.TransientModel): | ||
_name = "analytic.line.split" | ||
_description = "Analytic Line Split" | ||
|
||
def _get_default_line_id(self): | ||
analytic_line_id = self.env.context.get("line_id") | ||
return self.env["account.analytic.line"].browse(analytic_line_id) | ||
|
||
wizard_id = fields.Many2one( | ||
comodel_name="analytic.line.split.wizard", | ||
required=True, | ||
ondelete="cascade", | ||
) | ||
line_id = fields.Many2one( | ||
comodel_name="account.analytic.line", | ||
string="Analytic Line", | ||
readonly=True, | ||
default=_get_default_line_id, | ||
) | ||
name = fields.Char( | ||
string="Name", | ||
related="account_id.name", | ||
readonly=True, | ||
) | ||
account_id = fields.Many2one( | ||
comodel_name="account.analytic.account", | ||
string="Analytic Account", | ||
required=True, | ||
) | ||
percentage = fields.Float( | ||
string="Percentage", | ||
required=True, | ||
default=0.0, | ||
) | ||
amount = fields.Float( | ||
string="Amount", | ||
readonly=True, | ||
compute="_compute_amount", | ||
store=True, | ||
) | ||
|
||
@api.constrains("percentage") | ||
def _check_percentage(self): | ||
for line in self: | ||
if not (0 < line.percentage < 100): | ||
raise ValidationError( | ||
_("Percentage must be greater than 0 and less than 100.") | ||
) | ||
|
||
@api.depends("wizard_id.amount_total", "percentage") | ||
def _compute_amount(self): | ||
for line in self: | ||
line.amount = line.wizard_id.amount_total * (line.percentage / 100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters