Skip to content

Commit

Permalink
[REF] cooperator: improve unit tests
Browse files Browse the repository at this point in the history
Set up account test data in python rather than relying on module
data. accounting data needs to be created even if created in module
data because when launching tests, accounting data will be deleted
when odoo loads a test chart of account.
cf load_for_current_company in chart_template.py in account module

Also remove manual creation of the chart of account and remy on the
test one.

Setup is made in a mixin than can be imported in other cooperator
modules.

[REF] cooperator: use @users in tests

[IMP] cooperator: use datetime.date
  • Loading branch information
robinkeunen committed Oct 6, 2022
1 parent f8c041a commit 1ff4edd
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 263 deletions.
5 changes: 0 additions & 5 deletions cooperator/demo/coop.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@
<field name="property_cooperator_account" ref="account_cooperator_demo" />
</record>

<record id="subscription_journal" model="account.journal">
<field name="default_credit_account_id" ref="account_equity_demo" />
<field name="default_debit_account_id" ref="account_equity_demo" />
</record>

<record id="product_template_share_type_1_demo" model="product.template">
<field name="name">Part A - Founder</field>
<field name="short_name">Part A</field>
Expand Down
4 changes: 2 additions & 2 deletions cooperator/models/subscription_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import warnings
from datetime import datetime
from datetime import date

from odoo import _, api, fields, models
from odoo.exceptions import UserError, ValidationError
Expand Down Expand Up @@ -344,7 +344,7 @@ def _compute_subscription_amount(self):
required=True,
readonly=True,
states={"draft": [("readonly", False)]},
default=lambda self: datetime.strftime(datetime.now(), "%Y-%m-%d"),
default=lambda _: date.today(),
)
company_id = fields.Many2one(
"res.company",
Expand Down
1 change: 0 additions & 1 deletion cooperator/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from . import test_base
from . import test_cooperator
151 changes: 151 additions & 0 deletions cooperator/tests/cooperator_test_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# Copyright 2019 Coop IT Easy SCRL fs
# Robin Keunen <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from datetime import date, datetime, timedelta


class CooperatorTestMixin:
@classmethod
def set_up_cooperator_test_data(cls):
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
# accounting data needs to be created even if created in module data
# because when launching tests, accounting data will be deleted when odoo loads
# a test chart of account.
# cf load_for_current_company in chart_template.py in account module

# if these properties are not set, odoo thinks the chart of account was not
# loaded. The test CAO does not set those properties
# cf AccountingTestCase

cls._ensure_account_property_is_set(
"res.partner", "property_account_receivable_id"
)
cls._ensure_account_property_is_set(
"res.partner", "property_account_payable_id"
)
cls._ensure_account_property_is_set(
"product.template", "property_account_income_id"
)

account_model = cls.env["account.account"]
cls.company = cls.env.user.company_id
cls.company.coop_email_contact = "[email protected]"
cls.demo_partner = cls.env.ref("base.partner_demo")

receivable_account_type = cls.env.ref("account.data_account_type_receivable")
equity_account_type = cls.env.ref("account.data_account_type_equity")
cooperator_account = account_model.create(
{
"name": "Cooperator Test",
"code": "416109",
"user_type_id": receivable_account_type.id,
"reconcile": True,
}
)
cls.company.property_cooperator_account = cooperator_account
cls.equity_account = account_model.create(
{
"name": "Equity Test ",
"code": "100919",
"user_type_id": equity_account_type.id,
"reconcile": True,
}
)
subscription_journal_sequence = cls.env.ref(
"cooperator.sequence_subscription_journal"
)
cls.subscription_journal = cls.env["account.journal"].create(
{
"name": "Subscriptions Test",
"code": "SUBJT",
"type": "sale",
"sequence_id": subscription_journal_sequence.id,
}
)

cls.share_x = cls.env["product.product"].create(
{
"name": "Share X - Founder",
"short_name": "Part X",
"is_share": True,
"by_individual": True,
"by_company": False,
"list_price": 50,
}
)
cls.share_y = cls.env["product.product"].create(
{
"name": "Share Y - Worker",
"short_name": "Part Y",
"is_share": True,
"default_share_product": True,
"by_individual": True,
"by_company": True,
"list_price": 25,
}
)
cls.subscription_request_1 = cls.env["subscription.request"].create(
{
"firstname": "John",
"lastname": "Doe",
"email": "[email protected]",
"address": "Cooperation Street",
"zip_code": "1111",
"city": "Brussels",
"lang": "en_US",
"country_id": cls.env.ref("base.be").id,
"date": datetime.now() - timedelta(days=12),
"source": "manual",
"ordered_parts": 3,
"share_product_id": cls.share_y.id,
"data_policy_approved": True,
"internal_rules_approved": True,
"financial_risk_approved": True,
"generic_rules_approved": True,
"gender": "male",
"iban": "09898765454",
"birthdate": date(1990, 9, 21),
"skip_iban_control": True,
}
)

@classmethod
def _ensure_account_property_is_set(cls, model, property_name):
"""Ensure the ir.property is set.
In case it's not: create it with a random account.
This is useful when testing with partially defined localization
(especially test chart of account).
:param model: the name of the model the property is set on
:param property_name: The name of the property.
"""
company_id = cls.env.user.company_id
field_id = cls.env["ir.model.fields"].search(
[("model", "=", model), ("name", "=", property_name)],
limit=1,
)
property_id = cls.env["ir.property"].search(
[
("company_id", "=", company_id.id),
("name", "=", property_name),
("res_id", "=", None),
("fields_id", "=", field_id.id),
],
limit=1,
)
account_id = cls.env["account.account"].search(
[("company_id", "=", company_id.id)], limit=1
)
value_reference = "account.account,%d" % account_id.id
if property_id and not property_id.value_reference:
property_id.value_reference = value_reference
else:
cls.env["ir.property"].create(
{
"name": property_name,
"company_id": company_id.id,
"fields_id": field_id.id,
"value_reference": value_reference,
}
)
133 changes: 0 additions & 133 deletions cooperator/tests/test_base.py

This file was deleted.

Loading

0 comments on commit 1ff4edd

Please sign in to comment.