Jeremy Self avatar Jeremy Self committed 71cadf4

Pull request #10

Comments (0)

Files changed (2)

johnny/tests/cache.py

         return False
 
 # put tests in here to be included in the testing suite
-__all__ = ['MultiDbTest', 'SingleModelTest', 'MultiModelTest', 'TransactionSupportTest', 'BlackListTest']
+__all__ = ['MultiDbTest', 'SingleModelTest', 'MultiModelTest', 'TransactionSupportTest', 'BlackListTest', 'TransactionManagerTestCase']
 
 def _pre_setup(self):
     self.saved_DISABLE_SETTING = getattr(johnny_settings, 'DISABLE_QUERYSET_CACHE', False)
         self.failUnless(g.title == "In the Void")
         transaction.managed(False)
         transaction.leave_transaction_management()
+
+import johnny
+class TransactionManagerTestCase(base.TransactionJohnnyTestCase):
+
+    def setUp(self):
+        self.middleware = middleware.QueryCacheMiddleware()
+    
+    def tearDown(self):
+        from django.db import transaction
+        if transaction.is_managed():
+            transaction.managed(False)
+
+    def test_savepoint_localstore_flush(self):
+        """
+        This is a very simple test to see if savepoints will actually
+        be committed, i.e. flushed out from localstore into cache.
+        """
+        from django.db import transaction
+        transaction.enter_transaction_management()
+        transaction.managed()
+
+        TABLE_NAME = 'test_table'
+        cache_backend = johnny.cache.get_backend()
+        cache_backend.patch()
+        keyhandler = cache_backend.keyhandler
+        keygen = keyhandler.keygen
+        
+        tm = cache_backend.cache_backend
+        
+        # First, we set one key-val pair generated for our non-existing table.
+        table_key = keygen.gen_table_key(TABLE_NAME)
+        tm.set(table_key, 'val1')
+
+        # Then we create a savepoint.
+        # The key-value pair is moved into 'trans_sids' item of localstore.
+        tm._create_savepoint('savepoint1')
+        
+        # We then commit all the savepoints (i.e. only one in this case)
+        # The items stored in 'trans_sids' should be moved back to the
+        # top-level dictionary of our localstore
+        tm._commit_all_savepoints()
+        # And this checks if it actually happened.
+        self.failUnless(table_key in tm.local)

johnny/transaction.py

         return connection.features.uses_savepoints
 
     def _sid_key(self, sid, using=None):
-        if using != None:
-            return 'trans_savepoint_%s_%s' % (using, sid)
-        return 'trans_savepoint_%s' % sid
+        if using is not None:
+            prefix = 'trans_savepoint_%s'%using
+        else:
+            prefix = 'trans_savepoint'
+
+        if sid.startswith(prefix):
+            return sid
+        return '%s_%s'%(prefix, sid)
 
     def _create_savepoint(self, sid, using=None):
         key = self._sid_key(sid, using)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.