/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testtransactions.py

  • Committer: Robert Collins
  • Date: 2005-10-19 10:11:57 UTC
  • mfrom: (1185.16.78)
  • mto: This revision was merged to the branch mainline in revision 1470.
  • Revision ID: robertc@robertcollins.net-20051019101157-17438d311e746b4f
mergeĀ fromĀ upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2009, 2011 Canonical Ltd
 
1
# Copyright (C) 2005 by Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
"""Tests for the behaviour of the Transaction concept in bzr."""
19
19
 
20
 
# import breezy specific imports here
21
 
import breezy.errors as errors
22
 
from breezy.tests import TestCase
23
 
import breezy.transactions as transactions
 
20
# import system imports here
 
21
import os
 
22
import sys
 
23
 
 
24
#import bzrlib specific imports here
 
25
import bzrlib.errors as errors
 
26
from bzrlib.selftest import TestCase, TestCaseInTempDir
 
27
import bzrlib.transactions as transactions
24
28
 
25
29
 
26
30
class DummyWeave(object):
28
32
 
29
33
    def __init__(self, message):
30
34
        self._message = message
31
 
        self.finished = False
32
35
 
33
36
    def __eq__(self, other):
34
37
        if other is None:
35
38
            return False
36
39
        return self._message == other._message
37
40
 
38
 
    def __hash__(self):
39
 
        return hash((type(self), self._message))
40
 
 
41
 
    def transaction_finished(self):
42
 
        self.finished = True
43
 
 
44
41
 
45
42
class TestSymbols(TestCase):
46
43
 
47
44
    def test_public_symbols(self):
48
 
        from breezy.transactions import ReadOnlyTransaction  # noqa: F401
49
 
        from breezy.transactions import PassThroughTransaction  # noqa: F401
 
45
        from bzrlib.transactions import ReadOnlyTransaction
 
46
        from bzrlib.transactions import PassThroughTransaction
50
47
 
51
48
 
52
49
class TestReadOnlyTransaction(TestCase):
59
56
        self.transaction.register_clean("anobject")
60
57
 
61
58
    def test_register_dirty_raises(self):
62
 
        self.assertRaises(errors.ReadOnlyError,
63
 
                          self.transaction.register_dirty, "anobject")
 
59
        self.assertRaises(errors.ReadOnlyError, 
 
60
                          self.transaction.register_dirty,"anobject")
 
61
    
 
62
    def test_commit_raises(self):
 
63
        self.assertRaises(errors.CommitNotPossible, self.transaction.commit)
64
64
 
65
65
    def test_map(self):
66
66
        self.assertNotEqual(None, getattr(self.transaction, "map", None))
67
 
 
 
67
    
68
68
    def test_add_and_get(self):
69
69
        weave = "a weave"
70
70
        self.transaction.map.add_weave("id", weave)
71
71
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
72
72
 
 
73
    def test_rollback_returns(self):
 
74
        self.transaction.rollback()
 
75
 
73
76
    def test_finish_returns(self):
74
77
        self.transaction.finish()
75
78
 
76
 
    def test_finish_does_not_tell_versioned_file_finished(self):
77
 
        # read only transactions never write, so theres no
78
 
        # need to inform versioned files about finishing
79
 
        weave = DummyWeave('a weave')
80
 
        self.transaction.finish()
81
 
        self.assertFalse(weave.finished)
82
 
 
83
79
    def test_zero_size_cache(self):
84
80
        self.transaction.set_cache_size(0)
85
81
        weave = DummyWeave('a weave')
98
94
        # its not a weakref system
99
95
        self.assertEqual(DummyWeave("another weave"),
100
96
                         self.transaction.map.find_weave("id"))
101
 
 
 
97
        
102
98
    def test_small_cache(self):
103
99
        self.transaction.set_cache_size(1)
104
100
        # add an object, should not fall right out if there are no references
105
 
        # sys.getrefcounts(foo)
 
101
        #sys.getrefcounts(foo)
106
102
        self.transaction.map.add_weave("id", DummyWeave("a weave"))
