How to use the mixer.backend.sqlalchemy.Mixer function in mixer

To help you get started, we’ve selected a few mixer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github klen / mixer / tests / test_sqlalchemy.py View on Github external
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
github klen / mixer / tests / test_sqlalchemy.py View on Github external
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
github klen / mixer / tests / test_sqlalchemy.py View on Github external
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'
github klen / mixer / tests / test_sqlalchemy.py View on Github external
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
github klen / mixer / tests / test_sqlalchemy.py View on Github external
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'
github klen / mixer / tests / test_sqlalchemy.py View on Github external
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
github klen / mixer / mixer / backend / sqlalchemy.py View on Github external
: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()
github klen / mixer / mixer / backend / sqlalchemy.py View on Github external
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
github klen / mixer / mixer / backend / flask.py View on Github external
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: