[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) <mat@odoo.com>
This commit is contained in:
Martin Trigaux
2019-07-04 13:33:37 +00:00
parent 3b1d00acda
commit 2be2f64aeb
+15 -1
View File
@@ -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)