Skip to content

Commit

Permalink
Added unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
thadeusb committed Aug 31, 2010
1 parent df29c65 commit 0f41829
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 9 deletions.
11 changes: 8 additions & 3 deletions flaskext/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ def get_list():
.. code-block:: pycon
>>> my_list = get_list()
.. note::
You MUST have a request context to actually called any functions
that are cached.
:param timeout: Default None. If set to an integer, will cache for that
amount of time.
Expand All @@ -123,15 +128,15 @@ def decorator(f):
def decorated_function(*args, **kwargs):
#: Bypass the cache entirely.
if callable(unless) and unless() is True:
return f(*args, **kwargs)
return f(*args, **kwargs)

if '%s' in key_prefix:
cache_key = key_prefix % request.path
else:
cache_key = key_prefix

rv = self.cache.get(cache_key)
if not rv or current_app.debug:
if rv is None:
rv = f(*args, **kwargs)
self.cache.set(cache_key, rv, timeout=timeout)
return rv
Expand Down Expand Up @@ -174,7 +179,7 @@ def decorated_function(*args, **kwargs):
rv = self.cache.get(cache_key)
if rv is None:
rv = f(*args, **kwargs)
self.cache.set(cache_key, rv)
self.cache.set(cache_key, rv, timeout=timeout)
return rv
return decorated_function
return memoize
159 changes: 153 additions & 6 deletions test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,162 @@

import unittest
import datetime
import flask
from flaskext import cache
import time
import random

from flask import Flask
from werkzeug.contrib.cache import SimpleCache
from flaskext.cache import Cache

class BasicTestCase(unittest.TestCase):

def test_00_first(self):
pass
class CacheTestCase(unittest.TestCase):

def setUp(self):
app = Flask(__name__)

app.debug = False
app.config['CACHE_TYPE'] = 'Simple'

self.cache = Cache()
self.cache.init_app(app)

# Override the cache type. We need to do this
# since app['TESTING'] is True, this allows the
# test to properly make sure cache is working.
self.cache.cache = SimpleCache()

self.app = app

def tearDown(self):
self.app = None
self.cache = None
self.tc = None

def test_00_app(self):
@self.app.route('/')
def hello():
return 'world'

tc = self.app.test_client()

rv = tc.get('/')
assert rv.data == 'world'

def test_00_set(self):
self.cache.set('hi', 'hello')

assert self.cache.get('hi') == 'hello'

def test_01_add(self):
self.cache.add('hi', 'hello')

assert self.cache.get('hi') == 'hello'

self.cache.add('hi', 'foobar')

assert self.cache.get('hi') == 'hello'

def test_02_delete(self):
self.cache.set('hi', 'hello')

self.cache.delete('hi')

assert self.cache.get('hi') is None

def test_03_cached_view(self):

@self.app.route('/')
@self.cache.cached(5)
def cached_view():
return str(time.time())

tc = self.app.test_client()

rv = tc.get('/')
the_time = rv.data

time.sleep(2)

rv = tc.get('/')

assert the_time == rv.data

time.sleep(5)

rv = tc.get('/')
assert the_time != rv.data

def test_04_cached_view_unless(self):
@self.app.route('/a')
@self.cache.cached(5, unless=lambda: True)
def non_cached_view():
return str(time.time())

@self.app.route('/b')
@self.cache.cached(5, unless=lambda: False)
def cached_view():
return str(time.time())

tc = self.app.test_client()

rv = tc.get('/a')
the_time = rv.data

time.sleep(1)

rv = tc.get('/a')
assert the_time != rv.data

rv = tc.get('/b')
the_time = rv.data

time.sleep(1)
rv = tc.get('/b')

assert the_time == rv.data

def test_05_cached_function(self):

with self.app.test_request_context():
@self.cache.cached(2, key_prefix='MyBits')
def get_random_bits():
return [random.randrange(0, 2) for i in range(50)]

my_list = get_random_bits()
his_list = get_random_bits()

assert my_list == his_list

time.sleep(4)

his_list = get_random_bits()

assert my_list != his_list

def test_06_memoize(self):

with self.app.test_request_context():
@self.cache.memoize(5)
def big_foo(a, b):
return a+b+random.randrange(0, 100000)

result = big_foo(5, 2)

time.sleep(1)

assert big_foo(5, 2) == result

result2 = big_foo(5, 3)
assert result2 != result

time.sleep(4)

assert big_foo(5, 2) != result
assert big_foo(5, 3) == result2

time.sleep(1)

assert big_foo(5, 3) != result2


if __name__ == '__main__':
unittest.main()

0 comments on commit 0f41829

Please sign in to comment.