1
# Copyright (C) 2005 by Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
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.
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.
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
18
"""Tests for the behaviour of the Transaction concept in bzr."""
20
# import system imports here
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
30
class DummyWeave(object):
31
"""A class that can be instantiated and compared."""
33
def __init__(self, message):
34
self._message = message
36
def __eq__(self, other):
39
return self._message == other._message
42
class TestSymbols(TestCase):
44
def test_public_symbols(self):
45
from bzrlib.transactions import ReadOnlyTransaction
46
from bzrlib.transactions import PassThroughTransaction
49
class TestReadOnlyTransaction(TestCase):
52
self.transaction = transactions.ReadOnlyTransaction()
53
super(TestReadOnlyTransaction, self).setUp()
55
def test_register_clean(self):
56
self.transaction.register_clean("anobject")
58
def test_register_dirty_raises(self):
59
self.assertRaises(errors.ReadOnlyError,
60
self.transaction.register_dirty,"anobject")
63
self.assertNotEqual(None, getattr(self.transaction, "map", None))
65
def test_add_and_get(self):
67
self.transaction.map.add_weave("id", weave)
68
self.assertEqual(weave, self.transaction.map.find_weave("id"))
70
def test_finish_returns(self):
71
self.transaction.finish()
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"))
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"))
88
# its not a weakref system
89
self.assertEqual(DummyWeave("another weave"),
90
self.transaction.map.find_weave("id"))
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"))
103
self.assertEqual(None, self.transaction.map.find_weave("id"))
104
self.assertEqual(DummyWeave("a weave also"),
105
self.transaction.map.find_weave("id2"))
107
def test_small_cache_with_references(self):
108
# if we have a reference it should stick around
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"))
116
# its not a weakref system
117
self.assertEqual("a weave", self.transaction.map.find_weave("id"))
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"))
125
# add an object, should not fall out even with no references.
126
self.transaction.register_clean(self.transaction.map.find_weave("id"),
128
self.assertEqual(DummyWeave('a weave'),
129
self.transaction.map.find_weave("id"))
131
def test_precious_revision_history(self):
132
"""Disabled test until revision-history is a real object."""
133
print "Disabled: test_precious_revision_history"
135
self.transaction.set_cache_size(0)
137
self.transaction.map.add_revision_history(history)
138
self.assertEqual(history, self.transaction.map.find_revision_history())
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())
146
class TestPassThroughTransaction(TestCase):
148
def test_construct(self):
149
transactions.PassThroughTransaction()
151
def test_register_clean(self):
152
transaction = transactions.PassThroughTransaction()
153
transaction.register_clean("anobject")
155
def test_register_dirty(self):
156
transaction = transactions.PassThroughTransaction()
157
transaction.register_dirty("anobject")
160
transaction = transactions.PassThroughTransaction()
161
self.assertNotEqual(None, getattr(transaction, "map", None))
163
def test_add_and_get(self):
164
transaction = transactions.PassThroughTransaction()
166
transaction.map.add_weave("id", weave)
167
self.assertEqual(None, transaction.map.find_weave("id"))
169
def test_finish_returns(self):
170
transaction = transactions.PassThroughTransaction()
173
def test_cache_is_ignored(self):
174
transaction = transactions.PassThroughTransaction()
175
transaction.set_cache_size(100)
177
transaction.map.add_weave("id", weave)
178
self.assertEqual(None, transaction.map.find_weave("id"))
181
class TestWriteTransaction(TestCase):
184
self.transaction = transactions.WriteTransaction()
185
super(TestWriteTransaction, self).setUp()
187
def test_register_clean(self):
188
self.transaction.register_clean("anobject")
190
def test_register_dirty(self):
191
self.transaction.register_dirty("anobject")
194
self.assertNotEqual(None, getattr(self.transaction, "map", None))
196
def test_add_and_get(self):
198
self.transaction.map.add_weave("id", weave)
199
self.assertEqual(weave, self.transaction.map.find_weave("id"))
201
def test_finish_returns(self):
202
self.transaction.finish()
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"))
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"))
219
# its not a weakref system
220
self.assertEqual(DummyWeave("another weave"),
221
self.transaction.map.find_weave("id"))
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"))
230
self.transaction.register_dirty(self.transaction.map.find_weave("id"))
231
self.assertNotEqual(None, self.transaction.map.find_weave("id"))
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))
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"))
253
self.assertEqual(None, self.transaction.map.find_weave("id"))
254
self.assertEqual(DummyWeave("a weave also"),
255
self.transaction.map.find_weave("id2"))
257
def test_small_cache_with_references(self):
258
# if we have a reference it should stick around
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"))
266
# its not a weakref system
267
self.assertEqual("a weave", self.transaction.map.find_weave("id"))
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"))
275
# add an object, should not fall out even with no references.
276
self.transaction.register_clean(self.transaction.map.find_weave("id"),
278
self.assertEqual(DummyWeave('a weave'),
279
self.transaction.map.find_weave("id"))