diff --git a/test-requirements.txt b/test-requirements.txt index c59a0e9..4283f38 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,2 +1,2 @@ flake8 - +nose diff --git a/tests/__init__.py b/tests/__init__.py index b6c003d..5c64868 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,21 @@ -from alfajor import APIClient +from werkzeug.test import Client +from werkzeug.wrappers import BaseResponse +from lodgeit.application import make_app +from json import loads -client = APIClient() -client.configure_in_scope('default') +client = Client(make_app('sqlite://', 'NONE', False, True), BaseResponse) + + +def is_json(response): + """True if the response is JSON and the HTTP status was 200.""" + return (response.status_code == 200 and + response.headers.get('Content-Type', '') == 'application/json') + + +def json(response): + """The response parsed as JSON. + + No attempt is made to ensure the response is valid or even looks + like JSON before parsing. + """ + return loads(response.data) diff --git a/tests/alfajor.ini b/tests/alfajor.ini deleted file mode 100644 index a1ca9b3..0000000 --- a/tests/alfajor.ini +++ /dev/null @@ -1,9 +0,0 @@ -[default-targets] -default+apiclient=wsgi - -[default] -wsgi=wsgi - -[default+apiclient.wsgi] -server-entry-point = tests.utilities.runner:foo -base_url = http://www.localhost:5001 diff --git a/tests/unittest/test_api.py b/tests/unittest/test_api.py index 246d269..8e12396 100644 --- a/tests/unittest/test_api.py +++ b/tests/unittest/test_api.py @@ -1,5 +1,6 @@ -from tests import client +from tests import client, is_json, json from tests.utilities.runner import testcase +from lodgeit.lib.highlighting import STYLES def post_json(method, data=None): @@ -12,12 +13,12 @@ def test_json_post_and_get(): data = '{"language": "text", "code": "hello world"}' resp = post_json('pastes.newPaste', data) - assert resp.is_json + assert is_json(resp) 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" + '{"paste_id": "%d"}' % int(json(resp)['data'])) + assert is_json(resp) + assert json(resp)['data']['code'] == "hello world" + assert json(resp)['data']['language'] == "text" @testcase() @@ -25,28 +26,28 @@ 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 + assert is_json(resp) 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" + '{"paste_id": "%s"}' % json(resp)['data']) + assert is_json(resp) + assert json(resp)['data']['code'] == "hello world" + assert json(resp)['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 + assert is_json(resp) data = '{"language": "text", "code": "hello world again"}' resp = post_json('pastes.newPaste', data) - assert resp.is_json + assert is_json(resp) resp = post_json('pastes.getLast') - assert resp.is_json - assert resp.json['data']['code'] == "hello world again" - assert resp.json['data']['language'] == "text" + assert is_json(resp) + assert json(resp)['data']['code'] == "hello world again" + assert json(resp)['data']['language'] == "text" @testcase() @@ -54,43 +55,24 @@ 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 + assert is_json(resp) return resp paste_ids = [] for x in xrange(10): resp = run(x) - paste_ids.append(int(resp.json['data'])) + paste_ids.append(int(json(resp)['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 is_json(resp) + assert len(json(resp)['data']) == 7 + ids = [x['paste_id'] for x in json(resp)['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 + assert is_json(resp) + expected = [[u'%s' % x, u'%s' % STYLES[x]] for x in STYLES] + assert json(resp)['data'] == expected diff --git a/tox.ini b/tox.ini index 0fcbdae..d22eb75 100644 --- a/tox.ini +++ b/tox.ini @@ -1,11 +1,13 @@ [tox] minversion = 1.6 -envlist = pep8 +envlist = pep8, py27 skipsdist = True [testenv] setenv = VIRTUAL_ENV={envdir} -deps = -r{toxinidir}/test-requirements.txt +deps = -r{toxinidir}/requirements.txt + -r{toxinidir}/test-requirements.txt +commands = nosetests [testenv:pep8] commands = flake8