From aa297371c7fc2d1573252483c5444db4ade996af Mon Sep 17 00:00:00 2001 From: Shuhao Date: Sun, 16 Jun 2013 17:47:47 -0700 Subject: [PATCH] Decorators that accepts kwargs from URL test Test will fail with saying multiple id keyword. In reality, kwargs will have `id` and the actually `id` is passed with `self`, an instance of DecoratedView --- test_classy/test_decorators.py | 6 +++++- test_classy/view_classes.py | 14 +++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/test_classy/test_decorators.py b/test_classy/test_decorators.py index 6250959..fd08279 100644 --- a/test_classy/test_decorators.py +++ b/test_classy/test_decorators.py @@ -12,4 +12,8 @@ def test_func_decorator_index(): def test_func_decorator_get(): resp = client.get('/decorated/1234') - eq_(b"Get 1234", resp.data) \ No newline at end of file + eq_(b"Get 1234", resp.data) + +def test_func_special_decorator(): + resp = client.get('/decorated/special/someid') + eq_(b"someid1", resp.data) diff --git a/test_classy/view_classes.py b/test_classy/view_classes.py index 4812073..b8c2486 100644 --- a/test_classy/view_classes.py +++ b/test_classy/view_classes.py @@ -142,7 +142,14 @@ def decorated_view(*args, **kwargs): def wraps_decorator(f): @wraps(f) def decorated_view(*args, **kwargs): - return f(*args, **kwargs) + return f(*args, **kwargs) + return decorated_view + +def specialized_decorator(f): + @wraps(f) + def decorated_view(id, *args, **kwargs): + transformed_id = id + "1" + return f(transformed_id, *args, **kwargs) return decorated_view class DecoratedView(FlaskView): @@ -153,3 +160,8 @@ def index(self): @func_decorator def get(self, id): return "Get " + id + + @route("/special/") + @specialized_decorator + def special(self, transformed_id): + return transformed_id