Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
insights
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
insights
Commits
e8bcec7f
Commit
e8bcec7f
authored
Mar 29, 2013
by
Piotr Mitros
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added test for memoize_query
parent
89be6f41
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
19 deletions
+27
-19
src/djanalytics/decorators.py
+2
-19
src/djanalytics/tests.py
+25
-0
No files found.
src/djanalytics/decorators.py
View file @
e8bcec7f
...
...
@@ -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
)
src/djanalytics/tests.py
View file @
e8bcec7f
...
...
@@ -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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment