Re: Unit Testing POST request

2020-02-13 Thread onlinejudge95
On Thu, Feb 13, 2020 at 8:14 PM sachinbg sachin 
wrote:

> Fistly for setup create use instance for setup user credentials then call
> that the reverse url in that post put delite
>

I have already created my test users in the *setUpClass()* method. As
mentioned my problem was not something related to URL dispatching but more
on how to prepare a POST request with the proper payload. I looked into the
documentation and RequestFactory is exactly what i was looking for.


> On Thu, Feb 13, 2020, 8:01 PM Adam Mičuda  wrote:
>
>> Hi,
>> or you can extract the business logic from view to some service and unit
>> test it instead. =)
>>
>> Regards.
>>
>> Adam
>>
>> st 12. 2. 2020 v 21:15 odesílatel onlinejudge95 
>> napsal:
>>
>>> On Wed, Feb 12, 2020 at 6:22 PM onlinejudge95 
>>> wrote:
>>>
 Hi Devs,

 I was implementing unit-tests in my Django project and stumbled upon
 the following issue.

 I want to unit-test my POST route. I do not want to use the test client
 already shipped with Django (using it in my e2e tests). I want to know how
 do I prepare my request object to pass to my view. Here is what I have done
 currently.

 test_views.py

> class CreateBlogTest(BaseViewTest):

 @classmethod
> def setUpClass(cls):
> cls.request.method = "POST"

 def test_create_valid_blog(self):
> self.request.content_type = "application/json"
> self.request._body = json.dumps({"title": "new title", "body":
> "new body"})
>
> response = views.blog_collection(self.request)
> self.assertEqual(response.status_code, 201)
>

 In my view, I am accessing the data through *request.data* and passing
 it to a serializer.

 In my current setting, I am getting a 400 error message when I have
 checked that the user does not exist.

 Any suggestions regarding the same?

 Thanks,
 onlinejudge95

>>>
>>> In case if someone needs it in the future, go and look at
>>> *django.test.RequestFactory *
>>>
>>> https://docs.djangoproject.com/en/3.0/topics/testing/advanced/#django.test.RequestFactory
>>>
>>> --
>>> 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 view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAD%3DM5eTN3M5iAEvkPoB1fAi%3Du%3DOAXv8kr7S51HmaBsNd8Tubyg%40mail.gmail.com
>>> 
>>> .
>>>
>> --
>> 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 view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAB9%3DGXaU4QvmV%3D9fMCcm-NaRUogFtq2Fwd-3MFB5q6QOCxgRhQ%40mail.gmail.com
>> 
>> .
>>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAOs61rxaSRrE239aZrbBpZSo7QBoZx2BKxx4nBiERhio0RGSJg%40mail.gmail.com
> 
> .
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAD%3DM5eTyjickDTJVCMgHOGWVpXrTdS%2BRsyiJOtJSBpfWPrb44g%40mail.gmail.com.


Re: Unit Testing POST request

2020-02-13 Thread onlinejudge95
On Thu, Feb 13, 2020 at 8:02 PM Adam Mičuda  wrote:

> Hi,
> or you can extract the business logic from view to some service and unit
> test it instead. =)
>

I am following the same, it's just that I am also performing serialization
as of now in my views, since I want to push it out first, and have
documented the refactor I would be performing. My question was on how to
prepare a post request with proper payload, while i was running this test
my response object was an empty dictionary.

>
> Regards.
>
> Adam
>
> st 12. 2. 2020 v 21:15 odesílatel onlinejudge95 
> napsal:
>
>> On Wed, Feb 12, 2020 at 6:22 PM onlinejudge95 
>> wrote:
>>
>>> Hi Devs,
>>>
>>> I was implementing unit-tests in my Django project and stumbled upon the
>>> following issue.
>>>
>>> I want to unit-test my POST route. I do not want to use the test client
>>> already shipped with Django (using it in my e2e tests). I want to know how
>>> do I prepare my request object to pass to my view. Here is what I have done
>>> currently.
>>>
>>> test_views.py
>>>
 class CreateBlogTest(BaseViewTest):
>>>
>>> @classmethod
 def setUpClass(cls):
 cls.request.method = "POST"
>>>
>>> def test_create_valid_blog(self):
 self.request.content_type = "application/json"
 self.request._body = json.dumps({"title": "new title", "body":
 "new body"})

 response = views.blog_collection(self.request)
 self.assertEqual(response.status_code, 201)

