def setUp(self):
		self.maxDiff = None
		self.file_import = FileImportModel()
		self.supplier_catalog_filter = SupplierCatalogFilterModel()
		self.plugin = SupplierCatalogWalthersPlugin(self.supplier_catalog_filter)
class SupplierCatalogWalthersPluginTestCase(unittest.TestCase):
	
	def setUp(self):
		self.maxDiff = None
		self.file_import = FileImportModel()
		self.supplier_catalog_filter = SupplierCatalogFilterModel()
		self.plugin = SupplierCatalogWalthersPlugin(self.supplier_catalog_filter)
		
	def test_match_file_import(self):
		self.file_import.name = "blargh"
		self.file_import.content = "blargh"
		
		result = self.plugin.match_file_import(self.file_import)
		self.assertFalse(result)

		self.file_import.name = "blarghwalthers/CatalogUpdate-20100101blargh"
		result = self.plugin.match_file_import(self.file_import)
		self.assertTrue(result)

		self.file_import.name = "CatalogUpdate-20100101"
		result = self.plugin.match_file_import(self.file_import)
		self.assertTrue(result)

	def test_get_items(self):
		self.file_import.content = ""
		supplier_catalog = SupplierCatalogModel()
		supplier_catalog.file_import = self.file_import
		for result in self.plugin.get_items(supplier_catalog):
			self.assertIsNone(result)

		c = [
			bytes('19770901'),
			bytes('   1'), bytes('1         '), struct.pack(b'<LL', MASK_PRICE_RETAIL, 100),
			bytes('   1'), bytes('2         '), struct.pack(b'<LL', MASK_PRICE_DEALER, 100),
			bytes('   1'), bytes('3         '), struct.pack(b'<LL', MASK_AVAILABILITY, 19770901),
			bytes('   1'), bytes('4         '), struct.pack(b'<Lc', MASK_IS_PHASED_OUT, bytes('Y')),
			bytes('   1'), bytes('5         '), struct.pack(b'<Lc', MASK_IS_IN_STOCK, bytes('Y')),
			bytes('   1'), bytes('6         '), struct.pack(b'<L', MASK_SCALE), bytes('H   '),
			bytes('   1'), bytes('7         '), struct.pack(b'<L', MASK_CATEGORY), bytes('1  '),
			bytes('   1'), bytes('8         '), struct.pack(b'<L', MASK_NAME), bytes("blargh\x00"),
		]

		expected = [
			{'MANUFACTURER': '1', 'PRODUCT': '1', 'PRICE_RETAIL': 100},
			{'MANUFACTURER': '1', 'PRODUCT': '2', 'PRICE_DEALER': 100},
			{'MANUFACTURER': '1', 'PRODUCT': '3', 'AVAILABILITY': 19770901},
			{'MANUFACTURER': '1', 'PRODUCT': '4', 'IS_PHASED_OUT': 'Y'},
			{'MANUFACTURER': '1', 'PRODUCT': '5', 'IS_IN_STOCK': 'Y'},
			{'MANUFACTURER': '1', 'PRODUCT': '6', 'SCALE': 'H'},
			{'MANUFACTURER': '1', 'PRODUCT': '7', 'CATEGORY': '1'},
			{'MANUFACTURER': '1', 'PRODUCT': '8', 'NAME': 'blargh'},
		]

		self.file_import.content = str.join(bytes(), c)
		
		results = list(self.plugin.get_items(supplier_catalog))
		self.assertEqual(expected, results)

	def test_issue_date(self):
		self.file_import.content = "blarghblargh"
		self.file_import.effective = datetime.datetime(1976,7,4, 0, 0, 0)

		result = self.plugin.issue_date(self.file_import)
		self.assertEqual(result, self.file_import.effective)

		self.file_import.content = "19770901blargh"
		result = self.plugin.issue_date(self.file_import)
		self.assertEqual(result, datetime.datetime(1977, 9, 1, 0, 0, 0))

	def test_update_fields(self):
		fieldsets = [
			{'MANUFACTURER': '1', 'PRODUCT': '1', 'PRICE_RETAIL': 100},
			{'MANUFACTURER': '1', 'PRODUCT': '2', 'PRICE_DEALER': 100},
			{'MANUFACTURER': '1', 'PRODUCT': '3', 'AVAILABILITY': 19770901},
			{'MANUFACTURER': '1', 'PRODUCT': '4', 'IS_PHASED_OUT': 'Y'},
			{'MANUFACTURER': '1', 'PRODUCT': '5', 'IS_IN_STOCK': 'Y'},
			{'MANUFACTURER': '1', 'PRODUCT': '6', 'SCALE': 'H'},
			{'MANUFACTURER': '1', 'PRODUCT': '7', 'CATEGORY': '1'},
			{'MANUFACTURER': '1', 'PRODUCT': '8', 'NAME': 'blargh'},
			{'MANUFACTURER': '1', 'PRODUCT': '9', 'AVAILABILITY': 0},
			{'MANUFACTURER': '1', 'PRODUCT': '10', 'AVAILABILITY': 999999},
		]

		expected = [
			{'manufacturer_identifier': '1', 'product_identifier':'1', 'retail':Decimal(1)},
			{'manufacturer_identifier': '1', 'product_identifier':'2', 'cost':Decimal(1)},
			{'manufacturer_identifier': '1', 'product_identifier':'3', 'available': datetime.date(1977,9,1), 'availability_indefinite': False},
			{'manufacturer_identifier': '1', 'product_identifier':'4', 'phased_out':True},
			{'manufacturer_identifier': '1', 'product_identifier':'5', 'stock':True},
			{'manufacturer_identifier': '1', 'product_identifier':'6', 'scale_identifier':'H'},
			{'manufacturer_identifier': '1', 'product_identifier':'7', 'category_identifier':'1'},
			{'manufacturer_identifier': '1', 'product_identifier':'8', 'name':'blargh'},
			{'manufacturer_identifier': '1', 'product_identifier':'9', 'available': datetime.date(datetime.MINYEAR,1,1), 'availability_indefinite': False},
			{'manufacturer_identifier': '1', 'product_identifier':'10', 'available': datetime.date(datetime.MAXYEAR,1,1), 'availability_indefinite': True},
		]

		for i in xrange(len(fieldsets)):
			fieldset = fieldsets[i]
			expect = expected[i]

			result = self.plugin.update_fields(fieldset)
			self.assertEqual(result, expect)