def test_findSize(self): # Given sut = Product() sizeA = Size(sizeEU="41.25") sizeB = Size(sizeEU="38 2/3") sizeC = Size(sizeEU="39") sut.addSize(sizeA) sut.addSize(sizeB) sut.addSize(sizeC) # When given size not exists foundSize = sut.findSize(sizeStr="45") # Then self.assertIsNone(foundSize, f"Expected that size was not found and " f"returned value is None, but got '{foundSize}'") # When foundSize = sut.findSize(sizeStr=sizeA.sizeEU) # Then self.assertEqual(sizeA, foundSize) # When foundSize = sut.findSize(sizeStr=sizeB.sizeEU) # Then self.assertEqual(sizeB, foundSize) # When foundSize = sut.findSize(sizeStr=sizeC.sizeEU) # Then self.assertEqual(sizeC, foundSize)
def test_inStockReadable_shouldRaiseOnTypeError(self): # Given sut = Size() sut.isInStock = "This is a string. But must be a bool!" # When with self.assertRaises(TypeError): result = sut.inStockReadable # Then self.assertEqual(None, result)
def test_addSize(self): # Given sut = Product() sizeA = Size(sizeEU="45.2") sizeB = Size(sizeEU="38.5") sizeC = Size(sizeEU="39") expectedSizes = [sizeA, sizeB, sizeC] # When sut.addSize(sizeA) sut.addSize(sizeB) sut.addSize(sizeC) # Then self.assertListEqual(expectedSizes, sut.sizes)
def setUp(self) -> None: self.sut, self._session, self._request, self._messengerRepo = None, None, None, None # Make test shop shop = Shop(name="BSTN", url="https://www.bstn.com/") self._shopHelper = ConcreteShopHelper( shop=shop, productsUrlsRepoPath=PRODUCTS_URLS_INTEGRATION_TEST_PATH) self._shopHelper.assignProductsFromProductsUrlsRepo() assert len(self._shopHelper.shop.products) > 0 product = self._shopHelper.shop.products[0] product.name = "A Test Product" product.basePrice = 122.23 product.currency = "EUR" product.urlThumb = "https://nb.scene7.com/is/image/NB/m997web_nb_02_i" size1 = Size(sizeEU="42 2/3", isInStock=True) size2 = Size(sizeEU="40", isInStock=True) size3 = Size(sizeEU="39.5", isInStock=True) size4 = Size(sizeEU="37 1/3", isInStock=True) product.sizes = [size1, size2, size3, size4]
def test_init_shouldSetDefaultProperties(self): # Given sut = Size() # Then self.assertNotEqual("", sut.uid) self.assertEqual(sut.uid, str(uuid.UUID(hex=sut.uid, version=4))) self.assertEqual(None, sut.sizeEU) self.assertEqual(None, sut.price) self.assertEqual(None, sut.url) self.assertEqual(None, sut.urlAddToCart) self.assertEqual(None, sut.isInStock) self.assertEqual("Unknown", sut.inStockReadable)
def test_inStockReadable_shouldReturnCorrectString(self): # Given sut = Size() sut.isInStock = None # When result = sut.inStockReadable # Then self.assertEqual("Unknown", result) # Given sut.isInStock = False # When result = sut.inStockReadable # Then self.assertEqual("Out of stock", result) # Given sut.isInStock = True # When result = sut.inStockReadable # Then self.assertEqual("In stock", result)
def _processSizeChange(product: Product, sizeStr: str, isSizeInStock: bool) -> ProductChanged: isProductChanged = False # Check if scraped size already exists foundSize: Size = product.findSize(sizeStr) # If an existing size was found, copy its reference(!). Otherwise, take a new Size object. size: Size = foundSize if foundSize else Size() # 1. # Scraped size does not exist at all, so add to product and mark product as changed. if not foundSize: size.sizeEU = sizeStr product.addSize(size=size) isProductChanged = True logger.debug("New size '%s' detected & added. %s", size.sizeEU, product.url) # 2. # Size already exists but its stock indicator is marked as unavailable, whereas the # newly scraped size is marked as in-stock. Mark product as changed. if not size.isInStock and isSizeInStock: isProductChanged = True logger.debug("Size '%s' has been restocked. %s", size.sizeEU, product.url) # 3. # After comparison, set/update size stock indicator. Always update ALL # found sizes as their availability may have changed to True or False and # we have to be up-to-date in case we are sending a message. # Note that 'size' var whether is a yet unrelated Size object or a reference # to an existing product.size size.isInStock = isSizeInStock return isProductChanged
def _decodeSizes(cls, sizeItems: list) -> List[Size]: decodedSizes: List[Size] = list() for sizeItem in sizeItems: size = Size(**sizeItem) decodedSizes.append(size) return decodedSizes
def create2Shops(self) -> None: """ Caution: Do not change values as they are tested against :return: None """ size01 = Size() size01.uid = str( uuid.UUID(hex="1528dae6-188f-4d7e-8a6c-5af44ce5c222", version=4)) size01.sizeEU = "40 1/3" size01.isInStock = True size01.price = 56.99 size01.url = "http://oneshop.com/bottles/92743867ACTFGJ-UTU/40.1.3.htm" size01.urlAddToCart = "http://oneshop.com/bottles/atc/40.1.3-92743867ACTFGJ-UTU.htm" size02 = Size() size02.uid = str( uuid.UUID(hex="5f561c62-8502-4ec1-8f46-f0adb5e8254c", version=4)) size02.sizeEU = "43" size02.isInStock = False size02.price = 54.99 size02.url = "http://oneshop.com/bottles/92743867ACTFGJ-UTU/43.htm" size02.urlAddToCart = "http://oneshop.com/bottles/atc/43-92743867ACTFGJ-UTU.htm" size03 = Size() size03.uid = str( uuid.UUID(hex="e070b0c9-769d-4c13-a208-f7207f0970db", version=4)) size03.sizeEU = "44.5" size03.isInStock = True size03.price = 189.50 size03.url = "https://megashop.com/shoes/44.5-9a734hd78.htm" size03.urlAddToCart = "https://megashop.com/shoes/atc/44.5-9a734hd78#g89.php" product01 = Product() product01.uid = str( uuid.UUID(hex="2857027b-cf25-4639-965e-0e22f9f4c755", version=4)) product01.url = "http://oneshop.com/bottles/92743867ACTFGJ-UTU" product01.name = "Biggest Corona Bottle ever" product01.setReleaseDate(dtt.datetime(2020, 9, 30, 13, 50, 59), timezone="Europe/Berlin") product01.basePrice = 55.49 product01.lastScanStamp = 1588548868.304869 # setLastScanNow() product01.sizes = [size01, size02] product02 = Product() product02.uid = str( uuid.UUID(hex="9cab557a-419a-4883-8287-f09f7244b225", version=4)) product02.url = "http://oneshop.com/bottles/1362836400447GT-UTU" product02.name = "Neck Bottle" product02.setReleaseDate(dtt.datetime(2023, 1, 30, 15, 40, 35), timezone="Europe/Berlin") product02.basePrice = 3.22 product02.lastScanStamp = 1588548911.230381 # setLastScanNow() product02.sizes = [] product03 = Product() product03.uid = str( uuid.UUID(hex="f0700293-693c-48a6-8f01-014e07151d99", version=4)) product03.url = "https://www.megashop.com/shoes/9a734hd78.html" product03.urlThumb = "https://www.megashop.com/shoes/thumb-9a734hd78.html" product03.name = "Hey Bro Male" product03.setReleaseDate(dtt.datetime(2028, 11, 1, 8, 2, 40), timezone="Europe/Berlin") product03.basePrice = 190 product03.lastScanStamp = 1588548274.102859 # setLastScanNow() product03.sizes = [size03] shop01 = Shop() # ID is usually set by the DBMS, we explicitly set it for equality checks, too: shop01.uid = str( uuid.UUID(hex="73f9cac8-ebdc-4d9b-8163-d04d09f06cd9", version=4)) shop01.name = "Bottle shop" shop01.url = "http://oneshop.com/bottles" shop01.products = [product01, product02] shop02 = Shop() # ID is usually set by the DBMS, we explicitly set it for equality checks, too: shop02.uid = str( uuid.UUID(hex="69ec8e1b-8812-4413-ad72-b74364e2fa7a", version=4)) shop02.name = "Megashop" shop02.url = "https://www.megashop.com/shoes" shop02.products = [product03] self.shops = [shop01, shop02]