gallery.accords-library.com/server/szurubooru/tests/conftest.py

172 lines
4.8 KiB
Python
Raw Normal View History

import contextlib
2016-04-30 21:17:08 +00:00
import os
2016-04-16 08:57:42 +00:00
import datetime
import uuid
2016-04-15 12:36:00 +00:00
import pytest
import freezegun
2016-04-15 12:36:00 +00:00
import sqlalchemy
from szurubooru import api, config, db
from szurubooru.func import util
2016-04-15 12:36:00 +00:00
class QueryCounter(object):
def __init__(self):
self._statements = []
def __enter__(self):
self._statements = []
def __exit__(self, *args, **kwargs):
self._statements = []
def create_before_cursor_execute(self):
def before_cursor_execute(
_conn, _cursor, statement, _parameters, _context, _executemany):
self._statements.append(statement)
return before_cursor_execute
@property
def statements(self):
return self._statements
_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())
def get_unique_name():
return str(uuid.uuid4())
2016-04-16 08:57:42 +00:00
@pytest.fixture
def fake_datetime():
@contextlib.contextmanager
2016-04-16 08:57:42 +00:00
def injector(now):
2016-04-18 20:41:39 +00:00
freezer = freezegun.freeze_time(now)
freezer.start()
yield
freezer.stop()
2016-04-16 08:57:42 +00:00
return injector
@pytest.fixture()
def query_counter():
return _query_counter
@pytest.fixture
def query_logger():
if pytest.config.option.verbose > 0:
import logging
2016-05-11 20:08:10 +00:00
import coloredlogs
coloredlogs.install(fmt='[%(asctime)-15s] %(name)s %(message)s', isatty=True)
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
@pytest.yield_fixture(scope='function', autouse=True)
def session(query_logger):
2016-04-15 12:36:00 +00:00
session_maker = sqlalchemy.orm.sessionmaker(bind=engine)
2016-04-18 20:41:39 +00:00
session = sqlalchemy.orm.scoped_session(session_maker)
db.session = session
try:
yield session
finally:
session.remove()
for table in reversed(db.Base.metadata.sorted_tables):
session.execute(table.delete())
session.commit()
2016-04-15 12:36:00 +00:00
@pytest.fixture
def context_factory(session):
def factory(request=None, input=None, files=None, user=None):
ctx = api.Context()
ctx.input = input or {}
ctx.session = session
ctx.request = request or {}
ctx.files = files or {}
ctx.user = user or db.User()
return ctx
2016-04-15 12:36:00 +00:00
return factory
@pytest.fixture
def config_injector():
def injector(new_config_content):
config.config = new_config_content
return injector
@pytest.fixture
def user_factory():
2016-05-08 14:59:25 +00:00
def factory(name=None, rank=db.User.RANK_REGULAR, email='dummy'):
2016-04-15 12:36:00 +00:00
user = db.User()
user.name = name or get_unique_name()
2016-04-15 12:36:00 +00:00
user.password_salt = 'dummy'
user.password_hash = 'dummy'
user.email = email
2016-04-15 12:36:00 +00:00
user.rank = rank
2016-04-16 08:57:42 +00:00
user.creation_time = datetime.datetime(1997, 1, 1)
2016-04-15 12:36:00 +00:00
user.avatar_style = db.User.AVATAR_GRAVATAR
return user
return factory
2016-04-15 18:40:12 +00:00
@pytest.fixture
2016-04-24 07:50:11 +00:00
def tag_category_factory():
def factory(name=None, color='dummy', default=False):
category = db.TagCategory()
category.name = name or get_unique_name()
category.color = color
category.default = default
return category
return factory
2016-04-15 18:40:12 +00:00
@pytest.fixture
2016-04-24 07:50:11 +00:00
def tag_factory():
def factory(names=None, category=None):
if not category:
category = db.TagCategory(get_unique_name())
2016-04-24 07:50:11 +00:00
db.session.add(category)
2016-04-15 18:40:12 +00:00
tag = db.Tag()
tag.names = [db.TagName(name) for name in (names or [get_unique_name()])]
2016-04-15 18:40:12 +00:00
tag.category = category
2016-04-16 08:57:42 +00:00
tag.creation_time = datetime.datetime(1996, 1, 1)
2016-04-15 18:40:12 +00:00
return tag
return factory
2016-04-17 10:55:07 +00:00
@pytest.fixture
def post_factory():
def factory(
2016-04-22 18:58:04 +00:00
id=None,
2016-04-17 10:55:07 +00:00
safety=db.Post.SAFETY_SAFE,
type=db.Post.TYPE_IMAGE,
checksum='...'):
post = db.Post()
2016-04-22 18:58:04 +00:00
post.post_id = id
2016-04-17 10:55:07 +00:00
post.safety = safety
post.type = type
post.checksum = checksum
2016-04-22 18:58:04 +00:00
post.flags = []
2016-04-30 21:17:08 +00:00
post.mime_type = 'application/octet-stream'
2016-04-17 10:55:07 +00:00
post.creation_time = datetime.datetime(1996, 1, 1)
return post
return factory
2016-04-24 07:04:53 +00:00
@pytest.fixture
2016-04-24 07:50:11 +00:00
def comment_factory(user_factory, post_factory):
2016-04-24 07:04:53 +00:00
def factory(user=None, post=None, text='dummy'):
2016-04-24 07:50:11 +00:00
if not user:
user = user_factory()
db.session.add(user)
if not post:
post = post_factory()
db.session.add(post)
2016-04-24 07:04:53 +00:00
comment = db.Comment()
2016-04-24 07:50:11 +00:00
comment.user = user
comment.post = post
2016-04-24 07:04:53 +00:00
comment.text = text
comment.creation_time = datetime.datetime(1996, 1, 1)
return comment
return factory
2016-04-30 21:17:08 +00:00
@pytest.fixture
def read_asset():
def get(path):
path = os.path.join(os.path.dirname(__file__), 'assets', path)
with open(path, 'rb') as handle:
return handle.read()
return get