42
The queryset objects will not be identical if they are the result of different queries even if they have the same values in their result (compare ds1.query
and ds2.query
).
If you convert the query set to a list first, you should be able to do a normal comparison (assuming they have the same sort order of course):
self.assertEqual(list(ds1), list(ds2))
10
This alternative doesnβt need sorting:
self.assertQuerysetEqual(qs1, list(qs2), ordered=False)
See the assert reference.
Note: Only for django 1.4+.
- [Django]-Received "ValueError: Found wrong number (0) of constraints for β¦" during Django migration
- [Django]-How to use Django's assertJSONEqual to verify response of view returning JsonResponse
- [Django]-How can I use Django OAuth Toolkit with Python Social Auth?
2
You can use the assertQuerysetEqual method to compare two querysets without casting to a list. This will return True
if the values of the querysets are equal, regardless of if the queries are different.
One gotcha is that this method applies the repr
function as a transform to each object in the first argument before comparing, which can result in the following error when comparing two actually equal querysets:
AssertionError: Lists differ: ['<Object: id1'] != [<Object: id1>]
The solution is to pass your own transform
function to use instead of repr
:
self.assertQuerysetEqual(qs1, qs2, transform=lambda x: x)
- [Django]-Setting initial Django form field value in the __init__ method
- [Django]-PermissionError with pip3
- [Django]-Difference between ForeignKey(User, unique=True) and OneToOneField
2
if you want to check that Django querysets are completely the same
self.assertEqual(str(simple_qs.query), str(complex_qs.query))
- [Django]-Setting DEBUG = False causes 500 Error
- [Django]-Serializing Foreign Key objects in Django
- [Django]-How to add column in ManyToMany Table (Django)
1
For me works the transform
option:
def subject(self):
return Mission.objects.add_keynest_api_token().filter(keynest_api_token__isnull=False)
def test_mission_has_property(self):
self.mission.appartement = self.property
self.mission.save()
self.assertQuerysetEqual(self.subject(), [self.mission], transform=lambda x: x)
- [Django]-Django model "doesn't declare an explicit app_label"
- [Django]-Django Rest Framework custom authentication
- [Django]-Django check for any exists for a query
0
If youβd like to assert that the content of two querysets is equal without worrying about the order, use the following:
self.assertQuerysetEqual(actual_queryset, expected_queryset, transform=lambda x: x, ordered=False)
- [Django]-Per Field Permission in Django REST Framework
- [Django]-Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
- [Django]-How do you change the default widget for all Django date fields in a ModelForm?
-1
Found a solution. We need to convert Querysets
to sorted lists before we can compare them. Something as follows.
Class SomeTestCase(TestCase):
# ...
def test_simple_view(self):
# ... some other checks
docset1 = self.resonse.context['documents']
docset2 = self.user.document_set.all()
self.assertTrue(list(sorted(docset1)) == len(sorted(docset)))
# ...
- [Django]-Django β How to specify which field a validation fails on?
- [Django]-Django ModelChoiceField optgroup tag
- [Django]-Django pre_save signal: check if instance is created not updated, does kwargs['created'] (still) exist?
-1
def test_simple_view(self):
# ... some other checks
docset = set([i.pk for i in self.resonse.context['documents']])
prev = set([i.pk for i in self.user.document_set.all()])
diff = prev.difference(docset)
self.assertTrue(len(diff)==0)
- [Django]-How to update manytomany field in Django?
- [Django]-Django orm get latest for each group
- [Django]-How can I change the default Django date template format?