Commits

ollyc committed 02c8f57

added factoryoptions and make this avaible to create/destroy

factoryoptions allows factories to check for factory-specific
configuration and also to maintain state throughout the fixture
setup/teardown cycle

  • Participants
  • Parent commits cb0471e

Comments (0)

Files changed (2)

src/test_toffee.py

             anonymous_object = mf.x.s
             mf.teardown()
 
-            destructor.assert_any_call(anonymous_object)
+            destructor.assert_any_call(mf, anonymous_object)
 
     def test_factories_can_be_reused(self):
 
         args = tuple(context.resolve(a) for a in self.args)
         kwargs = dict((k, context.resolve(v)) for k, v in self.kwargs.items())
 
-        ob = self._create_object(args, kwargs)
+        ob = self._create_object(context, args, kwargs)
         ob = self._configure_object(ob, context)
         return ob
 
-    def destroy_object(self, ob):
-        return self._destroy_object(ob)
+    def destroy_object(self, context, ob):
+        return self._destroy_object(context, ob)
 
-    def _create_object(self, args, kwargs):
+    def _create_object(self, context, args, kwargs):
         return self.what(*args, **kwargs)
 
-    def _destroy_object(self, ob):
+    def _destroy_object(self, context, ob):
         pass
 
     def _configure_object(self, ob, context):
 
 class DjangoFactory(Factory):
 
-    def _create_object(self, args, kwargs):
+    def _create_object(self, context, args, kwargs):
         ob = self.what.objects.create(*args, **kwargs)
         ob.save()
         return ob
 
-    def _destroy_object(self, ob):
+    def _destroy_object(self, context, ob):
         ob.delete()
 
 
     #: Callable that can retrieve Storm's store object.
     getstore = lambda: None
 
+    storekey = '_StormFactory_store'
+
     @classmethod
     def configure(cls, getstore):
         return type('StormFactory', (StormFactory,),
                     {'getstore': staticmethod(getstore)})
 
-    def _create_object(self, args, kwargs):
-        store = self.getstore()
+    @classmethod
+    def _getstore_cached(cls, context):
+        try:
+            return context.factoryoptions[cls.storekey]
+        except KeyError:
+            return context.factoryoptions.setdefault(cls.storekey,
+                                                     cls.getstore())
+
+    def _create_object(self, context, args, kwargs):
+        store = self._getstore_cached(context)
         ob = self.what.__new__(self.what)
         for item, value in kwargs.items():
             setattr(ob, item, value)
             store.add(ob)
         return ob
 
-    def _destroy_object(self, ob):
-        store = self.getstore()
+    def _destroy_object(self, context, ob):
+        store = self._getstore_cached(context)
         if store is not None:
             store.remove(ob)
 
     @classmethod
     def setup_complete(cls, context, created):
-        store = cls.getstore()
+        store = cls._getstore_cached(context)
         if store:
             store.flush()
 
             self.factory_names[v] = k
         self.d.update(factories)
 
-    def setup(self, force=False):
+    def setup(self, force=False, **factoryoptions):
+        """
+        :param force: allow calls to setup to be nested
+        :param **factoryoptions: any factory specific options, for example
+                                 passing flags such as ``commit=True``. See
+                                 individual factory subclasses for information
+        """
         if self.created and not force:
             raise Exception("setup() has already been called on this fixture. "
                             "Call teardown first, or use setup(force=True).")
+
+        self.factoryoptions = factoryoptions
         self.o = AttrDict()
         self.created = []
         self.argument_generators = {}
             name, ob, factory = self.created.pop()
             if name is not None:
                 del self.o[name]
-            factory.destroy_object(ob)
+            factory.destroy_object(self, ob)
+        self.factoryoptions = {}
 
-    def __enter__(self):
-        return self.setup()
+    def __enter__(self, *args, **kwargs):
+        return self.setup(*args, **kwargs)
 
     def __exit__(self, type, value, tb):
         if type: