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 Version Downloads 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) 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))

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)

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 ow 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)

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

License

Licensed under a BSD license.

Project details


Release history Release notifications

History Node

6.0.1

History Node

6.0.0

History Node

5.6.6

History Node

5.6.5

History Node

5.6.4

History Node

5.6.3

History Node

5.6.2

History Node

5.6.1

History Node

5.6.0

History Node

5.5.8

History Node

5.5.7

History Node

5.5.6

History Node

5.5.5

History Node

5.5.4

History Node

5.5.3

History Node

5.5.2

History Node

5.5.1

History Node

5.5.0

History Node

5.4.1

History Node

5.4.0

History Node

5.3.1

History Node

5.3.0

History Node

5.2.2

History Node

5.2.1

History Node

5.2.0

History Node

5.1.10

History Node

5.1.8

History Node

5.1.7

History Node

5.1.6

History Node

5.1.5

History Node

5.1.4

History Node

5.1.3

History Node

5.1.2

History Node

5.1.1

History Node

5.1.0

History Node

5.0.12

History Node

5.0.11

History Node

5.0.10

History Node

5.0.9

History Node

5.0.8

History Node

5.0.7

History Node

5.0.6

History Node

5.0.5

History Node

5.0.4

History Node

5.0.3

History Node

5.0.2

History Node

5.0.1

History Node

4.10.2

History Node

4.10.1

History Node

4.10.0

History Node

4.9.6

History Node

4.9.5

History Node

4.9.4

History Node

4.9.3

History Node

4.9.2

History Node

4.9.1

History Node

4.8.1

History Node

4.8.0

History Node

4.7.5

History Node

4.7.4

History Node

4.7.3

History Node

4.7.2

History Node

4.7.1

History Node

4.7.0

History Node

4.6.2

History Node

4.6.1

History Node

4.6.0

History Node

4.5.5

History Node

4.5.4

History Node

4.5.3

History Node

4.5.2

History Node

4.5.1

History Node

4.5.0

History Node

4.4.0

History Node

4.3.2

History Node

4.3.1

History Node

4.3.0

History Node

4.2.0

History Node

4.1.0

History Node

4.0.7

History Node

4.0.6

History Node

4.0.5

History Node

4.0.4

History Node

4.0.3

History Node

4.0.2

History Node

4.0.1

History Node

4.0.0

History Node

3.0.0

History Node

2.3.0

History Node

2.2.0

History Node

2.1.0

History Node

2.0.1

History Node

2.0.0

History Node

1.6.0

History Node

1.5.1

History Node

1.5.0

This version
History Node

1.4.0

History Node

1.3.0

History Node

1.2.0

History Node

1.1.6

History Node

1.1.5

History Node

1.1.4

History Node

1.1.3

History Node

1.1.2

History Node

1.1.1

History Node

1.1.0

History Node

1.0.0

History Node

0.11.3

History Node

0.11.1

History Node

0.11.0

History Node

0.10.0

History Node

0.9.1

History Node

0.9.0

History Node

0.8.1

History Node

0.8.0

History Node

0.7.0

History Node

0.6.4

History Node

0.6.3

History Node

0.6.2

History Node

0.6.1

History Node

0.6.0

History Node

0.5.1

History Node

0.5.0

History Node

0.4.1

History Node

0.4.0

History Node

0.3.3

History Node

0.3.2

History Node

0.3.1

History Node

0.3.0

History Node

0.2.8

History Node

0.2.7

History Node

0.2.6

History Node

0.2.5

History Node

0.2.4

History Node

0.2.3

History Node

0.2.2

History Node

0.2.1

History Node

0.2.0

History Node

0.1.0

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
mixer-1.4.0.tar.gz (31.4 kB) Copy SHA256 hash SHA256 Source None Nov 21, 2013

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page