From b72488541b2ffa5376e367c69bc6d10e899cc02f Mon Sep 17 00:00:00 2001 From: richard Date: Thu, 18 Jul 2002 11:50:58 +0000 Subject: [PATCH] added tests for number type too git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@899 57a73879-2fb5-44c3-a270-3262357dd7e2 --- roundup/backends/back_anydbm.py | 44 +++++++++++++++++++++++---------- test/test_db.py | 29 ++++++++++++++++++---- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/roundup/backends/back_anydbm.py b/roundup/backends/back_anydbm.py index 84474cc..fa550fd 100644 --- a/roundup/backends/back_anydbm.py +++ b/roundup/backends/back_anydbm.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -#$Id: back_anydbm.py,v 1.49 2002-07-18 11:41:10 richard Exp $ +#$Id: back_anydbm.py,v 1.50 2002-07-18 11:50:58 richard Exp $ ''' This module defines a backend that saves the hyperdatabase in a database chosen by anydbm. It is guaranteed to always be available in python @@ -730,17 +730,29 @@ class Class(hyperdb.Class): if value is not None and not isinstance(value, date.Interval): raise TypeError, 'new property "%s" not an Interval'%key - elif isinstance(prop, Number): + elif value is not None and isinstance(prop, Number): try: int(value) - except TypeError: - raise TypeError, 'new property "%s" not numeric' % propname + except ValueError: + try: + float(value) + except ValueError: + raise TypeError, 'new property "%s" not numeric'%key - elif isinstance(prop, Boolean): - try: - int(value) - except TypeError: - raise TypeError, 'new property "%s" is not boolean' % propname + elif value is not None and isinstance(prop, Boolean): + if isinstance(value, type('')): + s = value.lower() + if s in ('0', 'false', 'no'): + value = 0 + elif s in ('1', 'true', 'yes'): + value = 1 + else: + raise TypeError, 'new property "%s" not boolean'%key + else: + try: + int(value) + except TypeError: + raise TypeError, 'new property "%s" not boolean'%key # make sure there's data where there needs to be for key, prop in self.properties.items(): @@ -1024,11 +1036,14 @@ class Class(hyperdb.Class): propvalues[propname] = value elif value is not None and isinstance(prop, Number): - # TODO: should we store floats too? try: int(value) - except TypeError: - raise TypeError, 'new property "%s" not numeric' % propname + except ValueError: + try: + float(value) + except ValueError: + raise TypeError, 'new property "%s" not '\ + 'numeric'%propname elif value is not None and isinstance(prop, Boolean): if isinstance(value, type('')): @@ -1043,7 +1058,7 @@ class Class(hyperdb.Class): else: try: int(value) - except TypeError: + except ValueError: raise TypeError, 'new property "%s" not '\ 'boolean'%propname @@ -1688,6 +1703,9 @@ class IssueClass(Class, roundupdb.IssueClass): # #$Log: not supported by cvs2svn $ +#Revision 1.49 2002/07/18 11:41:10 richard +#added tests for boolean type, and fixes to anydbm backend +# #Revision 1.48 2002/07/18 11:17:31 gmcm #Add Number and Boolean types to hyperdb. #Add conversion cases to web, mail & admin interfaces. diff --git a/test/test_db.py b/test/test_db.py index c6b4468..7863bc6 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: test_db.py,v 1.32 2002-07-18 11:41:10 richard Exp $ +# $Id: test_db.py,v 1.33 2002-07-18 11:50:58 richard Exp $ import unittest, os, shutil, time @@ -111,16 +111,23 @@ class anydbmDBTestCase(MyTestCase): self.assertNotEqual(self.db.issue.get('1', "foo"), a) def testBooleanChange(self): - self.db.user.create(username='foo', assignable='1') + self.db.user.create(username='foo', assignable=1) a = self.db.user.get('1', 'assignable') self.db.user.set('1', assignable='false') self.assertNotEqual(self.db.user.get('1', 'assignable'), a) self.db.user.set('1', assignable='FaLse') self.db.user.set('1', assignable='nO') - self.db.user.set('1', assignable='0') + self.db.user.set('1', assignable=0) self.db.user.set('1', assignable='tRuE') self.db.user.set('1', assignable='yEs') - self.db.user.set('1', assignable='1') + self.db.user.set('1', assignable=1) + + def testNumberChange(self): + self.db.user.create(username='foo', age='1') + a = self.db.user.get('1', 'age') + self.db.user.set('1', age='3') + self.assertNotEqual(self.db.user.get('1', 'age'), a) + self.db.user.set('1', age='1.0') def xtestNewProperty(self): ' make sure a new property is added ok ' @@ -186,7 +193,7 @@ class anydbmDBTestCase(MyTestCase): self.assertNotEqual(num_files, self.db.numfiles()) self.assertEqual(num_files2, self.db.numfiles()) - def xtestExceptions(self): + def testExceptions(self): # this tests the exceptions that should be raised ar = self.assertRaises @@ -255,6 +262,15 @@ class anydbmDBTestCase(MyTestCase): # invalid multilink index ar(IndexError, self.db.issue.set, '6', title='foo', status='1', nosy=['10']) + # invalid number value + ar(TypeError, self.db.user.create, username='foo', age='a') + # invalid boolean value + ar(TypeError, self.db.user.create, username='foo', assignable='fubar') + self.db.user.create(username='foo') + # invalid number value + ar(TypeError, self.db.user.set, '3', username='foo', age='a') + # invalid boolean value + ar(TypeError, self.db.user.set, '3', username='foo', assignable='fubar') def xtestJournals(self): self.db.issue.addprop(fixer=Link("user", do_journal='yes')) @@ -546,6 +562,9 @@ def suite(): # # $Log: not supported by cvs2svn $ +# Revision 1.32 2002/07/18 11:41:10 richard +# added tests for boolean type, and fixes to anydbm backend +# # Revision 1.31 2002/07/14 23:17:45 richard # minor change to make testing easier # -- 2.30.2