Commits

Anonymous committed e3d0d56

Added SQLAlchemyFactory

Comments (0)

Files changed (1)

     """
 
 
+class SQLAlchemyFactory(_DataMapperFactory):
+    """\
+
+    Typically you will need to configure this at the start of your test code,
+    like so::
+
+        Session = sessionmaker(...)
+        Factory = SQLAlchemyFactory.configure(Session)
+
+    You can then use Factory as normal::
+
+        class fixture(Fixture):
+            user = Factory(models.User, ...)
+
+    By default, SQLAlchemyFactory calls ``session.flush`` but not
+    ``session.commit``.
+    Change this behaviour by passing factory options to setup::
+
+        fixture.setup(flush=False)
+        fixture.setup(commit=True)
+
+    Alternatively you can supply factory options in the fixture class:
+
+        class fixture(Fixture):
+            factoryoptions = {'commit': True}
+
+    """
+
+    def _mapper_remove(self, context, ob):
+        """
+        Ask the datamapper to remove ``ob`` from the data store
+        """
+        mapper = self._getmapper_cached(context)
+        if mapper is not None:
+            mapper.delete(ob)
+
+    def _create_object(self, context, args, kwargs):
+        from sqlalchemy.orm.attributes import manager_of_class
+        ob = manager_of_class(self.what).new_instance()
+        for item, value in kwargs.items():
+            setattr(ob, item, value)
+        self._mapper_add(context, ob)
+        return ob
+
+
 class ArgumentGenerator(object):
     """
     A callable that dynamically generates factory arguments
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.