~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transactions.py

  • Committer: Martin Pool
  • Date: 2006-03-10 06:29:53 UTC
  • mfrom: (1608 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060310062953-bc1c7ade75c89a7a
[merge] bzr.dev; pycurl not updated for readv yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
    def __init__(self, message):
34
34
        self._message = message
 
35
        self.finished = False
35
36
 
36
37
    def __eq__(self, other):
37
38
        if other is None:
38
39
            return False
39
40
        return self._message == other._message
40
41
 
 
42
    def transaction_finished(self):
 
43
        self.finished = True
 
44
 
41
45
 
42
46
class TestSymbols(TestCase):
43
47
 
59
63
        self.assertRaises(errors.ReadOnlyError, 
60
64
                          self.transaction.register_dirty,"anobject")
61
65
    
62
 
    def test_commit_raises(self):
63
 
        self.assertRaises(errors.CommitNotPossible, self.transaction.commit)
64
 
 
65
66
    def test_map(self):
66
67
        self.assertNotEqual(None, getattr(self.transaction, "map", None))
67
68
    
70
71
        self.transaction.map.add_weave("id", weave)
71
72
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
72
73
 
73
 
    def test_rollback_returns(self):
74
 
        self.transaction.rollback()
75
 
 
76
74
    def test_finish_returns(self):
77
75
        self.transaction.finish()
78
76
 
 
77
    def test_finish_does_not_tell_versioned_file_finished(self):
 
78
        # read only transactions never write, so theres no
 
79
        # need to inform versioned files about finishing
 
80
        weave = DummyWeave('a weave')
 
81
        self.transaction.finish()
 
82
        self.assertFalse(weave.finished)
 
83
 
79
84
    def test_zero_size_cache(self):
80
85
        self.transaction.set_cache_size(0)
81
86
        weave = DummyWeave('a weave')
148
153
            self.transaction.map.find_revision_history(), precious=True)
149
154
        self.assertEqual([], self.transaction.map.find_revision_history())
150
155
 
 
156
    def test_writable(self):
 
157
        self.assertFalse(self.transaction.writeable())
 
158
 
151
159
 
152
160
class TestPassThroughTransaction(TestCase):
153
161
 
162
170
        transaction = transactions.PassThroughTransaction()
163
171
        transaction.register_dirty("anobject")
164
172
    
165
 
    def test_commit_nothing_returns(self):
166
 
        transaction = transactions.PassThroughTransaction()
167
 
        transaction.commit()
168
 
 
169
173
    def test_map(self):
170
174
        transaction = transactions.PassThroughTransaction()
171
175
        self.assertNotEqual(None, getattr(transaction, "map", None))
176
180
        transaction.map.add_weave("id", weave)
177
181
        self.assertEqual(None, transaction.map.find_weave("id"))
178
182
        
179
 
    def test_rollback_asserts(self):
180
 
        transaction = transactions.PassThroughTransaction()
181
 
        self.assertRaises(errors.AlreadyCommitted, transaction.rollback)
182
 
 
183
183
    def test_finish_returns(self):
184
184
        transaction = transactions.PassThroughTransaction()
185
185
        transaction.finish()
186
186
 
 
187
    def test_finish_tells_versioned_file_finished(self):
 
188
        # pass through transactions allow writes so they
 
189
        # need to inform versioned files about finishing
 
190
        weave = DummyWeave('a weave')
 
191
        transaction = transactions.PassThroughTransaction()
 
192
        transaction.register_dirty(weave)
 
193
        transaction.finish()
 
194
        self.assertTrue(weave.finished)
 
195
 
187
196
    def test_cache_is_ignored(self):
188
197
        transaction = transactions.PassThroughTransaction()
189
198
        transaction.set_cache_size(100)
190
199
        weave = "a weave"
191
200
        transaction.map.add_weave("id", weave)
192
201
        self.assertEqual(None, transaction.map.find_weave("id"))
 
202
 
 
203
    def test_writable(self):
 
204
        transaction = transactions.PassThroughTransaction()
 
205
        self.assertTrue(transaction.writeable())
 
206
        
 
207
 
 
208
class TestWriteTransaction(TestCase):
 
209
 
 
210
    def setUp(self):
 
211
        self.transaction = transactions.WriteTransaction()
 
212
        super(TestWriteTransaction, self).setUp()
 
213
 
 
214
    def test_register_clean(self):
 
215
        self.transaction.register_clean("anobject")
 
216
    
 
217
    def test_register_dirty(self):
 
218
        self.transaction.register_dirty("anobject")
 
