Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43047

Re: mock django cache

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=8039af83f=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'cache': 0.07; 'failing': 0.07; 'made.': 0.07; 'method.': 0.07; "'default':": 0.09; 'get(self,': 0.09; 'methods,': 0.09; 'django': 0.11; 'def': 0.12; '(lambda': 0.16; '......': 0.16; 'caches': 0.16; 'hits': 0.16; 'key):': 0.16; 'result)': 0.16; 'settings.py': 0.16; 'subject:django': 0.16; 'properly': 0.19; 'import': 0.22; 'putting': 0.22; 'tests': 0.22; 'print': 0.22; 'first,': 0.26; 'pass': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'tried': 0.27; 'idea': 0.28; 'external': 0.29; 'sets': 0.30; 'skip:g 30': 0.30; 'specified': 0.30; 'statement': 0.30; 'code': 0.31; 'requests': 0.31; 'reflected': 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'run': 0.32; '-----': 0.33; 'actual': 0.34; 'something': 0.35; 'done.': 0.35; 'test': 0.35; 'but': 0.35; 'thank': 0.38; 'skip:m 40': 0.38; 'work?': 0.38; 'to:addr:python- list': 0.38; 'anything': 0.39; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'you.': 0.62; 'save': 0.62; 'information': 0.63; 'real': 0.63; 'skip:n 10': 0.64; 'received:194': 0.64; 'provide': 0.64; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'sample': 0.67; 'notice:': 0.67; 'person,': 0.68; 'privileged.': 0.69; 'disclose': 0.74; "'here'": 0.84; 'get/set': 0.84; 'mock': 0.84; 'medium.': 0.91
X-IronPort-AV E=Sophos;i="4.87,430,1363129200"; d="scan'208";a="1349636"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Mon, 8 Apr 2013 10:56:53 +0200 (CEST)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To usmani kashif9957 <usmani.kashif9957@gmail.com>, python-list@python.org
In-Reply-To <66eb7077-77ab-434b-aa19-a7a6d8bc687a@googlegroups.com>
Subject Re: mock django cache
MIME-Version 1.0
X-Mailer Zimbra 7.2.2_GA_2852 (ZimbraWebClient - GC7 (Linux)/7.2.2_GA_2852)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.273.1365411422.3114.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1365411422 news.xs4all.nl 6934 [2001:888:2000:d::a6]:46018
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43047

Show key headers only | View raw



----- Original Message -----
> 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 prod has 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[url])
> 
> What code can I put after this to make it work? I tried something
> like
> 
> 
> class MockCacheValue(mock.MagicMock):
>     def get(self, key):
>         print 'here'
>         return None
>     def set(self, key, value):
>         print 'here 2'
>         pass
> 
> def test_get_aview(self):
>         with mock.patch('requests.get') as mymock:
>             mymock.side_effect = (lambda url: MOCKED_DATA[url])
>             mock.patch('django.core.cache.get_cache',
>             new=MockCacheValue)
> 
> but it does not work and putting a print statement inside get/set
> above does not print anything giving me an idea that its not mocked
> properly


Having a quick look at django doc, get_cache() returns a cache which has get set methods, so you need get_cache to return a Mock object that mock the get and set method.

Try something like :

mock.patch('django.core.cache.get_cache',Mock(return_value=MockCacheValue())

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: mock django cache Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-04-08 10:56 +0200

csiph-web