def test_consistent_read_updates_cache_outside_transaction(self): """ A read inside a transaction shouldn't update the context cache outside that transaction """ entity_data = { "field1": "Apple", "comb1": 1, "comb2": "Cherry" } original = CachingTestModel.objects.create(**entity_data) clear_context_cache() CachingTestModel.objects.get(pk=original.pk) # Should update the cache with sleuth.watch("google.appengine.api.datastore.Get") as datastore_get: CachingTestModel.objects.get(pk=original.pk) self.assertFalse(datastore_get.called) clear_context_cache() with transaction.atomic(): with sleuth.watch("google.appengine.api.datastore.Get") as datastore_get: CachingTestModel.objects.get(pk=original.pk) # Should *not* update the cache self.assertTrue(datastore_get.called) with sleuth.watch("google.appengine.api.datastore.Get") as datastore_get: CachingTestModel.objects.get(pk=original.pk) self.assertTrue(datastore_get.called)
def _create_test_db(self, verbosity, autoclobber, *args): from google.appengine.ext import testbed # Imported lazily to prevent warnings on GAE assert not self.testbed if args: logging.warning( "'keepdb' argument is not currently supported on the AppEngine backend" ) # We allow users to disable scattered IDs in tests. This primarily for running Django tests that # assume implicit ordering (yeah, annoying) use_scattered = not getattr(settings, "DJANGAE_SEQUENTIAL_IDS_IN_TESTS", False) kwargs = { "use_sqlite": True, "auto_id_policy": testbed.AUTO_ID_POLICY_SCATTERED if use_scattered else testbed.AUTO_ID_POLICY_SEQUENTIAL, "consistency_policy": datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1) } self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub(**kwargs) self.testbed.init_memcache_stub() caching.clear_context_cache()
def test_consistent_read_updates_cache_outside_transaction(self): """ A read inside a transaction shouldn't update the context cache outside that transaction """ entity_data = {"field1": "Apple", "comb1": 1, "comb2": "Cherry"} original = CachingTestModel.objects.create(**entity_data) clear_context_cache() CachingTestModel.objects.get(pk=original.pk) # Should update the cache with sleuth.watch( "google.appengine.api.datastore.Get") as datastore_get: CachingTestModel.objects.get(pk=original.pk) self.assertFalse(datastore_get.called) clear_context_cache() with transaction.atomic(): with sleuth.watch( "google.appengine.api.datastore.Get") as datastore_get: CachingTestModel.objects.get( pk=original.pk) # Should *not* update the cache self.assertTrue(datastore_get.called) with sleuth.watch( "google.appengine.api.datastore.Get") as datastore_get: CachingTestModel.objects.get(pk=original.pk) self.assertTrue(datastore_get.called)
def test_inconsistent_read_doesnt_update_cache(self): entity_data = {"field1": "Apple", "comb1": 1, "comb2": "Cherry"} original = CachingTestModel.objects.create(**entity_data) clear_context_cache() CachingTestModel.objects.all() # Inconsistent with sleuth.watch( "google.appengine.api.datastore.Get") as datastore_get: CachingTestModel.objects.get(pk=original.pk) self.assertTrue(datastore_get.called)
def execute(self): table = self.table query = datastore.Query(table, keys_only=True) while query.Count(): datastore.Delete(query.Run()) # Delete the markers we need to from djangae.db.constraints import UniqueMarker query = datastore.Query(UniqueMarker.kind(), keys_only=True) query["__key__ >="] = datastore.Key.from_path(UniqueMarker.kind(), self.table) query["__key__ <"] = datastore.Key.from_path(UniqueMarker.kind(), u"{}{}".format(self.table, u'\ufffd')) while query.Count(): datastore.Delete(query.Run()) cache.clear() clear_context_cache()
def test_inconsistent_read_doesnt_update_cache(self): entity_data = { "field1": "Apple", "comb1": 1, "comb2": "Cherry" } original = CachingTestModel.objects.create(**entity_data) clear_context_cache() CachingTestModel.objects.all() # Inconsistent with sleuth.watch("google.appengine.api.datastore.Get") as datastore_get: CachingTestModel.objects.get(pk=original.pk) self.assertTrue(datastore_get.called)
def _create_test_db(self, verbosity, autoclobber): assert not self.testbed # We allow users to disable scattered IDs in tests. This primarily for running Django tests that # assume implicit ordering (yeah, annoying) use_scattered = not getattr(settings, "DJANGAE_SEQUENTIAL_IDS_IN_TESTS", False) kwargs = { "use_sqlite": True, "auto_id_policy": testbed.AUTO_ID_POLICY_SCATTERED if use_scattered else testbed.AUTO_ID_POLICY_SEQUENTIAL, "consistency_policy": datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1) } self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub(**kwargs) self.testbed.init_memcache_stub() caching.clear_context_cache()
def execute(self): table = self.table query = datastore.Query(table, keys_only=True, namespace=self.namespace) while query.Count(): datastore.Delete(query.Run()) # Delete the markers we need to from djangae.db.constraints import UniqueMarker query = datastore.Query(UniqueMarker.kind(), keys_only=True, namespace=self.namespace) query["__key__ >="] = datastore.Key.from_path(UniqueMarker.kind(), self.table, namespace=self.namespace) query["__key__ <"] = datastore.Key.from_path( UniqueMarker.kind(), u"{}{}".format(self.table, u'\ufffd'), namespace=self.namespace ) while query.Count(): datastore.Delete(query.Run()) # TODO: ideally we would only clear the cached objects for the table that was flushed, but # we have no way of doing that cache.clear() clear_context_cache()
def _create_test_db(self, verbosity, autoclobber, *args): from google.appengine.ext import testbed # Imported lazily to prevent warnings on GAE assert not self.testbed if args: logging.warning("'keepdb' argument is not currently supported on the AppEngine backend") # We allow users to disable scattered IDs in tests. This primarily for running Django tests that # assume implicit ordering (yeah, annoying) use_scattered = not getattr(settings, "DJANGAE_SEQUENTIAL_IDS_IN_TESTS", False) kwargs = { "use_sqlite": True, "auto_id_policy": testbed.AUTO_ID_POLICY_SCATTERED if use_scattered else testbed.AUTO_ID_POLICY_SEQUENTIAL, "consistency_policy": datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1), } self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub(**kwargs) self.testbed.init_memcache_stub() caching.clear_context_cache()
def _destroy_test_db(self, name, verbosity): if self.testbed: caching.clear_context_cache() self.testbed.deactivate() self.testbed = None