2016-04-16 08:57:42 +00:00
|
|
|
import datetime
|
2016-04-17 10:53:58 +00:00
|
|
|
import uuid
|
2016-04-15 12:36:00 +00:00
|
|
|
import pytest
|
2016-04-18 17:42:24 +00:00
|
|
|
import freezegun
|
2016-04-15 12:36:00 +00:00
|
|
|
import sqlalchemy
|
2016-04-15 15:54:21 +00:00
|
|
|
from szurubooru import api, config, db
|
2016-04-15 12:36:00 +00:00
|
|
|
from szurubooru.util import misc
|
|
|
|
|
2016-04-17 10:53:58 +00:00
|
|
|
def get_unique_name():
|
|
|
|
return str(uuid.uuid4())
|
|
|
|
|
2016-04-16 08:57:42 +00:00
|
|
|
@pytest.fixture
|
2016-04-18 17:42:24 +00:00
|
|
|
def fake_datetime():
|
2016-04-16 08:57:42 +00:00
|
|
|
def injector(now):
|
2016-04-18 17:42:24 +00:00
|
|
|
class scope():
|
|
|
|
def __enter__(self):
|
|
|
|
self.freezer = freezegun.freeze_time(now)
|
|
|
|
self.freezer.start()
|
|
|
|
def __exit__(self, type, value, trackback):
|
|
|
|
self.freezer.stop()
|
|
|
|
return scope()
|
2016-04-16 08:57:42 +00:00
|
|
|
return injector
|
|
|
|
|
2016-04-15 12:36:00 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def session():
|
2016-04-15 21:21:33 +00:00
|
|
|
import logging
|
|
|
|
logging.basicConfig()
|
|
|
|
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
|
2016-04-15 12:36:00 +00:00
|
|
|
engine = sqlalchemy.create_engine('sqlite:///:memory:')
|
|
|
|
session_maker = sqlalchemy.orm.sessionmaker(bind=engine)
|
|
|
|
session_instance = sqlalchemy.orm.scoped_session(session_maker)
|
|
|
|
db.Base.query = session_instance.query_property()
|
|
|
|
db.Base.metadata.create_all(bind=engine)
|
|
|
|
return session_instance
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def context_factory(session):
|
2016-04-15 15:54:21 +00:00
|
|
|
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-04-17 10:53:58 +00:00
|
|
|
def factory(name=None, rank='regular_user', email='dummy'):
|
2016-04-15 12:36:00 +00:00
|
|
|
user = db.User()
|
2016-04-17 10:53:58 +00:00
|
|
|
user.name = name or get_unique_name()
|
2016-04-15 12:36:00 +00:00
|
|
|
user.password_salt = 'dummy'
|
|
|
|
user.password_hash = 'dummy'
|
2016-04-17 10:53:58 +00:00
|
|
|
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-18 19:41:47 +00:00
|
|
|
def tag_factory(session):
|
|
|
|
def factory(names=None, category_name='dummy'):
|
|
|
|
category = db.TagCategory(category_name)
|
|
|
|
session.add(category)
|
2016-04-15 18:40:12 +00:00
|
|
|
tag = db.Tag()
|
2016-04-17 10:53:58 +00:00
|
|
|
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(
|
|
|
|
safety=db.Post.SAFETY_SAFE,
|
|
|
|
type=db.Post.TYPE_IMAGE,
|
|
|
|
checksum='...'):
|
|
|
|
post = db.Post()
|
|
|
|
post.safety = safety
|
|
|
|
post.type = type
|
|
|
|
post.checksum = checksum
|
|
|
|
post.flags = 0
|
|
|
|
post.creation_time = datetime.datetime(1996, 1, 1)
|
|
|
|
return post
|
|
|
|
return factory
|