Modernize test suite

This modernizes the lodgeit testsuite to use stestr instead of nose. The
primary reason for this is nose is not python3.10 compatible and we
would like to use newer python with lodgeit.

Change-Id: I8ae480d22bbef0258afc9d6ffd6cd820993430a1
This commit is contained in:
Clark Boylan 2022-11-10 10:11:54 -08:00
parent bc2678dc73
commit 9e2d54bb5a
10 changed files with 112 additions and 124 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
.DS_Store
bootstrap.py
.tox
.stestr/
.project
.pydevproject
.settings

3
.stestr.conf Normal file
View File

@ -0,0 +1,3 @@
[DEFAULT]
test_path=./tests/unittest
top_dir=./

View File

@ -61,7 +61,8 @@
- tox-pep8
- tox-py36:
nodeset: ubuntu-bionic
- tox-py38
- tox-py38:
nodeset: ubuntu-focal
- opendev-buildset-registry
- lodgeit-build-opendev-image
gate:
@ -69,7 +70,8 @@
- tox-pep8
- tox-py36:
nodeset: ubuntu-bionic
- tox-py38
- tox-py38:
nodeset: ubuntu-focal
- opendev-buildset-registry
- lodgeit-upload-opendev-image
promote:

View File

@ -1,2 +1,3 @@
flake8
nose
python-subunit
stestr>=1.0.0 # Apache-2.0

View File

@ -1,9 +1,11 @@
import unittest
from werkzeug.test import Client
from werkzeug.wrappers import BaseResponse
from lodgeit.application import make_app
from json import loads
client = Client(make_app('sqlite://', 'NONE', False, True), BaseResponse)
from lodgeit.application import db, make_app
from lodgeit.models import Paste
from json import loads
def is_json(response):
@ -19,3 +21,15 @@ def json(response):
like JSON before parsing.
"""
return loads(response.data)
class BaseTestCase(unittest.TestCase):
def setUp(self):
self.client = Client(
make_app('sqlite://', 'NONE', False, True), BaseResponse)
def teardown(self):
Paste.query.delete()
db.session.commit()
db.session.remove()
super().teardown()

View File

@ -1,78 +1,69 @@
from tests import client, is_json, json
from tests.utilities.runner import testcase
from tests import BaseTestCase, is_json, json
from lodgeit.lib.highlighting import STYLES
def post_json(method, data=None):
return client.post('/json/', query_string={'method': method},
data=data, content_type='application/json')
class APITestCase(BaseTestCase):
def post_json(self, method, data=None):
return self.client.post('/json/', query_string={'method': method},
data=data, content_type='application/json')
def test_json_post_and_get(self):
data = '{"language": "text", "code": "hello world"}'
resp = self.post_json('pastes.newPaste', data)
@testcase()
def test_json_post_and_get():
data = '{"language": "text", "code": "hello world"}'
resp = post_json('pastes.newPaste', data)
assert is_json(resp)
resp = post_json('pastes.getPaste',
'{"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()
def test_json_post_private_and_get():
data = '{"language": "text", "code": "hello world", "private": "true"}'
resp = post_json('pastes.newPaste', data)
assert is_json(resp)
resp = post_json('pastes.getPaste',
'{"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 is_json(resp)
data = '{"language": "text", "code": "hello world again"}'
resp = post_json('pastes.newPaste', data)
assert is_json(resp)
resp = post_json('pastes.getLast')
assert is_json(resp)
assert json(resp)['data']['code'] == "hello world again"
assert json(resp)['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 is_json(resp)
return resp
resp = self.post_json('pastes.getPaste',
'{"paste_id": "%d"}' % int(json(resp)['data']))
assert is_json(resp)
assert json(resp)['data']['code'] == "hello world"
assert json(resp)['data']['language'] == "text"
paste_ids = []
for x in range(10):
resp = run(x)
paste_ids.append(int(json(resp)['data']))
def test_json_post_private_and_get(self):
data = '{"language": "text", "code": "hello world", "private": "true"}'
resp = self.post_json('pastes.newPaste', data)
resp = post_json('pastes.getRecent', '{"amount": 7}')
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:]
assert is_json(resp)
resp = self.post_json('pastes.getPaste',
'{"paste_id": "%s"}' % json(resp)['data'])
assert is_json(resp)
assert json(resp)['data']['code'] == "hello world"
assert json(resp)['data']['language'] == "text"
def test_json_get_last(self):
data = '{"language": "text", "code": "hello world"}'
resp = self.post_json('pastes.newPaste', data)
assert is_json(resp)
@testcase()
def test_json_get_styles():
resp = post_json('styles.getStyles')
assert is_json(resp)
expected = [[u'%s' % x, u'%s' % STYLES[x]] for x in STYLES]
assert json(resp)['data'] == expected
data = '{"language": "text", "code": "hello world again"}'
resp = self.post_json('pastes.newPaste', data)
assert is_json(resp)
resp = self.post_json('pastes.getLast')
assert is_json(resp)
assert json(resp)['data']['code'] == "hello world again"
assert json(resp)['data']['language'] == "text"
def test_json_get_recent(self):
def run(inc):
data = '{"language": "text", "code": "hello world %s"}' % inc
resp = self.post_json('pastes.newPaste', data)
assert is_json(resp)
return resp
paste_ids = []
for x in range(10):
resp = run(x)
paste_ids.append(int(json(resp)['data']))
resp = self.post_json('pastes.getRecent', '{"amount": 7}')
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:]
def test_json_get_styles(self):
resp = self.post_json('styles.getStyles')
assert is_json(resp)
expected = [[u'%s' % x, u'%s' % STYLES[x]] for x in STYLES]
assert json(resp)['data'] == expected

View File

@ -1,27 +1,27 @@
from tests import client
from tests import BaseTestCase
def test_get_urls():
rules = [
('/', 200),
('/all/', 200),
('/all/1/', 200),
('/xmlrpc/', 200),
('/json/', 200),
('/about/', 200),
('/help/', 200),
('/help/advanced/', 200),
('/help/api/', 200),
('/help/integration/', 200),
('/help/pasting/', 200),
('/language/de/', 302),
('/language/en/', 302),
]
for rule, code in rules:
resp = client.get(rule)
assert code == resp.status_code
class URLTestCase(BaseTestCase):
def test_get_urls(self):
rules = [
('/', 200),
('/all/', 200),
('/all/1/', 200),
('/xmlrpc/', 200),
('/json/', 200),
('/about/', 200),
('/help/', 200),
('/help/advanced/', 200),
('/help/api/', 200),
('/help/integration/', 200),
('/help/pasting/', 200),
('/language/de/', 302),
('/language/en/', 302),
]
for rule, code in rules:
resp = self.client.get(rule)
assert code == resp.status_code
def test_post_url():
resp = client.post('/')
assert 200 == resp.status_code
def test_post_url(self):
resp = self.client.post('/')
assert 200 == resp.status_code

View File

@ -1,25 +0,0 @@
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

View File

@ -7,7 +7,8 @@ skipsdist = True
setenv = VIRTUAL_ENV={envdir}
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
commands = nosetests
commands = stestr --test-path ./tests/unittest run --no-subunit-trace {posargs}
stestr slowest
[testenv:pep8]
commands = flake8