157
157
"name2": "sqlite:2",
158
158
"name3": "sqlite:3"})
160
def _isInTransaction(self, store):
161
"""Check if a Store is part of the current transaction."""
162
for dm in transaction.get()._resources:
163
if isinstance(dm, StoreDataManager) and dm._store is store:
167
def assertInTransaction(self, store):
168
"""Check that the given store is joined to the transaction."""
169
self.assertTrue(self._isInTransaction(store),
170
"%r should be joined to the transaction" % store)
172
def assertNotInTransaction(self, store):
173
"""Check that the given store is not joined to the transaction."""
174
self.assertTrue(not self._isInTransaction(store),
175
"%r should not be joined to the transaction" % store)
177
def test_wb_store_joins_transaction_on_register_event(self):
178
"""The Store joins the transaction when register-transaction
181
The Store tests check the various operations that trigger this
184
store = self.zstorm.get("name", "sqlite:")
185
self.assertNotInTransaction(store)
186
store._event.emit("register-transaction")
187
self.assertInTransaction(store)
189
def test_wb_store_joins_transaction_on_use_after_commit(self):
190
store = self.zstorm.get("name", "sqlite:")
191
store.execute("SELECT 1")
193
self.assertNotInTransaction(store)
194
store.execute("SELECT 1")
195
self.assertInTransaction(store)
197
def test_wb_store_joins_transaction_on_use_after_abort(self):
198
store = self.zstorm.get("name", "sqlite:")
199
store.execute("SELECT 1")
201
self.assertNotInTransaction(store)
202
store.execute("SELECT 1")
203
self.assertInTransaction(store)
160
205
def test_remove(self):
161
206
removed_store = self.zstorm.get("name", "sqlite:")
162
207
self.zstorm.remove(removed_store)
164
209
self.assertNotEquals(store, removed_store)
165
210
self.assertRaises(ZStormError, self.zstorm.get, "name")
167
# Abort the transaction so that the currently registered
171
# Let's try to make storm blow up when committing, so that
172
# we can be sure that the removed store isn't linked to the
173
# transaction system by a synchronizer anymore.
174
class BadTable(object):
175
__storm_table__ = "bad_table"
176
id = Int(primary=True)
177
removed_store.add(BadTable())
179
# If this fails, the store is still linked to the transaction
183
def test_double_abort(self):
185
Surprisingly, transaction.abort() won't detach the internal
186
transaction from the manager. This means that when
187
transaction.begin() runs, it will detect that there's still
188
a Transaction instance around, and will call abort on it once
189
more before instantiating a new Transaction. The Transaction's
190
abort() method will then call beforeCompletion() on our
191
synchronizer, which then tries to join() on the current
192
transaction, which is still the old one and blows up saying
193
that a previous transaction has failed (we know it, since we
196
class BadTable(object):
197
__storm_table__ = "bad_table"
198
id = Int(primary=True, default=1)
199
store = self.zstorm.get("name", "sqlite:")
200
store.add(BadTable())
201
self.assertRaises(OperationalError, transaction.commit)
212
def test_wb_removed_store_does_not_join_transaction(self):
213
"""If a store has been removed, it will not join the transaction."""
214
store = self.zstorm.get("name", "sqlite:")
215
self.zstorm.remove(store)
216
store.execute("SELECT 1")
217
self.assertNotInTransaction(store)
219
def test_wb_removed_store_does_not_join_future_transactions(self):
220
"""If a store has been removed after joining a transaction, it
221
will not join new transactions."""
222
store = self.zstorm.get("name", "sqlite:")
223
store.execute("SELECT 1")
224
self.zstorm.remove(store)
225
self.assertInTransaction(store)
228
store.execute("SELECT 1")
229
self.assertNotInTransaction(store)
205
231
def test_wb_reset(self):
206
232
"""_reset is used to reset the zstorm utility between zope test runs.
208
We must make sure the L{StoreSynchronizer} is removed from the
211
234
store = self.zstorm.get("name", "sqlite:")
213
len(transaction.manager._synchs.values()[0].data.values()), 1)
216
len(transaction.manager._synchs.values()[0].data.values()), 1)
217
235
self.zstorm._reset()
219
len(transaction.manager._synchs.values()[0].data.values()), 0)
236
self.assertEqual(list(self.zstorm.iterstores()), [])
221
238
def test_store_strong_reference(self):