intercept calls to reverse(..) in tests

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

intercept calls to reverse(..) in tests

Reikje-2
Hi, I have a bunch of WebTest's which are using the reverse method
from django.core.urlresolvers to resolve a URL which the test should
call. Example:

url = reverse('webapp_home')
form = self.app.get(url).form

Now I need to add a query parameter to every url. This is to mimic a
Facebook request which will always have the signed_request query
parameter. So in theory I could just do this:

url = reverse('webapp_home') + "?
signed_request=vlXgu64BQGFSQrY0ZcJBZASMvYvTHu9GQ0YM9rjPSso.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsIjAiOiJwYXlsb2FkIn0"
form = self.app.get(url).form

This is an example of a signed_request from the Facebook
documentation. Since, I am a bit lazy, is there a way to sort of
intercept all calls to reverse and add the signed_request parameter
every time?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: intercept calls to reverse(..) in tests

Daniel Roseman-2
On Wednesday, 7 September 2011 07:49:00 UTC+1, Reikje wrote:
Hi, I have a bunch of WebTest's which are using the reverse method
from django.core.urlresolvers to resolve a URL which the test should
call. Example:

url = reverse('webapp_home')
form = self.app.get(url).form

Now I need to add a query parameter to every url. This is to mimic a
Facebook request which will always have the signed_request query
parameter. So in theory I could just do this:

url = reverse('webapp_home') + "?
signed_request=vlXgu64BQGFSQrY0ZcJBZASMvYvTHu9GQ0YM9rjPSso.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsIjAiOiJwYXlsb2FkIn0"
form = self.app.get(url).form

This is an example of a signed_request from the Facebook
documentation. Since, I am a bit lazy, is there a way to sort of
intercept all calls to reverse and add the signed_request parameter
every time?


You probably just want to monkeypatch urlresolvers.reverse in your test setUp methods. Something like this would probably work.

    from django.core import urlresolvers
    reverse_original = urlresolvers.reverse
    def reverse(*args, **kwargs):
        url = reverse_original(*args, **kwargs)
        url += whatever_you_want_to_add
        return url
    urlresolvers.reverse = reverse

--
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/WRFBNCqIHwoJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: intercept calls to reverse(..) in tests

Reikje-2
Excellent Daniel, thanks a lot.

On Sep 7, 10:23 am, Daniel Roseman <[hidden email]> wrote:

> On Wednesday, 7 September 2011 07:49:00 UTC+1, Reikje wrote:
>
> > Hi, I have a bunch of WebTest's which are using the reverse method
> > from django.core.urlresolvers to resolve a URL which the test should
> > call. Example:
>
> > url = reverse('webapp_home')
> > form = self.app.get(url).form
>
> > Now I need to add a query parameter to every url. This is to mimic a
> > Facebook request which will always have the signed_request query
> > parameter. So in theory I could just do this:
>
> > url = reverse('webapp_home') + "?
> > signed_request=vlXgu64BQGFSQrY0ZcJBZASMvYvTHu9GQ0YM9rjPSso.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsIjAiOiJwYXlsb2FkIn0"
>
> > form = self.app.get(url).form
>
> > This is an example of a signed_request from the Facebook
> > documentation. Since, I am a bit lazy, is there a way to sort of
> > intercept all calls to reverse and add the signed_request parameter
> > every time?
>
> You probably just want to monkeypatch urlresolvers.reverse in your test
> setUp methods. Something like this would probably work.
>
>     from django.core import urlresolvers
>     reverse_original = urlresolvers.reverse
>     def reverse(*args, **kwargs):
>         url = reverse_original(*args, **kwargs)
>         url += whatever_you_want_to_add
>         return url
>     urlresolvers.reverse = reverse
>
> --
> DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: intercept calls to reverse(..) in tests

Reikje-2
In reply to this post by Daniel Roseman-2
Okay I am having some troubles getting this to work. In my application
I am using WebTest and django-webtest to do template testing. In order
to monkeypatch the django.core.urlresolvers.reverse function, I have
created a new Test base that looks like this:

