From 9246fa6d26ca655c02ae37bbfc389d9f34dfba16 Mon Sep 17 00:00:00 2001 From: Raphaƫl Barrois Date: Sun, 31 May 2015 10:57:53 +0100 Subject: Improve ORM layer import paths (Closes #186). You may now use the following code: import factory factory.alchemy.SQLAlchemyModelFactory factory.django.DjangoModelFactory factory.mongoengine.MongoEngineFactory --- docs/changelog.rst | 2 ++ docs/orms.rst | 31 ++++++++++++++++++++++++++++++- factory/__init__.py | 12 +++++++++--- factory/alchemy.py | 1 - 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index c871ce8..eea38c5 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -14,6 +14,8 @@ ChangeLog - :class:`factory.Iterator` no longer begins iteration of its argument at import time, thus allowing to pass in a lazy iterator such as a Django queryset (i.e ``factory.Iterator(models.MyThingy.objects.all())``). + - Simplify imports for ORM layers, now available through a simple ``factory`` import, + at ``factory.alchemy.SQLAlchemyModelFactory`` / ``factory.django.DjangoModelFactory`` / ``factory.mongoengine.MongoEngineFactory``. *Bugfix:* diff --git a/docs/orms.rst b/docs/orms.rst index bbe91e6..26390b5 100644 --- a/docs/orms.rst +++ b/docs/orms.rst @@ -273,6 +273,34 @@ factory_boy supports `MongoEngine`_-style models, through the :class:`MongoEngin This feature makes it possible to use :class:`~factory.SubFactory` to create embedded document. +A minimalist example: + +.. code-block:: python + + import mongoengine + + class Address(mongoengine.EmbeddedDocument): + street = mongoengine.StringField() + + class Person(mongoengine.Document): + name = mongoengine.StringField() + address = mongoengine.EmbeddedDocumentField(Address) + + import factory + + class AddressFactory(factory.mongoengine.MongoEngineFactory): + class Meta: + model = Address + + street = factory.Sequence(lambda n: 'street%d' % n) + + class PersonFactory(factory.mongoengine.MongoEngineFactory): + class Meta: + model = Person + + name = factory.Sequence(lambda n: 'name%d' % n) + address = factory.SubFactory(AddressFactory) + SQLAlchemy ---------- @@ -327,8 +355,9 @@ A (very) simple example: Base.metadata.create_all(engine) + import factory - class UserFactory(SQLAlchemyModelFactory): + class UserFactory(factory.alchemy.SQLAlchemyModelFactory): class Meta: model = User sqlalchemy_session = session # the SQLAlchemy session object diff --git a/factory/__init__.py b/factory/__init__.py index 80eb6a8..843cf99 100644 --- a/factory/__init__.py +++ b/factory/__init__.py @@ -40,9 +40,6 @@ from .base import ( use_strategy, ) -# Backward compatibility; this should be removed soon. -from .mogo import MogoFactory -from .django import DjangoModelFactory from .faker import Faker from .declarations import ( @@ -84,3 +81,12 @@ from .helpers import ( post_generation, ) +# Backward compatibility; this should be removed soon. +from . import alchemy +from . import django +from . import mogo +from . import mongoengine + +MogoFactory = mogo.MogoFactory +DjangoModelFactory = django.DjangoModelFactory + diff --git a/factory/alchemy.py b/factory/alchemy.py index 6408393..20da6cf 100644 --- a/factory/alchemy.py +++ b/factory/alchemy.py @@ -19,7 +19,6 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. from __future__ import unicode_literals -from sqlalchemy.sql.functions import max from . import base -- cgit v1.2.3