Changeset - 3d85857ea1a1
[Not reviewed]
0 1 0
Lance Edgar (lance) - 4 years ago 2020-07-19 20:11:14
lance@edbob.org
Fix a test per recent importer change
1 file changed with 6 insertions and 1 deletions:
0 comments (0 inline, 0 general)
rattail/tests/importing/test_importers.py
Show inline comments
 
@@ -97,193 +97,198 @@ class TestImporter(TestCase):
 
        self.assertFalse(importer.delete)
 
        importer.import_data(host_data=[], delete=True)
 
        self.assertTrue(importer.delete)
 

	
 
    def test_get_host_objects(self):
 
        importer = importers.Importer()
 
        objects = importer.get_host_objects()
 
        self.assertEqual(objects, [])
 

	
 
    def test_cache_local_data(self):
 
        importer = importers.Importer()
 
        self.assertRaises(NotImplementedError, importer.cache_local_data)
 

	
 
    def test_get_local_object(self):
 
        importer = importers.Importer()
 
        self.assertFalse(importer.caches_local_data)
 
        self.assertRaises(NotImplementedError, importer.get_local_object, None)
 

	
 
        someobj = object()
 
        with patch.object(importer, 'get_single_local_object', Mock(return_value=someobj)):
 
            obj = importer.get_local_object('somekey')
 
        self.assertIs(obj, someobj)
 

	
 
        importer.caches_local_data = True
 
        importer.cached_local_data = {'somekey': {'object': someobj, 'data': {}}}
 
        obj = importer.get_local_object('somekey')
 
        self.assertIs(obj, someobj)
 

	
 
    def test_get_single_local_object(self):
 
        importer = importers.Importer()
 
        self.assertRaises(NotImplementedError, importer.get_single_local_object, None)
 

	
 
    def test_get_cache_key(self):
 
        importer = importers.Importer(key='upc', fields=['upc'])
 
        obj = {'upc': '00074305001321'}
 
        normal = {'data': obj}
 
        key = importer.get_cache_key(obj, normal)
 
        self.assertEqual(key, ('00074305001321',))
 

	
 
    def test_normalize_cache_object(self):
 
        importer = importers.Importer()
 
        obj = {'upc': '00074305001321'}
 
        with patch.object(importer, 'normalize_local_object', new=lambda obj: obj):
 
            cached = importer.normalize_cache_object(obj)
 
        self.assertEqual(cached, {'object': obj, 'data': obj})
 

	
 
    def test_normalize_local_object(self):
 
        importer = importers.Importer(key='upc', fields=['upc', 'description'])
 
        importer.simple_fields = importer.fields
 
        obj = Mock(upc='00074305001321', description="Apple Cider Vinegar")
 
        data = importer.normalize_local_object(obj)
 
        self.assertEqual(data, {'upc': '00074305001321', 'description': "Apple Cider Vinegar"})
 

	
 
    def test_update_object(self):
 
        importer = importers.Importer(key='upc', fields=['upc', 'description'])
 
        importer.simple_fields = importer.fields
 
        obj = Mock(upc='00074305001321', description="Apple Cider Vinegar")
 

	
 
        newobj = importer.update_object(obj, {'upc': '00074305001321', 'description': "Apple Cider Vinegar"})
 
        self.assertIs(newobj, obj)
 
        self.assertEqual(obj.description, "Apple Cider Vinegar")
 

	
 
        newobj = importer.update_object(obj, {'upc': '00074305001321', 'description': "Apple Cider Vinegar 32oz"})
 
        self.assertIs(newobj, obj)
 
        self.assertEqual(obj.description, "Apple Cider Vinegar 32oz")
 

	
 
    def test_normalize_host_data(self):
 
        importer = importers.Importer(key='upc', fields=['upc', 'description'],
 
                                          progress=NullProgress)
 

	
 
        data = [
 
            {'upc': '00074305001161', 'description': "Apple Cider Vinegar 16oz"},
 
            {'upc': '00074305001321', 'description': "Apple Cider Vinegar 32oz"},
 
        ]
 

	
 
        host_data = importer.normalize_host_data(host_objects=[])
 
        self.assertEqual(host_data, [])
 

	
 
        host_data = importer.normalize_host_data(host_objects=data)
 
        self.assertEqual(host_data, data)
 

	
 
        with patch.object(importer, 'get_host_objects', new=Mock(return_value=data)):
 
            host_data = importer.normalize_host_data()
 
        self.assertEqual(host_data, data)
 

	
 
    def test_get_deletion_keys(self):
 
        importer = importers.Importer()
 
        self.assertFalse(importer.caches_local_data)
 
        keys = importer.get_deletion_keys()
 
        self.assertEqual(keys, set())
 

	
 
        importer.caches_local_data = True
 
        self.assertIsNone(importer.cached_local_data)
 
        keys = importer.get_deletion_keys()
 
        self.assertEqual(keys, set())
 

	
 
        importer.cached_local_data = {'delete-me': object()}
 
        importer.cached_local_data = {
 
            'delete-me': {
 
                'object': object(),
 
                'data': {},
 
            },
 
        }
 
        keys = importer.get_deletion_keys()
 
        self.assertEqual(keys, set(['delete-me']))
 

	
 
    def test_model_name_attr(self):
 
        # default is None
 
        importer = importers.Importer()
 
        self.assertIsNone(importer.model_name)
 

	
 
        # but may be overridden via init kwarg
 
        importer = importers.Importer(model_name='Foo')
 
        self.assertEqual(importer.model_name, 'Foo')
 

	
 
        # or may inherit its value from 'model_class'
 
        class Foo:
 
            pass
 
        importer = importers.Importer(model_class=Foo)
 
        self.assertEqual(importer.model_name, 'Foo')
 

	
 
    def test_batch_size_attr(self):
 
        # default is 200
 
        importer = importers.Importer()
 
        self.assertEqual(importer.batch_size, 200)
 

	
 
        # but may be overridden via init kwarg
 
        importer = importers.Importer(batch_size=0)
 
        self.assertEqual(importer.batch_size, 0)
 
        importer = importers.Importer(batch_size=42)
 
        self.assertEqual(importer.batch_size, 42)
 

	
 
        # batch size determines how often flush occurs
 
        data = [{'id': i} for i in range(1, 101)]
 
        importer = importers.Importer(model_name='Foo', key='id', fields=['id'], empty_local_data=True)
 
        importer.handler = Mock(local_title="Nevermind")
 
        with patch.object(importer, 'create_object'): # just mock that out
 
            with patch.object(importer, 'flush_create_update') as flush:
 

	
 
                # 4 batches @ 33/per
 
                importer.import_data(host_data=data, batch_size=33)
 
                self.assertEqual(flush.call_count, 4)
 
                flush.reset_mock()
 

	
 
                # 3 batches @ 34/per
 
                importer.import_data(host_data=data, batch_size=34)
 
                self.assertEqual(flush.call_count, 3)
 
                flush.reset_mock()
 

	
 
                # 2 batches @ 50/per
 
                importer.import_data(host_data=data, batch_size=100)
 
                self.assertEqual(flush.call_count, 2)
 
                flush.reset_mock()
 

	
 
                # one extra/final flush happens, whenever the total number of
 
                # changes happens to match the batch size...
 

	
 
                # 1 batch @ 100/per, plus final flush
 
                importer.import_data(host_data=data, batch_size=100)
 
                self.assertEqual(flush.call_count, 2)
 
                flush.reset_mock()
 

	
 
                # 1 batch @ 200/per
 
                importer.import_data(host_data=data, batch_size=200)
 
                self.assertEqual(flush.call_count, 1)
 
                flush.reset_mock()
 

	
 
                # one extra/final flush also happens when batching is disabled
 

	
 
                # 100 "batches" @ 0/per, plus final flush
 
                importer.import_data(host_data=data, batch_size=0)
 
                self.assertEqual(flush.call_count, 101)
 
                flush.reset_mock()
 

	
 

	
 
class TestFromQuery(RattailTestCase):
 

	
 
    def test_query(self):
 
        importer = importers.FromQuery()
 
        self.assertRaises(NotImplementedError, importer.query)
 

	
 
    def test_get_host_objects(self):
 
        query = self.session.query(model.Product)
 
        importer = importers.FromQuery()
 
        with patch.object(importer, 'query', Mock(return_value=query)):
 
            objects = importer.get_host_objects()
 
        self.assertIsInstance(objects, QuerySequence)
 

	
 

	
 
class TestBulkImporter(TestCase, BulkImporterBattery):
 
    importer_class = importers.BulkImporter
 
        
 
    def test_batch_size_attr(self):
 
        # default is 200
 
        importer = importers.BulkImporter()
 
        self.assertEqual(importer.batch_size, 200)
 

	
 
        # but may be overridden via init kwarg
 
        importer = importers.BulkImporter(batch_size=0)
0 comments (0 inline, 0 general)