/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.15 by John Arbash Meinel
Merge bzr.dev 5597 to resolve NEWS, aka bzr-2.3.txt
1
# Copyright (C) 2005, 2006, 2009, 2011 Canonical Ltd
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
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
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
17
18
"""Tests for the behaviour of the Transaction concept in bzr."""
19
7143.15.2 by Jelmer Vernooij
Run autopep8.
20
# import breezy specific imports here
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
21
import breezy.errors as errors
22
from breezy.tests import TestCase
23
import breezy.transactions as transactions
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
24
25
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
26
class DummyWeave(object):
27
    """A class that can be instantiated and compared."""
28
29
    def __init__(self, message):
30
        self._message = message
1594.2.20 by Robert Collins
Add finished() notifications to transactions.
31
        self.finished = False
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
32
33
    def __eq__(self, other):
34
        if other is None:
35
            return False
36
        return self._message == other._message
37
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
38
    def __hash__(self):
39
        return hash((type(self), self._message))
40
1594.2.20 by Robert Collins
Add finished() notifications to transactions.
41
    def transaction_finished(self):
42
        self.finished = True
43
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
44
45
class TestSymbols(TestCase):
46
1417.1.12 by Robert Collins
cache revision history during read transactions
47
    def test_public_symbols(self):
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
48
        from breezy.transactions import ReadOnlyTransaction  # noqa: F401
49
        from breezy.transactions import PassThroughTransaction  # noqa: F401
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
50
51
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
52
class TestReadOnlyTransaction(TestCase):
53
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
54
    def setUp(self):
55
        self.transaction = transactions.ReadOnlyTransaction()
56
        super(TestReadOnlyTransaction, self).setUp()
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
57
58
    def test_register_clean(self):
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
59
        self.transaction.register_clean("anobject")
1417.1.8 by Robert Collins
use transactions in the weave store interface, which enables caching for log
60
61
    def test_register_dirty_raises(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
62
        self.assertRaises(errors.ReadOnlyError,
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
63
                          self.transaction.register_dirty, "anobject")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
64
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
65
    def test_map(self):
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
66
        self.assertNotEqual(None, getattr(self.transaction, "map", None))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
67
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
68
    def test_add_and_get(self):
69
        weave = "a weave"
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
70
        self.transaction.map.add_weave("id", weave)
71
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
72
73
    def test_finish_returns(self):
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
74
        self.transaction.finish()
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
75
1594.2.20 by Robert Collins
Add finished() notifications to transactions.
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
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
83
    def test_zero_size_cache(self):
84
        self.transaction.set_cache_size(0)
85
        weave = DummyWeave('a weave')
86
        self.transaction.map.add_weave("id", weave)
87
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
88
        weave = None
89
        # add an object, should fall right out if there are no references
90
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
91
        self.assertEqual(None, self.transaction.map.find_weave("id"))
92
        # but if we have a reference it should stick around
93
        weave = DummyWeave("another weave")
94
        self.transaction.map.add_weave("id", weave)
95
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
96
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
97
        del weave
98
        # its not a weakref system
99
        self.assertEqual(DummyWeave("another weave"),
100
                         self.transaction.map.find_weave("id"))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
101
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
102
    def test_small_cache(self):
103
        self.transaction.set_cache_size(1)
104
        # add an object, should not fall right out if there are no references
7143.15.2 by Jelmer Vernooij
Run autopep8.
105
        # sys.getrefcounts(foo)
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
106
        self.transaction.map.add_weave("id", DummyWeave("a weave"))
107
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
108
        self.assertEqual(DummyWeave("a weave"),
109
                         self.transaction.map.find_weave("id"))
110
        self.transaction.map.add_weave("id2", DummyWeave("a weave also"))
111
        self.transaction.register_clean(self.transaction.map.find_weave("id2"))
112
        # currently a fifo
113
        self.assertEqual(None, self.transaction.map.find_weave("id"))
114
        self.assertEqual(DummyWeave("a weave also"),
115
                         self.transaction.map.find_weave("id2"))
116
117
    def test_small_cache_with_references(self):
118
        # if we have a reference it should stick around
119
        weave = "a weave"
120
        weave2 = "another weave"
121
        self.transaction.map.add_weave("id", weave)
122
        self.transaction.map.add_weave("id2", weave2)
123
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
124
        self.assertEqual(weave2, self.transaction.map.find_weave("id2"))
125
        weave = None
126
        # its not a weakref system
127
        self.assertEqual("a weave", self.transaction.map.find_weave("id"))
128
129
    def test_precious_with_zero_size_cache(self):
130
        self.transaction.set_cache_size(0)
131
        weave = DummyWeave('a weave')
132
        self.transaction.map.add_weave("id", weave)
133
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
134
        weave = None
135
        # add an object, should not fall out even with no references.
136
        self.transaction.register_clean(self.transaction.map.find_weave("id"),
137
                                        precious=True)
138
        self.assertEqual(DummyWeave('a weave'),
139
                         self.transaction.map.find_weave("id"))
140
1594.2.20 by Robert Collins
Add finished() notifications to transactions.
141
    def test_writable(self):
142
        self.assertFalse(self.transaction.writeable())
143
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
144
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
145
class TestPassThroughTransaction(TestCase):
146
147
    def test_construct(self):
148
        transactions.PassThroughTransaction()
149
150
    def test_register_clean(self):
151
        transaction = transactions.PassThroughTransaction()
152
        transaction.register_clean("anobject")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
153
1417.1.8 by Robert Collins
use transactions in the weave store interface, which enables caching for log
154
    def test_register_dirty(self):
155
        transaction = transactions.PassThroughTransaction()
156
        transaction.register_dirty("anobject")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
157
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
158
    def test_map(self):
159
        transaction = transactions.PassThroughTransaction()
160
        self.assertNotEqual(None, getattr(transaction, "map", None))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
161
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
162
    def test_add_and_get(self):
163
        transaction = transactions.PassThroughTransaction()
164
        weave = "a weave"
165
        transaction.map.add_weave("id", weave)
166
        self.assertEqual(None, transaction.map.find_weave("id"))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
167
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
168
    def test_finish_returns(self):
169
        transaction = transactions.PassThroughTransaction()
170
        transaction.finish()
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
171
1594.2.20 by Robert Collins
Add finished() notifications to transactions.
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
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
181
    def test_cache_is_ignored(self):
182
        transaction = transactions.PassThroughTransaction()
183
        transaction.set_cache_size(100)
184
        weave = "a weave"
185
        transaction.map.add_weave("id", weave)
186
        self.assertEqual(None, transaction.map.find_weave("id"))
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
187
1594.2.20 by Robert Collins
Add finished() notifications to transactions.
188
    def test_writable(self):
189
        transaction = transactions.PassThroughTransaction()
190
        self.assertTrue(transaction.writeable())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
191
1594.2.20 by Robert Collins
Add finished() notifications to transactions.
192
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
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")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
201
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
202
    def test_register_dirty(self):
203
        self.transaction.register_dirty("anobject")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
204
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
205
    def test_map(self):
206
        self.assertNotEqual(None, getattr(self.transaction, "map", None))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
207
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
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"))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
212
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
213
    def test_finish_returns(self):
214
        self.transaction.finish()
215
1594.2.20 by Robert Collins
Add finished() notifications to transactions.
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
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
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"))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
252
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
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
7143.15.2 by Jelmer Vernooij
Run autopep8.
265
        # sys.getrefcounts(foo)
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
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"))
1594.2.20 by Robert Collins
Add finished() notifications to transactions.
300
301
    def test_writable(self):
302
        self.assertTrue(self.transaction.writeable())