~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transactions.py

  • Committer: John Arbash Meinel
  • Date: 2006-03-08 14:31:23 UTC
  • mfrom: (1598 +trunk)
  • mto: (1685.1.1 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060308143123-448308b0db4de410
[merge] bzr.dev 1573, lots of updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
#   Authors: Robert Collins <robert.collins@canonical.com>
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
"""Tests for the behaviour of the Transaction concept in bzr."""
 
19
 
 
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.tests import TestCase, TestCaseInTempDir
 
27
import bzrlib.transactions as transactions
 
28
 
 
29
 
 
30
class DummyWeave(object):
 
31
    """A class that can be instantiated and compared."""
 
32
 
 
33
    def __init__(self, message):
 
34
        self._message = message
 
35
 
 
36
    def __eq__(self, other):
 
37
        if other is None:
 
38
            return False
 
39
        return self._message == other._message
 
40
 
 
41
 
 
42
class TestSymbols(TestCase):
 
43
 
 
44
    def test_public_symbols(self):
 
45
        from bzrlib.transactions import ReadOnlyTransaction
 
46
        from bzrlib.transactions import PassThroughTransaction
 
47
 
 
48
 
 
49
class TestReadOnlyTransaction(TestCase):
 
50
 
 
51
    def setUp(self):
 
52
        self.transaction = transactions.ReadOnlyTransaction()
 
53
        super(TestReadOnlyTransaction, self).setUp()
 
54
 
 
55
    def test_register_clean(self):
 
56
        self.transaction.register_clean("anobject")
 
57
 
 
58
    def test_register_dirty_raises(self):
 
59
        self.assertRaises(errors.ReadOnlyError, 
 
60
                          self.transaction.register_dirty,"anobject")
 
61
    
 
62
    def test_map(self):
 
63
        self.assertNotEqual(None, getattr(self.transaction, "map", None))
 
64
    
 
65
    def test_add_and_get(self):
 
66
        weave = "a weave"
 
67
        self.transaction.map.add_weave("id", weave)
 
68
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
69
 
 
70
    def test_finish_returns(self):
 
71
        self.transaction.finish()
 
72
 
 
73
    def test_zero_size_cache(self):
 
74
        self.transaction.set_cache_size(0)
 
75
        weave = DummyWeave('a weave')
 
76
        self.transaction.map.add_weave("id", weave)
 
77
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
78
        weave = None
 
79
        # add an object, should fall right out if there are no references
 
80
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
 
81
        self.assertEqual(None, self.transaction.map.find_weave("id"))
 
82
        # but if we have a reference it should stick around
 
83
        weave = DummyWeave("another weave")
 
84
        self.transaction.map.add_weave("id", weave)
 
85
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
 
86
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
87
        del weave
 
88
        # its not a weakref system
 
89
        self.assertEqual(DummyWeave("another weave"),
 
90
                         self.transaction.map.find_weave("id"))
 
91
        
 
92
    def test_small_cache(self):
 
93
        self.transaction.set_cache_size(1)
 
94
        # add an object, should not fall right out if there are no references
 
95
        #sys.getrefcounts(foo)
 
96
        self.transaction.map.add_weave("id", DummyWeave("a weave"))
 
97
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
 
98
        self.assertEqual(DummyWeave("a weave"),
 
99
                         self.transaction.map.find_weave("id"))
 
100
        self.transaction.map.add_weave("id2", DummyWeave("a weave also"))
 
101
        self.transaction.register_clean(self.transaction.map.find_weave("id2"))
 
102
        # currently a fifo
 
103
        self.assertEqual(None, self.transaction.map.find_weave("id"))
 
104
        self.assertEqual(DummyWeave("a weave also"),
 
105
                         self.transaction.map.find_weave("id2"))
 
106
 
 
107
    def test_small_cache_with_references(self):
 
108
        # if we have a reference it should stick around
 
109
        weave = "a weave"
 
110
        weave2 = "another weave"
 
111
        self.transaction.map.add_weave("id", weave)
 
112
        self.transaction.map.add_weave("id2", weave2)
 
113
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
114
        self.assertEqual(weave2, self.transaction.map.find_weave("id2"))
 
115
        weave = None
 
116
        # its not a weakref system
 
117
        self.assertEqual("a weave", self.transaction.map.find_weave("id"))
 
118
 
 
119
    def test_precious_with_zero_size_cache(self):
 
120
        self.transaction.set_cache_size(0)
 
121
        weave = DummyWeave('a weave')
 
122
        self.transaction.map.add_weave("id", weave)
 
123
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
124
        weave = None
 
125
        # add an object, should not fall out even with no references.
 
126
        self.transaction.register_clean(self.transaction.map.find_weave("id"),
 
127
                                        precious=True)
 
128
        self.assertEqual(DummyWeave('a weave'),
 
129
                         self.transaction.map.find_weave("id"))
 
130
 
 
131
    def test_precious_revision_history(self):
 
132
        """Disabled test until revision-history is a real object."""
 
133
        print "Disabled: test_precious_revision_history"
 
134
        return
 
135
        self.transaction.set_cache_size(0)
 
136
        history = []
 
137
        self.transaction.map.add_revision_history(history)
 
138
        self.assertEqual(history, self.transaction.map.find_revision_history())
 
139
        history = None
 
140
        # add an object, should not fall out even with no references.
 
141
        self.transaction.register_clean(
 
142
            self.transaction.map.find_revision_history(), precious=True)
 
143
        self.assertEqual([], self.transaction.map.find_revision_history())
 
144
 
 
145
 
 
146
class TestPassThroughTransaction(TestCase):
 
147
 
 
148
    def test_construct(self):
 
149
        transactions.PassThroughTransaction()
 
150
 
 
151
    def test_register_clean(self):
 
152
        transaction = transactions.PassThroughTransaction()
 
153
        transaction.register_clean("anobject")
 
154
    
 
155
    def test_register_dirty(self):
 
156
        transaction = transactions.PassThroughTransaction()
 
157
        transaction.register_dirty("anobject")
 
158
    
 
159
    def test_map(self):
 
160
        transaction = transactions.PassThroughTransaction()
 
161
        self.assertNotEqual(None, getattr(transaction, "map", None))
 
162
    
 
163
    def test_add_and_get(self):
 
164
        transaction = transactions.PassThroughTransaction()
 
165
        weave = "a weave"
 
166
        transaction.map.add_weave("id", weave)
 
167
        self.assertEqual(None, transaction.map.find_weave("id"))
 
168
        
 
169
    def test_finish_returns(self):
 
170
        transaction = transactions.PassThroughTransaction()
 
171
        transaction.finish()
 
172
 
 
173
    def test_cache_is_ignored(self):
 
174
        transaction = transactions.PassThroughTransaction()
 
175
        transaction.set_cache_size(100)
 
176
        weave = "a weave"
 
177
        transaction.map.add_weave("id", weave)
 
178
        self.assertEqual(None, transaction.map.find_weave("id"))
 
179
 
 
180
        
 
181
class TestWriteTransaction(TestCase):
 
182
 
 
183
    def setUp(self):
 
184
        self.transaction = transactions.WriteTransaction()
 
185
        super(TestWriteTransaction, self).setUp()
 
186
 
 
187
    def test_register_clean(self):
 
188
        self.transaction.register_clean("anobject")
 
189
    
 
190
    def test_register_dirty(self):
 
191
        self.transaction.register_dirty("anobject")
 
192
    
 
193
    def test_map(self):
 
194
        self.assertNotEqual(None, getattr(self.transaction, "map", None))
 
195
    
 
196
    def test_add_and_get(self):
 
197
        weave = "a weave"
 
198
        self.transaction.map.add_weave("id", weave)
 
199
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
200
        
 
201
    def test_finish_returns(self):
 
202
        self.transaction.finish()
 
203
 
 
204
    def test_zero_size_cache(self):
 
205
        self.transaction.set_cache_size(0)
 
206
        # add an object, should fall right out if there are no references
 
207
        weave = DummyWeave('a weave')
 
208
        self.transaction.map.add_weave("id", weave)
 
209
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
210
        weave = None
 
211
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
 
212
        self.assertEqual(None, self.transaction.map.find_weave("id"))
 
213
        # but if we have a reference to a clean object it should stick around
 
214
        weave = DummyWeave("another weave")
 
215
        self.transaction.map.add_weave("id", weave)
 
216
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
 
217
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
218
        del weave
 
219
        # its not a weakref system
 
220
        self.assertEqual(DummyWeave("another weave"),
 
221
                         self.transaction.map.find_weave("id"))
 
222
 
 
223
    def test_zero_size_cache_dirty_objects(self):
 
224
        self.transaction.set_cache_size(0)
 
225
        # add a dirty object, which should not fall right out.
 
226
        weave = DummyWeave('a weave')
 
227
        self.transaction.map.add_weave("id", weave)
 
228
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
229
        weave = None
 
230
        self.transaction.register_dirty(self.transaction.map.find_weave("id"))
 
231
        self.assertNotEqual(None, self.transaction.map.find_weave("id"))
 
232
    
 
233
    def test_clean_to_dirty(self):
 
234
        # a clean object may become dirty.
 
235
        weave = DummyWeave('A weave')
 
236
        self.transaction.map.add_weave("id", weave)
 
237
        self.transaction.register_clean(weave)
 
238
        self.transaction.register_dirty(weave)
 
239
        self.assertTrue(self.transaction.is_dirty(weave))
 
240
        self.assertFalse(self.transaction.is_clean(weave))
 
241
 
 
242
    def test_small_cache(self):
 
243
        self.transaction.set_cache_size(1)
 
244
        # add an object, should not fall right out if there are no references
 
245
        #sys.getrefcounts(foo)
 
246
        self.transaction.map.add_weave("id", DummyWeave("a weave"))
 
247
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
 
248
        self.assertEqual(DummyWeave("a weave"),
 
249
                         self.transaction.map.find_weave("id"))
 
250
        self.transaction.map.add_weave("id2", DummyWeave("a weave also"))
 
251
        self.transaction.register_clean(self.transaction.map.find_weave("id2"))
 
252
        # currently a fifo
 
253
        self.assertEqual(None, self.transaction.map.find_weave("id"))
 
254
        self.assertEqual(DummyWeave("a weave also"),
 
255
                         self.transaction.map.find_weave("id2"))
 
256
 
 
257
    def test_small_cache_with_references(self):
 
258
        # if we have a reference it should stick around
 
259
        weave = "a weave"
 
260
        weave2 = "another weave"
 
261
        self.transaction.map.add_weave("id", weave)
 
262
        self.transaction.map.add_weave("id2", weave2)
 
263
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
264
        self.assertEqual(weave2, self.transaction.map.find_weave("id2"))
 
265
        weave = None
 
266
        # its not a weakref system
 
267
        self.assertEqual("a weave", self.transaction.map.find_weave("id"))
 
268
 
 
269
    def test_precious_with_zero_size_cache(self):
 
270
        self.transaction.set_cache_size(0)
 
271
        weave = DummyWeave('a weave')
 
272
        self.transaction.map.add_weave("id", weave)
 
273
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
274
        weave = None
 
275
        # add an object, should not fall out even with no references.
 
276
        self.transaction.register_clean(self.transaction.map.find_weave("id"),
 
277
                                        precious=True)
 
278
        self.assertEqual(DummyWeave('a weave'),
 
279
                         self.transaction.map.find_weave("id"))