Esempio n. 1
0
 def test_export_supplier_pricelist(self):
     """Export supplier pricelist to file."""
     supplier_pricelist_filepath = "tmp/test_supplier_pricelist.csv"
     item_count = 5
     supplier_items = []
     for i in range(item_count):
         inventory_item = random_inventory_item()
         # pylint:disable=no-member
         self.session.add(inventory_item)
         supplier_item = random_supplier_item(inventory_item)
         # pylint:disable=no-member
         self.session.add(supplier_item)
         supplier_items.append(supplier_item)
     export_supplier_pricelist(supplier_pricelist_filepath, supplier_items)
     file = open(supplier_pricelist_filepath)
     supplier_pricelist_reader = csv.DictReader(file, SPL_FIELDNAMES)
     supplier_pricelist_items = list(supplier_pricelist_reader)
     for i, supplier_pricelist_item in enumerate(supplier_pricelist_items):
         supplier_item = supplier_items[i]
         expected_item_values = {
             "supplier_code": supplier_item.code,
             "supp_item_code": supplier_item.item_code,
             "item_code": supplier_item.inventory_item.code,
             "supp_price_1": str(supplier_item.buy_price),
         }
         for key, expected_value in expected_item_values.items():
             value = supplier_pricelist_item[key]
             self.assertEqual(
                 expected_value, value,
                 "'{}' != '{}' for key: {}".format(expected_value, value,
                                                   key))
     file.close()
     delete_temporary_file(supplier_pricelist_filepath)
Esempio n. 2
0
 def test_export_supplier_price_changes_report(self):
     """Export supplier price updates to XLSX report."""
     report_filepath = "tmp/test_supplier_price_changes_report.xlsx"
     item_count = 5
     price_changes = []
     # pylint:disable=unused-variable
     for i in range(item_count):
         inventory_item = random_inventory_item()
         # pylint:disable=no-member
         self.session.add(inventory_item)
         supplier_item = random_supplier_item(inventory_item)
         # pylint:disable=no-member
         self.session.add(supplier_item)
         price_was = supplier_item.buy_price
         price_diff = price_was * Decimal(random.randint(-10, 10)) / 100
         price_diff_percentage = Decimal(1)
         if price_was != Decimal(0):
             price_diff_percentage = price_diff / price_was
         price_now = price_was + price_diff
         price_changes.append({
             "supplier_item":
             supplier_item,
             # Create price difference between +/- 10%.
             "price_was":
             price_was,
             "price_now":
             price_now,
             "price_diff":
             price_diff,
             "price_diff_percentage":
             price_diff_percentage,
         })
     uom_errors = []
     export_supplier_price_changes_report(report_filepath, price_changes,
                                          uom_errors)
     report_reader = ReportReader(report_filepath)
     fieldnames = [
         "item_code",
         "supplier",
         "brand",
         "apn",
         "description",
         "price_was",
         "price_now",
         "price_diff",
         "price_diff_%",
     ]
     for fieldname in fieldnames:
         self.assertIn(fieldname, report_reader.fieldnames)
     data = report_reader.load()
     self.assertEqual(item_count, len(data))
     delete_temporary_file(report_filepath)
Esempio n. 3
0
    def test_fetch_url(self):
        """Fetch image URLs."""
        inventory_item = random_inventory_item()
        # pylint:disable=no-member
        for fetcher_class in FETCHER_CLASSES:
            supplier_item = random_supplier_item(inventory_item)
            supplier_item.code = fetcher_class.__name__
            # pylint:disable=no-member

        fetchers = get_fetchers(inventory_item)
        for fetcher in fetchers:
            url = fetcher.get_image_url(inventory_item)
            self.assertIsInstance(url, str)
Esempio n. 4
0
    def test_AVD_fetch_url(self):
        """Fetch image URL from Avery website."""
        inventory_item = random_inventory_item()
        supplier_item = random_supplier_item(inventory_item)
        supplier_item.code = "AVD"
        supplier_item.item_code = random_string(20)
        # pylint:disable=no-member
        self.session.commit()
        fetcher = get_fetchers(inventory_item)[0]
        url = fetcher.get_image_url(inventory_item)
        self.assertIsNone(url)

        inventory_item = random_inventory_item()
        supplier_item = random_supplier_item(inventory_item)
        supplier_item.code = "AVD"
        supplier_item.item_code = "44540"
        fetchers = get_fetchers(inventory_item)
        self.assertEqual(1, len(fetchers))
        self.session.commit()
        fetcher = get_fetchers(inventory_item)[0]
        expected_url = "https://www.averyproducts.com.au/sites/avery.au/files/styles/scale_1_1_ratio_style/public/avery_importer/media/44540_pac.jpg?itok=L9_umi6U"
        fetcher = get_fetchers(inventory_item)[0]
        url = fetcher.get_image_url(inventory_item)
        self.assertEqual(url, expected_url)
Esempio n. 5
0
 def test_download_image(self):
     """Download image"""
     sku = "107001"
     inventory_item = random_inventory_item()
     supplier_item = random_supplier_item(inventory_item)
     supplier_item.code = "ACO"
     supplier_item.item_code = sku
     expected_filepath = os.path.join("tmp",
                                      "{}.jpg".format(inventory_item.code))
     fetchers = get_fetchers(inventory_item)
     for fetcher in fetchers:
         filepath = fetcher.download_image(inventory_item, "tmp")
         self.assertTrue(os.path.exists(filepath))
         self.assertEqual(expected_filepath, filepath)
         os.remove(filepath)
Esempio n. 6
0
    def test_update_supplier_items(self):
        """Apply supplier pricelist prices to supplier items."""
        item_count = 10
        supplier_items = []
        # pylint:disable=unused-variable
        for i in range(item_count):
            inventory_item = random_inventory_item()
            # pylint:disable=no-member
            self.session.add(inventory_item)
            supplier_item = random_supplier_item(inventory_item)
            # pylint:disable=no-member
            self.session.add(supplier_item)
            supplier_items.append(supplier_item)

        def random_supplier_pricelist_item(supplier_item):
            supplier_code = supplier_item.code
            item_code = supplier_item.item_code
            # Mock a price increase by multiplying the existing buy price.
            buy_price = supplier_item.buy_price * \
                Decimal(random.randint(200, 400)) / 100
            return {
                "item_code": supplier_item.inventory_item.code,
                "supplier_code": supplier_code,
                "supp_item_code": item_code,
                "supp_price_1": buy_price,
                "supp_uom": supplier_item.uom,
                "supp_conv_factor": supplier_item.conv_factor,
            }

        supplier_pricelist_items = [
            random_supplier_pricelist_item(item) for item in supplier_items
        ]

        price_changes, uom_errors = update_supplier_items(
            supplier_pricelist_items, self.session)
        self.assertEqual(10, len(price_changes))