Fix up the tests and add tox configuration to run them

The alfajor HTTP test wrapper is not working properly.  All the tests
are failing with:

  AttributeError: '_APIClientResponse' object has no attribute '_status_code'

It doesn't look like alfajor is being maintained any more [1, 2], so
remove the dependency on it and run the tests directly through the
werkzeug client instead.

The test_json_get_styles test still failed because the content and
ordering of the styles returned from the API is dependent on the
platform on which the tests run.  Instead of testing against a fixed
expected result, which could be different on another platform,
generate the expected results dynamically.

Update the tox configuration and test-requirements.txt to be able to
run the tests via tox.

[1] https://github.com/idealist/Alfajor
[2] https://github.com/idealistdev/alfajor

Change-Id: I4db6783b9d725ce096575035ea27d28be8e6fed5
This commit is contained in:
David Pursehouse 2015-10-14 16:46:25 +09:00
parent 2acd8c9881
commit f79c65815a
5 changed files with 50 additions and 58 deletions

View File

@ -1,2 +1,2 @@
flake8 flake8
nose

View File

@ -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 = Client(make_app('sqlite://', 'NONE', False, True), BaseResponse)
client.configure_in_scope('default')
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)

View File

@ -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

View File

@ -1,5 +1,6 @@
from tests import client from tests import client, is_json, json
from tests.utilities.runner import testcase from tests.utilities.runner import testcase
from lodgeit.lib.highlighting import STYLES
def post_json(method, data=None): def post_json(method, data=None):
@ -12,12 +13,12 @@ def test_json_post_and_get():
data = '{"language": "text", "code": "hello world"}' data = '{"language": "text", "code": "hello world"}'
resp = post_json('pastes.newPaste', data) resp = post_json('pastes.newPaste', data)
assert resp.is_json assert is_json(resp)
resp = post_json('pastes.getPaste', resp = post_json('pastes.getPaste',
'{"paste_id": "%d"}' % int(resp.json['data'])) '{"paste_id": "%d"}' % int(json(resp)['data']))
assert resp.is_json assert is_json(resp)
assert resp.json['data']['code'] == "hello world" assert json(resp)['data']['code'] == "hello world"
assert resp.json['data']['language'] == "text" assert json(resp)['data']['language'] == "text"
@testcase() @testcase()
@ -25,28 +26,28 @@ def test_json_post_private_and_get():
data = '{"language": "text", "code": "hello world", "private": "true"}' data = '{"language": "text", "code": "hello world", "private": "true"}'
resp = post_json('pastes.newPaste', data) resp = post_json('pastes.newPaste', data)
assert resp.is_json assert is_json(resp)
resp = post_json('pastes.getPaste', resp = post_json('pastes.getPaste',
'{"paste_id": "%s"}' % resp.json['data']) '{"paste_id": "%s"}' % json(resp)['data'])
assert resp.is_json assert is_json(resp)
assert resp.json['data']['code'] == "hello world" assert json(resp)['data']['code'] == "hello world"
assert resp.json['data']['language'] == "text" assert json(resp)['data']['language'] == "text"
@testcase() @testcase()
def test_json_get_last(): def test_json_get_last():
data = '{"language": "text", "code": "hello world"}' data = '{"language": "text", "code": "hello world"}'
resp = post_json('pastes.newPaste', data) resp = post_json('pastes.newPaste', data)
assert resp.is_json assert is_json(resp)
data = '{"language": "text", "code": "hello world again"}' data = '{"language": "text", "code": "hello world again"}'
resp = post_json('pastes.newPaste', data) resp = post_json('pastes.newPaste', data)
assert resp.is_json assert is_json(resp)
resp = post_json('pastes.getLast') resp = post_json('pastes.getLast')
assert resp.is_json assert is_json(resp)
assert resp.json['data']['code'] == "hello world again" assert json(resp)['data']['code'] == "hello world again"
assert resp.json['data']['language'] == "text" assert json(resp)['data']['language'] == "text"
@testcase() @testcase()
@ -54,43 +55,24 @@ def test_json_get_recent():
def run(inc): def run(inc):
data = '{"language": "text", "code": "hello world %s"}' % inc data = '{"language": "text", "code": "hello world %s"}' % inc
resp = post_json('pastes.newPaste', data) resp = post_json('pastes.newPaste', data)
assert resp.is_json assert is_json(resp)
return resp return resp
paste_ids = [] paste_ids = []
for x in xrange(10): for x in xrange(10):
resp = run(x) resp = run(x)
paste_ids.append(int(resp.json['data'])) paste_ids.append(int(json(resp)['data']))
resp = post_json('pastes.getRecent', '{"amount": 7}') resp = post_json('pastes.getRecent', '{"amount": 7}')
assert resp.is_json assert is_json(resp)
assert len(resp.json['data']) == 7 assert len(json(resp)['data']) == 7
ids = [x['paste_id'] for x in resp.json['data']] ids = [x['paste_id'] for x in json(resp)['data']]
assert ids[::-1] == paste_ids[3:] assert ids[::-1] == paste_ids[3:]
@testcase() @testcase()
def test_json_get_styles(): 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') resp = post_json('styles.getStyles')
assert resp.is_json assert is_json(resp)
assert resp.json['data'] == styles expected = [[u'%s' % x, u'%s' % STYLES[x]] for x in STYLES]
assert json(resp)['data'] == expected

View File

@ -1,11 +1,13 @@
[tox] [tox]
minversion = 1.6 minversion = 1.6
envlist = pep8 envlist = pep8, py27
skipsdist = True skipsdist = True
[testenv] [testenv]
setenv = VIRTUAL_ENV={envdir} setenv = VIRTUAL_ENV={envdir}
deps = -r{toxinidir}/test-requirements.txt deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
commands = nosetests
[testenv:pep8] [testenv:pep8]
commands = flake8 commands = flake8