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
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.
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)
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.
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.
mongoengine_fuel hanle with these guys to. It creates randoms embedded documents for you with exactly the same usage as common documents.
BooleanField
StringField
FloatField
DecimalField
IntField
URLField
EmailField
ReferenceField
EmbeddedDocumentField
ListField
- Fork it.
- If you use virtualenv, clone your fork to your virtuaenvs dir. If you don't use it, you should do...
- Inside the project dir, run
python bootstrap/bootstrap.py
- Bootstrap script will create a virtualenv for mongoengine_fuel and install all dependencies. So, now, you just need to code =)
Mail me: falecomigo at bernardofontes dot net