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
 
@@ -106,49 +106,48 @@ class ToSQLAlchemy(Importer):
 
            return query.one()
 
        except NoResultFound:
 
            pass
 

	
 
    def create_object(self, key, host_data):
 
        """
 
        Create and return a new local object for the given key, fully populated
 
        from the given host data.  This may return ``None`` if no object is
 
        created.
 

	
 
        Note that this also adds the new object to the local database session.
 
        """
 
        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)
 

	
 
    def cache_local_data(self, host_data=None):
 
        """
 
        Cache all local objects and data using SA ORM.
 
        """
 
        return self.cache_model(self.model_class, key=self.get_cache_key,
 
                                # omit_duplicates=True,
 
                                query_options=self.cache_query_options(),
 
                                normalizer=self.normalize_cache_object)
 

	
 
    def cache_query_options(self):
 
        """
rattail/tests/importing/test_sqlalchemy.py
Show inline comments
 
@@ -81,59 +81,61 @@ class TestToSQLAlchemy(TestCase):
 

	
 
        # simple key
 
        importer = self.make_importer(key='id', session=self.session)
 
        widget = importer.get_single_local_object((1,))
 
        self.assertEqual(widget.id, 1)
 
        self.assertEqual(widget.description, "Main Widget")
 

	
 
        # compound key
 
        importer = self.make_importer(key=('id', 'description'), session=self.session)
 
        widget = importer.get_single_local_object((1, "Main Widget"))
 
        self.assertEqual(widget.id, 1)
 
        self.assertEqual(widget.description, "Main Widget")
 

	
 
        # 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'])
 

	
 
    def test_cache_local_data(self):
 
        importer = self.make_importer(key='id', session=self.session)
 
        cache = importer.cache_local_data()
 
        self.assertEqual(len(cache), 3)
 
        for i in range(1, 4):
 
            self.assertIn((i,), cache)
 
            cached = cache[(i,)]
 
            self.assertIsInstance(cached, dict)
 
            self.assertIsInstance(cached['object'], Widget)
 
            self.assertEqual(cached['object'].id, i)
 
            self.assertEqual(cached['object'].description, WIDGETS[i-1]['description'])
 
            self.assertIsInstance(cached['data'], dict)
0 comments (0 inline, 0 general)