Git Product home page Git Product logo

mongoengine_fuel's Introduction

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

mongoengine_fuel's People

Watchers

 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.