From b03af04f78c7a28351f7bc4af22060bb0e9848bc Mon Sep 17 00:00:00 2001 From: Dan Colish Date: Mon, 4 Apr 2011 09:37:11 -0700 Subject: [PATCH] add more tests and some comments --- lodgeit/lib/json.py | 3 +- tests/__init__.py | 1 - tests/unittest/test_api.py | 96 ++++++++++++++++++++++++++++++++++++++ tests/utilities/runner.py | 23 ++++++++- 4 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 tests/unittest/test_api.py diff --git a/lodgeit/lib/json.py b/lodgeit/lib/json.py index 81ae596..3db32ea 100644 --- a/lodgeit/lib/json.py +++ b/lodgeit/lib/json.py @@ -31,12 +31,13 @@ class JSONRequestHandler(object): args = loads(local.request.data) if isinstance(args, dict): kwargs = dict((str(key), value) for - key, value in args.iteritems()) + key, value in args.items()) args = () elif isinstance(args, list): kwargs = {} else: raise TypeError('arguments as object or list expected') + #XXX:dc: use flatland to validate these args before passing onward response = { 'data': self.funcs[method_name](*args, **kwargs), 'error': None diff --git a/tests/__init__.py b/tests/__init__.py index ab1cf0e..b6c003d 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -2,4 +2,3 @@ from alfajor import APIClient client = APIClient() client.configure_in_scope('default') - diff --git a/tests/unittest/test_api.py b/tests/unittest/test_api.py new file mode 100644 index 0000000..246d269 --- /dev/null +++ b/tests/unittest/test_api.py @@ -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 diff --git a/tests/utilities/runner.py b/tests/utilities/runner.py index 040fe57..0857940 100644 --- a/tests/utilities/runner.py +++ b/tests/utilities/runner.py @@ -1,5 +1,24 @@ -from werkzeug import create_environ -from lodgeit.application import make_app +from nose import with_setup +from lodgeit.application import db, make_app +from lodgeit.models import Paste 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