def test_basic_usage(self): for i in xrange(5): EnsureCreatedModel.objects.create(pk=i + 1, field1=i) with inconsistent_db(): new_instance = EnsureCreatedModel.objects.create(pk=7, field1=3) qs = EnsureCreatedModel.objects.all() self.assertEqual( 5, len(qs)) # Make sure we don't get the new instance self.assertEqual(6, len(ensure_instance_included( qs, new_instance.pk))) # Make sure we do self.assertEqual( 5, len(ensure_instance_included(qs[:5], new_instance.pk) )) # Make sure slicing returns the right number self.assertEqual( 3, len(ensure_instance_included(qs[2:5], new_instance.pk) )) # Make sure slicing returns the right number evaled = [ x for x in ensure_instance_included(qs.order_by("field1"), new_instance.pk) ] self.assertEqual( new_instance, evaled[4]) # Make sure our instance is correctly positioned evaled = [ x for x in ensure_instance_included(qs.order_by("-field1"), new_instance.pk) ] self.assertEqual( new_instance, evaled[1], evaled) # Make sure our instance is correctly positioned evaled = [ x for x in ensure_instance_included(qs.order_by("-field1"), new_instance.pk)[:2] ] self.assertEqual( new_instance, evaled[1], evaled) # Make sure our instance is correctly positioned # Now we're in consistent land! self.assertTrue(EnsureCreatedModel.objects.filter(pk=7)[:1]) self.assertEqual(6, len(ensure_instance_included(qs, new_instance.pk))) # Make sure it's not returned if it was deleted new_instance.delete() self.assertEqual(5, len(ensure_instance_included(qs, 7))) new_instance = EnsureCreatedModel.objects.create(pk=8, field1=8) self.assertEqual( 1, list(ensure_instance_included(qs, 8)).count(new_instance))
def test_ordering_retained(self): """ Test that using improve_queryset_consistency still retains the ordering. """ b = TestModel.objects.create(name='B') a = TestModel.objects.create(name='A') c = TestModel.objects.create(name='C') queryset = TestModel.objects.all().order_by('name') # To be sure that we test a query which combines the Datastore result with our cache we # include some inconsistent_db stuff here... with inconsistent_db(): d = TestModel.objects.create(name='D') queryset = improve_queryset_consistency(queryset) self.assertEqual(list(queryset), [a, b, c, d])
def test_newly_created_objects_returned(self): existing = TestModel.objects.create(name='existing') queryset = TestModel.objects.all() self.assertItemsEqual(queryset.all(), [existing]) # Add a new object with eventual consistency being slow with inconsistent_db(): new = TestModel.objects.create(name='new') # The new object should not yet be returned self.assertItemsEqual(queryset.all(), [existing]) # But using our handy function it should be returned consistent = improve_queryset_consistency(queryset) self.assertItemsEqual(consistent.all(), [existing, new])
def test_new_user(self): with inconsistent_db(): new_user = get_user_model().objects.create(username="******", email="*****@*****.**") response = self.client.get((reverse("admin:user-index"))) self.assertEqual(list(response.context["users"]), [self.user]) response = self.client.get( "%s?new_user=%s" % (reverse("admin:user-index"), new_user.pk)) self.assertItemsEqual(list(response.context["users"]), [new_user, self.user])
def test_basic_usage(self): for i in range(5): EnsureCreatedModel.objects.create( pk=i + 1, field1=i ) with inconsistent_db(): new_instance = EnsureCreatedModel.objects.create( pk=7, field1=3 ) qs = EnsureCreatedModel.objects.all() self.assertEqual(5, len(qs)) # Make sure we don't get the new instance self.assertEqual(6, len(ensure_instance_consistent(qs, new_instance.pk))) # Make sure we do self.assertEqual(5, len(ensure_instance_consistent(qs[:5], new_instance.pk))) # Make sure slicing returns the right number self.assertEqual(3, len(ensure_instance_consistent(qs[2:5], new_instance.pk))) # Make sure slicing returns the right number evaled = [ x for x in ensure_instance_consistent(qs.order_by("field1"), new_instance.pk) ] self.assertEqual(new_instance, evaled[4]) # Make sure our instance is correctly positioned evaled = [ x for x in ensure_instance_consistent(qs.order_by("-field1"), new_instance.pk) ] self.assertEqual(new_instance, evaled[1], evaled) # Make sure our instance is correctly positioned evaled = [ x for x in ensure_instance_consistent(qs.order_by("-field1"), new_instance.pk)[:2] ] self.assertEqual(new_instance, evaled[1], evaled) # Make sure our instance is correctly positioned another_instance = EnsureCreatedModel.objects.create( pk=8, field1=3 ) self.assertEqual(5, len(qs)) # Make sure we don't get the new instance self.assertEqual(7, len(ensure_instances_consistent(qs, [new_instance.pk, another_instance.pk]))) # Make sure we do instance_id = another_instance.pk another_instance.delete() self.assertEqual(6, len(ensure_instances_consistent(qs, [new_instance.pk, instance_id]))) # Make sure we do # Now we're in consistent land! self.assertTrue(EnsureCreatedModel.objects.filter(pk=7)[:1]) self.assertEqual(6, len(ensure_instance_consistent(qs, new_instance.pk))) # Make sure it's not returned if it was deleted new_instance.delete() self.assertEqual(5, len(ensure_instance_consistent(qs, 7))) new_instance = EnsureCreatedModel.objects.create(pk=8, field1=8) self.assertEqual(1, list(ensure_instance_consistent(qs, 8)).count(new_instance))
def test_deleted_objects_not_returned(self): """ When an object is deleted, improve_queryset_consistency should ensure that it is not returned. """ obj = TestModel.objects.create(name='A') queryset = TestModel.objects.filter(name='A') self.assertItemsEqual(queryset.all(), [obj]) with inconsistent_db(): obj.delete() # The object no longer exists, but the inconsistent db will still return it self.assertItemsEqual(queryset.all(), [obj]) # improve_queryset_consistency to the rescue! consistent = improve_queryset_consistency(queryset) self.assertEqual(consistent.count(), 0)
def test_newly_modified_objects_returned(self): """ If an object which previously did not match the query is modified to now match it, then improve_queryset_consistency should include it even when the DB hasn't caught up yet. """ obj = TestModel.objects.create(name='A') queryset = TestModel.objects.filter(name='B') self.assertEqual(queryset.all().count(), 0) with inconsistent_db(): obj.name = 'B' obj.save() # The DB is inconsistent, so the queryset should still return nothing self.assertEqual(queryset.all().count(), 0) # But improve_queryset_consistency should include the object consistent = improve_queryset_consistency(queryset) self.assertEqual(consistent.all().count(), 1)
def test_add_many_instances(self): for i in range(5): EnsureCreatedModel.objects.create(pk=i + 1, field1=i + 5) with inconsistent_db(): new_instances = [] for i in range(3): instance = EnsureCreatedModel.objects.create(pk=i + 7, field1=i) new_instances.append(instance) new_instance_pks = [i.pk for i in new_instances] qs = EnsureCreatedModel.objects.all() self.assertEqual( 5, len(qs)) # Make sure we don't get the new instance self.assertEqual( 8, len(ensure_instances_consistent(qs, new_instance_pks))) evaled = [ x for x in ensure_instances_consistent(qs.order_by("field1"), new_instance_pks) ] self.assertEqual( new_instances[0], evaled[0]) # Make sure our instance is correctly positioned self.assertEqual( new_instances[1], evaled[1]) # Make sure our instance is correctly positioned self.assertEqual( new_instances[2], evaled[2]) # Make sure our instance is correctly positioned # Now we're in consistent land! self.assertTrue(EnsureCreatedModel.objects.filter(pk=7)[:1]) self.assertTrue(EnsureCreatedModel.objects.filter(pk=8)[:1]) self.assertEqual( 8, len(ensure_instances_consistent(qs, new_instance_pks))) # Make sure it's not returned if it was deleted for instance in new_instances: instance.delete() self.assertEqual( 5, len(ensure_instances_consistent(qs, new_instance_pks)))
def test_delete_many_instances(self): for i in range(5): EnsureCreatedModel.objects.create(pk=i + 1, field1=i + 5) instances_to_delete = [] for i in range(3): instance = EnsureCreatedModel.objects.create(pk=i + 7, field1=i + 1) instances_to_delete.append(instance) instances_to_delete_pks = [i.pk for i in instances_to_delete] qs = EnsureCreatedModel.objects.all() self.assertEqual( 8, len(ensure_instances_consistent(qs, instances_to_delete_pks))) with inconsistent_db(): # Make sure it's not returned if it was deleted for instance in instances_to_delete: instance.delete() self.assertEqual( 5, len(ensure_instances_consistent(qs, instances_to_delete_pks)))
def test_add_many_instances(self): for i in range(5): EnsureCreatedModel.objects.create( pk=i + 1, field1=i + 5 ) with inconsistent_db(): new_instances = [] for i in range(3): instance = EnsureCreatedModel.objects.create( pk=i + 7, field1=i ) new_instances.append(instance) new_instance_pks = [i.pk for i in new_instances] qs = EnsureCreatedModel.objects.all() self.assertEqual(5, len(qs)) # Make sure we don't get the new instance self.assertEqual(8, len(ensure_instances_consistent(qs, new_instance_pks))) evaled = [ x for x in ensure_instances_consistent(qs.order_by("field1"), new_instance_pks) ] self.assertEqual(new_instances[0], evaled[0]) # Make sure our instance is correctly positioned self.assertEqual(new_instances[1], evaled[1]) # Make sure our instance is correctly positioned self.assertEqual(new_instances[2], evaled[2]) # Make sure our instance is correctly positioned # Now we're in consistent land! self.assertTrue(EnsureCreatedModel.objects.filter(pk=7)[:1]) self.assertTrue(EnsureCreatedModel.objects.filter(pk=8)[:1]) self.assertEqual(8, len(ensure_instances_consistent(qs, new_instance_pks))) # Make sure it's not returned if it was deleted for instance in new_instances: instance.delete() self.assertEqual(5, len(ensure_instances_consistent(qs, new_instance_pks)))
def test_delete_many_instances(self): for i in range(5): EnsureCreatedModel.objects.create( pk=i + 1, field1=i + 5 ) instances_to_delete = [] for i in range(3): instance = EnsureCreatedModel.objects.create( pk=i + 7, field1=i + 1 ) instances_to_delete.append(instance) instances_to_delete_pks = [i.pk for i in instances_to_delete] qs = EnsureCreatedModel.objects.all() self.assertEqual(8, len(ensure_instances_consistent(qs, instances_to_delete_pks))) with inconsistent_db(): # Make sure it's not returned if it was deleted for instance in instances_to_delete: instance.delete() self.assertEqual(5, len(ensure_instances_consistent(qs, instances_to_delete_pks)))
def test_bucket_new_inconsistent(self): with inconsistent_db(): bucket = BucketFactory(title='Cheese', user=self.user) response = self.client.get( reverse('bucket', kwargs={'bucket': bucket.pk})) self.assertEqual(response.status_code, 200)