server/tests: don't recreate database every test

This commit is contained in:
rr- 2016-04-20 11:48:34 +02:00
parent 661c0248d2
commit d3f2ef296b

View file

@ -1,6 +1,6 @@
import contextlib
import datetime import datetime
import uuid import uuid
from contextlib import contextmanager
import pytest import pytest
import freezegun import freezegun
import sqlalchemy import sqlalchemy
@ -10,35 +10,34 @@ from szurubooru.util import misc
class QueryCounter(object): class QueryCounter(object):
def __init__(self): def __init__(self):
self._statements = [] self._statements = []
def __enter__(self): def __enter__(self):
self._statements = [] self._statements = []
def __exit__(self, *args, **kwargs): def __exit__(self, *args, **kwargs):
self._statements = [] self._statements = []
def create_before_cursor_execute(self): def create_before_cursor_execute(self):
def before_cursor_execute( def before_cursor_execute(
_conn, _cursor, statement, _parameters, _context, _executemany): _conn, _cursor, statement, _parameters, _context, _executemany):
self._statements.append(statement) self._statements.append(statement)
return before_cursor_execute return before_cursor_execute
@property @property
def statements(self): def statements(self):
return self._statements return self._statements
_query_counter = QueryCounter() _query_counter = QueryCounter()
engine = sqlalchemy.create_engine('sqlite:///:memory:')
db.Base.metadata.create_all(bind=engine)
sqlalchemy.event.listen(
engine,
'before_cursor_execute',
_query_counter.create_before_cursor_execute())
@pytest.fixture
def query_counter():
return _query_counter
def get_unique_name(): def get_unique_name():
return str(uuid.uuid4()) return str(uuid.uuid4())
@pytest.fixture @pytest.fixture
def fake_datetime(): def fake_datetime():
@contextmanager @contextlib.contextmanager
def injector(now): def injector(now):
freezer = freezegun.freeze_time(now) freezer = freezegun.freeze_time(now)
freezer.start() freezer.start()
@ -46,23 +45,28 @@ def fake_datetime():
freezer.stop() freezer.stop()
return injector return injector
@pytest.yield_fixture(autouse=True) @pytest.fixture()
def session(query_counter): def query_counter():
return _query_counter
@pytest.fixture
def query_logger():
import logging import logging
logging.basicConfig() logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
engine = sqlalchemy.create_engine('sqlite:///:memory:')
sqlalchemy.event.listen( @pytest.yield_fixture(scope='function', autouse=True)
engine, def session(query_logger):
'before_cursor_execute',
query_counter.create_before_cursor_execute())
session_maker = sqlalchemy.orm.sessionmaker(bind=engine) session_maker = sqlalchemy.orm.sessionmaker(bind=engine)
session = sqlalchemy.orm.scoped_session(session_maker) session = sqlalchemy.orm.scoped_session(session_maker)
db.Base.query = session.query_property()
db.Base.metadata.create_all(bind=engine)
db.session = session db.session = session
yield session try:
session.remove() yield session
finally:
session.remove()
for table in reversed(db.Base.metadata.sorted_tables):
session.execute(table.delete())
session.commit()
@pytest.fixture @pytest.fixture
def context_factory(session): def context_factory(session):