>>>
>>> In my view, I am accessing the data through *request.data* and passing
>>> it to a serializer.
>>>
>>> In my current setting, I am getting a 400 error message when I have
>>> checked that the user does not exist.
>>>
>>> Any suggestions regarding the same?
>>>
>>> Thanks,
>>> onlinejudge95
>>>
>>
>> In case if someone needs it in the future, go and look at
>> *django.test.RequestFactory *
>>
>> https://docs.djangoproject.com/en/3.0/topics/testing/advanced/#django.test.RequestFactory
>>
>> --
>> 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 view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAD%3DM5eTN3M5iAEvkPoB1fAi%3Du%3DOAXv8kr7S51HmaBsNd8Tubyg%40mail.gmail.com
>> 
>> .
>>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAB9%3DGXaU4QvmV%3D9fMCcm-NaRUogFtq2Fwd-3MFB5q6QOCxgRhQ%40mail.gmail.com
> 
> .
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAD%3DM5eTWpMRcZ%2BG_zRf%2B9hB2Xds3SKB%3Dm6PT5C60cCDvGUa5EA%40mail.gmail.com.


Re: Unit Testing POST request

2020-02-13 Thread sachinbg sachin
Fistly for setup create use instance for setup user credentials then call
that the reverse url in that post put delite

On Thu, Feb 13, 2020, 8:01 PM Adam Mičuda  wrote:

> Hi,
> or you can extract the business logic from view to some service and unit
> test it instead. =)
>
> Regards.
>
> Adam
>
> st 12. 2. 2020 v 21:15 odesílatel onlinejudge95 
> napsal:
>
>> On Wed, Feb 12, 2020 at 6:22 PM onlinejudge95 
>> wrote:
>>
>>> Hi Devs,
>>>
>>> I was implementing unit-tests in my Django project and stumbled upon the
>>> following issue.
>>>
>>> I want to unit-test my POST route. I do not want to use the test client
>>> already shipped with Django (using it in my e2e tests). I want to know how
>>> do I prepare my request object to pass to my view. Here is what I have done
>>> currently.
>>>
>>> test_views.py
>>>
 class CreateBlogTest(BaseViewTest):
>>>
>>> @classmethod
 def setUpClass(cls):
 cls.request.method = "POST"
>>>
>>> def test_create_valid_blog(self):
 self.request.content_type = "application/json"
 self.request._body = json.dumps({"title": "new title", "body":
 "new body"})

 response = views.blog_collection(self.request)
 self.assertEqual(response.status_code, 201)

>>>
>>> In my view, I am accessing the data through *request.data* and passing
>>> it to a serializer.
>>>
>>> In my current setting, I am getting a 400 error message when I have
>>> checked that the user does not exist.
>>>
>>> Any suggestions regarding the same?
>>>
>>> Thanks,
>>> onlinejudge95
>>>
>>
>> In case if someone needs it in the future, go and look at
>> *django.test.RequestFactory *
>>
>> https://docs.djangoproject.com/en/3.0/topics/testing/advanced/#django.test.RequestFactory
>>
>> --
>> 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 view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAD%3DM5eTN3M5iAEvkPoB1fAi%3Du%3DOAXv8kr7S51HmaBsNd8Tubyg%40mail.gmail.com
>> 
>> .
>>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAB9%3DGXaU4QvmV%3D9fMCcm-NaRUogFtq2Fwd-3MFB5q6QOCxgRhQ%40mail.gmail.com
> 
> .
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOs61rxaSRrE239aZrbBpZSo7QBoZx2BKxx4nBiERhio0RGSJg%40mail.gmail.com.


Re: Unit Testing POST request

2020-02-13 Thread Adam Mičuda
Hi,
or you can extract the business logic from view to some service and unit
test it instead. =)

Regards.

Adam

st 12. 2. 2020 v 21:15 odesílatel onlinejudge95 
napsal:

