1

I have trying to do an assertQuerysetEqual with django testing using the following commands but all of them work if there is only one object in the list, but when I insert multiple objects in the list to compare both sets of queries all of them fail sometimes and pass sometimes which is completely strange(running with same set of code). The list of assertQuerysetEqual I used to test I sourced from two other questions in How do I test Django QuerySets are equal? and in Django 1.4 - assertQuerysetEqual - how to use method as well as the django documentation.

This could be because the sequence is not in order when it fails the comparison test. Because when I did a print - my querysets were exactly matching. Because when I run the tests, sometimes the tests passes, sometimes it fails when I compare multiple objects in my lists. I can tell them the list differ when it fails because of the error message but I don't understand why the commands that I used does not compare them in order. (They were accepted/upvoted answers)

Any advice on how I can fix this permanently would be welcome. Thank you.

class TestViews(TestCase):

    def setUp(self):
        self.client = Client()

        self.user = User.objects.create_user(
            username='normaluser', email='s@gmail.com', password='secret123')

        self.notcreateuser = User.objects.create_user(
            username='notcreateuser', email='s@gmail.com', password='secret123')

        self.adminuser = User.objects.create_user(
            username='adminuser', email='s@gmail.com', password='secret123', is_staff=True)

        obj_puser = mixer.blend('users.PUser', user=self.user)
        obj_puser_oth = mixer.blend('users.PUser', user=self.notcreateuser)
        obj_puser_staff = mixer.blend('users.PUser', user=self.adminuser)

        obj_ptype = mixer.blend('polls.Ptype', active=True)
        obj_pitem0 = mixer.blend('polls.PollItem', polltype=obj_ptype, user_submit=self.user, allowed=True)
        obj_pitem1 = mixer.blend('polls.PollItem', polltype=obj_ptype, user_submit=self.user, allowed=True)

    def test_poll_list_view_query(self):
        self.client.login(username="normaluser", password="secret123")
        ptype_obj = Ptype.objects.get(pk__in=[1])
        path = "/polls/"
        data = {'type': ptype_obj.slug }
        res = self.client.get(path, data)


        pitemlist = PollItem.objects.filter(pk__in=[1,2])

        #list of assertQuerysetEqual I am testing
        self.assertQuerysetEqual(res.context['object_list'], [repr(r) for r in pitemlist])
        self.assertQuerysetEqual(res.context['object_list'], pitemlist, transform=lambda x:x)
        self.assertQuerysetEqual(res.context['object_list'], map(repr, pitemlist))
krubo
  • 5,969
  • 4
  • 37
  • 46
user3655574
  • 692
  • 2
  • 9
  • 27

1 Answers1

0

You are not comparing two querysets, hence why it fails. I'm not too sure why you're comparing the queryset with a list generated with [repr(r) for r in pitemlist]? Just compare the two querysets directly:

self.assertQuerysetEqual(res.context['object_list'], pitemlist)
solarissmoke
  • 30,039
  • 14
  • 71
  • 73
  • Thanks for you response. I can't compare two querysets directly because they are formatted differently, one is surrounded by apostrophes and the other is not, while the other has a comma in between the items, the other does not, thats why I tried the others - AssertionError: Lists differ: [''] != [] – user3655574 Dec 05 '18 at 00:23
  • @user3655574 I had the same issue, and use a list comprehension with `repr()` on the response context, e.g. `[repr(i) for i in res.context['object_list']]`; the representation had the correct format for my purposes; I haven't run your actual code though. This was the official Django tutorial, part 5 – Life5ign Jun 09 '23 at 18:57