comparison test/test_db.py @ 1479:405e91b5be46

fixed rdbms mutation of properties
author Richard Jones <richard@users.sourceforge.net>
date Thu, 27 Feb 2003 11:07:39 +0000
parents b953750bdc04
children f5f60c75a458
comparison
equal deleted inserted replaced
1478:2ec91ead3add 1479:405e91b5be46
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" 14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, 15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 # 17 #
18 # $Id: test_db.py,v 1.71 2003-02-15 23:19:01 kedder Exp $ 18 # $Id: test_db.py,v 1.72 2003-02-27 11:07:39 richard Exp $
19 19
20 import unittest, os, shutil, time 20 import unittest, os, shutil, time
21 21
22 from roundup.hyperdb import String, Password, Link, Multilink, Date, \ 22 from roundup.hyperdb import String, Password, Link, Multilink, Date, \
23 Interval, DatabaseError, Boolean, Number 23 Interval, DatabaseError, Boolean, Number
83 shutil.rmtree(config.DATABASE) 83 shutil.rmtree(config.DATABASE)
84 os.makedirs(config.DATABASE + '/files') 84 os.makedirs(config.DATABASE + '/files')
85 self.db = anydbm.Database(config, 'admin') 85 self.db = anydbm.Database(config, 'admin')
86 setupSchema(self.db, 1, anydbm) 86 setupSchema(self.db, 1, anydbm)
87 87
88 def testIDGeneration(self): 88 #
89 id1 = self.db.issue.create(title="spam", status='1') 89 # schema mutation
90 id2 = self.db.issue.create(title="eggs", status='2') 90 #
91 self.assertNotEqual(id1, id2) 91 def testAddProperty(self):
92
93 def testStringChange(self):
94 for commit in (0,1):
95 # test set & retrieve
96 nid = self.db.issue.create(title="spam", status='1')
97 self.assertEqual(self.db.issue.get(nid, 'title'), 'spam')
98
99 # change and make sure we retrieve the correct value
100 self.db.issue.set(nid, title='eggs')
101 if commit: self.db.commit()
102 self.assertEqual(self.db.issue.get(nid, 'title'), 'eggs')
103
104 def testStringUnset(self):
105 for commit in (0,1):
106 nid = self.db.issue.create(title="spam", status='1')
107 if commit: self.db.commit()
108 self.assertEqual(self.db.issue.get(nid, 'title'), 'spam')
109 # make sure we can unset
110 self.db.issue.set(nid, title=None)
111 if commit: self.db.commit()
112 self.assertEqual(self.db.issue.get(nid, "title"), None)
113
114 def testLinkChange(self):
115 for commit in (0,1):
116 nid = self.db.issue.create(title="spam", status='1')
117 if commit: self.db.commit()
118 self.assertEqual(self.db.issue.get(nid, "status"), '1')
119 self.db.issue.set(nid, status='2')
120 if commit: self.db.commit()
121 self.assertEqual(self.db.issue.get(nid, "status"), '2')
122
123 def testLinkUnset(self):
124 for commit in (0,1):
125 nid = self.db.issue.create(title="spam", status='1')
126 if commit: self.db.commit()
127 self.db.issue.set(nid, status=None)
128 if commit: self.db.commit()
129 self.assertEqual(self.db.issue.get(nid, "status"), None)
130
131 def testMultilinkChange(self):
132 for commit in (0,1):
133 u1 = self.db.user.create(username='foo%s'%commit)
134 u2 = self.db.user.create(username='bar%s'%commit)
135 nid = self.db.issue.create(title="spam", nosy=[u1])
136 if commit: self.db.commit()
137 self.assertEqual(self.db.issue.get(nid, "nosy"), [u1])
138 self.db.issue.set(nid, nosy=[])
139 if commit: self.db.commit()
140 self.assertEqual(self.db.issue.get(nid, "nosy"), [])
141 self.db.issue.set(nid, nosy=[u1,u2])
142 if commit: self.db.commit()
143 self.assertEqual(self.db.issue.get(nid, "nosy"), [u1,u2])
144
145 def testDateChange(self):
146 for commit in (0,1):
147 nid = self.db.issue.create(title="spam", status='1')
148 a = self.db.issue.get(nid, "deadline")
149 if commit: self.db.commit()
150 self.db.issue.set(nid, deadline=date.Date())
151 b = self.db.issue.get(nid, "deadline")
152 if commit: self.db.commit()
153 self.assertNotEqual(a, b)
154 self.assertNotEqual(b, date.Date('1970-1-1 00:00:00'))
155
156 def testDateUnset(self):
157 for commit in (0,1):
158 nid = self.db.issue.create(title="spam", status='1')
159 self.db.issue.set(nid, deadline=date.Date())
160 if commit: self.db.commit()
161 self.assertNotEqual(self.db.issue.get(nid, "deadline"), None)
162 self.db.issue.set(nid, deadline=None)
163 if commit: self.db.commit()
164 self.assertEqual(self.db.issue.get(nid, "deadline"), None)
165
166 def testIntervalChange(self):
167 for commit in (0,1):
168 nid = self.db.issue.create(title="spam", status='1')
169 if commit: self.db.commit()
170 a = self.db.issue.get(nid, "foo")
171 i = date.Interval('-1d')
172 self.db.issue.set(nid, foo=i)
173 if commit: self.db.commit()
174 self.assertNotEqual(self.db.issue.get(nid, "foo"), a)
175 self.assertEqual(i, self.db.issue.get(nid, "foo"))
176 j = date.Interval('1y')
177 self.db.issue.set(nid, foo=j)
178 if commit: self.db.commit()
179 self.assertNotEqual(self.db.issue.get(nid, "foo"), i)
180 self.assertEqual(j, self.db.issue.get(nid, "foo"))
181
182 def testIntervalUnset(self):
183 for commit in (0,1):
184 nid = self.db.issue.create(title="spam", status='1')
185 self.db.issue.set(nid, foo=date.Interval('-1d'))
186 if commit: self.db.commit()
187 self.assertNotEqual(self.db.issue.get(nid, "foo"), None)
188 self.db.issue.set(nid, foo=None)
189 if commit: self.db.commit()
190 self.assertEqual(self.db.issue.get(nid, "foo"), None)
191
192 def testBooleanChange(self):
193 userid = self.db.user.create(username='foo', assignable=1)
194 self.assertEqual(1, self.db.user.get(userid, 'assignable'))
195 self.db.user.set(userid, assignable=0)
196 self.assertEqual(self.db.user.get(userid, 'assignable'), 0)
197
198 def testBooleanUnset(self):
199 nid = self.db.user.create(username='foo', assignable=1)
200 self.db.user.set(nid, assignable=None)
201 self.assertEqual(self.db.user.get(nid, "assignable"), None)
202
203 def testNumberChange(self):
204 nid = self.db.user.create(username='foo', age=1)
205 self.assertEqual(1, self.db.user.get(nid, 'age'))
206 self.db.user.set(nid, age=3)
207 self.assertNotEqual(self.db.user.get(nid, 'age'), 1)
208 self.db.user.set(nid, age=1.0)
209
210 def testNumberUnset(self):
211 nid = self.db.user.create(username='foo', age=1)
212 self.db.user.set(nid, age=None)
213 self.assertEqual(self.db.user.get(nid, "age"), None)
214
215 def testKeyValue(self):
216 newid = self.db.user.create(username="spam")
217 self.assertEqual(self.db.user.lookup('spam'), newid)
218 self.db.commit()
219 self.assertEqual(self.db.user.lookup('spam'), newid)
220 self.db.user.retire(newid)
221 self.assertRaises(KeyError, self.db.user.lookup, 'spam')
222
223 # use the key again now that the old is retired
224 newid2 = self.db.user.create(username="spam")
225 self.assertNotEqual(newid, newid2)
226
227 def testNewProperty(self):
228 self.db.issue.create(title="spam", status='1') 92 self.db.issue.create(title="spam", status='1')
93 self.db.commit()
94
229 self.db.issue.addprop(fixer=Link("user")) 95 self.db.issue.addprop(fixer=Link("user"))
230 # force any post-init stuff to happen 96 # force any post-init stuff to happen
231 self.db.post_init() 97 self.db.post_init()
232 props = self.db.issue.getprops() 98 props = self.db.issue.getprops()
233 keys = props.keys() 99 keys = props.keys()
234 keys.sort() 100 keys.sort()
235 self.assertEqual(keys, ['activity', 'assignedto', 'creation', 101 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
236 'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages', 102 'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
237 'nosy', 'status', 'superseder', 'title']) 103 'nosy', 'status', 'superseder', 'title'])
238 self.assertEqual(self.db.issue.get('1', "fixer"), None) 104 self.assertEqual(self.db.issue.get('1', "fixer"), None)
105
106 def testRemoveProperty(self):
107 self.db.issue.create(title="spam", status='1')
108 self.db.commit()
109
110 del self.db.issue.properties['title']
111 self.db.post_init()
112 props = self.db.issue.getprops()
113 keys = props.keys()
114 keys.sort()
115 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
116 'creator', 'deadline', 'files', 'foo', 'id', 'messages',
117 'nosy', 'status', 'superseder'])
118 self.assertEqual(self.db.issue.list(), ['1'])
119
120 def testAddRemoveProperty(self):
121 self.db.issue.create(title="spam", status='1')
122 self.db.commit()
123
124 self.db.issue.addprop(fixer=Link("user"))
125 del self.db.issue.properties['title']
126 self.db.post_init()
127 props = self.db.issue.getprops()
128 keys = props.keys()
129 keys.sort()
130 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
131 'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
132 'nosy', 'status', 'superseder'])
133 self.assertEqual(self.db.issue.list(), ['1'])
134
135 #
136 # basic operations
137 #
138 def testIDGeneration(self):
139 id1 = self.db.issue.create(title="spam", status='1')
140 id2 = self.db.issue.create(title="eggs", status='2')
141 self.assertNotEqual(id1, id2)
142
143 def testStringChange(self):
144 for commit in (0,1):
145 # test set & retrieve
146 nid = self.db.issue.create(title="spam", status='1')
147 self.assertEqual(self.db.issue.get(nid, 'title'), 'spam')
148
149 # change and make sure we retrieve the correct value
150 self.db.issue.set(nid, title='eggs')
151 if commit: self.db.commit()
152 self.assertEqual(self.db.issue.get(nid, 'title'), 'eggs')
153
154 def testStringUnset(self):
155 for commit in (0,1):
156 nid = self.db.issue.create(title="spam", status='1')
157 if commit: self.db.commit()
158 self.assertEqual(self.db.issue.get(nid, 'title'), 'spam')
159 # make sure we can unset
160 self.db.issue.set(nid, title=None)
161 if commit: self.db.commit()
162 self.assertEqual(self.db.issue.get(nid, "title"), None)
163
164 def testLinkChange(self):
165 for commit in (0,1):
166 nid = self.db.issue.create(title="spam", status='1')
167 if commit: self.db.commit()
168 self.assertEqual(self.db.issue.get(nid, "status"), '1')
169 self.db.issue.set(nid, status='2')
170 if commit: self.db.commit()
171 self.assertEqual(self.db.issue.get(nid, "status"), '2')
172
173 def testLinkUnset(self):
174 for commit in (0,1):
175 nid = self.db.issue.create(title="spam", status='1')
176 if commit: self.db.commit()
177 self.db.issue.set(nid, status=None)
178 if commit: self.db.commit()
179 self.assertEqual(self.db.issue.get(nid, "status"), None)
180
181 def testMultilinkChange(self):
182 for commit in (0,1):
183 u1 = self.db.user.create(username='foo%s'%commit)
184 u2 = self.db.user.create(username='bar%s'%commit)
185 nid = self.db.issue.create(title="spam", nosy=[u1])
186 if commit: self.db.commit()
187 self.assertEqual(self.db.issue.get(nid, "nosy"), [u1])
188 self.db.issue.set(nid, nosy=[])
189 if commit: self.db.commit()
190 self.assertEqual(self.db.issue.get(nid, "nosy"), [])
191 self.db.issue.set(nid, nosy=[u1,u2])
192 if commit: self.db.commit()
193 self.assertEqual(self.db.issue.get(nid, "nosy"), [u1,u2])
194
195 def testDateChange(self):
196 for commit in (0,1):
197 nid = self.db.issue.create(title="spam", status='1')
198 a = self.db.issue.get(nid, "deadline")
199 if commit: self.db.commit()
200 self.db.issue.set(nid, deadline=date.Date())
201 b = self.db.issue.get(nid, "deadline")
202 if commit: self.db.commit()
203 self.assertNotEqual(a, b)
204 self.assertNotEqual(b, date.Date('1970-1-1 00:00:00'))
205
206 def testDateUnset(self):
207 for commit in (0,1):
208 nid = self.db.issue.create(title="spam", status='1')
209 self.db.issue.set(nid, deadline=date.Date())
210 if commit: self.db.commit()
211 self.assertNotEqual(self.db.issue.get(nid, "deadline"), None)
212 self.db.issue.set(nid, deadline=None)
213 if commit: self.db.commit()
214 self.assertEqual(self.db.issue.get(nid, "deadline"), None)
215
216 def testIntervalChange(self):
217 for commit in (0,1):
218 nid = self.db.issue.create(title="spam", status='1')
219 if commit: self.db.commit()
220 a = self.db.issue.get(nid, "foo")
221 i = date.Interval('-1d')
222 self.db.issue.set(nid, foo=i)
223 if commit: self.db.commit()
224 self.assertNotEqual(self.db.issue.get(nid, "foo"), a)
225 self.assertEqual(i, self.db.issue.get(nid, "foo"))
226 j = date.Interval('1y')
227 self.db.issue.set(nid, foo=j)
228 if commit: self.db.commit()
229 self.assertNotEqual(self.db.issue.get(nid, "foo"), i)
230 self.assertEqual(j, self.db.issue.get(nid, "foo"))
231
232 def testIntervalUnset(self):
233 for commit in (0,1):
234 nid = self.db.issue.create(title="spam", status='1')
235 self.db.issue.set(nid, foo=date.Interval('-1d'))
236 if commit: self.db.commit()
237 self.assertNotEqual(self.db.issue.get(nid, "foo"), None)
238 self.db.issue.set(nid, foo=None)
239 if commit: self.db.commit()
240 self.assertEqual(self.db.issue.get(nid, "foo"), None)
241
242 def testBooleanChange(self):
243 userid = self.db.user.create(username='foo', assignable=1)
244 self.assertEqual(1, self.db.user.get(userid, 'assignable'))
245 self.db.user.set(userid, assignable=0)
246 self.assertEqual(self.db.user.get(userid, 'assignable'), 0)
247
248 def testBooleanUnset(self):
249 nid = self.db.user.create(username='foo', assignable=1)
250 self.db.user.set(nid, assignable=None)
251 self.assertEqual(self.db.user.get(nid, "assignable"), None)
252
253 def testNumberChange(self):
254 nid = self.db.user.create(username='foo', age=1)
255 self.assertEqual(1, self.db.user.get(nid, 'age'))
256 self.db.user.set(nid, age=3)
257 self.assertNotEqual(self.db.user.get(nid, 'age'), 1)
258 self.db.user.set(nid, age=1.0)
259
260 def testNumberUnset(self):
261 nid = self.db.user.create(username='foo', age=1)
262 self.db.user.set(nid, age=None)
263 self.assertEqual(self.db.user.get(nid, "age"), None)
264
265 def testKeyValue(self):
266 newid = self.db.user.create(username="spam")
267 self.assertEqual(self.db.user.lookup('spam'), newid)
268 self.db.commit()
269 self.assertEqual(self.db.user.lookup('spam'), newid)
270 self.db.user.retire(newid)
271 self.assertRaises(KeyError, self.db.user.lookup, 'spam')
272
273 # use the key again now that the old is retired
274 newid2 = self.db.user.create(username="spam")
275 self.assertNotEqual(newid, newid2)
239 276
240 def testRetire(self): 277 def testRetire(self):
241 self.db.issue.create(title="spam", status='1') 278 self.db.issue.create(title="spam", status='1')
242 b = self.db.status.get('1', 'name') 279 b = self.db.status.get('1', 'name')
243 a = self.db.status.list() 280 a = self.db.status.list()
848 db.close() 885 db.close()
849 self.db = metakit.Database(config) 886 self.db = metakit.Database(config)
850 setupSchema(self.db, 0, metakit) 887 setupSchema(self.db, 0, metakit)
851 888
852 def suite(): 889 def suite():
853 l = [] 890 l = [
854 # l = [ 891 unittest.makeSuite(anydbmDBTestCase, 'test'),
855 # unittest.makeSuite(anydbmDBTestCase, 'test'), 892 unittest.makeSuite(anydbmReadOnlyDBTestCase, 'test')
856 # unittest.makeSuite(anydbmReadOnlyDBTestCase, 'test') 893 ]
857 # ]
858 # return unittest.TestSuite(l) 894 # return unittest.TestSuite(l)
859 895
860 from roundup import backends 896 from roundup import backends
861 p = [] 897 p = []
862 if hasattr(backends, 'mysql'): 898 if hasattr(backends, 'mysql'):

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