From 2be2f64aeb33e29821d53baa0b540dc9474b9cdd Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Thu, 4 Jul 2019 12:28:38 +0000 Subject: [PATCH] [FIX] tests: properly log errors If a test is failing with assertRecordValues, the comparison and the error logging were not computed the same way. The _compare_candidate was accessing the record via record[field_name] which uses the cache While the _format_message was doing records.read(...) which queries directly the database. If the test was failing (correctly) because of a wrong cache invalidation (e.g. update the value directly in SQL without invalidating the cache), the error message would look like: AssertionError: Record doesn't match expected values at index 0. Current values: {'id': 42, 'name': 'Foo'} Expected values: {'name': 'Foo'} closes odoo/odoo#34589 Signed-off-by: Martin Trigaux (mat) --- odoo/tests/common.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/odoo/tests/common.py b/odoo/tests/common.py index d2aff0013cb..adb33d79616 100644 --- a/odoo/tests/common.py +++ b/odoo/tests/common.py @@ -334,9 +334,23 @@ class BaseCase(TreeCase, MetaCase('DummyCase', (object,), {})): return False return True + def _repr_field_value(record, field_name): + record_value = record[field_name] + field_type = record._fields[field_name].type + if field_type == 'monetary': + currency_field_name = record._fields[field_name].currency_field + record_currency = record[currency_field_name] + return record_currency and record_currency.round(record_value) or record_value + elif field_type in ('one2many', 'many2many'): + return set(record_value.ids) + elif field_type == 'many2one': + return record_value.id + else: + return record_value + def _format_message(records, expected_values): ''' Return a formatted representation of records/expected_values. ''' - all_records_values = records.read(list(expected_values[0].keys()), load=False) + all_records_values = [{key: _repr_field_value(record, key) for key in expected_values[0]} for record in records] msg1 = '\n'.join(pprint.pformat(dic) for dic in all_records_values) msg2 = '\n'.join(pprint.pformat(dic) for dic in expected_values) return 'Current values:\n\n%s\n\nExpected values:\n\n%s' % (msg1, msg2)