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 _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