Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_guard(session):
from mixer.backend.sqlalchemy import Mixer, mixer
with pytest.raises(ValueError):
mixer.guard(User.name == 'maxi').blend(User)
mixer = Mixer(session=session, commit=True)
u1 = mixer.guard(User.name == 'maxi').blend(User, name='maxi')
u2 = mixer.guard(User.name == 'maxi').blend(User)
assert u1
assert u1 == u2
def test_mixer(session):
from mixer.backend.sqlalchemy import Mixer
mixer = Mixer(session=session, commit=True)
role = mixer.blend('tests.test_sqlalchemy.Role')
assert role and role.user
role = mixer.blend(Role, user__name='test2')
assert role.user.name == 'test2'
profile = mixer.blend('tests.test_sqlalchemy.Profile')
user = mixer.blend(User, profile__name='test')
assert user.profile.name == 'test'
user = mixer.blend(User, profile=profile)
assert user.profile == profile
user = mixer.blend(User, score=mixer.RANDOM)
assert user.score != 50
def test_cycle(session):
from mixer.backend.sqlalchemy import Mixer
mixer = Mixer(session=session, commit=True)
profile1 = mixer.blend('tests.test_sqlalchemy.Profile', name='first')
profile2 = mixer.blend('tests.test_sqlalchemy.Profile', name='second')
users = mixer.cycle(2).blend(User, profile=(p for p in (profile1, profile2)))
assert len(users) == 2
assert users[0].profile.name == 'first'
assert users[1].profile.name == 'second'
def test_select(session):
from mixer.backend.sqlalchemy import Mixer
mixer = Mixer(session=session, commit=True)
users = session.query(User).all()
role = mixer.blend(Role, user=mixer.SELECT)
assert role.user in users
user = users.pop()
role = mixer.blend(Role, user=mixer.SELECT(User.id == user.id))
assert user == role.user
def test_reload(session):
from mixer.backend.sqlalchemy import Mixer
mixer = Mixer(session=session, commit=True)
u1 = mixer.blend(User)
u1.name = 'wrong name'
u2 = mixer.reload(u1)
assert u2 == u1
assert u2.name != 'wrong name'
def test_mix22(session):
from mixer.backend.sqlalchemy import Mixer
mixer = Mixer(session=session, commit=True)
role = mixer.blend(Role, name=mixer.MIX.user.name)
assert role.name == role.user.name
:return value: A generated value
"""
if self.params.get('commit'):
session = self.params.get('session')
if not session:
LOGGER.warn("'commit' set true but session not initialized.")
else:
session.add(target)
session.commit()
return target
# Default mixer
mixer = Mixer()
def __init__(self, session=None, commit=True, **params):
"""Initialize the SQLAlchemy Mixer.
:param fake: (True) Generate fake data instead of random data.
:param session: SQLAlchemy session. Using for commits.
:param commit: (True) Commit instance to session after creation.
"""
super(Mixer, self).__init__(**params)
self.params['session'] = session
self.params['commit'] = bool(session) and commit
See example: ::
from mixer.backend.flask import mixer
mixer.init_app(flask_app)
user = mixer.blend('path.to.models.User')
"""
from __future__ import absolute_import
from .sqlalchemy import TypeMixer, Mixer as BaseMixer
class Mixer(BaseMixer):
""" Init application. """
type_mixer_cls = TypeMixer
def __init__(self, app=None, commit=True, **kwargs):
""" Initialize the SQLAlchemy Mixer.
:param fake: (True) Generate fake data instead of random data.
:param app: Flask application
:param commit: (True) Commit instance to session after creation.
"""
super(Mixer, self).__init__(**kwargs)
self.params['commit'] = commit
if app: