def index_instance(instance, fields_to_index, defer_index=True): if defer_index: deferred.defer(_unindex_then_reindex, instance, fields_to_index, _queue=QUEUE_FOR_INDEXING, _transactional=transaction.in_atomic_block()) else: _unindex_then_reindex(instance, fields_to_index)
def test_non_atomic_context_manager(self): from .test_connector import TestUser existing = TestUser.objects.create(username="******", field2="exists") with transaction.atomic(): self.assertTrue(transaction.in_atomic_block()) user = TestUser.objects.create(username="******", field2="bar") with transaction.non_atomic(): # We're outside the transaction, so the user should not exist self.assertRaises(TestUser.DoesNotExist, TestUser.objects.get, pk=user.pk) self.assertFalse(transaction.in_atomic_block()) with sleuth.watch( "google.appengine.api.datastore.Get") as datastore_get: TestUser.objects.get( pk=existing.pk ) #Should hit the cache, not the datastore self.assertFalse(datastore_get.called) with transaction.atomic(independent=True): user2 = TestUser.objects.create(username="******", field2="bar2") self.assertTrue(transaction.in_atomic_block()) with transaction.non_atomic(): self.assertFalse(transaction.in_atomic_block()) self.assertRaises(TestUser.DoesNotExist, TestUser.objects.get, pk=user2.pk) with transaction.non_atomic(): self.assertFalse(transaction.in_atomic_block()) self.assertRaises(TestUser.DoesNotExist, TestUser.objects.get, pk=user2.pk) with sleuth.watch("google.appengine.api.datastore.Get" ) as datastore_get: TestUser.objects.get( pk=existing.pk ) #Should hit the cache, not the datastore self.assertFalse(transaction.in_atomic_block()) self.assertRaises(TestUser.DoesNotExist, TestUser.objects.get, pk=user2.pk) self.assertTrue(TestUser.objects.filter(pk=user2.pk).exists()) self.assertTrue(transaction.in_atomic_block())
def test_non_atomic_context_manager(self): from .test_connector import TestUser existing = TestUser.objects.create(username="******", field2="exists") with transaction.atomic(): self.assertTrue(transaction.in_atomic_block()) user = TestUser.objects.create(username="******", field2="bar") with transaction.non_atomic(): # We're outside the transaction, so the user should not exist self.assertRaises(TestUser.DoesNotExist, TestUser.objects.get, pk=user.pk) self.assertFalse(transaction.in_atomic_block()) with sleuth.watch("google.appengine.api.datastore.Get") as datastore_get: TestUser.objects.get(pk=existing.pk) # Should hit the cache, not the datastore self.assertFalse(datastore_get.called) with transaction.atomic(independent=True): user2 = TestUser.objects.create(username="******", field2="bar2") self.assertTrue(transaction.in_atomic_block()) with transaction.non_atomic(): self.assertFalse(transaction.in_atomic_block()) self.assertRaises(TestUser.DoesNotExist, TestUser.objects.get, pk=user2.pk) with transaction.non_atomic(): self.assertFalse(transaction.in_atomic_block()) self.assertRaises(TestUser.DoesNotExist, TestUser.objects.get, pk=user2.pk) with sleuth.watch("google.appengine.api.datastore.Get") as datastore_get: # Should hit the cache, not the Datastore TestUser.objects.get(pk=existing.pk) self.assertFalse(transaction.in_atomic_block()) self.assertRaises(TestUser.DoesNotExist, TestUser.objects.get, pk=user2.pk) self.assertTrue(TestUser.objects.filter(pk=user2.pk).exists()) self.assertTrue(transaction.in_atomic_block())
def txn(): TestUser.objects.create(username="******", field2="bar") self.assertTrue(transaction.in_atomic_block()) raise ValueError()