You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_import.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import logging
  2. import os
  3. from decimal import Decimal as D
  4. from django.test import TestCase
  5. from oscar.apps.catalogue.models import Product, ProductClass
  6. from oscar.apps.partner.exceptions import ImportingError
  7. from oscar.apps.partner.importers import CatalogueImporter
  8. from oscar.apps.partner.models import Partner
  9. from oscar.test.factories import create_product
  10. from tests._site.apps.partner.models import StockRecord
  11. TEST_BOOKS_CSV = os.path.join(os.path.dirname(__file__), 'fixtures/books-small.csv')
  12. TEST_BOOKS_SEMICOLON_CSV = os.path.join(os.path.dirname(__file__), 'fixtures/books-small-semicolon.csv')
  13. class NullHandler(logging.Handler):
  14. def emit(self, record):
  15. pass
  16. logger = logging.getLogger("Null")
  17. logger.addHandler(NullHandler())
  18. class CommandEdgeCasesTest(TestCase):
  19. def setUp(self):
  20. self.importer = CatalogueImporter(logger)
  21. def test_sending_no_file_argument_raises_exception(self):
  22. self.importer.afile = None
  23. with self.assertRaises(ImportingError):
  24. self.importer.handle()
  25. def test_sending_directory_as_file_raises_exception(self):
  26. self.importer.afile = "/tmp"
  27. with self.assertRaises(ImportingError):
  28. self.importer.handle()
  29. def test_importing_nonexistant_file_raises_exception(self):
  30. self.importer.afile = "/tmp/catalogue-import.zgvsfsdfsd"
  31. with self.assertRaises(ImportingError):
  32. self.importer.handle()
  33. class ImportSmokeTest(TestCase):
  34. # First row is:
  35. # "9780115531446","Prepare for Your Practical Driving Test",NULL,"Book","Gardners","9780115531446","10.32","6"
  36. #
  37. # Second row is (has no stock data):
  38. # "9780955337819","Better Photography",NULL,"Book"
  39. def setUp(self):
  40. self.importer = CatalogueImporter(logger)
  41. self.importer.handle(TEST_BOOKS_CSV)
  42. self.product = Product.objects.get(upc='9780115531446')
  43. def test_all_rows_are_imported(self):
  44. self.assertEqual(10, Product.objects.all().count())
  45. def test_class_is_created(self):
  46. try:
  47. ProductClass.objects.get(name="Book")
  48. except Product.DoesNotExist:
  49. self.fail()
  50. def test_only_one_class_is_created(self):
  51. self.assertEqual(1, ProductClass.objects.all().count())
  52. def test_item_is_created(self):
  53. try:
  54. Product.objects.get(upc="9780115531446")
  55. except Product.DoesNotExist:
  56. self.fail()
  57. def test_title_is_imported(self):
  58. self.assertEqual("Prepare for Your Practical Driving Test", self.product.title)
  59. def test_partner_is_created(self):
  60. try:
  61. Partner.objects.get(name="Gardners")
  62. except Product.DoesNotExist:
  63. self.fail()
  64. def test_stockrecord_is_created(self):
  65. try:
  66. StockRecord.objects.get(partner_sku="9780115531446")
  67. except Product.DoesNotExist:
  68. self.fail()
  69. def test_null_fields_are_skipped(self):
  70. self.assertEqual("", self.product.description)
  71. def test_price_is_imported(self):
  72. stockrecord = self.product.stockrecords.all()[0]
  73. self.assertEqual(D('10.32'), stockrecord.price)
  74. def test_num_in_stock_is_imported(self):
  75. stockrecord = self.product.stockrecords.all()[0]
  76. self.assertEqual(6, stockrecord.num_in_stock)
  77. class ImportSemicolonDelimitedFileTest(TestCase):
  78. def setUp(self):
  79. self.importer = CatalogueImporter(logger, delimiter=";")
  80. def test_import(self):
  81. self.importer.handle(TEST_BOOKS_SEMICOLON_CSV)
  82. class ImportWithFlushTest(TestCase):
  83. def setUp(self):
  84. self.importer = CatalogueImporter(logger, flush=True)
  85. def test_items_are_flushed_by_importer(self):
  86. upc = "0000000000000"
  87. create_product(price=D('10.00'), upc=upc)
  88. self.importer.handle(TEST_BOOKS_CSV)
  89. with self.assertRaises(Product.DoesNotExist):
  90. Product.objects.get(upc=upc)