Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unittesting Pyramid/Cornice resource with query string in a URL

I have Pyramid/cornice resource, that requires a ?query=keyword in end of the url. But I don't know how to add this in a pyramid's dummyRequest object. Code works perfectly on browser and I will get correct response when using this url to get stuff: *url*/foo?query=keyword.

My class/resource is defined like this:

@resource(path='/bar/search/foo')
class SearchFooResource(object):
    def __init__(self, request):
        self.request = request

    @view(renderer='json')
    def get(self):
        #get query string, it's a tuple
        req = self.request.GET.items()
        #do stuff with req

Now req should contain the all the query string 'stuffs' in a list that contains them as a tuple's, for example: [('query', 'bar'),('query', 'asd')]. But how do I make unittest to this resource? I can't seem to add anything to self.request.GET.items() method. When running unittest req is empty, and I will get this error: AttributeError: 'list' object has no attribute 'items'.

My current unittest:

def test_passing_GetFooBaarResource(self):
    request = testing.DummyRequest()
    request.GET = [('query', 'keyword')]    
    info = SearchFooResource.get(SearchFooResource(request))

    self.assertEqual(info['foo'], 'baar')
like image 850
Miro K. Avatar asked Nov 20 '25 20:11

Miro K.


2 Answers

In addition to what @matino has suggested, you can just use a plain dictionary (instead of a list of tuples you tried).

def test_passing_GetFooBaarResource(self):
    request = testing.DummyRequest()
    request.GET = {'query': 'keyword'}   
    info = SearchShowResource.get(SearchShowResource(request))

    self.assertEqual(info['foo'], 'baar')

This will work in uncomplicated cases where you don't have multiple parameters with the same name (/someurl?name=foo&name=baz&name=bar).

If you need to test those more complicated queries you can replace your DummyRequest's GET attribute with a WebOb MultiDict

from webob.multidict import MultiDict

def test_passing_GetFooBaarResource(self):
    request = testing.DummyRequest()
    request.GET = MultiDict([('query', 'foo'), ('query', 'bar'), ('query', 'baz')])    
    info = SearchShowResource.get(SearchShowResource(request))

    self.assertEqual(info['foo'], 'baar')

Then, normally, in your actual view method, if you need to handle multiple parameters with the same name you use request.GET.getall('query') which should return ['foo', 'bar', 'baz'].

In simpler cases you can just use request.GET['query'] or request.GET.get('query', 'default'). I mean, your use of request.GET.items() is a bit unusual...

like image 150
Sergey Avatar answered Nov 25 '25 00:11

Sergey


According to the docs I think you need to pass it as params argument (not tested):

request = testing.DummyRequest(params={'query': 'keyword'})
like image 40
matino Avatar answered Nov 24 '25 23:11

matino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!