ab7251a0e87a84dc57f1ff6807950c4c05ae39ec
1 # $Id: test_db.py,v 1.3 2001-07-29 07:01:39 richard Exp $
3 import unittest, os, shutil
5 from roundup.backends import anydbm
6 from roundup.hyperdb import String, Link, Multilink, Date, Interval, Class, \
7 DatabaseError
9 def setupSchema(db, create):
10 status = Class(db, "status", name=String())
11 status.setkey("name")
12 if create:
13 status.create(name="unread")
14 status.create(name="in-progress")
15 status.create(name="testing")
16 status.create(name="resolved")
17 Class(db, "user", username=String(), password=String())
18 Class(db, "issue", title=String(), status=Link("status"),
19 nosy=Multilink("user"))
21 class DBTestCase(unittest.TestCase):
22 def setUp(self):
23 class Database(anydbm.Database):
24 pass
25 # remove previous test, ignore errors
26 if os.path.exists('_test_dir'):
27 shutil.rmtree('_test_dir')
28 os.mkdir('_test_dir')
29 self.db = Database('_test_dir', 'test')
30 setupSchema(self.db, 1)
32 def tearDown(self):
33 self.db.close()
34 shutil.rmtree('_test_dir')
36 def testChanges(self):
37 self.db.issue.create(title="spam", status='1')
38 self.db.issue.create(title="eggs", status='2')
39 self.db.issue.create(title="ham", status='4')
40 self.db.issue.create(title="arguments", status='2')
41 self.db.issue.create(title="abuse", status='1')
42 self.db.issue.addprop(fixer=Link("user"))
43 props = self.db.issue.getprops()
44 keys = props.keys()
45 keys.sort()
46 self.assertEqual(keys, ['fixer', 'id', 'nosy', 'status', 'title'])
47 self.db.issue.set('5', status='2')
48 self.db.issue.get('5', "status")
49 self.db.status.get('2', "name")
50 self.db.issue.get('5', "title")
51 self.db.issue.find(status = self.db.status.lookup("in-progress"))
52 self.db.issue.history('5')
53 self.db.status.history('1')
54 self.db.status.history('2')
56 def testExceptions(self):
57 # this tests the exceptions that should be raised
58 ar = self.assertRaises
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'])
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')
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'])
118 def testRetire(self):
119 ''' test retiring a node
120 '''
121 pass
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)
138 def tearDown(self):
139 self.db.close()
140 shutil.rmtree('_test_dir')
142 def testExceptions(self):
143 # this tests the exceptions that should be raised
144 ar = self.assertRaises
146 # this tests the exceptions that should be raised
147 ar(DatabaseError, self.db.status.create, name="foo")
148 ar(DatabaseError, self.db.status.set, '1', name="foo")
149 ar(DatabaseError, self.db.status.retire, '1')
152 def suite():
153 db = unittest.makeSuite(DBTestCase, 'test')
154 readonlydb = unittest.makeSuite(ReadOnlyDBTestCase, 'test')
155 return unittest.TestSuite((db, readonlydb))
158 #
159 # $Log: not supported by cvs2svn $
160 # Revision 1.2 2001/07/29 04:09:20 richard
161 # Added the fabricated property "id" to all hyperdb classes.
162 #
163 # Revision 1.1 2001/07/27 06:55:07 richard
164 # moving tests -> test
165 #
166 # Revision 1.7 2001/07/27 06:26:43 richard
167 # oops - wasn't deleting the test dir after the read-only tests
168 #
169 # Revision 1.6 2001/07/27 06:23:59 richard
170 # consistency
171 #
172 # Revision 1.5 2001/07/27 06:23:09 richard
173 # Added some new hyperdb tests to make sure we raise the right exceptions.
174 #
175 # Revision 1.4 2001/07/25 04:34:31 richard
176 # Added id and log to tests files...
177 #
178 #
179 # vim: set filetype=python ts=4 sw=4 et si