219
    
 
220
    def test_map(self):
 
221
        self.assertNotEqual(None, getattr(self.transaction, "map", None))
 
222
    
 
223
    def test_add_and_get(self):
 
224
        weave = "a weave"
 
225
        self.transaction.map.add_weave("id", weave)
 
226
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
227
        
 
228
    def test_finish_returns(self):
 
229
        self.transaction.finish()
 
230
 
 
231
    def test_finish_tells_versioned_file_finished(self):
 
232
        # write transactions allow writes so they
 
233
        # need to inform versioned files about finishing
 
234
        weave = DummyWeave('a weave')
 
235
        self.transaction.register_dirty(weave)
 
236
        self.transaction.finish()
 
237
        self.assertTrue(weave.finished)
 
238
 
 
239
    def test_zero_size_cache(self):
 
240
        self.transaction.set_cache_size(0)
 
241
        # add an object, should fall right out if there are no references
 
242
        weave = DummyWeave('a weave')
 
243
        self.transaction.map.add_weave("id", weave)
 
244
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
245
        weave = None
 
246
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
 
247
        self.assertEqual(None, self.transaction.map.find_weave("id"))
 
248
        # but if we have a reference to a clean object it should stick around
 
249
        weave = DummyWeave("another weave")
 
250
        self.transaction.map.add_weave("id", weave)
 
251
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
 
252
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
253
        del weave
 
254
        # its not a weakref system
 
255
        self.assertEqual(DummyWeave("another weave"),
 
256
                         self.transaction.map.find_weave("id"))
 
257
 
 
258
    def test_zero_size_cache_dirty_objects(self):
 
259
        self.transaction.set_cache_size(0)
 
260
        # add a dirty object, which should not fall right out.
 
261
        weave = DummyWeave('a weave')
 
262
        self.transaction.map.add_weave("id", weave)
 
263
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
264
        weave = None
 
265
        self.transaction.register_dirty(self.transaction.map.find_weave("id"))
 
266
        self.assertNotEqual(None, self.transaction.map.find_weave("id"))
 
267
    
 
268
    def test_clean_to_dirty(self):
 
269
        # a clean object may become dirty.
 
270
        weave = DummyWeave('A weave')
 
271
        self.transaction.map.add_weave("id", weave)
 
272
        self.transaction.register_clean(weave)
 
273
        self.transaction.register_dirty(weave)
 
274
        self.assertTrue(self.transaction.is_dirty(weave))
 
275
        self.assertFalse(self.transaction.is_clean(weave))
 
276
 
 
277
    def test_small_cache(self):
 
278
        self.transaction.set_cache_size(1)
 
279
        # add an object, should not fall right out if there are no references
 
280
        #sys.getrefcounts(foo)
 
281
        self.transaction.map.add_weave("id", DummyWeave("a weave"))
 
282
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
 
283
        self.assertEqual(DummyWeave("a weave"),
 
284
                         self.transaction.map.find_weave("id"))
 
285
        self.transaction.map.add_weave("id2", DummyWeave("a weave also"))
 
286
        self.transaction.register_clean(self.transaction.map.find_weave("id2"))
 
287
        # currently a fifo
 
288
        self.assertEqual(None, self.transaction.map.find_weave("id"))
 
289
        self.assertEqual(DummyWeave("a weave also"),
 
290
                         self.transaction.map.find_weave("id2"))
 
291
 
 
292
    def test_small_cache_with_references(self):
 
293
        # if we have a reference it should stick around
 
294
        weave = "a weave"
 
295
        weave2 = "another weave"
 
296
        self.transaction.map.add_weave("id", weave)
 
297
        self.transaction.map.add_weave("id2", weave2)
 
298
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
299
        self.assertEqual(weave2, self.transaction.map.find_weave("id2"))
 
300
        weave = None
 
301
        # its not a weakref system
 
302
        self.assertEqual("a weave", self.transaction.map.find_weave("id"))
 
303
 
 
304
    def test_precious_with_zero_size_cache(self):
 
305
        self.transaction.set_cache_size(0)
 
306
        weave = DummyWeave('a weave')
 
307
        self.transaction.map.add_weave("id", weave)
 
308
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
309
        weave = None
 
310
        # add an object, should not fall out even with no references.
 
311
        self.transaction.register_clean(self.transaction.map.find_weave("id"),
 
312
                                        precious=True)
 
313
        self.assertEqual(DummyWeave('a weave'),
 
314
                         self.transaction.map.find_weave("id"))
 
315
 
 
316
    def test_writable(self):
 
317
        self.assertTrue(self.transaction.writeable())