I am trying to mock the below django query object : 1.) if MyModel.objects.filter(data='some_data').exists(): then 2.) for row in MyModel.objects.filter(ListId=id):
I am trying to test below django query inside my method. def my_method(some_parameter): if formsList.objects.filter(data=some_data).exists(): for item in formsList.objects.filter(data1='data1',data2='data2'): formNameInDb = (item.fileId).formName if formNameInDb == formName: return True Below is my approach: @mock.patch('MyModel.objects') def test_checkCombinationOfStateAndProduct(self, formsList_mock): formsList_mock_data = mock.MagicMock(spec=MyModel) formsList_mock_data.fileId.formName ='test data' formsList_queryset = Mock() formsList_mock.filter.return_value = formsList_queryset # formsList_mock.filter.return_value = [formsList_queryset] formsList_queryset.exists.return_value = True For the query 1). It is working like I am able to mockupto **if formsList.objects.filter(data=some_data).exists()** but again for the query 2) for item in formsList.objects.filter(data1='data1',data2='data2'): I am getting **mock object(formsList_queryset) should be iterable** so if I make it iterable like this **[formsList_queryset]**. Then i am getting error **" AttributeError: 'list' object has no attribute 'exists'**. I guess it is because after making the mock object iterable it is behaving like list so it does not has the exists attribute. My problem is I am not able to make the mock object(formsList_queryset) iterable so that it will work in the both above mentioned query. Is there other way mock both query to handle this issue. Can anyone help to solve the chain queries. Any help or lead, I will really appreciate. Please let me know if any information required. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f5558d18-6872-4715-b5e2-8f2baa627478%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.