• Stars
    star
    942
  • Rank 48,525 (Top 1.0 %)
  • Language
    Python
  • License
    Other
  • Created over 11 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Mixer -- Is a fixtures replacement. Supported Django, Flask, SqlAlchemy and custom python objects.

image

The Mixer is a helper to generate instances of Django or SQLAlchemy models. It's useful for testing and fixture replacement. Fast and convenient test-data generation.

Mixer supports:

Tests Status

Version

Downloads

License

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.html

Important

From version 6.2 the Mixer library doesn't support Python 2. The latest version with python<3 support is mixer 6.1.3

Requirements

  • Python 3.7+
  • Django (3.0, 3.1) for Django ORM support;
  • Flask-SQLALchemy for SQLAlchemy ORM support and integration as Flask application;
  • Faker >= 0.7.3
  • Mongoengine for Mongoengine ODM support;
  • SQLAlchemy for SQLAlchemy ORM support;
  • Peewee ORM support;

Installation

Mixer should be installed using pip: :

pip install mixer

Usage

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

Django workflow

Quick example:

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

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

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

# Generate an UserMessage and an User. Set username for generated user 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 money field from default to random
some = mixer.blend('someapp.somemodel', money=mixer.RANDOM)

# Generate SomeModel from SomeApp and skip the generation of money field
some = mixer.blend('someapp.somemodel', money=mixer.SKIP)

# Generate 5 SomeModel's instances and take company field's values from custom generator
some_models = mixer.cycle(5).blend('somemodel', company=(name for name in company_names))

Flask, Flask-SQLAlchemy

Quick example:

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

mixer.init_app(self.app)

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

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

# Generate an UserMessage and an User. Set username for generated user 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 money field from default to random
some = mixer.blend('project.models.SomeModel', money=mixer.RANDOM)

# Generate SomeModel from SomeApp and skip the generation of money field
some = mixer.blend('project.models.SomeModel', money=mixer.SKIP)

# Generate 5 SomeModel's instances and take company field's 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 workflow

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 workflow

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)
    username = 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')

Marshmallow workflow

Example usage:

from mixer.backend.marshmallow import mixer
import marshmallow as ma

class User(ma.Schema):
    created_at = ma.fields.DateTime(required=True)
    email = ma.fields.Email(required=True)
    first_name = ma.fields.String(required=True)
    last_name = ma.fields.String(required=True)
    username = ma.fields.String(required=True)

class Post(ma.Schema):
    title = ma.fields.String(required=True)
    author = ma.fields.Nested(User, required=True)

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 in database. For preventing this behavior init mixer manually:

from mixer.backend.django import Mixer

mixer = Mixer(commit=False)

Or you can temporary switch context use the mixer as context manager:

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

The mixer allows you to define generators for fields by manually. Quick example:

from mixer.main import mixer

class Test:
    id = int
    name = str

