How do I test Django QuerySets are equal?

前端 未结 6 761
挽巷
挽巷 2020-12-17 07:40

I am trying to test my Django views. This view passes a QuerySet to the template:

def merchant_home(request, slug):
  merchant = Merchant.objects.get(slug=sl         


        
6条回答
  •  太阳男子
    2020-12-17 08:32

    An alternative, but not necessarily better, method might look like this (testing context in a view, for example) when using pytest:

    all_the_things = Things.objects.all()
    assert set(response.context_data['all_the_things']) == set(all_the_things)
    

    This converts it to a set, which is directly comparable with another set. Be careful with the behaviour of set though, it might not be exactly what you want since it will remove duplicates and ignore the order of objects.

提交回复
热议问题