Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block decorator #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ env/**
texts/**
.idea/**
# }}}

# Local development installs {{{
*.egg-info/
# }}}

# Text editor cruft {{{
*.sw[po]
# }}}
17 changes: 17 additions & 0 deletions blockspring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import base64
import re
import tempfile
import inspect
import functools

try:
from urlparse import urlparse
Expand Down Expand Up @@ -206,6 +208,21 @@ def processArgs(request):
response = Response()
block(request, response)

def decorate(func):
""" Decorate any function into one readily parsed by blockspring.
The arguments to the function will be looked for by name in request.params
TODO: respect default values in wrapped function.
"""
@functools.wraps(func)
def block(request, response):
arguments = inspect.getargspec(func).args #get a list of arguments accepted by wrapped
parameters = tuple(request.params[arg] for arg in arguments)
output = func(*parameters) #unpack them and call them into func
response.addOutput('output', output)
response.end()

return block

class Request:
def __init__(self):
self.params = {}
Expand Down
62 changes: 62 additions & 0 deletions blockspring/test_decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pytest
import blockspring

# ***** Fixtures ****** #

def decorator_fixture(a,b=0):
"""This will be the test function for our decorator"""
return a+b

def dict_outputs(a, b):
"""returns a dict"""
output = {}
return {'a_squared':a*a, 'b_squared':b*b}

def list_outputs(a, b):
"""returns a list"""
return [a*a, b*b]

@pytest.fixture
def request():
request = blockspring.parse({'a':1, 'b':3})
return request

@pytest.fixture
def response():
response = blockspring.Response()
return response

@pytest.fixture
def wrapped():
wrapped = blockspring.decorate(decorator_fixture)
return wrapped

@pytest.fixture
def dict_output_wrapped():
wrapped = blockspring.decorate(dict_outputs)
return wrapped

@pytest.fixture
def list_output_wrapped():
wrapped = blockspring.decorate(list_outputs)
return wrapped

########## Tests #########

def test_decorator_respects_name(wrapped):
assert wrapped.__name__ == 'decorator_fixture'

def test_creates_blockspring_function(wrapped,request, response):
assert response.result['_blockspring_spec']==True

def test_adds_output(wrapped, request, response):
test = wrapped(request, response)
assert response.result['output']==4

def test_wraps_functions_that_returns_dicts(dict_output_wrapped, request, response):
test = dict_output_wrapped(request, response)
assert response.result['output']['b_squared']==9

def test_wraps_functions_that_return_lists(list_output_wrapped, request, response):
test = list_output_wrapped(request, response)
assert response.result['output'][1]==9