From 9e2d54bb5a5a7acb8c6ae4000d4c5621ebeb3641 Mon Sep 17 00:00:00 2001 From: Clark Boylan Date: Thu, 10 Nov 2022 10:11:54 -0800 Subject: [PATCH] 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 --- .gitignore | 1 + .stestr.conf | 3 + .zuul.yaml | 6 +- test-requirements.txt | 3 +- tests/__init__.py | 20 +++++- tests/unittest/test_api.py | 127 +++++++++++++++++------------------- tests/unittest/test_urls.py | 48 +++++++------- tests/utilities/__init__.py | 0 tests/utilities/runner.py | 25 ------- tox.ini | 3 +- 10 files changed, 112 insertions(+), 124 deletions(-) create mode 100644 .stestr.conf delete mode 100644 tests/utilities/__init__.py delete mode 100644 tests/utilities/runner.py diff --git a/.gitignore b/.gitignore index f9a110a..4a3f4ba 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .DS_Store bootstrap.py .tox +.stestr/ .project .pydevproject .settings diff --git a/.stestr.conf b/.stestr.conf new file mode 100644 index 0000000..45b434e --- /dev/null +++ b/.stestr.conf @@ -0,0 +1,3 @@ +[DEFAULT] +test_path=./tests/unittest +top_dir=./ diff --git a/.zuul.yaml b/.zuul.yaml index a4b7f2c..540530d 100644 --- a/.zuul.yaml +++ b/.zuul.yaml @@ -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: diff --git a/test-requirements.txt b/test-requirements.txt index 4283f38..053a2ec 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,2 +1,3 @@ flake8 -nose +python-subunit +stestr>=1.0.0 # Apache-2.0 diff --git a/tests/__init__.py b/tests/__init__.py index 5c64868..c360a55 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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() diff --git a/tests/unittest/test_api.py b/tests/unittest/test_api.py index d58fdf1..ad4ed46 100644 --- a/tests/unittest/test_api.py +++ b/tests/unittest/test_api.py @@ -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 diff --git a/tests/unittest/test_urls.py b/tests/unittest/test_urls.py index fc9a3e6..3f92dc7 100644 --- a/tests/unittest/test_urls.py +++ b/tests/unittest/test_urls.py @@ -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 diff --git a/tests/utilities/__init__.py b/tests/utilities/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/utilities/runner.py b/tests/utilities/runner.py deleted file mode 100644 index c1e04f8..0000000 --- a/tests/utilities/runner.py +++ /dev/null @@ -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 diff --git a/tox.ini b/tox.ini index 0a61eba..f03e6c7 100644 --- a/tox.ini +++ b/tox.ini @@ -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