Skip to content

Commit 8125bca

Browse files
committed
Optimize unit tests - don't drop db betewen tests
1 parent 3bfd0ec commit 8125bca

File tree

6 files changed

+78
-50
lines changed

6 files changed

+78
-50
lines changed

flask_app/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def testing_login():
2828

2929
testing_email = 'testing@localhost'
3030

31-
user = user_datastore.get_user(testing_email)
31+
user = _get_or_create_user({'email': testing_email})
3232
assert user
3333
login_user(user)
3434
user_info = {}

flask_app/blueprints/rest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ def decorator(resource):
2727
class SessionResource(ModelResource):
2828

2929
MODEL = Session
30-
DEFAULT_SORT = ((Session.end_time == None).desc(), Session.end_time.desc())
30+
DEFAULT_SORT = (Session.start_time.desc(),)
3131

3232

3333
@_resource('/tests', '/tests/<int:object_id>', '/sessions/<int:session_id>/tests')
3434
class TestResource(ModelResource):
3535

3636
MODEL = Test
37-
DEFAULT_SORT = ((Test.end_time == None).desc(), Test.end_time.desc())
37+
DEFAULT_SORT = (Test.start_time.desc(),)
3838

3939
def _get_iterator(self):
4040
session_id = request.args.get('session_id')

pytest.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
[pytest]
2-
addopts = tests

tests/conftest.py

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
1717

1818

19+
@pytest.fixture(scope='session', autouse=True)
20+
def patch_proxy_bypass():
21+
"""Work around a bug causing should_bypass_proxies to take forever when VPN is active
22+
"""
23+
import requests
24+
requests.utils.should_bypass_proxies = lambda url: True
25+
26+
@pytest.fixture(scope='session', autouse=True)
27+
def invalidate_cache():
28+
cache.invalidate()
29+
30+
1931
def pytest_addoption(parser):
2032
parser.addoption("--url", action="store", default=None)
2133

@@ -50,35 +62,43 @@ def webapp_without_login(request):
5062
return _create_webapp(request)
5163

5264

65+
_cached_app = None
66+
_cached_config = None
67+
68+
def _create_flask_app():
69+
global _cached_app
70+
global _cached_config
71+
72+
if _cached_app is None:
73+
74+
returned = _cached_app = app.create_app({
75+
#'SQLALCHEMY_DATABASE_URI': 'postgresql://127.0.0.1/backslash-ut',
76+
'SECRET_KEY': 'testing-key',
77+
'TESTING': True,
78+
'SECURITY_PASSWORD_SALT': 'testing_salt',
79+
'JSONIFY_PRETTYPRINT_REGULAR': False,
80+
'JSON_SORT_KEYS': False,
81+
})
82+
_cached_config = returned.config.copy()
83+
returned.extensions['security'].password_salt = returned.config[
84+
'SECURITY_PASSWORD_SALT']
85+
86+
else:
87+
returned = _cached_app
88+
returned.config.update(_cached_config.copy())
89+
return returned
90+
91+
5392
def _create_webapp(request):
54-
returned = Webapp(app.create_app({
55-
'SQLALCHEMY_DATABASE_URI': 'postgresql://127.0.0.1/backslash-ut',
56-
'SECRET_KEY': 'testing-key',
57-
'TESTING': True,
58-
'SECURITY_PASSWORD_SALT': 'testing_salt',
59-
}))
93+
returned = Webapp(_create_flask_app())
6094
returned.activate()
6195
request.addfinalizer(returned.deactivate)
62-
returned.app.extensions['security'].password_salt = returned.app.config[
63-
'SECURITY_PASSWORD_SALT']
6496
return returned
6597

66-
67-
@pytest.fixture(scope='function', autouse=True)
68-
def db(request, webapp_without_login):
69-
global _test_index
70-
with webapp_without_login.app.app_context():
71-
models.db.session.close()
72-
models.db.drop_all()
73-
models.db.create_all()
98+
@pytest.fixture
99+
def db():
74100
return models.db
75101

76-
77-
@pytest.fixture(scope='function', autouse=True)
78-
def invalidate_cache():
79-
cache.invalidate()
80-
81-
82102
@pytest.fixture
83103
def runtoken(db, webapp_without_login, testuser):
84104
with webapp_without_login.app.app_context():
@@ -88,19 +108,27 @@ def runtoken(db, webapp_without_login, testuser):
88108
db.session.commit()
89109
return token_string
90110

