27
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
30
49
class TestReadOnlyTransaction(TestCase):
32
def test_symbols(self):
33
from bzrlib.transactions import ReadOnlyTransaction
35
def test_construct(self):
36
transactions.ReadOnlyTransaction()
52
self.transaction = transactions.ReadOnlyTransaction()
53
super(TestReadOnlyTransaction, self).setUp()
38
55
def test_register_clean(self):
39
transaction = transactions.ReadOnlyTransaction()
40
transaction.register_clean("anobject")
56
self.transaction.register_clean("anobject")
42
58
def test_register_dirty_raises(self):
43
transaction = transactions.ReadOnlyTransaction()
44
59
self.assertRaises(errors.ReadOnlyError,
45
transaction.register_dirty,"anobject")
60
self.transaction.register_dirty,"anobject")
47
62
def test_commit_raises(self):
48
transaction = transactions.ReadOnlyTransaction()
49
self.assertRaises(errors.CommitNotPossible, transaction.commit)
63
self.assertRaises(errors.CommitNotPossible, self.transaction.commit)
51
65
def test_map(self):
52
transaction = transactions.ReadOnlyTransaction()
53
self.assertNotEqual(None, getattr(transaction, "map", None))
66
self.assertNotEqual(None, getattr(self.transaction, "map", None))
55
68
def test_add_and_get(self):
56
transaction = transactions.ReadOnlyTransaction()
58
transaction.map.add_weave("id", weave)
59
self.assertEqual(weave, transaction.map.find_weave("id"))
70
self.transaction.map.add_weave("id", weave)
71
self.assertEqual(weave, self.transaction.map.find_weave("id"))
61
73
def test_rollback_returns(self):
62
transaction = transactions.ReadOnlyTransaction()
63
transaction.rollback()
74
self.transaction.rollback()
65
76
def test_finish_returns(self):
66
transaction = transactions.ReadOnlyTransaction()
77
self.transaction.finish()
79
def test_zero_size_cache(self):
80
self.transaction.set_cache_size(0)
81
weave = DummyWeave('a weave')
82
self.transaction.map.add_weave("id", weave)
83
self.assertEqual(weave, self.transaction.map.find_weave("id"))
85
# add an object, should fall right out if there are no references
86
self.transaction.register_clean(self.transaction.map.find_weave("id"))
87
self.assertEqual(None, self.transaction.map.find_weave("id"))
88
# but if we have a reference it should stick around
89
weave = DummyWeave("another weave")
90
self.transaction.map.add_weave("id", weave)
91
self.transaction.register_clean(self.transaction.map.find_weave("id"))
92
self.assertEqual(weave, self.transaction.map.find_weave("id"))
94
# its not a weakref system
95
self.assertEqual(DummyWeave("another weave"),
96
self.transaction.map.find_weave("id"))
98
def test_small_cache(self):
99
self.transaction.set_cache_size(1)
100
# add an object, should not fall right out if there are no references
101
#sys.getrefcounts(foo)
102
self.transaction.map.add_weave("id", DummyWeave("a weave"))
103
self.transaction.register_clean(self.transaction.map.find_weave("id"))
104
self.assertEqual(DummyWeave("a weave"),
105
self.transaction.map.find_weave("id"))
106
self.transaction.map.add_weave("id2", DummyWeave("a weave also"))
107
self.transaction.register_clean(self.transaction.map.find_weave("id2"))
109
self.assertEqual(None, self.transaction.map.find_weave("id"))
110
self.assertEqual(DummyWeave("a weave also"),
111
self.transaction.map.find_weave("id2"))
113
def test_small_cache_with_references(self):
114
# if we have a reference it should stick around
116
weave2 = "another weave"
117
self.transaction.map.add_weave("id", weave)
118
self.transaction.map.add_weave("id2", weave2)
119
self.assertEqual(weave, self.transaction.map.find_weave("id"))
120
self.assertEqual(weave2, self.transaction.map.find_weave("id2"))
122
# its not a weakref system
123
self.assertEqual("a weave", self.transaction.map.find_weave("id"))
125
def test_precious_with_zero_size_cache(self):
126
self.transaction.set_cache_size(0)
127
weave = DummyWeave('a weave')
128
self.transaction.map.add_weave("id", weave)
129
self.assertEqual(weave, self.transaction.map.find_weave("id"))
131
# add an object, should not fall out even with no references.
132
self.transaction.register_clean(self.transaction.map.find_weave("id"),
134
self.assertEqual(DummyWeave('a weave'),
135
self.transaction.map.find_weave("id"))
137
def test_precious_revision_history(self):
138
"""Disabled test until revision-history is a real object."""
139
print "Disabled: test_precious_revision_history"
141
self.transaction.set_cache_size(0)
143
self.transaction.map.add_revision_history(history)
144
self.assertEqual(history, self.transaction.map.find_revision_history())
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())
70
152
class TestPassThroughTransaction(TestCase):
72
def test_symbols(self):
73
from bzrlib.transactions import PassThroughTransaction
75
154
def test_construct(self):
76
155
transactions.PassThroughTransaction()