Mercurial > p > roundup > code
diff 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 |
line wrap: on
line diff
--- a/test/db_test_base.py Tue Jul 28 05:51:00 2020 -0400 +++ b/test/db_test_base.py Tue Jul 28 06:24:39 2020 -0400 @@ -2982,15 +2982,66 @@ b.setkey("name") self.db.post_init() + def test_splitDesignator(self): + from roundup.hyperdb import splitDesignator + + valid_test_cases = [('zip2py44', ('zip2py', '44')), + ('zippy2', ('zippy', '2')), + ('a9', ('a', '9')), + ('a1234', ('a', '1234')), + ('a_1234', ('a_', '1234')), + ] + + invalid_test_cases = ['_zip2py44','1zippy44', + 'zippy244a' ] + + for designator in valid_test_cases: + print("Testing %s"%designator[0]) + self.assertEqual(splitDesignator(designator[0]), designator[1]) + + for designator in invalid_test_cases: + print("Testing %s"%designator) + with self.assertRaises(ValueError) as ctx: + splitDesignator(designator) + error = 'DesignatorError: "%s" not a node designator' % designator + self.assertEqual(str(ctx.exception), error) + + def test_addNewClass(self): self.init_a() - self.assertRaises(ValueError, self.module.Class, self.db, "a", - name=String()) + with self.assertRaises(ValueError) as ctx: + self.module.Class(self.db, "a", name=String()) + error = 'Class "a" already defined.' + self.assertEqual(str(ctx.exception), error) aid = self.db.a.create(name='apple') self.db.commit(); self.db.close() + # Test permutations of valid/invalid classnames + self.init_a() + + for classname in [ "1badclassname", "badclassname1", + "_badclassname", "_", "5" ]: + print("testing %s\n" % classname) + with self.assertRaises(ValueError) as ctx: + self.module.Class(self.db, classname, name=String()) + + error = ('Class name %s is not valid. It must start ' + 'with a letter, end with a letter or "_", and ' + 'only have alphanumerics and "_" in the middle.' % (classname,)) + self.assertEqual(str(ctx.exception), error) + + for classname in [ 'cla2ss', 'c_lass', 'CL_2ass', 'Z', + 'class2_' ]: + print("testing %s\n" % classname) + c = self.module.Class(self.db, classname, name=String()) + self.assertEqual(str(c), '<hyperdb.Class "%s">' % classname) + + # don't pollute the db with junk valid cases + # self.db.commit(); close to discard all changes in this block. + self.db.close() + # add a new class to the schema and check creation of new items # (and existence of old ones) self.init_ab()