class FacebookWebTest(WebTest):

    def __init__(self, methodName='runTest'):
        super(FacebookWebTest, self).__init__(methodName)
        import django.core.urlresolvers
        django.core.urlresolvers.reverse = self.reverse

    def reverse(*args, **kwargs):
        return "foo"


Then hier is a test:

from django.core.urlresolvers import reverse

class HomepageViewTestCase(FacebookWebTest):

    def testPopulateFieldsInitially(self):
        url = reverse('webapp_home')
        form = self.app.get(url).form
        ...

Within the HomepageViewTestCase it is still calling the original
reverse method in django.core.urlresolvers even though it is calling
the FacebookWebTest constructor before. Any suggestions :)


On Sep 7, 10:23 am, Daniel Roseman <[hidden email]> wrote:

> On Wednesday, 7 September 2011 07:49:00 UTC+1, Reikje wrote:
>
> > Hi, I have a bunch of WebTest's which are using the reverse method
> > from django.core.urlresolvers to resolve a URL which the test should
> > call. Example:
>
> > url = reverse('webapp_home')
> > form = self.app.get(url).form
>
> > Now I need to add a query parameter to every url. This is to mimic a
> > Facebook request which will always have the signed_request query
> > parameter. So in theory I could just do this:
>
> > url = reverse('webapp_home') + "?
> > signed_request=vlXgu64BQGFSQrY0ZcJBZASMvYvTHu9GQ0YM9rjPSso.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsIjAiOiJwYXlsb2FkIn0"
>
> > form = self.app.get(url).form
>
> > This is an example of a signed_request from the Facebook
> > documentation. Since, I am a bit lazy, is there a way to sort of
> > intercept all calls to reverse and add the signed_request parameter
> > every time?
>
> You probably just want to monkeypatch urlresolvers.reverse in your test
> setUp methods. Something like this would probably work.
>
>     from django.core import urlresolvers
>     reverse_original = urlresolvers.reverse
>     def reverse(*args, **kwargs):
>         url = reverse_original(*args, **kwargs)
>         url += whatever_you_want_to_add
>         return url
>     urlresolvers.reverse = reverse
>
> --
> DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: intercept calls to reverse(..) in tests

DrBloodmoney
On Sat, Sep 17, 2011 at 7:47 AM, Reikje <[hidden email]> wrote:

> Okay I am having some troubles getting this to work. In my application
> I am using WebTest and django-webtest to do template testing. In order
> to monkeypatch the django.core.urlresolvers.reverse function, I have
> created a new Test base that looks like this:
>
> class FacebookWebTest(WebTest):
>
>    def __init__(self, methodName='runTest'):
>        super(FacebookWebTest, self).__init__(methodName)
>        import django.core.urlresolvers
>        django.core.urlresolvers.reverse = self.reverse
>
>    def reverse(*args, **kwargs):
>        return "foo"
>
>
> Then hier is a test:
>
> from django.core.urlresolvers import reverse
>
> class HomepageViewTestCase(FacebookWebTest):
>
>    def testPopulateFieldsInitially(self):
>        url = reverse('webapp_home')
>        form = self.app.get(url).form
>        ...
>
> Within the HomepageViewTestCase it is still calling the original
> reverse method in django.core.urlresolvers even though it is calling
> the FacebookWebTest constructor before. Any suggestions :)

I usually monkey patch like so:

# myviews.py

from django.core.urlresolvers import reverse

def view(request):
     blah blah
     x = reverse('whaterrr')
     return HttpResponse('monkey-patch')

# tests.py

import myviews

def patch_reverse(name):
     return whatever_you_want

class MyViewTests(TestCase):

    def setUp(self):
        self.old_reverse = getattr(myviews, 'reverse')
        myviews.reverse = patch_reverse

    def tearDown(self):
        myviews.reverse = self.old_reverse

Gets the job done

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.