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
 
@@ -145,97 +145,102 @@ class TestImporter(TestCase):
 
        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)
0 comments (0 inline, 0 general)