111+
@pytest.fixture
112+
def testuser(testuser_and_id):
113+
return testuser_and_id[0]
114+
115+
@pytest.fixture
116+
def testuser_id(testuser_and_id):
117+
return testuser_and_id[1]
91118

92119
@pytest.fixture
93-
def testuser(db, webapp_without_login, testuser_email):
120+
def testuser_and_id(db, webapp_without_login, testuser_email):
94121
with webapp_without_login.app.app_context():
95122
user = models.User(email=testuser_email, active=True)
96123
db.session.add(user)
97124
db.session.commit()
98-
return user
125+
user_id = user.id
126+
return user, user_id
99127

100128

101129
@pytest.fixture
102130
def testuser_email():
103-
return 'testing@localhost'
131+
return 'testing{}@localhost'.format(uuid4())
104132

105133

106134
@pytest.fixture
@@ -182,3 +210,8 @@ def raw_method(self, *args, **kwargs):
182210

183211
for _method in ("get", "put", "post", "delete"):
184212
_make_request_shortcut(_method)
213+
214+
215+
@pytest.fixture
216+
def flask_app(webapp):
217+
return webapp.app

tests/test_ut/conftest.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import math
2-
from uuid import uuid1
2+
from uuid import uuid4
33

44
import flux
55
from munch import Munch
@@ -9,7 +9,7 @@
99

1010
@pytest.fixture
1111
def subjects():
12-
return [
12+
returned = [
1313
Munch(name='prod1', product='Car',
1414
version=None, revision='120'),
1515
Munch(name='prod2', product='Car',
@@ -19,6 +19,12 @@ def subjects():
1919
Munch(name='prod4', product='Car',
2020
version=None, revision='120'),
2121
]
22+
salt = '_{}'.format(uuid4())
23+
24+
for subj in returned:
25+
subj.name += salt
26+
subj.product += salt
27+
return returned
2228

2329

2430
@pytest.fixture(autouse=True, scope='session')
@@ -35,6 +41,10 @@ def finalizer():
3541
next_round_time = math.ceil(current_time * 10000) / 10000.0
3642
flux.current_timeline.sleep(next_round_time - current_time)
3743

44+
@pytest.fixture(autouse=True, scope='function')
45+
def advance_timeline():
46+
flux.current_timeline.sleep(10)
47+
3848

3949
@pytest.fixture
4050
def started_session(client):
@@ -127,7 +137,3 @@ def metadata_holder(request, client, test_info):
127137
test = session.report_test_start(**test_info)
128138
return test
129139

130-
131-
@pytest.fixture
132-
def flask_app(webapp):
133-
return webapp.app

tests/test_ut/test_session_api.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import itertools
12
import socket
23

34
import flux
4-
55
import pytest
6+
from flask_app.models import User
67

7-
from .utils import raises_not_found, raises_conflict, raises_bad_request
8+
from .utils import raises_bad_request, raises_conflict, raises_not_found
89

910

1011
def test_start_session(client):
@@ -37,25 +38,15 @@ def test_started_session_hostname_specify_explicitly(client):
3738
assert session.hostname == hostname
3839

3940

40-
def test_session_user(started_session):
41-
assert started_session.user_id == 1
42-
41+
def test_session_user(started_session, testuser_id):
42+
assert started_session.user_id == testuser_id
4343

4444

4545
def test_started_session_times(started_session):
4646
assert started_session.start_time is not None
4747
assert started_session.end_time is None
4848

4949

50-
def test_query_all_sessions(client, started_session):
51-
[session] = client.query_sessions()
52-
assert session.id == started_session.id
53-
54-
def test_query_all_tests(client, started_test):
55-
[test] = client.query_tests()
56-
assert test.id == started_test.id
57-
assert test == started_test
58-
5950

6051
@pytest.mark.parametrize('use_duration', [True, False])
6152
def test_end_session(started_session, use_duration):
@@ -123,4 +114,3 @@ def test_session_query_tests(started_session_with_ended_test):
123114
assert queried_test.id == test.id
124115
test.refresh() # need to update end_time
125116
assert queried_test == test
126-

0 commit comments

Comments
 (0)