add more tests and some comments

This commit is contained in:
Dan Colish 2011-04-04 09:37:11 -07:00
parent 6ac6777a58
commit b03af04f78
4 changed files with 119 additions and 4 deletions

View File

@ -31,12 +31,13 @@ class JSONRequestHandler(object):
args = loads(local.request.data) args = loads(local.request.data)
if isinstance(args, dict): if isinstance(args, dict):
kwargs = dict((str(key), value) for kwargs = dict((str(key), value) for
key, value in args.iteritems()) key, value in args.items())
args = () args = ()
elif isinstance(args, list): elif isinstance(args, list):
kwargs = {} kwargs = {}
else: else:
raise TypeError('arguments as object or list expected') raise TypeError('arguments as object or list expected')
#XXX:dc: use flatland to validate these args before passing onward
response = { response = {
'data': self.funcs[method_name](*args, **kwargs), 'data': self.funcs[method_name](*args, **kwargs),
'error': None 'error': None

View File

@ -2,4 +2,3 @@ from alfajor import APIClient
client = APIClient() client = APIClient()
client.configure_in_scope('default') client.configure_in_scope('default')

View File

@ -0,0 +1,96 @@
from tests import client
from tests.utilities.runner import testcase
def post_json(method, data=None):
return client.post('/json/', query_string={'method': method},
data=data, content_type='application/json')
@testcase()
def test_json_post_and_get():
data = '{"language": "text", "code": "hello world"}'
resp = post_json('pastes.newPaste', data)
assert resp.is_json
resp = post_json('pastes.getPaste',
'{"paste_id": "%d"}' % int(resp.json['data']))
assert resp.is_json
assert resp.json['data']['code'] == "hello world"
assert resp.json['data']['language'] == "text"
@testcase()
def test_json_post_private_and_get():
data = '{"language": "text", "code": "hello world", "private": "true"}'
resp = post_json('pastes.newPaste', data)
assert resp.is_json
resp = post_json('pastes.getPaste',
'{"paste_id": "%s"}' % resp.json['data'])
assert resp.is_json
assert resp.json['data']['code'] == "hello world"
assert resp.json['data']['language'] == "text"
@testcase()
def test_json_get_last():
data = '{"language": "text", "code": "hello world"}'
resp = post_json('pastes.newPaste', data)
assert resp.is_json
data = '{"language": "text", "code": "hello world again"}'
resp = post_json('pastes.newPaste', data)
assert resp.is_json
resp = post_json('pastes.getLast')
assert resp.is_json
assert resp.json['data']['code'] == "hello world again"
assert resp.json['data']['language'] == "text"
@testcase()
def test_json_get_recent():
def run(inc):
data = '{"language": "text", "code": "hello world %s"}' % inc
resp = post_json('pastes.newPaste', data)
assert resp.is_json
return resp
paste_ids = []
for x in xrange(10):
resp = run(x)
paste_ids.append(int(resp.json['data']))
resp = post_json('pastes.getRecent', '{"amount": 7}')
assert resp.is_json
assert len(resp.json['data']) == 7
ids = [x['paste_id'] for x in resp.json['data']]
assert ids[::-1] == paste_ids[3:]
@testcase()
def test_json_get_styles():
styles = [
['monokai', 'Monokai'],
['manni', 'Manni'],
['perldoc', 'Perldoc'],
['borland', 'Borland'],
['colorful', 'Colorful'],
['default', 'Default'],
['murphy', 'Murphy'],
['trac', 'Trac'],
['tango', 'Tango'],
['vim', 'Vim'],
['autumn', 'Autumn'],
['vs', 'Vs'],
['emacs', 'Emacs'],
['friendly', 'Friendly'],
['bw', 'Bw'],
['pastie', 'Pastie'],
['fruity', 'Fruity'],
['native', 'Native'],
]
resp = post_json('styles.getStyles')
assert resp.is_json
assert resp.json['data'] == styles

View File

@ -1,5 +1,24 @@
from werkzeug import create_environ from nose import with_setup
from lodgeit.application import make_app
from lodgeit.application import db, make_app
from lodgeit.models import Paste
foo = make_app('sqlite://', 'NONE', False, True) foo = make_app('sqlite://', 'NONE', False, True)
def setup():
pass
def teardown():
Paste.query.delete()
db.session.commit()
db.session.remove()
def testcase():
def dec(f):
return with_setup(setup, teardown)(f)
return dec
testcase.__test__ = False