[IMP] test_new_api: test precompute monetary fields

Part-of: odoo/odoo#81423
This commit is contained in:
Victor Feyens
2022-02-07 14:17:50 +00:00
committed by Raphael Collet
parent 6f9343b8c3
commit a6c786090c
3 changed files with 47 additions and 0 deletions
@@ -1516,6 +1516,23 @@ class PrecomputeRequired(models.Model):
name = fields.Char(related='partner_id.name', precompute=True, store=True, required=True)
class PrecomputeMonetary(models.Model):
_name = 'test_new_api.precompute.monetary'
_description = 'a model with precomputed monetary and currency'
amount = fields.Monetary(
compute='_compute_amount', store=True, precompute=True)
currency_id = fields.Many2one(
'res.currency', compute="_compute_currency_id", store=True, precompute=True)
def _compute_amount(self):
for record in self:
record.amount = 12.333
def _compute_currency_id(self):
self.currency_id = 1 # EUR
class PrefetchTranslateField(models.Model):
_name = 'test_new_api.prefetch.translate'
_description = 'A model with some translate fields to check prefetch'
@@ -84,4 +84,5 @@ access_test_new_api_precompute_line,access_test_new_api_precompute_line,model_te
access_test_new_api_precompute_combo,access_test_new_api_precompute_combo,model_test_new_api_precompute_combo,,1,0,0,0
access_test_new_api_precompute_editable,access_test_new_api_precompute_editable,model_test_new_api_precompute_editable,,1,0,0,0
access_test_new_api_precompute_required,access_test_new_api_precompute_required,model_test_new_api_precompute_required,,1,0,0,0
access_test_new_api_precompute_monetary,access_test_new_api_precompute_monetary,model_test_new_api_precompute_monetary,,1,0,0,0
access_test_new_api_prefetch_translate,access_test_new_api_prefetch_translate,model_test_new_api_prefetch_translate,,1,0,0,0
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
84 access_test_new_api_precompute_combo access_test_new_api_precompute_combo model_test_new_api_precompute_combo 1 0 0 0
85 access_test_new_api_precompute_editable access_test_new_api_precompute_editable model_test_new_api_precompute_editable 1 0 0 0
86 access_test_new_api_precompute_required access_test_new_api_precompute_required model_test_new_api_precompute_required 1 0 0 0
87 access_test_new_api_precompute_monetary access_test_new_api_precompute_monetary model_test_new_api_precompute_monetary 1 0 0 0
88 access_test_new_api_prefetch_translate access_test_new_api_prefetch_translate model_test_new_api_prefetch_translate 1 0 0 0
@@ -3399,6 +3399,16 @@ class TestFieldParametersValidation(common.TransactionCase):
))
def select(model, *fnames):
""" Return the expected query string to SELECT the given columns. """
table = model._table
terms = ", ".join(
f'"{table}"."{fname}" AS "{fname}"'
for fname in ['id'] + list(fnames)
)
return f'SELECT {terms} FROM "{table}" WHERE "{table}".id IN %s'
def insert(model, *fnames, rowcount=1):
""" Return the expected query string to INSERT the given columns. """
columns = sorted(fnames + ('create_uid', 'create_date', 'write_uid', 'write_date'))
@@ -3788,3 +3798,22 @@ class TestPrecompute(common.TransactionCase):
# check the number of queries: 1 SELECT + 1 INSERT
with self.assertQueryCount(2):
model.create([{'partner_id': pid} for pid in partners.ids])
def test_precompute_monetary(self):
"""Make sure the rounding of monetaries correctly prefetches currency fields"""
model = self.env['test_new_api.precompute.monetary']
currency = self.env['res.currency']
# warmup
model.create({})
model.flush()
model.invalidate_cache()
QUERIES = [
select(currency, 'rounding'),
select(currency, 'decimal_places'),
insert(model, 'amount', 'currency_id'),
select(model, 'currency_id'),
]
with self.assertQueries(QUERIES):
model.create({})