test_new_fields.py - # # test cases for new-style fields #...

This preview shows page 1 - 3 out of 17 pages.

The preview shows page 1 - 3 out of 17 pages.
## test cases for new-style fields#from datetime import date, datetimefrom odoo.exceptions import AccessError, except_ormfrom odoo.tests import commonfrom odoo.tools import mute_logger, float_repr, pycompatclass TestFields(common.TransactionCase):def test_00_basics(self):""" test accessing new fields """# find a discussiondiscussion = self.env.ref('test_new_api.discussion_0')# read field as a record attribute or as a record itemself.assertIsInstance(discussion.name, pycompat.string_types)self.assertIsInstance(discussion['name'], pycompat.string_types)self.assertEqual(discussion['name'], discussion.name)# read it with method read()values = discussion.read(['name'])[0]self.assertEqual(values['name'], discussion.name)def test_01_basic_get_assertion(self):""" test item getter """# field access works on single recordrecord = self.env.ref('test_new_api.message_0_0')self.assertEqual(len(record), 1)ok = record.body# field access fails on multiple recordsrecords = self.env['test_new_api.message'].search([])assert len(records) > 1with self.assertRaises(ValueError):faulty = records.bodydef test_01_basic_set_assertion(self):""" test item setter """# field assignment works on single recordrecord = self.env.ref('test_new_api.message_0_0')self.assertEqual(len(record), 1)record.body = 'OK'# field assignment fails on multiple recordsrecords = self.env['test_new_api.message'].search([])assert len(records) > 1with self.assertRaises(ValueError):records.body = 'Faulty'def test_10_computed(self):""" check definition of computed fields """# by default function fields are not stored and readonlyfield = self.env['test_new_api.message']._fields['size']self.assertFalse(field.store)self.assertTrue(field.readonly)
field = self.env['test_new_api.message']._fields['name']self.assertTrue(field.store)self.assertTrue(field.readonly)def test_10_computed_custom(self):""" check definition of custom computed fields """self.env['ir.model.fields'].create({'name': 'x_bool_false_computed','model_id': self.env.ref('test_new_api.model_test_new_api_message').id,'field_description': 'A boolean computed to false','compute': "for r in self: r['x_bool_false_computed'] = False",'store': False,'ttype': 'boolean'})field = self.env['test_new_api.message']._fields['x_bool_false_computed']self.assertFalse(field.depends)def test_10_non_stored(self):""" test non-stored fields """# a field declared with store=False should not have a columnfield = self.env['test_new_api.category']._fields['dummy']self.assertFalse(field.store)self.assertFalse(field.compute)self.assertFalse(field.inverse)# find messagesfor message in self.env['test_new_api.message'].search([]):# check definition of fieldself.assertEqual(message.size, len(message.body or ''))# check recomputation after record is modifiedsize = message.sizemessage.write({'body': (message.body or '') + "!!!"})self.assertEqual(message.size, size + 3)# special case: computed field without dependency must be computedrecord = self.env['test_new_api.mixed'].create({})self.assertTrue(record.now)def test_11_stored(self):""" test stored fields """def check_stored(disc):""" Check the stored computed field on disc.messages """for msg in disc.messages:self.assertEqual(msg.name, "[%s] %s" % (disc.name, msg.author.name))

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 17 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
One
Professor
N/A
Tags
Gramophone record

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture