aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README1
-rw-r--r--dev_requirements.txt1
-rw-r--r--docs/changelog.rst1
-rw-r--r--docs/orms.rst23
-rw-r--r--factory/__init__.py1
-rw-r--r--factory/mongoengine.py45
-rw-r--r--tests/__init__.py1
-rw-r--r--tests/test_mongoengine.py62
8 files changed, 135 insertions, 0 deletions
diff --git a/README b/README
index dee01b4..bc5b55f 100644
--- a/README
+++ b/README
@@ -219,6 +219,7 @@ factory_boy has specific support for a few ORMs, through specific :class:`~facto
* Django, with :class:`~factory.django.DjangoModelFactory`
* Mogo, with :class:`~factory.mogo.MogoFactory`
+* MongoEngine, with :class:`~factory.mongoengine.MongoEngineFactory`
* SQLAlchemy, with :class:`~factory.alchemy.SQLAlchemyModelFactory`
Contributing
diff --git a/dev_requirements.txt b/dev_requirements.txt
index f799ef8..e828644 100644
--- a/dev_requirements.txt
+++ b/dev_requirements.txt
@@ -2,3 +2,4 @@ coverage
Django
Pillow
sqlalchemy
+mongoengine \ No newline at end of file
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 75f1c41..0367246 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -15,6 +15,7 @@ ChangeLog
- The :class:`~factory.django.DjangoModelFactory` now supports the ``FACTORY_FOR = 'myapp.MyModel'``
syntax, making it easier to shove all factories in a single module (:issue:`66`).
+ - Adding factory support for mongoengine with :class:`~factory.mongoengine.MongoEngineFactory`.
.. _v2.1.2:
diff --git a/docs/orms.rst b/docs/orms.rst
index 8215fe6..74c5c62 100644
--- a/docs/orms.rst
+++ b/docs/orms.rst
@@ -163,6 +163,29 @@ factory_boy supports `Mogo`_-style models, through the :class:`MogoFactory` clas
* :func:`~factory.Factory.create()` builds an instance through ``new()`` then
saves it.
+
+MongoEngine
+----
+
+.. currentmodule:: factory.mongoengine
+
+factory_boy supports `MongoEngine`_-style models, through the :class:`MongoEngineFactory` class.
+
+`mongoengine`_ is a wrapper around the ``pymongo`` library for MongoDB.
+
+.. _mongoengine:: http://mongoengine.org/
+
+.. class:: MongoEngineFactory(factory.Factory)
+
+ Dedicated class for `MongoEngine`_ models.
+
+ This class provides the following features:
+
+ * :func:`~factory.Factory.build()` calls a model's ``__init__`` method
+ * :func:`~factory.Factory.create()` builds an instance through ``__init__`` then
+ saves it.
+
+
SQLAlchemy
----------
diff --git a/factory/__init__.py b/factory/__init__.py
index ea45a9a..a27eb40 100644
--- a/factory/__init__.py
+++ b/factory/__init__.py
@@ -38,6 +38,7 @@ from .base import (
use_strategy,
)
+# Backward compatibility; this should be removed soon.
from .mogo import MogoFactory
from .django import DjangoModelFactory
diff --git a/factory/mongoengine.py b/factory/mongoengine.py
new file mode 100644
index 0000000..8cd3a67
--- /dev/null
+++ b/factory/mongoengine.py
@@ -0,0 +1,45 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2010 Mark Sandstrom
+# Copyright (c) 2011-2013 Raphaƫl Barrois
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+
+from __future__ import unicode_literals
+
+
+"""factory_boy extensions for use with the mongoengine library (pymongo wrapper)."""
+
+
+from . import base
+
+
+class MongoEngineFactory(base.Factory):
+ """Factory for mongoengine objects."""
+ ABSTRACT_FACTORY = True
+
+ @classmethod
+ def _build(cls, target_class, *args, **kwargs):
+ return target_class(*args, **kwargs)
+
+ @classmethod
+ def _create(cls, target_class, *args, **kwargs):
+ instance = target_class(*args, **kwargs)
+ instance.save()
+ return instance
diff --git a/tests/__init__.py b/tests/__init__.py
index 9960382..d823a87 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -9,3 +9,4 @@ from .test_fuzzy import *
from .test_using import *
from .test_utils import *
from .test_alchemy import *
+from .test_mongoengine import *
diff --git a/tests/test_mongoengine.py b/tests/test_mongoengine.py
new file mode 100644
index 0000000..e078aed
--- /dev/null
+++ b/tests/test_mongoengine.py
@@ -0,0 +1,62 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2013 Romain Command&
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+"""Tests for factory_boy/SQLAlchemy interactions."""
+
+import factory
+from .compat import unittest
+
+
+try:
+ import mongoengine
+except ImportError:
+ mongoengine = None
+
+if mongoengine:
+ from factory.mongoengine import MongoEngineFactory
+
+ class Person(mongoengine.Document):
+ name = mongoengine.StringField()
+
+ class PersonFactory(MongoEngineFactory):
+ FACTORY_FOR = Person
+
+ name = factory.Sequence(lambda n: 'name%d' % n)
+
+
+
+@unittest.skipIf(mongoengine is None, "mongoengine not installed.")
+class MongoEngineTestCase(unittest.TestCase):
+
+ def setUp(self):
+ mongoengine.connect('factory_boy_test')
+
+ def test_build(self):
+ std = PersonFactory.build()
+ self.assertEqual('name0', std.name)
+ self.assertIsNone(std.id)
+
+ def test_creation(self):
+ std1 = PersonFactory.create()
+ self.assertEqual('name1', std1.name)
+ self.assertIsNotNone(std1.id)
+
+