From e966beb3ca90c7c59af37e181e4169bb39f1d5f2 Mon Sep 17 00:00:00 2001 From: Xavier-Do Date: Wed, 20 Oct 2021 09:15:42 +0000 Subject: [PATCH] [FIX] base: testing overhead of related field creation The real overhead of creating a related field is actually two queries: - one query in update_db_related, the purpose of this test - one more query for _compute_related_field_id in flush() With the previous version of this test, one prefetching done in the first create was not present in the second create since it was in cache. The fix clears the caches to make the creation of both fields in the same situation, such that query counts are compared in a fair way. Part-of: odoo/odoo#78514 --- odoo/addons/base/tests/test_ir_actions.py | 30 +++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/odoo/addons/base/tests/test_ir_actions.py b/odoo/addons/base/tests/test_ir_actions.py index f060af2ecf7..d046e1c1b16 100644 --- a/odoo/addons/base/tests/test_ir_actions.py +++ b/odoo/addons/base/tests/test_ir_actions.py @@ -501,20 +501,24 @@ class TestCustomFields(common.TransactionCase): ]) partners.flush() - # determine how many queries it takes to create a non-computed field - query_count = self.cr.sql_log_count - self.env['ir.model.fields'].create({ - 'model_id': self.env['ir.model']._get_id('res.partner'), - 'name': 'x_oh_box', - 'field_description': 'x_oh_box', - 'ttype': 'char', - }) - query_count = self.cr.sql_log_count - query_count - - # create the related field, and assert it only takes 1 extra query - with self.assertQueryCount(query_count + 1): + # create a non-computed field, and assert how many queries it takes + model_id = self.env['ir.model']._get_id('res.partner') + query_count = 42 + with self.assertQueryCount(query_count): + self.env.registry.clear_caches() self.env['ir.model.fields'].create({ - 'model_id': self.env['ir.model']._get_id('res.partner'), + 'model_id': model_id, + 'name': 'x_oh_box', + 'field_description': 'x_oh_box', + 'ttype': 'char', + 'store': True, + }) + + # same with a related field, it only takes 2 extra queries + with self.assertQueryCount(query_count + 2): + self.env.registry.clear_caches() + self.env['ir.model.fields'].create({ + 'model_id': model_id, 'name': 'x_oh_boy', 'field_description': 'x_oh_boy', 'ttype': 'char',