Giter VIP home page Giter VIP logo

model_mommy's Introduction

Creating objects for testing shouldn't hurt

model_mommy is a tool for creating objects for testing in Django, inspired in ruby's ObjectDaddy and FactoryGirl. It generate the values according with the field type, but i will add support to custom values as well.

#Installing

pip install model_mommy

Basic Usage:

If you have a model like this in your app:

class Kid(models.Model):
    happy = models.BooleanField()
    name = models.CharField(max_length=30)
    age = models.IntegerField()
    bio = models.TextField()
    wanted_games_qtd = models.BigIntegerField()
    birthday = models.DateField()
    appointment = models.DateTimeField()

just call the mommy =):

from model_mommy import mommy
from model_mommy.models import Kid

kid = mommy.make_one(Kid)

and your object is created! No boring attributes passing like 'foobar' every damn time.

Import every model you need can be very boring, so, you can also call mommy using a string like this:

kid = mommy.make_one('model_mommy.Kid')

Note that you must use 'app_label.model_name'. The model_name is case insensitive.

mommy also handles relationships. Suppose the kid has a dog:

class Dog(models.Model):
    owner = models.ForeignKey('Kid')

when you do:

rex = mommy.make_one(Dog)

it will also create the Kid, automatically.

You can also specify values for one or more attribute.

another_kid = mommy.make_one(Kid, age = 3)
assert another_kid.age == 3

But, if don't need a persisted object, mommy can handle this for you as well:

from model_mommy import mommy
from model_mommy.models import Kid

kid = mommy.prepare_one(Kid)

It works like make_one, but like was said, it doesn't persist the instance.

How mommy behaves?

model_mommy skips fields with null=True or blank=True. Also if the field has a default value, mommy will use it.

When you shouldn't let mommy do the things for you:

If you have a field that has any special validation, you should set the value by yourself. model_mommy should be used to handle the fields that doesn't have relation with the test that you're doing at the moment and don't require special validation(like unique, etc), but still required in order to create the object.

Doubts? Loved it? Hated it? Suggestions?

Mail us!:

  • vanderson.mota at gmail dot com
  • italo.maia at gmail dot com

##Currently supports the fields: CharField, TextField, FloatField, ForeignKey, Date and DateTimeField, BooleanField, URLField, and all the integer-type Fields

model_mommy's People

Contributors

vandersonmota avatar italomaia avatar douglas avatar selwin avatar aoqfonseca avatar rodrigomanhaes avatar hltbra avatar andrewsmedina avatar

Stargazers

Gregory Jones avatar

Watchers

Gregory Jones avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.