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_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)