mixer.register(Test,
    name=lambda: 'John',
    id=lambda: str(mixer.faker.small_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.

It's also possible to unregister a middleware:

mixer.unregister_middleware(encrypt_password)

Locales

By default mixer uses 'en' locale. You could switch mixer default locale by creating your own mixer:

from mixer.backend.django import Mixer

mixer = Mixer(locale='it')
mixer.faker.name()          ## u'Acchisio Conte'

At any time you could switch mixer current locale:

mixer.faker.locale = 'cz'
mixer.faker.name()          ## u'Miloslava Urbanov\xe1 CSc.'

mixer.faker.locale = 'en'
mixer.faker.name()          ## u'John Black'

# Use the mixer context manager
mixer.faker.phone()         ## u'1-438-238-1116'
with mixer.ctx(locale='fr'):
    mixer.faker.phone()     ## u'08 64 92 11 79'

mixer.faker.phone()         ## u'1-438-238-1116'

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 mixer happens at Github: https://github.com/klen/mixer

Contributors

License

Licensed under a BSD license.

More Repositories

1

pylama

Code audit tool for python.
Python
1,048
star
2

py-frameworks-bench

Another benchmark for some python frameworks
Python
712
star
3

muffin

Muffin is a fast, simple and asyncronous web-framework for Python 3
Python
671
star
4

graphite-beacon

Simple alerting system for Graphite metrics
Python
453
star
5

django_markdown

Django markdown support and wysiwig
JavaScript
391
star
6

peewee_migrate

Simple migration engine for Peewee
Python
353
star
7

nvim-test

A Neovim wrapper for running tests
Lua
181
star
8

Flask-Foundation

Quick start with Flask
Python
154
star
9

nvim-config-local

Secure load local config files for neovim
Lua
150
star
10

aioauth-client

OAuth client for aiohttp
Python
141
star
11

atmark

Awk+Sed for humans
Python
69
star
12

python-scss

Python scss parser.
Python
67
star
13

dealer

Make some staff
Python
62
star
14

marshmallow-peewee

Peewee ORM integration with the marshmallow (de)serialization library.
Python
59
star
15

zeta-library

Css, scss, js parser and linker. Also framework for working with static files
Python
52
star
16

Flask-Collect

Collect static files in flask application
Python
49
star
17

makesite

makesite is a collection of scripts for deploying and managing web projects
Python
47
star
18

flask-pw

Peewee ORM integration for Flask framework
Python
45
star
19

peewee-aio

Async support for Peewee ORM
Python
45
star
20

adrest

Another django rest framework
Python
42
star
21

.vim

my vim settings
Vim Script
39
star
22

django-netauth

django auth backend
Python
36
star
23

asgi-tools

Tools to build ASGI apps
Python
35
star
24

pomodoro-tracker-locales

Language files
24
star
25

asgi-babel

Adds internationalization (i18n) support to ASGI applications (Asyncio/Trio)
Python
24
star
26

http-router

A simple router for HTTP applications
Python
24
star
27

rope-vim

Pathogen compatable ropevim plugin. Dont need install rope libs in system.
Python
21
star
28

bottle-peewee

Integrate Peewee ORM to Bottle framework
Python
19
star
29

muffin-admin

Admin interface for Muffin Framework
Python
18
star
30

mahjong.horneds.com

Riichi Mahjong Scores Trainer
CoffeeScript
18
star
31

flask-restler

Yet another REST library for Flask
Python
15
star
32

unite-radio.vim

Play radio stations in your VIM
Vim Script
15
star
33

aio-databases

Async Support for various databases
Python
14
star
34

django-gitrevision

Django git revision, simple add current git revision to request object for use in tempaltes and views.
Python
14
star
35

imgproxy

Python support for ImgProxy image processing server (https://imgproxy.net)
Python
14
star
36

pytest-aio

Is a simple pytest plugin for testing async python code
Python
12
star
37

klen.github.io

My github powered site.
HTML
12
star
38

muffin-rest

REST helpers for Muffin Framework
Python
12
star
39

aio-peewee

Tools to make Peewee work when using Asyncio
Python
11
star
40

muffin-jinja2

Jinja2 templates for Muffin framework
Python
11
star
41

donald

Make asyncio great again
Python
10
star
42

pypika-orm

Async ORM based on PyPika
Python
9
star
43

bottle-login

Implement users' sessions in Bottle framework
Python
9
star
44

asgi-prometheus

Support Prometheus metrics for ASGI applications
Python
9
star
45

muffin-peewee

Peewee integration to Muffin framework
Python
9
star
46

asgi-sessions

Signed Cookie-Based HTTP sessions for ASGI applications
Python
8
star
47

muffin-session

Session for Muffin Framework
Python
7
star
48

muffin-mongo

MongoDB support for Muffin framework
Python
7
star
49

starter

Create the skeleton for new projects.
Python
6
star
50

muffin-redis

Redis support for Muffin framework
Python
6
star
51

muffin-sentry

Sentry integration to Muffin Framework.
Python
5
star
52

pyserve

Serve local dirs (human version)
Python
5
star
53

muffin-example

Example Muffin application
Python
5
star
54

dotfiles

kk .dotfiles / use it for your own risks
Shell
4
star
55

bottle-jade

Provide Jade templates for Bottle framework
Python
4
star
56

django-gishelper

Useful commands for geodjango
Python
4
star
57

muffin-oauth

OAuth1/2 support for Muffin framework.
Python
4
star
58

muffin-databases

Async support for a range of databases for Muffin Framework
Python
4
star
59

knocker

A self contained service to make HTTP calls
Python
4
star
60

muffin-debugtoolbar

Debug Toolbar for Muffin applications
JavaScript
4
star
61

muffin-babel

Extension to Muffin that adds localization support with help of babel.
Python
3
star
62

inirama

Simple INI parser
Python
3
star
63

pylama_pylint

Pylint support for pylama.
Makefile
3
star
64

starlette-plugins

Create Starlette Plugins easier
Python
3
star
65

example_tornadio_project

Sources for http://klen.github.com/tornadio_socket-io-ru.html
Python
3
star
66

muffin-peewee-aio

Peewee integration to Muffin framework with async support
Python
2
star
67

redux-axios-reducers

Redux Reducers for Axios
CoffeeScript
2
star
68

pytest-redislite

Pytest plugin for testing code using Redi
Python
2
star
69

bottle-manage

Script manager for bottle framework.
Python
2
star
70

muffin-grpc

GRPC Support for Muffin Framework
Python
2
star
71

fquest

ZeroQuest lazy RPG. Moscow Facebook Hackday.
Python
2
star
72

aio-apiclient

Simple Asyncio Client for any HTTP APIs
Python
2
star
73

muffin-jade

Jade templates for Muffin Framework
Python
2
star
74

signalbus

Simple and small library to broadcast signals with typing support
Python
2
star
75

muffin-metrics

Send application metrics to Graphite
Python
2
star
76

hvim

Haskell mode for vim
Vim Script
2
star
77

Flask-jsonrpc-example

Some flask experements
Python
2
star
78

starlette-views

A helper to make views faster with Starlette
Python
2
star
79

hydrogenjs

Simple MVC system for atomjs
JavaScript
1
star
80

simpletree

Fastest and simplest tree implementations for Django
Python
1
star
81

filler

Simple game on javascript and canvas.
JavaScript
1
star
82

modconfig

Simple hierarchic configuration manager for apps
Python
1
star
83

sailplay

Python client for API sailplay.ru
Python
1
star
84

zeta-libs

Frameworks repo for zetalibrary
JavaScript
1
star
85

redux-code

Yet another creators library
TypeScript
1
star
86

tweetchi

Python
1
star
87

muffin-prometheus

Prometheus metrics exporter for Muffin framework
Python
1
star
88

fl

Forbidden lands russian generator
TypeScript
1
star