def test_merchant_remove_product(self): self.merchant.add_product(self.product) transaction.commit() self.keeper.close() keeper = open_storage() product = Product.fetch(u'Молоко Great Milk 1L', keeper) merchant = Merchant.fetch('test merchant', keeper) merchant.remove_product(product) transaction.commit() keeper.close() keeper = open_storage() product = Product.fetch(u'Молоко Great Milk 1L', keeper) merchant = Merchant.fetch('test merchant', keeper) self.assertNotIn(product, merchant.products)
def test_report_assembly_new_product(self): from uuid import uuid4 product_title = u'Молоко Great Milk TWO 1L' reporter_name = 'Jill' merchant_title = "Scotty's grocery" raw_data1 = { 'product_title': product_title, 'price_value': 42.6, 'url': 'http://scottys.com/products/milk/1', 'merchant_title': merchant_title, 'date_time': None, 'reporter_name': reporter_name } uuid_ = uuid4() report, stats = PriceReport.assemble(storage_manager=self.keeper, uuid=uuid_, **raw_data1) transaction.commit() self.keeper.close() self.keeper = open_storage() stored_report = PriceReport.fetch(report.key, self.keeper) self.assertEqual(report.key, stored_report.key) self.assertEqual(report.key, str(uuid_)) product = Product.fetch(product_title, self.keeper) self.assertEqual(product_title, product.title) self.assertIn(report, product.reports) category = ProductCategory.fetch('milk', self.keeper) self.assertIn(product, category.products) merchant = Merchant.fetch(merchant_title, self.keeper) self.assertEqual(merchant_title, merchant.title) self.assertIn(product, merchant)
def test_report_assembly_new_product(self): from uuid import uuid4 product_title = u"Молоко Great Milk TWO 1L" reporter_name = "Jill" merchant_title = "Scotty's grocery" raw_data1 = { "product_title": product_title, "price_value": 42.6, "url": "http://scottys.com/products/milk/1", "merchant_title": merchant_title, "date_time": None, "reporter_name": reporter_name, } uuid_ = uuid4() report, stats = PriceReport.assemble(storage_manager=self.keeper, uuid=uuid_, **raw_data1) transaction.commit() self.keeper.close() self.keeper = open_storage() stored_report = PriceReport.fetch(report.key, self.keeper) self.assertEqual(report.key, stored_report.key) self.assertEqual(report.key, str(uuid_)) product = Product.fetch(product_title, self.keeper) self.assertEqual(product_title, product.title) self.assertIn(report, product.reports) category = ProductCategory.fetch("milk", self.keeper) self.assertIn(product, category.products) merchant = Merchant.fetch(merchant_title, self.keeper) self.assertEqual(merchant_title, merchant.title) self.assertIn(product, merchant)
def test_merchant_remove_product(self): self.merchant.add_product(self.product) transaction.commit() self.keeper.close() keeper = open_storage() product = Product.fetch(u"Молоко Great Milk 1L", keeper) merchant = Merchant.fetch("test merchant", keeper) merchant.remove_product(product) transaction.commit() keeper.close() keeper = open_storage() product = Product.fetch(u"Молоко Great Milk 1L", keeper) merchant = Merchant.fetch("test merchant", keeper) self.assertNotIn(product, merchant.products)
def setUp(self): try: shutil.rmtree(STORAGE_DIR) except OSError: pass os.mkdir(STORAGE_DIR) self.keeper = open_storage() category = ProductCategory('milk') product = Product(u'Молоко Great Milk 1L') merchant = Merchant('test merchant') self.keeper.register(category, product, merchant) transaction.commit() self.keeper.close() self.keeper = open_storage() self.category = ProductCategory.fetch('milk', self.keeper) self.product = Product.fetch(u'Молоко Great Milk 1L', self.keeper) self.merchant = Merchant.fetch('test merchant', self.keeper)
def setUp(self): try: shutil.rmtree(STORAGE_DIR) except OSError: pass os.mkdir(STORAGE_DIR) self.keeper = open_storage() category = ProductCategory("milk") product = Product(u"Молоко Great Milk 1L") merchant = Merchant("test merchant") self.keeper.register(category, product, merchant) transaction.commit() self.keeper.close() self.keeper = open_storage() self.category = ProductCategory.fetch("milk", self.keeper) self.product = Product.fetch(u"Молоко Great Milk 1L", self.keeper) self.merchant = Merchant.fetch("test merchant", self.keeper)
def test_report_assembly_stored_product(self): product_title = u'Молоко Great Milk three 1L' product = Product(product_title, self.category) self.keeper.register(product) transaction.commit() self.keeper.close() self.keeper = open_storage() reporter_name = 'Jill' merchant_title = "Scotty's grocery 2" raw_data1 = { 'product_title': product_title, 'price_value': 42.6, 'url': 'http://scottys2.com/products/milk/1', 'merchant_title': merchant_title, 'date_time': None, 'reporter_name': reporter_name } report, stats = PriceReport.assemble(self.keeper, **raw_data1) transaction.commit() self.keeper.close() self.keeper = open_storage() stored_report = PriceReport.fetch(report.key, self.keeper) self.assertEqual(report.key, stored_report.key) product = Product.fetch(product_title, self.keeper) self.assertEqual(product_title, product.title) self.assertIn(report, product) category = ProductCategory.fetch('milk', self.keeper) self.assertIn(product, category) merchant = Merchant.fetch(merchant_title, self.keeper) self.assertEqual(merchant_title, merchant.title) self.assertIn(product, merchant) self.assertIn(merchant, product.merchants)
def test_report_assembly_stored_product(self): product_title = u"Молоко Great Milk three 1L" product = Product(product_title, self.category) self.keeper.register(product) transaction.commit() self.keeper.close() self.keeper = open_storage() reporter_name = "Jill" merchant_title = "Scotty's grocery 2" raw_data1 = { "product_title": product_title, "price_value": 42.6, "url": "http://scottys2.com/products/milk/1", "merchant_title": merchant_title, "date_time": None, "reporter_name": reporter_name, } report, stats = PriceReport.assemble(self.keeper, **raw_data1) transaction.commit() self.keeper.close() self.keeper = open_storage() stored_report = PriceReport.fetch(report.key, self.keeper) self.assertEqual(report.key, stored_report.key) product = Product.fetch(product_title, self.keeper) self.assertEqual(product_title, product.title) self.assertIn(report, product) category = ProductCategory.fetch("milk", self.keeper) self.assertIn(product, category) merchant = Merchant.fetch(merchant_title, self.keeper) self.assertEqual(merchant_title, merchant.title) self.assertIn(product, merchant) self.assertIn(merchant, product.merchants)
def test_report_assembly(self): raw_data1 = { 'product_title': u'Молоко Great Milk 1L', 'sku': 'ART97665', 'price_value': 42.6, 'url': 'http://scottys.com/products/milk/1', 'merchant_title': "Scotty's grocery", 'date_time': None, 'reporter_name': 'Jill' } raw_data2 = { 'product_title': u'Молоко Great Milk 0.93 L', 'price_value': 50, 'url': 'http://scottys.com/products/milk/5', 'merchant_title': "Scotty's grocery", 'date_time': None, 'reporter_name': 'Jill' } raw_data3 = { 'product_title': u'Сметана Great Sour Cream 450g', 'price_value': 60.4, 'url': 'http://scottys.com/products/sc/6', 'merchant_title': "Scotty's grocery", 'date_time': datetime.datetime(2014, 10, 5), 'reporter_name': 'Jill' } raw_data4 = { 'product_title': u'Сметана Great Sour Cream 987987g', 'price_value': 60.4, 'url': 'http://scottys.com/products/sc/8978', 'merchant_title': "Scotty's grocery", 'date_time': datetime.datetime(2014, 10, 5), 'reporter_name': 'Jill' } raw_data5 = { 'product_title': u'Картофель Вегетория для варки 3кг', 'price_value': 80.5, 'url': 'http://scottys.com/products/pot/324', 'merchant_title': "Scotty's grocery", 'reporter_name': 'Jill' } report1, stats1 = PriceReport.assemble(storage_manager=self.keeper, **raw_data1) report2, stats2 = PriceReport.assemble(storage_manager=self.keeper, **raw_data2) report3, stats3 = PriceReport.assemble(storage_manager=self.keeper, **raw_data3) report5, stats5 = PriceReport.assemble(storage_manager=self.keeper, **raw_data5) try: PriceReport.assemble(storage_manager=self.keeper, **raw_data4) except PackageLookupError: pass transaction.commit() # check category and products milk = ProductCategory.fetch('milk', self.keeper) sc = ProductCategory.fetch('sour cream', self.keeper) self.assertEqual(6, len(milk.products)) self.assertIn(42.6, milk.get_prices()) product1 = Product.fetch(u'Молоко Great Milk 1L', self.keeper) product2 = Product.fetch(u'Молоко Great Milk 0.93 L', self.keeper) self.assertIs(milk, product1.category) self.assertEqual('1 l', product1.package.title) self.assertEqual('ART97665', report1.sku) self.assertEqual('0.93 l', product2.package.title) self.assertEqual(1, product1.package_ratio) self.assertEqual(0.93, product2.package_ratio) self.assertFalse(hasattr(report2, 'sku')) self.assertIn(product1, milk.products) self.assertEqual('sour cream', report3.product.category.title) self.assertIn(u'Сметана Great Sour Cream 450g', [p.title for p in sc.products]) self.assertNotIn(u'Сметана Great Sour Cream 987987g', [p.title for p in sc.products]) # check references potato = ProductCategory.fetch('potato', self.keeper) self.assertIn(report5, PriceReport.fetch_all(self.keeper)) self.assertIn(report5, potato.products[0].reports) self.assertIn(report5, Product.fetch(u'Картофель Вегетория для варки 3кг', self.keeper).reports) # check reporter jill = Reporter.fetch('Jill', self.keeper) self.assertIs(jill, report1.reporter) self.assertIs(jill, report2.reporter) self.assertIs(jill, report3.reporter) # check price calculations self.assertEqual(42.60, round(report1.normalized_price_value, 2)) self.assertEqual(53.76, round(report2.normalized_price_value, 2)) self.assertEqual(53.69, round(report3.normalized_price_value, 2)) # check merchant merchant = Merchant.fetch("Scotty's grocery", self.keeper) self.assertIs(merchant, report1.merchant) self.assertIn(merchant, product1.merchants) self.assertIn(product1, merchant) # check datetime that is not now date_in_past = datetime.datetime(2014, 10, 5) self.assertEqual(date_in_past, report3.date_time) # check if no false product is registered false_product = Product.fetch(u'Сметана Great Sour Cream 987987g', self.keeper) self.assertIsNone(false_product)
def test_report_assembly(self): raw_data1 = { "product_title": u"Молоко Great Milk 1L", "sku": "ART97665", "price_value": 42.6, "url": "http://scottys.com/products/milk/1", "merchant_title": "Scotty's grocery", "date_time": None, "reporter_name": "Jill", } raw_data2 = { "product_title": u"Молоко Great Milk 0.93 L", "price_value": 50, "url": "http://scottys.com/products/milk/5", "merchant_title": "Scotty's grocery", "date_time": None, "reporter_name": "Jill", } raw_data3 = { "product_title": u"Сметана Great Sour Cream 450g", "price_value": 60.4, "url": "http://scottys.com/products/sc/6", "merchant_title": "Scotty's grocery", "date_time": datetime.datetime(2014, 10, 5), "reporter_name": "Jill", } raw_data4 = { "product_title": u"Сметана Great Sour Cream 987987g", "price_value": 60.4, "url": "http://scottys.com/products/sc/8978", "merchant_title": "Scotty's grocery", "date_time": datetime.datetime(2014, 10, 5), "reporter_name": "Jill", } raw_data5 = { "product_title": u"Картофель Вегетория для варки 3кг", "price_value": 80.5, "url": "http://scottys.com/products/pot/324", "merchant_title": "Scotty's grocery", "reporter_name": "Jill", } report1, stats1 = PriceReport.assemble(storage_manager=self.keeper, **raw_data1) report2, stats2 = PriceReport.assemble(storage_manager=self.keeper, **raw_data2) report3, stats3 = PriceReport.assemble(storage_manager=self.keeper, **raw_data3) report5, stats5 = PriceReport.assemble(storage_manager=self.keeper, **raw_data5) try: PriceReport.assemble(storage_manager=self.keeper, **raw_data4) except PackageLookupError: pass transaction.commit() # check category and products milk = ProductCategory.fetch("milk", self.keeper) sc = ProductCategory.fetch("sour cream", self.keeper) self.assertEqual(6, len(milk.products)) self.assertIn(42.6, milk.get_prices()) product1 = Product.fetch(u"Молоко Great Milk 1L", self.keeper) product2 = Product.fetch(u"Молоко Great Milk 0.93 L", self.keeper) self.assertIs(milk, product1.category) self.assertEqual("1 l", product1.package.title) self.assertEqual("ART97665", report1.sku) self.assertEqual("0.93 l", product2.package.title) self.assertEqual(1, product1.package_ratio) self.assertEqual(0.93, product2.package_ratio) self.assertFalse(hasattr(report2, "sku")) self.assertIn(product1, milk.products) self.assertEqual("sour cream", report3.product.category.title) self.assertIn(u"Сметана Great Sour Cream 450g", [p.title for p in sc.products]) self.assertNotIn(u"Сметана Great Sour Cream 987987g", [p.title for p in sc.products]) # check references potato = ProductCategory.fetch("potato", self.keeper) self.assertIn(report5, PriceReport.fetch_all(self.keeper)) self.assertIn(report5, potato.products[0].reports) self.assertIn(report5, Product.fetch(u"Картофель Вегетория для варки 3кг", self.keeper).reports) # check reporter jill = Reporter.fetch("Jill", self.keeper) self.assertIs(jill, report1.reporter) self.assertIs(jill, report2.reporter) self.assertIs(jill, report3.reporter) # check price calculations self.assertEqual(42.60, round(report1.normalized_price_value, 2)) self.assertEqual(53.76, round(report2.normalized_price_value, 2)) self.assertEqual(53.69, round(report3.normalized_price_value, 2)) # check merchant merchant = Merchant.fetch("Scotty's grocery", self.keeper) self.assertIs(merchant, report1.merchant) self.assertIn(merchant, product1.merchants) self.assertIn(product1, merchant) # check datetime that is not now date_in_past = datetime.datetime(2014, 10, 5) self.assertEqual(date_in_past, report3.date_time) # check if no false product is registered false_product = Product.fetch(u"Сметана Great Sour Cream 987987g", self.keeper) self.assertIsNone(false_product)