107
103
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
108
104
        self.assertEqual(DummyWeave("a weave"),
138
134
        self.assertEqual(DummyWeave('a weave'),
139
135
                         self.transaction.map.find_weave("id"))
140
136
 
141
 
    def test_writable(self):
142
 
        self.assertFalse(self.transaction.writeable())
 
137
    def test_precious_revision_history(self):
 
138
        """Disabled test until revision-history is a real object."""
 
139
        print "Disabled: test_precious_revision_history"
 
140
        return
 
141
        self.transaction.set_cache_size(0)
 
142
        history = []
 
143
        self.transaction.map.add_revision_history(history)
 
144
        self.assertEqual(history, self.transaction.map.find_revision_history())
 
145
        history = None
 
146
        # add an object, should not fall out even with no references.
 
147
        self.transaction.register_clean(
 
148
            self.transaction.map.find_revision_history(), precious=True)
 
149
        self.assertEqual([], self.transaction.map.find_revision_history())
143
150
 
144
151
 
145
152
class TestPassThroughTransaction(TestCase):
150
157
    def test_register_clean(self):
151
158
        transaction = transactions.PassThroughTransaction()
152
159
        transaction.register_clean("anobject")
153
 
 
 
160
    
154
161
    def test_register_dirty(self):
155
162
        transaction = transactions.PassThroughTransaction()
156
163
        transaction.register_dirty("anobject")
 
164
    
 
165
    def test_commit_nothing_returns(self):
 
166
        transaction = transactions.PassThroughTransaction()
 
167
        transaction.commit()
157
168
 
158
169
    def test_map(self):
159
170
        transaction = transactions.PassThroughTransaction()
160
171
        self.assertNotEqual(None, getattr(transaction, "map", None))
161
 
 
 
172
    
162
173
    def test_add_and_get(self):
163
174
        transaction = transactions.PassThroughTransaction()
164
175
        weave = "a weave"
165
176
        transaction.map.add_weave("id", weave)
166
177
        self.assertEqual(None, transaction.map.find_weave("id"))
 
178
        
 
179
    def test_rollback_asserts(self):
 
180
        transaction = transactions.PassThroughTransaction()
 
181
        self.assertRaises(errors.AlreadyCommitted, transaction.rollback)
167
182
 
168
183
    def test_finish_returns(self):
169
184
        transaction = transactions.PassThroughTransaction()
170
185
        transaction.finish()
171
186
 
172
 
    def test_finish_tells_versioned_file_finished(self):
173
 
        # pass through transactions allow writes so they
174
 
        # need to inform versioned files about finishing
175
 
        weave = DummyWeave('a weave')
176
 
        transaction = transactions.PassThroughTransaction()
177
 
        transaction.register_dirty(weave)
178
 
        transaction.finish()
179
 
        self.assertTrue(weave.finished)
180
 
 
181
187
    def test_cache_is_ignored(self):
182
188
        transaction = transactions.PassThroughTransaction()
183
189
        transaction.set_cache_size(100)
184
190
        weave = "a weave"
185
191
        transaction.map.add_weave("id", weave)
186
192
        self.assertEqual(None, transaction.map.find_weave("id"))
187
 
 
188
 
    def test_writable(self):
189
 
        transaction = transactions.PassThroughTransaction()
190
 
        self.assertTrue(transaction.writeable())
191
 
 
192
 
 
193
 
class TestWriteTransaction(TestCase):
194
 
 
195
 
    def setUp(self):
196
 
        self.transaction = transactions.WriteTransaction()
197
 
        super(TestWriteTransaction, self).setUp()
198
 
 
199
 
    def test_register_clean(self):
200
 
        self.transaction.register_clean("anobject")
201
 
 
202
 
    def test_register_dirty(self):
203
 
        self.transaction.register_dirty("anobject")
204
 
 
205
 
    def test_map(self):
206
 
        self.assertNotEqual(None, getattr(self.transaction, "map", None))
207
 
 
208
 
    def test_add_and_get(self):
209
 
        weave = "a weave"
210
 
        self.transaction.map.add_weave("id", weave)
211
 
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
212
 
 
213
 
    def test_finish_returns(self):
214
 
        self.transaction.finish()
215
 
 
216
 
    def test_finish_tells_versioned_file_finished(self):
217
 
        # write transactions allow writes so they
218
 
        # need to inform versioned files about finishing
219
 
        weave = DummyWeave('a weave')
220
 
        self.transaction.register_dirty(weave)
221
 
        self.transaction.finish()
222
 
        self.assertTrue(weave.finished)
223
 
 
224
 
    def test_zero_size_cache(self):
225
 
        self.transaction.set_cache_size(0)
226
 
        # add an object, should fall right out if there are no references
227
 
        weave = DummyWeave('a weave')
228
 
        self.transaction.map.add_weave("id", weave)
229
 
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
230
 
        weave = None
231
 
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
232
 
        self.assertEqual(None, self.transaction.map.find_weave("id"))
233
 
        # but if we have a reference to a clean object it should stick around
234
 
        weave = DummyWeave("another weave")
235
 
        self.transaction.map.add_weave("id", weave)
236
 
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
237
 
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
238
 
        del weave
239
 
        # its not a weakref system
240
 
        self.assertEqual(DummyWeave("another weave"),
241
 
                         self.transaction.map.find_weave("id"))
242
 
 
243
 
    def test_zero_size_cache_dirty_objects(self):
244
 
        self.transaction.set_cache_size(0)
245
 
        # add a dirty object, which should not fall right out.
246
 
        weave = DummyWeave('a weave')
247
 
        self.transaction.map.add_weave("id", weave)
248
 
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
249
 
        weave = None
250
 
        self.transaction.register_dirty(self.transaction.map.find_weave("id"))
251
 
        self.assertNotEqual(None, self.transaction.map.find_weave("id"))
252
 
 
253
 
    def test_clean_to_dirty(self):
254
 
        # a clean object may become dirty.
255
 
        weave = DummyWeave('A weave')
256
 
        self.transaction.map.add_weave("id", weave)
257
 
        self.transaction.register_clean(weave)
258
 
        self.transaction.register_dirty(weave)
259
 
        self.assertTrue(self.transaction.is_dirty(weave))
260
 
        self.assertFalse(self.transaction.is_clean(weave))
261
 
 
262
 
    def test_small_cache(self):
263
 
        self.transaction.set_cache_size(1)
264
 
        # add an object, should not fall right out if there are no references
265
 
        # sys.getrefcounts(foo)
266
 
        self.transaction.map.add_weave("id", DummyWeave("a weave"))
267
 
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
268
 
        self.assertEqual(DummyWeave("a weave"),
269
 
                         self.transaction.map.find_weave("id"))
270
 
        self.transaction.map.add_weave("id2", DummyWeave("a weave also"))
271
 
        self.transaction.register_clean(self.transaction.map.find_weave("id2"))
272
 
        # currently a fifo
273
 
        self.assertEqual(None, self.transaction.map.find_weave("id"))
274
 
        self.assertEqual(DummyWeave("a weave also"),
275
 
                         self.transaction.map.find_weave("id2"))
276
 
 
277
 
    def test_small_cache_with_references(self):
278
 
        # if we have a reference it should stick around
279
 
        weave = "a weave"
280
 
        weave2 = "another weave"
281
 
        self.transaction.map.add_weave("id", weave)
282
 
        self.transaction.map.add_weave("id2", weave2)
283
 
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
284
 
        self.assertEqual(weave2, self.transaction.map.find_weave("id2"))
285
 
        weave = None
286
 
        # its not a weakref system
287
 
        self.assertEqual("a weave", self.transaction.map.find_weave("id"))
288
 
 
289
 
    def test_precious_with_zero_size_cache(self):
290
 
        self.transaction.set_cache_size(0)
291
 
        weave = DummyWeave('a weave')
292
 
        self.transaction.map.add_weave("id", weave)
293
 
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
294
 
        weave = None
295
 
        # add an object, should not fall out even with no references.
296
 
        self.transaction.register_clean(self.transaction.map.find_weave("id"),
297
 
                                        precious=True)
298
 
        self.assertEqual(DummyWeave('a weave'),
299
 
                         self.transaction.map.find_weave("id"))
300
 
 
301
 
    def test_writable(self):
302
 
        self.assertTrue(self.transaction.writeable())