Files
@ 8c0514e49d47
Branch filter:
Location: rattail-project/rattail/tests/importing/test_importers.py
8c0514e49d47
19.3 KiB
text/x-python
Update changelog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | # -*- coding: utf-8; -*-
from __future__ import unicode_literals, absolute_import
from unittest import TestCase
from mock import Mock, patch, call
from rattail.db import model
from rattail.db.util import QuerySequence
from rattail.importing import importers
from rattail.config import make_config
from rattail.util import OrderedDict
from .. import NullProgress, RattailTestCase
from . import ImporterTester
class ImporterBattery(ImporterTester):
"""
Battery of tests which can hopefully be ran for any non-bulk importer.
"""
def setUp(self):
self.config = self.make_config()
def make_config(self):
config = make_config([], extend=False)
config.setdefault('rattail', 'timezone.default', 'America/Los_Angeles')
return config
def test_import_data_empty(self):
importer = self.make_importer()
result = importer.import_data()
self.assertEqual(result, {})
def test_import_data_dry_run(self):
importer = self.make_importer()
self.assertFalse(importer.dry_run)
importer.import_data(dry_run=True)
self.assertTrue(importer.dry_run)
def test_import_data_create(self):
importer = self.make_importer()
with patch.object(importer, 'get_key', lambda k: k):
with patch.object(importer, 'create_object') as create:
importer.import_data(host_data=[1, 2, 3])
self.assertEqual(create.call_args_list, [
call(1, 1), call(2, 2), call(3, 3)])
def test_import_data_max_create(self):
importer = self.make_importer()
with patch.object(importer, 'get_key', lambda k: k):
with patch.object(importer, 'create_object') as create:
importer.import_data(host_data=[1, 2, 3], max_create=1)
self.assertEqual(create.call_args_list, [call(1, 1)])
class BulkImporterBattery(ImporterBattery):
"""
Battery of tests which can hopefully be ran for any bulk importer.
"""
def test_import_data_empty(self):
importer = self.make_importer()
result = importer.import_data()
self.assertEqual(result, 0)
class TestImporter(TestCase):
def setUp(self):
self.config = self.make_config()
def make_config(self):
config = make_config([], extend=False)
config.setdefault('rattail', 'timezone.default', 'America/Los_Angeles')
return config
def test_init(self):
# defaults
importer = importers.Importer(self.config)
self.assertIsNone(importer.model_class)
self.assertIsNone(importer.key)
self.assertEqual(importer.fields, [])
self.assertIsNone(importer.host_system_title)
# key must be included among the fields
self.assertRaises(ValueError, importers.Importer, key='upc', fields=[])
importer = importers.Importer(self.config, key='upc', fields=['upc'])
self.assertEqual(importer.key, ('upc',))
self.assertEqual(importer.fields, ['upc'])
# extra bits are passed as-is
importer = importers.Importer(self.config)
self.assertFalse(hasattr(importer, 'extra_bit'))
extra_bit = object()
importer = importers.Importer(self.config, extra_bit=extra_bit)
self.assertIs(importer.extra_bit, extra_bit)
def test_delete_flag(self):
# disabled by default
importer = importers.Importer(self.config)
self.assertTrue(importer.allow_delete)
self.assertFalse(importer.delete)
importer.import_data(host_data=[])
self.assertFalse(importer.delete)
# but can be enabled
importer = importers.Importer(self.config, delete=True)
self.assertTrue(importer.allow_delete)
self.assertTrue(importer.delete)
importer = importers.Importer(self.config)
self.assertFalse(importer.delete)
importer.import_data(host_data=[], delete=True)
self.assertTrue(importer.delete)
def test_get_host_objects(self):
importer = importers.Importer(self.config)
objects = importer.get_host_objects()
self.assertEqual(objects, [])
def test_cache_local_data(self):
importer = importers.Importer(self.config)
self.assertRaises(NotImplementedError, importer.cache_local_data)
def test_get_local_object(self):
importer = importers.Importer(self.config)
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.config)
self.assertRaises(NotImplementedError, importer.get_single_local_object, None)
def test_get_cache_key(self):
importer = importers.Importer(self.config, 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(self.config)
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(self.config, 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(self.config, 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(self.config, 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.config)
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': 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.config)
self.assertIsNone(importer.model_name)
# but may be overridden via init kwarg
importer = importers.Importer(self.config, model_name='Foo')
self.assertEqual(importer.model_name, 'Foo')
# or may inherit its value from 'model_class'
class Foo:
pass
importer = importers.Importer(self.config, model_class=Foo)
self.assertEqual(importer.model_name, 'Foo')
def test_batch_size_attr(self):
# default is 200
importer = importers.Importer(self.config)
self.assertEqual(importer.batch_size, 200)
# but may be overridden via init kwarg
importer = importers.Importer(self.config, batch_size=0)
self.assertEqual(importer.batch_size, 0)
importer = importers.Importer(self.config, 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(self.config, 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 setUp(self):
super(TestFromQuery, self).setUp()
self.config.setdefault('rattail', 'timezone.default', 'America/Chicago')
def test_query(self):
importer = importers.FromQuery(self.config)
self.assertRaises(NotImplementedError, importer.query)
def test_get_host_objects(self):
query = self.session.query(model.Product)
importer = importers.FromQuery(self.config)
with patch.object(importer, 'query', Mock(return_value=query)):
objects = importer.get_host_objects()
self.assertIsInstance(objects, QuerySequence)
class TestBulkImporter(BulkImporterBattery, TestCase):
importer_class = importers.BulkImporter
def test_batch_size_attr(self):
# default is 200
importer = importers.BulkImporter(self.config)
self.assertEqual(importer.batch_size, 200)
# but may be overridden via init kwarg
importer = importers.BulkImporter(self.config, batch_size=0)
self.assertEqual(importer.batch_size, 0)
importer = importers.BulkImporter(self.config, 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.BulkImporter(self.config, model_name='Foo',
key='id', fields=['id'],
empty_local_data=True)
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()
######################################################################
# fake importer class, tested mostly for basic coverage
######################################################################
class Product(object):
upc = None
description = None
class MockImporter(importers.Importer):
model_class = Product
key = 'upc'
simple_fields = ['upc', 'description']
supported_fields = simple_fields
caches_local_data = True
flush_every_x = 1
session = Mock()
def normalize_local_object(self, obj):
return obj
def update_object(self, obj, host_data, local_data=None):
return host_data
class TestMockImporter(ImporterTester, TestCase):
importer_class = MockImporter
sample_data = OrderedDict([
('16oz', {'upc': '00074305001161', 'description': "Apple Cider Vinegar 16oz"}),
('32oz', {'upc': '00074305001321', 'description': "Apple Cider Vinegar 32oz"}),
('1gal', {'upc': '00074305011283', 'description': "Apple Cider Vinegar 1gal"}),
])
def setUp(self):
self.config = self.make_config()
self.importer = self.make_importer()
self.importer.handler = Mock(local_title="Nevermind")
def make_config(self):
config = make_config([], extend=False)
config.setdefault('rattail', 'timezone.default', 'America/Los_Angeles')
return config
def test_create(self):
local = self.copy_data()
del local['32oz']
self.import_data(local_data=local)
self.assert_import_created('32oz')
self.assert_import_updated()
self.assert_import_deleted()
def test_create_empty(self):
self.import_data(host_data={}, local_data={})
self.assert_import_created()
self.assert_import_updated()
self.assert_import_deleted()
def test_update(self):
local = self.copy_data()
local['16oz']['description'] = "wrong description"
self.import_data(local_data=local)
self.assert_import_created()
self.assert_import_updated('16oz')
self.assert_import_deleted()
def test_delete(self):
local = self.copy_data()
local['bogus'] = {'upc': '00000000000000', 'description': "Delete Me"}
self.import_data(local_data=local, delete=True)
self.assert_import_created()
self.assert_import_updated()
self.assert_import_deleted('bogus')
def test_duplicate(self):
host = self.copy_data()
host['32oz-dupe'] = host['32oz']
self.import_data(host_data=host)
self.assert_import_created()
self.assert_import_updated()
self.assert_import_deleted()
def test_max_create(self):
local = self.copy_data()
del local['16oz']
del local['1gal']
self.import_data(local_data=local, max_create=1)
self.assert_import_created('16oz')
self.assert_import_updated()
self.assert_import_deleted()
def test_max_total_create(self):
local = self.copy_data()
del local['16oz']
del local['1gal']
self.import_data(local_data=local, max_total=1)
self.assert_import_created('16oz')
self.assert_import_updated()
self.assert_import_deleted()
def test_max_update(self):
local = self.copy_data()
local['16oz']['description'] = "wrong"
local['1gal']['description'] = "wrong"
self.import_data(local_data=local, max_update=1)
self.assert_import_created()
self.assert_import_updated('16oz')
self.assert_import_deleted()
def test_max_total_update(self):
local = self.copy_data()
local['16oz']['description'] = "wrong"
local['1gal']['description'] = "wrong"
self.import_data(local_data=local, max_total=1)
self.assert_import_created()
self.assert_import_updated('16oz')
self.assert_import_deleted()
def test_max_delete(self):
local = self.copy_data()
local['bogus1'] = {'upc': '00000000000001', 'description': "Delete Me"}
local['bogus2'] = {'upc': '00000000000002', 'description': "Delete Me"}
self.import_data(local_data=local, delete=True, max_delete=1)
self.assert_import_created()
self.assert_import_updated()
self.assert_import_deleted('bogus1')
def test_max_total_delete(self):
local = self.copy_data()
local['bogus1'] = {'upc': '00000000000001', 'description': "Delete Me"}
local['bogus2'] = {'upc': '00000000000002', 'description': "Delete Me"}
self.import_data(local_data=local, delete=True, max_total=1)
self.assert_import_created()
self.assert_import_updated()
self.assert_import_deleted('bogus1')
def test_max_total_delete_with_changes(self):
local = self.copy_data()
del local['16oz']
local['32oz']['description'] = "wrong"
local['1gal']['description'] = "wrong"
local['bogus1'] = {'upc': '00000000000001', 'description': "Delete Me"}
local['bogus2'] = {'upc': '00000000000002', 'description': "Delete Me"}
self.import_data(local_data=local, delete=True, max_total=3)
self.assert_import_created('16oz')
self.assert_import_updated('32oz', '1gal')
self.assert_import_deleted()
|