comparison tests/test_db.py @ 87:d3dcac043ab0

Added some new hyperdb tests to make sure we raise the right exceptions.
author Richard Jones <richard@users.sourceforge.net>
date Fri, 27 Jul 2001 06:23:09 +0000
parents f1cffcad4903
children 168def1ce74d
comparison
equal deleted inserted replaced
86:b89712288636 87:d3dcac043ab0
1 # $Id: test_db.py,v 1.4 2001-07-25 04:34:31 richard Exp $ 1 # $Id: test_db.py,v 1.5 2001-07-27 06:23:09 richard Exp $
2 2
3 import unittest, os, shutil 3 import unittest, os, shutil
4 4
5 from roundup.backends import anydbm 5 from roundup.backends import anydbm
6 from roundup.hyperdb import String, Link, Multilink, Date, Interval, Class 6 from roundup.hyperdb import String, Link, Multilink, Date, Interval, Class, \
7 DatabaseError
7 8
8 def setupSchema(db): 9 def setupSchema(db, create):
9 status = Class(db, "status", name=String()) 10 status = Class(db, "status", name=String())
10 status.setkey("name") 11 status.setkey("name")
11 status.create(name="unread") 12 if create:
12 status.create(name="in-progress") 13 status.create(name="unread")
13 status.create(name="testing") 14 status.create(name="in-progress")
14 status.create(name="resolved") 15 status.create(name="testing")
16 status.create(name="resolved")
15 Class(db, "user", username=String(), password=String()) 17 Class(db, "user", username=String(), password=String())
16 Class(db, "issue", title=String(), status=Link("status")) 18 Class(db, "issue", title=String(), status=Link("status"),
19 nosy=Multilink("user"))
17 20
18 class DBTestCase(unittest.TestCase): 21 class DBTestCase(unittest.TestCase):
19 def setUp(self): 22 def setUp(self):
20 class Database(anydbm.Database): 23 class Database(anydbm.Database):
21 pass 24 pass
22 # remove previous test, ignore errors 25 # remove previous test, ignore errors
23 if os.path.exists('_test_dir'): 26 if os.path.exists('_test_dir'):
24 shutil.rmtree('_test_dir') 27 shutil.rmtree('_test_dir')
25 os.mkdir('_test_dir') 28 os.mkdir('_test_dir')
26 self.db = Database('_test_dir', 'test') 29 self.db = Database('_test_dir', 'test')
27 setupSchema(self.db) 30 setupSchema(self.db, 1)
28 31
29 def tearDown(self): 32 def tearDown(self):
30 self.db.close() 33 self.db.close()
31 shutil.rmtree('_test_dir') 34 shutil.rmtree('_test_dir')
32 35
38 self.db.issue.create(title="abuse", status='1') 41 self.db.issue.create(title="abuse", status='1')
39 self.db.issue.addprop(fixer=Link("user")) 42 self.db.issue.addprop(fixer=Link("user"))
40 props = self.db.issue.getprops() 43 props = self.db.issue.getprops()
41 keys = props.keys() 44 keys = props.keys()
42 keys.sort() 45 keys.sort()
43 self.assertEqual(keys, ['fixer', 'status', 'title'], 'wrong prop list') 46 self.assertEqual(keys, ['fixer', 'nosy', 'status', 'title'])
44 self.db.issue.set('5', status=2) 47 self.db.issue.set('5', status='2')
45 self.db.issue.get('5', "status") 48 self.db.issue.get('5', "status")
46 self.db.status.get('2', "name") 49 self.db.status.get('2', "name")
47 self.db.issue.get('5', "title") 50 self.db.issue.get('5', "title")
48 self.db.issue.find(status = self.db.status.lookup("in-progress")) 51 self.db.issue.find(status = self.db.status.lookup("in-progress"))
49 self.db.issue.history('5') 52 self.db.issue.history('5')
50 self.db.status.history('1') 53 self.db.status.history('1')
51 self.db.status.history('2') 54 self.db.status.history('2')
52 55
53 def testExceptions(self): 56 def testExceptions(self):
54 # this tests the exceptions that should be raised 57 # this tests the exceptions that should be raised
58 ar = self.assertRaises
59
60 #
61 # class create
62 #
63 # string property
64 ar(TypeError, self.db.status.create, name=1)
65 # invalid property name
66 ar(KeyError, self.db.status.create, foo='foo')
67 # key name clash
68 ar(ValueError, self.db.status.create, name='unread')
69 # invalid link index
70 ar(IndexError, self.db.issue.create, title='foo', status='bar')
71 # invalid link value
72 ar(ValueError, self.db.issue.create, title='foo', status=1)
73 # invalid multilink type
74 ar(TypeError, self.db.issue.create, title='foo', status='1',
75 nosy='hello')
76 # invalid multilink index type
77 ar(ValueError, self.db.issue.create, title='foo', status='1',
78 nosy=[1])
79 # invalid multilink index
80 ar(IndexError, self.db.issue.create, title='foo', status='1',
81 nosy=['10'])
82
83 #
84 # class get
85 #
86 # invalid node id
87 ar(IndexError, self.db.status.get, '10', 'name')
88 # invalid property name
89 ar(KeyError, self.db.status.get, '2', 'foo')
90
91 #
92 # class set
93 #
94 # invalid node id
95 ar(IndexError, self.db.issue.set, '1', name='foo')
96 # invalid property name
97 ar(KeyError, self.db.status.set, '1', foo='foo')
98 # string property
99 ar(TypeError, self.db.status.set, '1', name=1)
100 # key name clash
101 ar(ValueError, self.db.status.set, '2', name='unread')
102 # set up a valid issue for me to work on
103 self.db.issue.create(title="spam", status='1')
104 # invalid link index
105 ar(IndexError, self.db.issue.set, '1', title='foo', status='bar')
106 # invalid link value
107 ar(ValueError, self.db.issue.set, '1', title='foo', status=1)
108 # invalid multilink type
109 ar(TypeError, self.db.issue.set, '1', title='foo', status='1',
110 nosy='hello')
111 # invalid multilink index type
112 ar(ValueError, self.db.issue.set, '1', title='foo', status='1',
113 nosy=[1])
114 # invalid multilink index
115 ar(IndexError, self.db.issue.set, '1', title='foo', status='1',
116 nosy=['10'])
117
118 def testRetire(self):
119 ''' test retiring a node
120 '''
55 pass 121 pass
56 122
123
124 class ReadOnlyDBTestCase(unittest.TestCase):
125 def setUp(self):
126 class Database(anydbm.Database):
127 pass
128 # remove previous test, ignore errors
129 if os.path.exists('_test_dir'):
130 shutil.rmtree('_test_dir')
131 os.mkdir('_test_dir')
132 db = Database('_test_dir', 'test')
133 setupSchema(db, 1)
134 db.close()
135 self.db = Database('_test_dir')
136 setupSchema(self.db, 0)
137
138 def testExceptions(self):
139 # this tests the exceptions that should be raised
140 self.assertRaises(DatabaseError, self.db.status.create, name="foo")
141 self.assertRaises(DatabaseError, self.db.status.set, '1', name="foo")
142 self.assertRaises(DatabaseError, self.db.status.retire, '1')
143
144
57 def suite(): 145 def suite():
58 return unittest.makeSuite(DBTestCase, 'test') 146 db = unittest.makeSuite(DBTestCase, 'test')
147 readonlydb = unittest.makeSuite(ReadOnlyDBTestCase, 'test')
148 return unittest.TestSuite((db, readonlydb))
59 149
60 150
61 # 151 #
62 # $Log: not supported by cvs2svn $ 152 # $Log: not supported by cvs2svn $
153 # Revision 1.4 2001/07/25 04:34:31 richard
154 # Added id and log to tests files...
63 # 155 #
156 #

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