comparison test/db_test_base.py @ 6238:6834bb5473da

Summary: Constrain format of classname and document Define the format of a valid classname. Updated design doc, function doc and glossary to document format. Error message for case where we have a redefinition of a classname now says why ValueError is raised since we are raising ValueError for incorrect classname format as well. Tests for all cases including verification of cause for ValueError exceptions.
author John Rouillard <rouilj@ieee.org>
date Tue, 28 Jul 2020 06:24:39 -0400
parents a701c9c81597
children d3878ac549e9
comparison
equal deleted inserted replaced
6237:0a37979bbd46 6238:6834bb5473da
2980 b = self.module.Class(self.db, "b", name=String(), 2980 b = self.module.Class(self.db, "b", name=String(),
2981 fooz=Multilink('a')) 2981 fooz=Multilink('a'))
2982 b.setkey("name") 2982 b.setkey("name")
2983 self.db.post_init() 2983 self.db.post_init()
2984 2984
2985 def test_splitDesignator(self):
2986 from roundup.hyperdb import splitDesignator
2987
2988 valid_test_cases = [('zip2py44', ('zip2py', '44')),
2989 ('zippy2', ('zippy', '2')),
2990 ('a9', ('a', '9')),
2991 ('a1234', ('a', '1234')),
2992 ('a_1234', ('a_', '1234')),
2993 ]
2994
2995 invalid_test_cases = ['_zip2py44','1zippy44',
2996 'zippy244a' ]
2997
2998 for designator in valid_test_cases:
2999 print("Testing %s"%designator[0])
3000 self.assertEqual(splitDesignator(designator[0]), designator[1])
3001
3002 for designator in invalid_test_cases:
3003 print("Testing %s"%designator)
3004 with self.assertRaises(ValueError) as ctx:
3005 splitDesignator(designator)
3006 error = 'DesignatorError: "%s" not a node designator' % designator
3007 self.assertEqual(str(ctx.exception), error)
3008
3009
2985 def test_addNewClass(self): 3010 def test_addNewClass(self):
2986 self.init_a() 3011 self.init_a()
2987 3012
2988 self.assertRaises(ValueError, self.module.Class, self.db, "a", 3013 with self.assertRaises(ValueError) as ctx:
2989 name=String()) 3014 self.module.Class(self.db, "a", name=String())
3015 error = 'Class "a" already defined.'
3016 self.assertEqual(str(ctx.exception), error)
2990 3017
2991 aid = self.db.a.create(name='apple') 3018 aid = self.db.a.create(name='apple')
2992 self.db.commit(); self.db.close() 3019 self.db.commit(); self.db.close()
3020
3021 # Test permutations of valid/invalid classnames
3022 self.init_a()
3023
3024 for classname in [ "1badclassname", "badclassname1",
3025 "_badclassname", "_", "5" ]:
3026 print("testing %s\n" % classname)
3027 with self.assertRaises(ValueError) as ctx:
3028 self.module.Class(self.db, classname, name=String())
3029
3030 error = ('Class name %s is not valid. It must start '
3031 'with a letter, end with a letter or "_", and '
3032 'only have alphanumerics and "_" in the middle.' % (classname,))
3033 self.assertEqual(str(ctx.exception), error)
3034
3035 for classname in [ 'cla2ss', 'c_lass', 'CL_2ass', 'Z',
3036 'class2_' ]:
3037 print("testing %s\n" % classname)
3038 c = self.module.Class(self.db, classname, name=String())
3039 self.assertEqual(str(c), '<hyperdb.Class "%s">' % classname)
3040
3041 # don't pollute the db with junk valid cases
3042 # self.db.commit(); close to discard all changes in this block.
3043 self.db.close()
2993 3044
2994 # add a new class to the schema and check creation of new items 3045 # add a new class to the schema and check creation of new items
2995 # (and existence of old ones) 3046 # (and existence of old ones)
2996 self.init_ab() 3047 self.init_ab()
2997 bid = self.db.b.create(name='bear', fooz=[aid]) 3048 bid = self.db.b.create(name='bear', fooz=[aid])

Roundup Issue Tracker: http://roundup-tracker.org/