From fc36345ca0730cf8cbd197d9177bc884764188f2 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 23 Jul 2012 17:03:06 +0200 Subject: [PATCH] [ADD] test cases for export of simple fields bzr revid: xmo@openerp.com-20120723150306-6zgtr9ho0d3ud78u --- openerp/modules/__init__.py | 6 +- openerp/osv/orm.py | 6 +- openerp/tests/__init__.py | 11 +- openerp/tests/common.py | 5 +- openerp/tests/test_export.py | 265 +++++++++++++++++++++++++++++++++++ 5 files changed, 276 insertions(+), 17 deletions(-) create mode 100644 openerp/tests/test_export.py diff --git a/openerp/modules/__init__.py b/openerp/modules/__init__.py index f28c397bb23..56265e353a2 100644 --- a/openerp/modules/__init__.py +++ b/openerp/modules/__init__.py @@ -24,11 +24,7 @@ """ -import openerp.modules.db -import openerp.modules.graph -import openerp.modules.loading -import openerp.modules.migration -import openerp.modules.module +from . import db, graph, loading, migration, module, registry # TODO temporarily expose those things from openerp.modules.module import \ diff --git a/openerp/osv/orm.py b/openerp/osv/orm.py index 78487e7641c..02a13a41dcb 100644 --- a/openerp/osv/orm.py +++ b/openerp/osv/orm.py @@ -4179,11 +4179,7 @@ class BaseModel(object): # Try-except added to filter the creation of those records whose filds are readonly. # Example : any dashboard which has all the fields readonly.(due to Views(database views)) - try: - cr.execute("SELECT nextval('"+self._sequence+"')") - except: - raise except_orm(_('UserError'), - _('You cannot perform this operation. New Record Creation is not allowed for this object as this object is for reporting purpose.')) + cr.execute("SELECT nextval('"+self._sequence+"')") id_new = cr.fetchone()[0] for table in tocreate: diff --git a/openerp/tests/__init__.py b/openerp/tests/__init__.py index 913c5c759c9..7b30adece79 100644 --- a/openerp/tests/__init__.py +++ b/openerp/tests/__init__.py @@ -8,18 +8,17 @@ Tests can be explicitely added to the `fast_suite` or `checks` lists or not. See the :ref:`test-framework` section in the :ref:`features` list. """ -import test_expression -import test_ir_sequence -import test_orm -import test_uninstall +from . import test_expression, test_ir_sequence, test_orm,\ + test_uninstall, test_export fast_suite = [ test_ir_sequence, - ] +] checks = [ test_expression, test_orm, - ] + test_export, +] # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/openerp/tests/common.py b/openerp/tests/common.py index ef5b0586136..b1af4338312 100644 --- a/openerp/tests/common.py +++ b/openerp/tests/common.py @@ -39,13 +39,16 @@ class TransactionCase(unittest2.TestCase): """ def setUp(self): - self.cr = openerp.modules.registry.RegistryManager.get(DB).db.cursor() + self.cr = self.cursor() self.uid = openerp.SUPERUSER_ID def tearDown(self): self.cr.rollback() self.cr.close() + def cursor(self): + return openerp.modules.registry.RegistryManager.get(DB).db.cursor() + def registry(self, model): return openerp.modules.registry.RegistryManager.get(DB)[model] diff --git a/openerp/tests/test_export.py b/openerp/tests/test_export.py new file mode 100644 index 00000000000..b1b51a608dc --- /dev/null +++ b/openerp/tests/test_export.py @@ -0,0 +1,265 @@ +# -*- coding: utf-8 -*- +import psycopg2 + +import openerp.modules.registry +import openerp +from openerp.osv import orm, fields + +from . import common + +models = [ + ('boolean', fields.boolean()), + ('integer', fields.integer()), + ('float', fields.float()), + ('decimal', fields.float(digits=(16, 3))), + ('string.bounded', fields.char('unknown', size=16)), + ('string', fields.char('unknown', size=None)), + ('date', fields.date()), + ('datetime', fields.datetime()), + ('text', fields.text()), + ('selection', fields.selection([(1, "Foo"), (2, "Bar"), (3, "Qux")])), + # TODO: m2o, o2m, m2m + # TODO: function? + # TODO: related? + # TODO: reference? +] +for name, field in models: + attrs = { + '_name': 'export.%s' % name, + '_module': 'base', + '_columns': { + 'value': field + } + } + NewModel = type( + 'Export%s' % ''.join(section.capitalize() for section in name.split('.')), + (orm.Model,), + attrs) + +def setUpModule(): + openerp.tools.config['update'] = dict(base=1) + openerp.modules.registry.RegistryManager.new( + common.DB, update_module=True) + +class CreatorCase(common.TransactionCase): + model_name = False + + def __init__(self, *args, **kwargs): + super(CreatorCase, self).__init__(*args, **kwargs) + self.model = None + + def setUp(self): + super(CreatorCase, self).setUp() + self.model = self.registry(self.model_name) + def make(self, value): + id = self.model.create(self.cr, openerp.SUPERUSER_ID, {'value': value}) + return self.model.browse(self.cr, openerp.SUPERUSER_ID, [id])[0] + def export(self, value, context=None): + record = self.make(value) + return self.model._BaseModel__export_row( + self.cr, openerp.SUPERUSER_ID, record, [["value"]], context=context) + +class test_boolean_field(CreatorCase): + model_name = 'export.boolean' + + def test_true(self): + self.assertEqual( + self.export(True), + [[u'True']]) + def test_false(self): + """ ``False`` value to boolean fields is unique in being exported as a + (unicode) string, not a boolean + """ + self.assertEqual( + self.export(False), + [[u'False']]) + +class test_integer_field(CreatorCase): + model_name = 'export.integer' + + def test_empty(self): + self.assertEqual(self.model.search(self.cr, openerp.SUPERUSER_ID, []), [], + "Test model should have no records") + def test_0(self): + self.assertEqual( + self.export(0), + [[False]]) + + def test_basic_value(self): + self.assertEqual( + self.export(42), + [[u'42']]) + + def test_negative(self): + self.assertEqual( + self.export(-32), + [[u'-32']]) + + def test_huge(self): + self.assertEqual( + self.export(2**31-1), + [[unicode(2**31-1)]]) + +class test_float_field(CreatorCase): + model_name = 'export.float' + + def test_0(self): + self.assertEqual( + self.export(0.0), + [[False]]) + + def test_epsilon(self): + self.assertEqual( + self.export(0.000000000027), + [[u'2.7e-11']]) + + def test_negative(self): + self.assertEqual( + self.export(-2.42), + [[u'-2.42']]) + + def test_positive(self): + self.assertEqual( + self.export(47.36), + [[u'47.36']]) + + def test_big(self): + self.assertEqual( + self.export(87654321.4678), + [[u'87654321.4678']]) + +class test_decimal_field(CreatorCase): + model_name = 'export.decimal' + + def test_0(self): + self.assertEqual( + self.export(0.0), + [[False]]) + + def test_epsilon(self): + """ epsilon gets sliced to 0 due to precision + """ + self.assertEqual( + self.export(0.000000000027), + [[False]]) + + def test_negative(self): + self.assertEqual( + self.export(-2.42), + [[u'-2.42']]) + + def test_positive(self): + self.assertEqual( + self.export(47.36), + [[u'47.36']]) + + def test_big(self): + self.assertEqual( + self.export(87654321.4678), [[u'87654321.468']]) + +class test_string_field(CreatorCase): + model_name = 'export.string.bounded' + + def test_empty(self): + self.assertEqual( + self.export(""), + [[False]]) + def test_within_bounds(self): + self.assertEqual( + self.export("foobar"), + [[u"foobar"]]) + def test_out_of_bounds(self): + self.assertEqual( + self.export("C for Sinking, " + "Java for Drinking, " + "Smalltalk for Thinking. " + "...and Power to the Penguin!"), + [[u"C for Sinking, J"]]) + +class test_unbound_string_field(CreatorCase): + model_name = 'export.string' + + def test_empty(self): + self.assertEqual( + self.export(""), + [[False]]) + def test_small(self): + self.assertEqual( + self.export("foobar"), + [[u"foobar"]]) + def test_big(self): + self.assertEqual( + self.export("We flew down weekly to meet with IBM, but they " + "thought the way to measure software was the amount " + "of code we wrote, when really the better the " + "software, the fewer lines of code."), + [[u"We flew down weekly to meet with IBM, but they thought the " + u"way to measure software was the amount of code we wrote, " + u"when really the better the software, the fewer lines of " + u"code."]]) + +class test_text(CreatorCase): + model_name = 'export.text' + + def test_empty(self): + self.assertEqual( + self.export(""), + [[False]]) + def test_small(self): + self.assertEqual( + self.export("foobar"), + [[u"foobar"]]) + def test_big(self): + self.assertEqual( + self.export("So, `bind' is `let' and monadic programming is" + " equivalent to programming in the A-normal form. That" + " is indeed all there is to monads"), + [[u"So, `bind' is `let' and monadic programming is equivalent to" + u" programming in the A-normal form. That is indeed all there" + u" is to monads"]]) + +class test_date(CreatorCase): + model_name = 'export.date' + + def test_empty(self): + self.assertEqual( + self.export(False), + [[False]]) + def test_basic(self): + self.assertEqual( + self.export('2011-11-07'), + [[u'2011-11-07']]) + +class test_datetime(CreatorCase): + model_name = 'export.datetime' + + def test_empty(self): + self.assertEqual( + self.export(False), + [[False]]) + def test_basic(self): + self.assertEqual( + self.export('2011-11-07 21:05:48'), + [[u'2011-11-07 21:05:48']]) + def test_tz(self): + """ Export ignores the timezone and always exports to UTC + """ + self.assertEqual( + self.export('2011-11-07 21:05:48', {'tz': 'Pacific/Norfolk'}), + [[u'2011-11-07 21:05:48']]) + +class test_selection(CreatorCase): + model_name = 'export.selection' + + def test_empty(self): + self.assertEqual( + self.export(False), + [[False]]) + + def test_value(self): + """ selections export the *label* for their value + """ + self.assertEqual( + self.export(2), + [[u"Bar"]]) +