Navigation Menu

Skip to content

Commit

Permalink
Support for annotations in @cache_region and use real objects in test…
Browse files Browse the repository at this point in the history
…s for cached object methods
  • Loading branch information
amol- committed Jan 25, 2016
1 parent 783a9ad commit 8cc7e31
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
2 changes: 1 addition & 1 deletion beaker/_compat.py
Expand Up @@ -165,4 +165,4 @@ def exec_(code, globs=None, locs=None):

def bindfuncargs(arginfo, args, kwargs):
boundargs = arginfo.bind(*args, **kwargs)
return boundargs.args, boundargs.kwargs
return boundargs.args, boundargs.kwargs
7 changes: 4 additions & 3 deletions beaker/util.py
@@ -1,5 +1,6 @@
"""Beaker utilities"""
from ._compat import PY2, string_type, unicode_text, NoneType, dictkeyslist, im_class, im_func, pickle
from ._compat import PY2, string_type, unicode_text, NoneType, dictkeyslist, im_class, im_func, pickle, func_signature, \
default_im_func

try:
import threading as _threading
Expand Down Expand Up @@ -89,8 +90,8 @@ def verify_directory(dir):

def has_self_arg(func):
"""Return True if the given function has a 'self' argument."""
args = inspect.getargspec(func)
if args and args[0] and args[0][0] in ('self', 'cls'):
args = list(func_signature(func).parameters)
if args and args[0] in ('self', 'cls'):
return True
else:
return False
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Expand Up @@ -10,3 +10,4 @@ with-doctest=True
#with-coverage=True
cover-package=beaker
cover-inclusive=True
ignore-files=annotated_functions.py
15 changes: 15 additions & 0 deletions tests/annotated_functions.py
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
"""This is a collection of annotated functions used by tests.
They are grouped here to provide an easy way to import them at runtime
to check whenever tests for annotated functions should be skipped or not
on current python version.
"""
from beaker.cache import cache_region
import time

class AnnotatedAlfredCacher(object):
@cache_region('short_term')
def alfred_self(self, xx: int, y=None) -> str:
return str(time.time()) + str(self) + str(xx) + str(y)

55 changes: 47 additions & 8 deletions tests/test_cache_decorator.py
Expand Up @@ -4,6 +4,7 @@
import beaker.cache as cache
from beaker.cache import CacheManager, cache_region, region_invalidate
from beaker import util
from nose import SkipTest

defaults = {'cache.data_dir':'./cache', 'cache.type':'dbm', 'cache.expire': 2}

Expand All @@ -28,9 +29,15 @@ def albert(x):
def alfred(x, xx, y=None):
return str(time.time()) + str(x) + str(xx) + str(y)

@cache_region('short_term')
def alfred_self(self, xx, y=None):
return str(time.time()) + str(self) + str(xx) + str(y)
class AlfredCacher(object):
@cache_region('short_term')
def alfred_self(self, xx, y=None):
return str(time.time()) + str(self) + str(xx) + str(y)

try:
from .annotated_functions import AnnotatedAlfredCacher
except (ImportError, SyntaxError):
AnnotatedAlfredCacher = None


def make_cache_obj(**kwargs):
Expand Down Expand Up @@ -259,17 +266,49 @@ def test_check_region_decorator_with_kwargs():


def test_check_region_decorator_with_kwargs_and_self():
result = alfred_self('fake_self', xx=5, y='blah')
a1 = AlfredCacher()
a2 = AlfredCacher()

result = a1.alfred_self(xx=5, y='blah')
time.sleep(0.1)

result2 = alfred_self('fake_self2', y='blah', xx=5)
result2 = a2.alfred_self(y='blah', xx=5)
assert result == result2

result3 = alfred_self('fake_self2', 5, y=5)
result3 = a2.alfred_self(5, y=5)
assert result != result3

result4 = alfred_self('fake_self2', 5, 'blah')
result4 = a2.alfred_self(5, 'blah')
assert result == result4

result5 = alfred_self('fake_self2', 5, y='blah')
result5 = a2.alfred_self(5, y='blah')
assert result == result5

result6 = a2.alfred_self(6, 'blah')
assert result != result6


def test_check_region_decorator_with_kwargs_self_and_annotations():
if AnnotatedAlfredCacher is None:
raise SkipTest('Python version not supporting annotations')

a1 = AnnotatedAlfredCacher()
a2 = AnnotatedAlfredCacher()

result = a1.alfred_self(xx=5, y='blah')
time.sleep(0.1)

result2 = a2.alfred_self(y='blah', xx=5)
assert result == result2

result3 = a2.alfred_self(5, y=5)
assert result != result3

result4 = a2.alfred_self(5, 'blah')
assert result == result4

result5 = a2.alfred_self(5, y='blah')
assert result == result5

result6 = a2.alfred_self(6, 'blah')
assert result != result6

0 comments on commit 8cc7e31

Please sign in to comment.