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.

import_tests.py 3.7KB

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