Commit e8bcec7f by Piotr Mitros

Added test for memoize_query

parent 89be6f41
......@@ -127,7 +127,7 @@ def memoize_query(cache_time = 60*4, timeout = 60*15):
# process of computing it
cached = cache.get(key)
if cached:
print "Cache hit", key
#print "Cache hit", key
# If we're already computing it, wait to finish
# computation
while cached == 'Processing':
......@@ -139,7 +139,7 @@ def memoize_query(cache_time = 60*4, timeout = 60*15):
results = cached
if not cached:
print "Cache miss", key
#print "Cache miss", key
# HACK: There's a slight race condition here, where we
# might recompute twice.
cache.set(key, 'Processing', timeout)
......@@ -171,20 +171,3 @@ def cron_new(period, params=None):
f(fs, db, params)
return decorator(run,f)
return factory
if False:
# Test case. Should be made into a proper test case.
@memoize_query(1)
def test_function(x):
print "Boo", x, ">",2*x
return 2*x
print test_function(2)
print test_function(4)
print test_function(2)
print test_function(4)
time.sleep(2)
print test_function(2)
print test_function(4)
print test_function(2)
print test_function(4)
......@@ -4,9 +4,11 @@ when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
import time
from django.test import TestCase
from decorators import memoize_query
class SimpleTest(TestCase):
def test_basic_addition(self):
......@@ -14,3 +16,26 @@ class SimpleTest(TestCase):
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
def __init__(self, arg):
TestCase.__init__(self, arg)
self.calls = 0
def test_memoize(self):
self.calls = 0
@memoize_query(0.05)
def double_trouble(x):
self.calls = self.calls + 1
return 2*x
self.assertEqual(double_trouble(2), 4)
self.assertEqual(double_trouble(4), 8)
self.assertEqual(double_trouble(2), 4)
self.assertEqual(double_trouble(4), 8)
self.assertEqual(self.calls, 2)
time.sleep(0.1)
self.assertEqual(double_trouble(2), 4)
self.assertEqual(double_trouble(4), 8)
self.assertEqual(double_trouble(2), 4)
self.assertEqual(double_trouble(4), 8)
self.assertEqual(self.calls, 4)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment