Skip to content

berinhard/mongoengine_fuel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

I guess you need some fuel before start your mongoengine, right?

mongoengine_fuel is a tool for creating objects for testing in Python projetcs which uses mongoengine ORM to talk to MongoDB. mongoengine_fuel is inspired in model_mommy, a tool with the same purpose but for Django projects.

#Installing

pip install mongoengine_fuel

Simple example:

Here is our pretty new car document and a person document:

from mongoengine import *

class Person(Document):
    name = StringField()
    age = IntField()

    def __unicode__(self):
        return u'%s - %d years' % (self.name, self.age)

class Car(Document):
    wheels = IntField()
    name = StringField()
    max_speed = DecimalField()
    owner = ReferenceField(Person)

    def __unicode__(self):
        return u'Car --> name: %s, wheels: %d, max_speed:%f, owner:%s' \
            % (self.name, self.wheels, self.max_speed, self.owner)

Now, just add some fuel:

from mongoengine_fuel import MongoFuel
from your_models import Car, Person

car = MongoFuel.create_one(Car)

And now you can ride with your car! Note that mongoengine_fuel already handles relationships like ReferenceField just like the example above. It creates an Person's instance automatically for you and persists both documents.

Please, don't polute my database

If you don't want the behaviour mentioned above, you just need to ask to mongoengine_fuel to don't save your document on the database like this:

car = MongoFuel.create_one(Car, persists=False)

I want more fuel!

If you want to create more than one instance of your document, you can use create_many like this:

cars = MongoFuel.create_many(Car)

persons = MongoFuel.create_many(Person, instances=3)

create_many also accepts the persists parameter.

This call will just return an Car's instance without saving it. The Person's instance which is created for you isn't save either.

I wanna a specific value for a field

If you need a particular value for a field inside your document, you can force a value to it. You just need to give it as a parameter. Like this:

richard = Person.objects.create(name='Richard', age=30)
car = MongoFuel.create_one(Car, owner=richard)

You will see that a random Car object is created, but the owner is the one that you specified.

What about embedded documents?

mongoengine_fuel hanle with these guys to. It creates randoms embedded documents for you with exactly the same usage as common documents.

mongoengine's fields supported:

  • BooleanField
  • StringField
  • FloatField
  • DecimalField
  • IntField
  • URLField
  • EmailField
  • ReferenceField
  • EmbeddedDocumentField
  • ListField

How to contribute?

  1. Fork it.
  2. If you use virtualenv, clone your fork to your virtuaenvs dir. If you don't use it, you should do...
  3. Inside the project dir, run python bootstrap/bootstrap.py
  4. Bootstrap script will create a virtualenv for mongoengine_fuel and install all dependencies. So, now, you just need to code =)

Any question, suggestion, applauses or tomatos?

Mail me: falecomigo at bernardofontes dot net

About

A factory for mongoengine documents

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published