Exemplo n.º 1
0
    def test_assignProducts(self):
        # Given
        sut = Shop()
        shopURL = "http://this-is-the-netloc-part.com"
        sut.url = shopURL

        # Any product with the same URL netloc part as the shop's URL netloc part
        # is expected to be added to the shop's products list.
        productMock_01 = mock.Mock(spec=Product)
        productMock_01.url = "http://this-is-the-netloc-part.com/en/some_product_link_284734.htm"
        productMock_02 = mock.Mock(spec=Product)
        productMock_02.url = "http://this-is-the-netloc-part.com/en/some_other_product_9274692"
        productMock_03 = mock.Mock(spec=Product)
        productMock_03.url = "http://another-shop.org/some_other_product_9274692.php"

        products = list()
        products.extend([productMock_01, productMock_02, productMock_03])

        # When
        addedProducts = sut.assignProducts(products)

        # Then
        # productMock_03 should NOT be in the shop's products list.
        self.assertEqual(2, len(sut.products))
        self.assertEqual(productMock_01.url, sut.products[0].url)
        self.assertEqual(productMock_02.url, sut.products[1].url)
        self.assertIsInstance(addedProducts, list)
        self.assertEqual(2, len(addedProducts))
Exemplo n.º 2
0
    def test_assignProducts_shouldNotAddIfNoProducts(self):
        # Given
        sut = Shop()
        shopURL = "http://this-again-is-a-netloc-part.com"
        sut.url = shopURL

        products = None
        # When
        # noinspection PyTypeChecker
        sut.assignProducts(products)
        # Then
        self.assertEqual(0, len(sut.products))

        # Given
        products = list()
        # When
        addedProducts = sut.assignProducts(products)
        # Then
        self.assertEqual(0, len(sut.products))
        self.assertIsInstance(addedProducts, list)
        self.assertEqual(0, len(addedProducts))
Exemplo n.º 3
0
    def createShops(self) -> Optional[List[Shop]]:
        try:
            products: List[Product] = self.getAll()  # raises

        except Exception as e:
            raise LookupError(
                "No URLs for products found. Check your ProductsURLs "
                f"repository file. {e}")

        if not products:
            return None

        shops: List[Shop] = list()
        shopsNetlocs: List[str] = list()
        assignedProducts: List[Product] = list()

        for product in reversed(products):
            if product in assignedProducts: continue

            try:
                urlParts = urlparse.urlparse(url=product.url)
                productUrlScheme = urlParts.scheme
                productNetloc = urlParts.netloc

            except Exception as e:
                raise ValueError(f"URL could not be parsed into parts. {e}")

            if not productUrlScheme or not productNetloc:
                continue

            if productNetloc in shopsNetlocs:
                # Shop for this product already created, skip
                continue

            else:
                # Create shop.
                # Note: Leave shop.name empty, so it has a chance to become set by scraping.
                shopsNetlocs.append(productNetloc)
                shopURL = urlparse.urlunparse(
                    (productUrlScheme, productNetloc, '', '', '', ''))
                shop = Shop(url=shopURL)
                assignedProducts.extend(shop.assignProducts(products))
                shops.append(shop)

        if shops:
            return shops
        else:
            return None