Skip to main content

Mixer -- Is a fixtures replacement. Supported Django ORM, SqlAlchemy ORM, Mongoengine ODM and custom python objects.

Project description

Mixer is application to generate instances of Django or SQLAlchemy models. It’s useful for testing and fixtures replacement. Fast and convenient test-data generation.

Mixer supports:

Build Status Coverals Donate

Docs are available at https://mixer.readthedocs.org/. Pull requests with documentation enhancements and/or fixes are awesome and most welcome.

Описание на русском языке: http://klen.github.io/mixer-ru.html

Requirements

  • python (2.6, 2.7, 3.2, 3.3)

  • Django (1.4, 1.5, 1.6) for django ORM suport;

  • SQLAlchemy for SQLAlchemy ORM suport;

  • Mongoengine for Mongoengine ODM support;

  • Flask-SQLALchemy for SQLAlchemy ORM suport and integration as Flask application;

Installation

Mixer should be installed using pip:

pip install mixer

Usage

By default Mixer try to generate fake data. If you want randomize values
initialize the Mixer by manual like: Mixer(fake=False)
By default Mixer saves generated objects in database. If you want disable
this, initialize the Mixer by manual like: Mixer(commit=False)

Django

Quick example:

from mixer.backend.django import mixer
from customapp.models import User, UserMessage

# Generate random User
user = mixer.blend(User)

# Generate UserMessage
message = mixer.blend(UserMessage, user=user)

# Generate UserMessage and User. Set User.username to 'testname'.
message = mixer.blend(UserMessage, user__username='testname')

# Generate SomeModel from SomeApp and select FK or M2M values from db
some = mixer.blend('someapp.somemodel', somerelation=mixer.SELECT)

# Generate SomeModel from SomeApp and force a value of field with default to random
some = mixer.blend('someapp.somemodel', money=mixer.RANDOM)

# Generate 5 SomeModel instances and get a field values from custom generator
some_models = mixer.cycle(5).blend('somemodel', company=(company for company in companies))

Flask, Flask-SQLAlchemy

Quick example:

from mixer.backend.flask import mixer
from models import User, UserMessage

mixer.init_app(self.app)

# Generate random User
user = mixer.blend(User)

# Generate UserMessage
message = mixer.blend(UserMessage, user=user)

# Generate UserMessage and User. Set User.username to 'testname'.
message = mixer.blend(UserMessage, user__username='testname')

# Generate SomeModel and select FK or M2M values from db
some = mixer.blend('project.models.SomeModel', somerelation=mixer.SELECT)

# Generate SomeModel from SomeApp and force a value of field with default to random
some = mixer.blend('project.models.SomeModel', money=mixer.RANDOM)

# Generate 5 SomeModel instances and get a field values from custom generator
some_models = mixer.cycle(5).blend('project.models.SomeModel', company=(company for company in companies))

Support for Flask-SQLAlchemy models that have __init__ arguments

For support this scheme, just create your own mixer class, like this:

from mixer.backend.sqlalchemy import Mixer

class MyOwnMixer(Mixer):

    def populate_target(self, values):
        target = self.__scheme(**values)
        return target

mixer = MyOwnMixer()

SQLAlchemy

Example of initialization:

from mixer.backend.sqlalchemy import Mixer

ENGINE = create_engine('sqlite:///:memory:')
BASE = declarative_base()
SESSION = sessionmaker(bind=ENGINE)

mixer = Mixer(session=SESSION(), commit=True)
role = mixer.blend('package.models.Role')

Also see Flask, Flask-SQLALchemy.

Mongoengine

Example usage:

from mixer.backend.mongoengine import mixer

class User(Document):
    created_at = DateTimeField(default=datetime.datetime.now)
    email = EmailField(required=True)
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)

class Post(Document):
    title = StringField(max_length=120, required=True)
    author = ReferenceField(User)
    tags = ListField(StringField(max_length=30))

post = mixer.blend(Post, author__username='foo')

Common usage

Quick example:

from mixer.main import mixer

class Test:
    one = int
    two = int
    name = str

class Scheme:
    name = str
    money = int
    male = bool
    prop = Test

scheme = mixer.blend(Scheme, prop__one=1)

DB commits

By default ‘django’, ‘flask’, ‘mongoengine’ backends tries to save objects to database. For prevent this behaviour init mixer manually:

from mixer.backend.django import Mixer

mixer = Mixer(commit=False)

Or you can use mixer with custom params as context:

from mixer.backend.django import mixer

# Will be save to db
user1 = mixer.blend('auth.user')

# Will not be save to db
with mixer.ctx(commit=False):
    user2 = mixer.blend('auth.user')

Custom fields

Mixer allows you to define generators for fields by manualy.

Quick example:

from mixer.main import mixer

class Test:
    id = int
    name = str

mixer.register(Test,
    name=lambda: 'John',
    id=lambda: str(mixer.g.get_positive_integer())
)

test = mixer.blend(Test)
test.name == 'John'
isinstance(test.id, str)

# You could pinned just a value to field
mixer.register(Test, name='Just John')
test = mixer.blend(Test)
test.name == 'Just John'

Also you can make your own factory for field types:

from mixer.backend.django import Mixer, GenFactory

def get_func(*args, **kwargs):
    return "Always same"

class MyFactory(GenFactory):
    generators = {
        models.CharField: get_func
    }

mixer = Mixer(factory=MyFactory)

Middlewares

You can add middleware layers to process generation:

from mixer.backend.django import mixer

# Register middleware to model
@mixer.middleware('auth.user')
def encrypt_password(user):
    user.set_password('test')
    return user

You can add several middlewares. Each middleware should get one argument (generated value) and return them.

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/mixer/issues

Contributing

Development of starter happens at github: https://github.com/klen/mixer

Contributors

License

Licensed under a BSD license.

Project details


Release history Release notifications | RSS feed

This version

4.5.3

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mixer-4.5.3.tar.gz (34.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mixer-4.5.3-py2.py3-none-any.whl (47.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file mixer-4.5.3.tar.gz.

File metadata

  • Download URL: mixer-4.5.3.tar.gz
  • Upload date:
  • Size: 34.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mixer-4.5.3.tar.gz
Algorithm Hash digest
SHA256 ce59474a8f7b77dbf8975e45d4546375012d64aef6fb897a667ea0de9683b36b
MD5 9781cb58ee14e914154496dbc8285c8c
BLAKE2b-256 6a635de1866962853b317813ea71b19506cab55d2715f889da381fd8c876a26b

See more details on using hashes here.

File details

Details for the file mixer-4.5.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for mixer-4.5.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d7c7e556cda488d61cf66310cb123c51ff66e2230b384ed222dc84d20fe68c9e
MD5 9d268dc391a2b6304330a56d5cabec00
BLAKE2b-256 06626254c7bd5b9145521d0dfd0ef65a614c77d49ec9d5952559020e1428c2a7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page