Skip to content

Commit

Permalink
[IMP] mis_builder: test performance improvement
Browse files Browse the repository at this point in the history
- Create setUpClass method
- Include context keys for avoiding mail operations overhead.
  • Loading branch information
josep-tecnativa committed Oct 27, 2023
1 parent e2b1388 commit eac5a0b
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 190 deletions.
79 changes: 47 additions & 32 deletions mis_builder/tests/test_aep.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,67 +13,81 @@


class TestAEP(common.TransactionCase):
def setUp(self):
super().setUp()
self.res_company = self.env["res.company"]
self.account_model = self.env["account.account"]
self.move_model = self.env["account.move"]
self.journal_model = self.env["account.journal"]
self.curr_year = datetime.date.today().year
self.prev_year = self.curr_year - 1
@classmethod
def setUpClass(cls):
super().setUpClass()
# Remove this variable in v16 and put instead:
# from odoo.addons.base.tests.common import DISABLED_MAIL_CONTEXT
DISABLED_MAIL_CONTEXT = {
"tracking_disable": True,
"mail_create_nolog": True,
"mail_create_nosubscribe": True,
"mail_notrack": True,
"no_reset_password": True,
}
cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT))
cls.res_company = cls.env["res.company"]
cls.account_model = cls.env["account.account"]
cls.move_model = cls.env["account.move"]
cls.journal_model = cls.env["account.journal"]
cls.curr_year = datetime.date.today().year
cls.prev_year = cls.curr_year - 1
# create company
self.company = self.res_company.create({"name": "AEP Company"})
cls.company = cls.res_company.create({"name": "AEP Company"})
# create receivable bs account
type_ar = self.browse_ref("account.data_account_type_receivable")
self.account_ar = self.account_model.create(
type_ar = cls.env.ref("account.data_account_type_receivable")
cls.account_ar = cls.account_model.create(
{
"company_id": self.company.id,
"company_id": cls.company.id,
"code": "400AR",
"name": "Receivable",
"user_type_id": type_ar.id,
"reconcile": True,
}
)
# create income pl account
type_in = self.browse_ref("account.data_account_type_revenue")
self.account_in = self.account_model.create(
type_in = cls.env.ref("account.data_account_type_revenue")
cls.account_in = cls.account_model.create(
{
"company_id": self.company.id,
"company_id": cls.company.id,
"code": "700IN",
"name": "Income",
"user_type_id": type_in.id,
}
)
# create journal
self.journal = self.journal_model.create(
cls.journal = cls.journal_model.create(
{
"company_id": self.company.id,
"company_id": cls.company.id,
"name": "Sale journal",
"code": "VEN",
"type": "sale",
}
)
# create move in December last year
self._create_move(
date=datetime.date(self.prev_year, 12, 1),
cls._create_move(
date=datetime.date(cls.prev_year, 12, 1),
amount=100,
debit_acc=self.account_ar,
credit_acc=self.account_in,
debit_acc=cls.account_ar,
credit_acc=cls.account_in,
)
# create move in January this year
self._create_move(
date=datetime.date(self.curr_year, 1, 1),
cls._create_move(
date=datetime.date(cls.curr_year, 1, 1),
amount=300,
debit_acc=self.account_ar,
credit_acc=self.account_in,
debit_acc=cls.account_ar,
credit_acc=cls.account_in,
)
# create move in March this year
self._create_move(
date=datetime.date(self.curr_year, 3, 1),
cls._create_move(
date=datetime.date(cls.curr_year, 3, 1),
amount=500,
debit_acc=self.account_ar,
credit_acc=self.account_in,
debit_acc=cls.account_ar,
credit_acc=cls.account_in,
)

def setUp(self):
super().setUp()
# create the AEP, and prepare the expressions we'll need
self.aep = AEP(self.company)
self.aep.parse_expr("bali[]")
Expand Down Expand Up @@ -109,10 +123,11 @@ def setUp(self):
self.aep.parse_expr("bal_700IN") # deprecated
self.aep.parse_expr("bals[700IN]") # deprecated

def _create_move(self, date, amount, debit_acc, credit_acc, post=True):
move = self.move_model.create(
@classmethod
def _create_move(cls, date, amount, debit_acc, credit_acc, post=True):
move = cls.move_model.create(
{
"journal_id": self.journal.id,
"journal_id": cls.journal.id,
"date": fields.Date.to_string(date),
"line_ids": [
(0, 0, {"name": "/", "debit": amount, "account_id": debit_acc.id}),
Expand Down
19 changes: 15 additions & 4 deletions mis_builder/tests/test_analytic_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@


class TestAnalyticFilters(TransactionCase):
def setUp(self):
super().setUp()
self.aag = self.env["account.analytic.group"].search([], limit=1)
self.aat = self.env["account.analytic.tag"].search([], limit=1)
@classmethod
def setUpClass(cls):
super().setUpClass()
# Remove this variable in v16 and put instead:
# from odoo.addons.base.tests.common import DISABLED_MAIL_CONTEXT
DISABLED_MAIL_CONTEXT = {
"tracking_disable": True,
"mail_create_nolog": True,
"mail_create_nosubscribe": True,
"mail_notrack": True,
"no_reset_password": True,
}
cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT))
cls.aag = cls.env["account.analytic.group"].search([], limit=1)
cls.aat = cls.env["account.analytic.tag"].search([], limit=1)

def test_context_with_filters(self):
aaa = self.env["account.analytic.account"].search([], limit=1)
Expand Down
90 changes: 51 additions & 39 deletions mis_builder/tests/test_data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
class TestMisReportInstanceDataSources(common.TransactionCase):
"""Test sum and comparison data source."""

def _create_move(self, date, amount, debit_acc, credit_acc):
move = self.move_model.create(
@classmethod
def _create_move(cls, date, amount, debit_acc, credit_acc):
move = cls.move_model.create(
{
"journal_id": self.journal.id,
"journal_id": cls.journal.id,
"date": date,
"line_ids": [
(0, 0, {"name": "/", "debit": amount, "account_id": debit_acc.id}),
Expand All @@ -35,106 +36,117 @@ def _create_move(self, date, amount, debit_acc, credit_acc):
move._post()
return move

def setUp(self):
super().setUp()
self.account_model = self.env["account.account"]
self.move_model = self.env["account.move"]
self.journal_model = self.env["account.journal"]
@classmethod
def setUpClass(cls):
super().setUpClass()
# Remove this variable in v16 and put instead:
# from odoo.addons.base.tests.common import DISABLED_MAIL_CONTEXT
DISABLED_MAIL_CONTEXT = {
"tracking_disable": True,
"mail_create_nolog": True,
"mail_create_nosubscribe": True,
"mail_notrack": True,
"no_reset_password": True,
}
cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT))
cls.account_model = cls.env["account.account"]
cls.move_model = cls.env["account.move"]
cls.journal_model = cls.env["account.journal"]
# create receivable bs account
type_ar = self.browse_ref("account.data_account_type_receivable")
self.account_ar = self.account_model.create(
type_ar = cls.env.ref("account.data_account_type_receivable")
cls.account_ar = cls.account_model.create(
{
"company_id": self.env.user.company_id.id,
"company_id": cls.env.user.company_id.id,
"code": "400AR",
"name": "Receivable",
"user_type_id": type_ar.id,
"reconcile": True,
}
)
# create income account
type_in = self.browse_ref("account.data_account_type_revenue")
self.account_in = self.account_model.create(
type_in = cls.env.ref("account.data_account_type_revenue")
cls.account_in = cls.account_model.create(
{
"company_id": self.env.user.company_id.id,
"company_id": cls.env.user.company_id.id,
"code": "700IN",
"name": "Income",
"user_type_id": type_in.id,
}
)
self.account_in2 = self.account_model.create(
cls.account_in2 = cls.account_model.create(
{
"company_id": self.env.user.company_id.id,
"company_id": cls.env.user.company_id.id,
"code": "700IN2",
"name": "Income",
"user_type_id": type_in.id,
}
)
# create journal
self.journal = self.journal_model.create(
cls.journal = cls.journal_model.create(
{
"company_id": self.env.user.company_id.id,
"company_id": cls.env.user.company_id.id,
"name": "Sale journal",
"code": "VEN",
"type": "sale",
}
)
# create move
self._create_move(
cls._create_move(
date="2017-01-01",
amount=11,
debit_acc=self.account_ar,
credit_acc=self.account_in,
debit_acc=cls.account_ar,
credit_acc=cls.account_in,
)
# create move
self._create_move(
cls._create_move(
date="2017-02-01",
amount=13,
debit_acc=self.account_ar,
credit_acc=self.account_in,
debit_acc=cls.account_ar,
credit_acc=cls.account_in,
)
self._create_move(
cls._create_move(
date="2017-02-01",
amount=17,
debit_acc=self.account_ar,
credit_acc=self.account_in2,
debit_acc=cls.account_ar,
credit_acc=cls.account_in2,
)
# create report
self.report = self.env["mis.report"].create(dict(name="test report"))
self.kpi1 = self.env["mis.report.kpi"].create(
cls.report = cls.env["mis.report"].create(dict(name="test report"))
cls.kpi1 = cls.env["mis.report.kpi"].create(
dict(
report_id=self.report.id,
report_id=cls.report.id,
name="k1",
description="kpi 1",
expression="-balp[700IN]",
compare_method=CMP_DIFF,
)
)
self.expr1 = self.kpi1.expression_ids[0]
self.kpi2 = self.env["mis.report.kpi"].create(
cls.expr1 = cls.kpi1.expression_ids[0]
cls.kpi2 = cls.env["mis.report.kpi"].create(
dict(
report_id=self.report.id,
report_id=cls.report.id,
name="k2",
description="kpi 2",
expression="-balp[700%]",
compare_method=CMP_DIFF,
auto_expand_accounts=True,
)
)
self.instance = self.env["mis.report.instance"].create(
dict(name="test instance", report_id=self.report.id, comparison_mode=True)
cls.instance = cls.env["mis.report.instance"].create(
dict(name="test instance", report_id=cls.report.id, comparison_mode=True)
)
self.p1 = self.env["mis.report.instance.period"].create(
cls.p1 = cls.env["mis.report.instance.period"].create(
dict(
name="p1",
report_instance_id=self.instance.id,
report_instance_id=cls.instance.id,
manual_date_from="2017-01-01",
manual_date_to="2017-01-31",
)
)
self.p2 = self.env["mis.report.instance.period"].create(
cls.p2 = cls.env["mis.report.instance.period"].create(
dict(
name="p2",
report_instance_id=self.instance.id,
report_instance_id=cls.instance.id,
manual_date_from="2017-02-01",
manual_date_to="2017-02-28",
)
Expand Down
10 changes: 10 additions & 0 deletions mis_builder/tests/test_kpi_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class MisKpiDataTestItem(models.Model):
@classmethod
def setUpClass(cls):
super().setUpClass()
# Remove this variable in v16 and put instead:
# from odoo.addons.base.tests.common import DISABLED_MAIL_CONTEXT
DISABLED_MAIL_CONTEXT = {
"tracking_disable": True,
"mail_create_nolog": True,
"mail_create_nosubscribe": True,
"mail_notrack": True,
"no_reset_password": True,
}
cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT))
setup_test_model(cls.env, cls.MisKpiDataTestItem)

report = cls.env["mis.report"].create(dict(name="test report"))
Expand Down
Loading

0 comments on commit eac5a0b

Please sign in to comment.