mock django cache

Discussion in 'Python' started by usmani.kashif9957@gmail.com, Apr 6, 2013.

  1. Guest

    In my settings.py , I have specified my cache as :
    CACHES = {
    'default': {
    ......
    }
    }

    In my views.py, I have

    import requests
    from django.core.cache import cache, get_cache

    def aview():
    #check cache
    if not get_cache('default').get('key'):
    #make request and save in cache
    result = request.get('some_url')
    get_cache('default').set('key', result)
    return result
    else:
    return get_cache('default').get('key')


    Now in my tests.py, I have been able to mock requests.get('aurl'), so that makes sure that no external requests are made.

    But the test code still hits the cache and gets/sets from it. So if my prodhas already set the cache, then test is failing because it gets the data from same cache. Or if I run my tests first, then the test case is setting the cache with test data and I see that same reflected when I run prod website.

    How can I mock the calls to get_cache('default').set('key', result) and get_cache('default').get('key') so that the set call does not sets the real cache ( return None?) and get does not return anything in actual cache.

    Please provide me with code sample to how to get this done.

    Here is how I have mocked my requests.get

    def test_get_aview(self):
    with mock.patch('requests.get') as mymock:
    mymock.side_effect = (lambda url: MOCKED_DATA) What code can I put after this t...iving me an idea that its not mocked properly
    , Apr 6, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    Mock objects for nunit tests?

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Mar 17, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    4,963
  2. Ken Connor

    Mock Objects for Web Applications

    Ken Connor, Sep 15, 2003, in forum: Java
    Replies:
    0
    Views:
    311
    Ken Connor
    Sep 15, 2003
  3. John Posner

    Question on Django and Django Book

    John Posner, Nov 13, 2010, in forum: Python
    Replies:
    0
    Views:
    307
    John Posner
    Nov 13, 2010
  4. eGenix Team: M.-A. Lemburg
    Replies:
    0
    Views:
    126
    eGenix Team: M.-A. Lemburg
    Jun 18, 2013
  5. Gary Roach
    Replies:
    0
    Views:
    124
    Gary Roach
    Jan 2, 2014
Loading...

Share This Page