~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testtransactions.py

  • Committer: Robert Collins
  • Date: 2005-10-11 02:52:47 UTC
  • mfrom: (1417.1.13)
  • Revision ID: robertc@robertcollins.net-20051011025247-4b95466bb6509385
merge in revision-history caching, and tuning of fetch to not retrieve more data than needed when nothing needs to be pulled

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import bzrlib.transactions as transactions
28
28
 
29
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
 
30
49
class TestReadOnlyTransaction(TestCase):
31
50
 
32
 
    def test_symbols(self):
33
 
        from bzrlib.transactions import ReadOnlyTransaction
34
 
 
35
 
    def test_construct(self):
36
 
        transactions.ReadOnlyTransaction()
 
51
    def setUp(self):
 
52
        self.transaction = transactions.ReadOnlyTransaction()
 
53
        super(TestReadOnlyTransaction, self).setUp()
37
54
 
38
55
    def test_register_clean(self):
39
 
        transaction = transactions.ReadOnlyTransaction()
40
 
        transaction.register_clean("anobject")
 
56
        self.transaction.register_clean("anobject")
41
57
 
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")
46
61
    
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)
50
64
 
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))
54
67
    
55
68
    def test_add_and_get(self):
56
 
        transaction = transactions.ReadOnlyTransaction()
57
69
        weave = "a weave"
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"))
60
72
 
61
73
    def test_rollback_returns(self):
62
 
        transaction = transactions.ReadOnlyTransaction()
63
 
        transaction.rollback()
 
74
        self.transaction.rollback()
64
75
 
65
76
    def test_finish_returns(self):
66
 
        transaction = transactions.ReadOnlyTransaction()
67
 
        transaction.finish()
 
77
        self.transaction.finish()
68
78
 
 
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"))
 
84
        weave = None
 
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"))
 
93
        del weave
 
94
        # its not a weakref system
 
95
        self.assertEqual(DummyWeave("another weave"),
 
96
                         self.transaction.map.find_weave("id"))
69
97
        
 
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"))
 
108
        # currently a fifo
 
109
        self.assertEqual(None, self.transaction.map.find_weave("id"))
 
110
        self.assertEqual(DummyWeave("a weave also"),
 
111
                         self.transaction.map.find_weave("id2"))
 
112
 
 
113
    def test_small_cache_with_references(self):
 
114
        # if we have a reference it should stick around
 
115
        weave = "a weave"
 
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"))
 
121
        weave = None
 
122
        # its not a weakref system
 
123
        self.assertEqual("a weave", self.transaction.map.find_weave("id"))
 
124
 
 
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"))
 
130
        weave = None
 
131
        # add an object, should not fall out even with no references.
 
132
        self.transaction.register_clean(self.transaction.map.find_weave("id"),
 
133
                                        precious=True)
 
134
        self.assertEqual(DummyWeave('a weave'),
 
135
                         self.transaction.map.find_weave("id"))
 
136
 
 
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())
 
150
 
 
151
 
70
152
class TestPassThroughTransaction(TestCase):
71
153
 
72
 
    def test_symbols(self):
73
 
        from bzrlib.transactions import PassThroughTransaction
74
 
 
75
154
    def test_construct(self):
76
155
        transactions.PassThroughTransaction()
77
156
 
104
183
    def test_finish_returns(self):
105
184
        transaction = transactions.PassThroughTransaction()
106
185
        transaction.finish()
 
186
 
 
187
    def test_cache_is_ignored(self):
 
188
        transaction = transactions.PassThroughTransaction()
 
189
        transaction.set_cache_size(100)
 
190
        weave = "a weave"
 
191
        transaction.map.add_weave("id", weave)
 
192
        self.assertEqual(None, transaction.map.find_weave("id"))