Commits

ollyc  committed a3edb43

Make StormFactory flush/commit behavior configurable

  • Participants
  • Parent commits 02c8f57

Comments (0)

Files changed (2)

File src/test_toffee.py

 
 class TestStormFactory(object):
 
+    def setup(self):
+        self.store = Mock()
+        self.factory = StormFactory.configure(lambda: self.store)
+
     def test_it_calls_store_add_and_remove(self):
-        store = Mock()
-        factory = StormFactory.configure(lambda: store)
+        with Fixture(x=self.factory(int)):
+            self.store.add.assert_called_once_with(0)
+            assert self.store.remove.called is False
+        self.store.remove.assert_called_once_with(0)
 
-        with Fixture(x=factory(int)):
-            store.add.assert_called_once_with(0)
-            assert store.remove.called is False
-        store.remove.assert_called_once_with(0)
+    def test_it_doesnt_commit_by_default(self):
+        Fixture(x=self.factory(int)).setup()
+        assert self.store.commit.called is False
+
+    def test_it_commits_if_requested(self):
+        Fixture(x=self.factory(int)).setup(commit=True)
+        assert self.store.commit.called is True
+
+    def test_it_flushes_by_default(self):
+        Fixture(x=self.factory(int)).setup()
+        assert self.store.flush.called is True
+
+    def test_it_doesnt_flushes_if_requested(self):
+        Fixture(x=self.factory(int)).setup(flush=False)
+        assert self.store.flush.called is False
 
 
 class TestCallFactory(object):

File src/toffee.py

 
         class fixture(Fixture):
             user = Factory(models.User, ...)
+
+    By default, StormFactory call ``store.flush`` but not ``store.commit``.
+    Change this behaviour by passing factory options to setup::
+
+        fixture.setup(flush=False)
+        fixture.setup(commit=True)
+
     """
 
     #: Callable that can retrieve Storm's store object.
     def setup_complete(cls, context, created):
         store = cls._getstore_cached(context)
         if store:
-            store.flush()
+            if context.factoryoptions.get('flush', True):
+                store.flush()
+            if context.factoryoptions.get('commit', True):
+                store.commit()
 
 
 class ArgumentGenerator(object):