> On Wed, Feb 12, 2020 at 6:22 PM onlinejudge95 
> wrote:
>
>> Hi Devs,
>>
>> I was implementing unit-tests in my Django project and stumbled upon the
>> following issue.
>>
>> I want to unit-test my POST route. I do not want to use the test client
>> already shipped with Django (using it in my e2e tests). I want to know how
>> do I prepare my request object to pass to my view. Here is what I have done
>> currently.
>>
>> test_views.py
>>
>>> class CreateBlogTest(BaseViewTest):
>>
>> @classmethod
>>> def setUpClass(cls):
>>> cls.request.method = "POST"
>>
>> def test_create_valid_blog(self):
>>> self.request.content_type = "application/json"
>>> self.request._body = json.dumps({"title": "new title", "body":
>>> "new body"})
>>>
>>> response = views.blog_collection(self.request)
>>> self.assertEqual(response.status_code, 201)
>>>
>>
>> In my view, I am accessing the data through *request.data* and passing
>> it to a serializer.
>>
>> In my current setting, I am getting a 400 error message when I have
>> checked that the user does not exist.
>>
>> Any suggestions regarding the same?
>>
>> Thanks,
>> onlinejudge95
>>
>
> In case if someone needs it in the future, go and look at
> *django.test.RequestFactory *
>
> https://docs.djangoproject.com/en/3.0/topics/testing/advanced/#django.test.RequestFactory
>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAD%3DM5eTN3M5iAEvkPoB1fAi%3Du%3DOAXv8kr7S51HmaBsNd8Tubyg%40mail.gmail.com
> 
> .
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAB9%3DGXaU4QvmV%3D9fMCcm-NaRUogFtq2Fwd-3MFB5q6QOCxgRhQ%40mail.gmail.com.


Re: Unit Testing POST request

2020-02-12 Thread onlinejudge95
On Wed, Feb 12, 2020 at 6:22 PM onlinejudge95 
wrote:

> Hi Devs,
>
> I was implementing unit-tests in my Django project and stumbled upon the
> following issue.
>
> I want to unit-test my POST route. I do not want to use the test client
> already shipped with Django (using it in my e2e tests). I want to know how
> do I prepare my request object to pass to my view. Here is what I have done
> currently.
>
> test_views.py
>
>> class CreateBlogTest(BaseViewTest):
>
> @classmethod
>> def setUpClass(cls):
>> cls.request.method = "POST"
>
> def test_create_valid_blog(self):
>> self.request.content_type = "application/json"
>> self.request._body = json.dumps({"title": "new title", "body":
>> "new body"})
>>
>> response = views.blog_collection(self.request)
>> self.assertEqual(response.status_code, 201)
>>
>
> In my view, I am accessing the data through *request.data* and passing it
> to a serializer.
>
> In my current setting, I am getting a 400 error message when I have
> checked that the user does not exist.
>
> Any suggestions regarding the same?
>
> Thanks,
> onlinejudge95
>

In case if someone needs it in the future, go and look at
*django.test.RequestFactory *
https://docs.djangoproject.com/en/3.0/topics/testing/advanced/#django.test.RequestFactory

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAD%3DM5eTN3M5iAEvkPoB1fAi%3Du%3DOAXv8kr7S51HmaBsNd8Tubyg%40mail.gmail.com.


Re: Unit Testing POST request

2020-02-12 Thread onlinejudge95
Hi Guys,

Any leads would be appreciated


On Wed, Feb 12, 2020 at 6:22 PM onlinejudge95 
wrote:

> Hi Devs,
>
> I was implementing unit-tests in my Django project and stumbled upon the
> following issue.
>
> I want to unit-test my POST route. I do not want to use the test client
> already shipped with Django (using it in my e2e tests). I want to know how
> do I prepare my request object to pass to my view. Here is what I have done
> currently.
>
> test_views.py
>
>> class CreateBlogTest(BaseViewTest):
>
> @classmethod
>> def setUpClass(cls):
>> cls.request.method = "POST"
>
> def test_create_valid_blog(self):
>> self.request.content_type = "application/json"
>> self.request._body = json.dumps({"title": "new title", "body":
>> "new body"})
>>
>> response = views.blog_collection(self.request)
>> self.assertEqual(response.status_code, 201)
>>
>
> In my view, I am accessing the data through *request.data* and passing it
> to a serializer.
>
> In my current setting, I am getting a 400 error message when I have
> checked that the user does not exist.
>
> Any suggestions regarding the same?
>
> Thanks,
> onlinejudge95
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAD%3DM5eS3nFRxHm_aSqSt8YxFhaZGahF%3DP6Uj0Zco7wafM1%2BHOQ%40mail.gmail.com.