Lets you create fixtures based on python dicts. Supports string references between documents. Has been inspired by yii2 fixtures.
- Free software: MIT license
As an example, I'm going to assume we have a application with the following directory structure.
/myapp
__init__.py
models.py
/tests
/data
users.py
posts.py
fixtures.py
test_something.py
Simple mongoengine documents in models.py
from mongoengine import Document, fields
class User(Document):
first_name = fields.StringField(required=True)
last_name = fields.StringField(required=True)
email = fields.StringField(required=True)
birthday = fields.DateTimeField()
class Post(Document):
title = fields.StringField(required=True)
text = fields.StringField(required=True)
author = fields.ObjectIdField(required=True)
created_at = fields.DateTimeField()
Models data are stored inside fixture class (added in 1.3.0)
from fixtures_mongoengine import Fixture
from myapp.models import User, Post
class FixtureUser(Fixture):
document_class = User
data = {
'user1': {
'first_name': 'Joyce',
'last_name': 'Ray',
'email': '[email protected]',
'birthday': datetime.date(1983, 7, 12)
},
'user2': {
'first_name': 'Amy',
'last_name': 'Myers',
'email': '[email protected]',
'birthday': datetime.date(1987, 11, 27)
}
}
class FixturePost(Fixture):
document_class = Post
depends = {
'users': FixtureUser
}
data = {
'post1': {
'title': 'Sherlock Gets A Musical Parody',
'text': 'Sherlock fans still have a way to go before Season 4 of the BBC drama finally arrives.',
'author': '{users.user1}'
},
'post2': {
'title': 'NASA prepares to sample an asteroid',
'text': 'On September 8, NASA will launch its first sample return mission to an asteroid.',
'author': '{users.user2}',
}
}
Models data are stored in separate files.
from fixtures_mongoengine import Fixture
from myapp.models import User, Post
class FixtureUser(Fixture):
document_class = User
data_file = 'myapp.tests.data.users'
class FixturePost(Fixture):
document_class = Post
depends = {
'users': FixtureUser
}
data_file = 'myapp.tests.data.posts'
Add fixtures data
users.py
import datetime as datetime
fixture_data = {
'user1': {
'first_name': 'Joyce',
'last_name': 'Ray',
'email': '[email protected]',
'birthday': datetime.date(1983, 7, 12)
},
'user2': {
'first_name': 'Amy',
'last_name': 'Myers',
'email': '[email protected]',
'birthday': datetime.date(1987, 11, 27)
}
}
posts.py
fixture_data = {
'post1': {
'title': 'Sherlock Gets A Musical Parody',
'text': 'Sherlock fans still have a way to go before Season 4 of the BBC drama finally arrives.',
'author': '{users.user1}'
},
'post2': {
'title': 'NASA prepares to sample an asteroid',
'text': 'On September 8, NASA will launch its first sample return mission to an asteroid.',
'author': '{users.user2}',
}
}
Make sure the app that you're testing is initialized with the proper configuration (including db connection).
import unittest
from myapp.tests.fixtures import FixturePost
class SomeTestCase(unittest.TestCase, FixturesMixin):
fixtures_conf = {
'posts': FixturePost
}
def test_something(self):
assert len(Post.objects()) == 2
assert len(User.objects()) == 2
assert self.posts['post1'].title == 'Sherlock Gets A Musical Parody'
author = User.objects(pk=self.posts['post1'].author).first()
assert author.last_name == 'Ray'