Skip to content

Commit

Permalink
[IMP] account_analytic_line_split: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WesleyOliveira98 committed Aug 22, 2024
1 parent 5af8ce6 commit 83cc187
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 81 deletions.
1 change: 0 additions & 1 deletion account_analytic_line_split/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@
"wizard/analytic_line_split_wizard.xml",
"security/ir.model.access.csv",
],
"demo": [],
}
15 changes: 4 additions & 11 deletions account_analytic_line_split/models/account_analytic_line.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
# Copyright 2024 - TODAY, Wesley Oliveira <wesley.oliveira@escodoo.com.br>
# 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"
Expand Down
3 changes: 3 additions & 0 deletions account_analytic_line_split/tests/__init__.py
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 account_analytic_line_split/tests/test_account_analytic_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2024 - TODAY, Wesley Oliveira <wesley.oliveira@escodoo.com.br>
# 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)
72 changes: 72 additions & 0 deletions account_analytic_line_split/tests/test_account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright 2024 - TODAY, Wesley Oliveira <wesley.oliveira@escodoo.com.br>
# 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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright 2024 - TODAY, Wesley Oliveira <wesley.oliveira@escodoo.com.br>
# 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)
8 changes: 4 additions & 4 deletions account_analytic_line_split/views/account_analytic_line.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
</sheet>
<field name="name" position="before">
<field
name="parent_id"
attrs="{'invisible': [('parent_id', '=', False)]}"
name="parent_line_id"
attrs="{'invisible': [('parent_line_id', '=', False)]}"
/>
</field>
<sheet position="inside">
<notebook attrs="{'invisible': [('child_ids', '=', [])]}">
<notebook attrs="{'invisible': [('child_line_ids', '=', [])]}">
<page string="Child Lines">
<field name="child_ids" />
<field name="child_line_ids" />
</page>
</notebook>
</sheet>
Expand Down
1 change: 1 addition & 0 deletions account_analytic_line_split/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import analytic_line_split_wizard
from . import analytic_line_split
60 changes: 60 additions & 0 deletions account_analytic_line_split/wizard/analytic_line_split.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2024 - TODAY, Wesley Oliveira <wesley.oliveira@escodoo.com.br>
# 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(

Check warning on line 53 in account_analytic_line_split/wizard/analytic_line_split.py

View check run for this annotation

Codecov / codecov/patch

account_analytic_line_split/wizard/analytic_line_split.py#L53

Added line #L53 was not covered by tests
_("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)
69 changes: 4 additions & 65 deletions account_analytic_line_split/wizard/analytic_line_split_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,78 +73,17 @@ def _compute_amount(self):
@api.onchange("analytic_line_split_ids")
def _onchange_analytic_line_split_ids(self):
total_percentage = sum(line.percentage for line in self.analytic_line_split_ids)
if total_percentage > 100:
raise ValidationError(_("The total percentage cannot exceed 100%!"))
if total_percentage >= 100:
raise ValidationError(_("The total percentage cannot exceed or be 100%!"))

Check warning on line 77 in account_analytic_line_split/wizard/analytic_line_split_wizard.py

View check run for this annotation

Codecov / codecov/patch

account_analytic_line_split/wizard/analytic_line_split_wizard.py#L77

Added line #L77 was not covered by tests

def action_split_line(self):
self.line_id.write(
{
"amount": self.amount,
}
)
self.line_id.write({"amount": self.amount})
for line in self.analytic_line_split_ids:
copy_line_id = self.line_id.copy()
copy_line_id.write(
{
"account_id": line.account_id,
"amount": line.amount,
"parent_id": self.line_id,
"parent_line_id": self.line_id,
}
)


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 or equal to 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)

0 comments on commit 83cc187

Please sign in to comment.