Changeset - c3461507a792
[Not reviewed]
0 2 0
Lance Edgar - 8 years ago 2016-05-23 14:27:35
ledgar@sacfoodcoop.com
Fix delete behavior for `ToSQLAlchemy` importer, fix tests

No need to expunge the object when deleting, and that was making tests
break in a way that suggested production breakage also..?
2 files changed with 5 insertions and 4 deletions:
0 comments (0 inline, 0 general)
rattail/importing/sqlalchemy.py
Show inline comments
 
@@ -118,25 +118,24 @@ class ToSQLAlchemy(Importer):
 
        obj = super(ToSQLAlchemy, self).create_object(key, host_data)
 
        if obj:
 
            self.session.add(obj)
 
            return obj
 

	
 
    def delete_object(self, obj):
 
        """
 
        Delete the given object from the local system (or not), and return a
 
        boolean indicating whether deletion was successful.  Default logic will
 
        truly delete and expunge the local object from the session.
 
        """
 
        self.session.delete(obj)
 
        self.session.expunge(obj)
 
        return True
 

	
 
    def cache_model(self, model, **kwargs):
 
        """
 
        Convenience method which invokes :func:`rattail.db.cache.cache_model()`
 
        with the given model and keyword arguments.  It will provide the
 
        ``session`` and ``progress`` parameters by default, setting them to the
 
        importer's attributes of the same names.
 
        """
 
        session = kwargs.pop('session', self.session)
 
        kwargs.setdefault('progress', self.progress)
 
        return cache.cache_model(session, model, **kwargs)
rattail/tests/importing/test_sqlalchemy.py
Show inline comments
 
@@ -93,35 +93,37 @@ class TestToSQLAlchemy(TestCase):
 

	
 
        # widget not found
 
        importer = self.make_importer(key='id', session=self.session)
 
        self.assertIsNone(importer.get_single_local_object((42,)))
 

	
 
        # multiple widgets found
 
        importer = self.make_importer(key='description', session=self.session)
 
        self.assertRaises(MultipleResultsFound, importer.get_single_local_object, ("Other Widget",))
 

	
 
    def test_create_object(self):
 
        importer = self.make_importer(key='id', session=self.session)
 
        widget = importer.create_object((42,), {'id': 42, 'description': "Latest Widget"})
 
        self.assertFalse(self.session.new or self.session.dirty or self.session.deleted) # i.e. has been flushed
 
        self.assertIn(widget, self.session) # therefore widget has been flushed and would be committed
 
        self.assertIn(widget, self.session)
 
        self.assertIn(widget, self.session.new)
 
        self.assertEqual(widget.id, 42)
 
        self.assertEqual(widget.description, "Latest Widget")
 

	
 
    def test_delete_object(self):
 
        widget = self.session.query(Widget).get(1)
 
        self.assertIn(widget, self.session)
 
        importer = self.make_importer(session=self.session)
 
        self.assertTrue(importer.delete_object(widget))
 
        self.assertNotIn(widget, self.session)
 
        self.assertIn(widget, self.session)
 
        self.assertIn(widget, self.session.deleted)
 
        self.session.flush()
 
        self.assertIsNone(self.session.query(Widget).get(1))
 

	
 
    def test_cache_model(self):
 
        importer = self.make_importer(key='id', session=self.session)
 
        cache = importer.cache_model(Widget, key='id')
 
        self.assertEqual(len(cache), 3)
 
        for i in range(1, 4):
 
            self.assertIn(i, cache)
 
            self.assertIsInstance(cache[i], Widget)
 
            self.assertEqual(cache[i].id, i)
 
            self.assertEqual(cache[i].description, WIDGETS[i-1]['description'])
 

	
0 comments (0 inline, 0 general)