def test_findByName(self): # Given # Create test data to search for. We use two shops with the same name here. shopData1 = dict(uid="ca0f5926-7d55-4973-a8e1-d3e2cc89fca6", name="Shop with same name") shopData2 = dict(uid="e68782fd-19af-428e-881f-99d7af9b83b0", name="This shop should not be found") shopData3 = dict(uid="b0e2e467-6fd5-4a06-bb1e-9ad60223cafa", name="Shop with same name") expectedShops = [Shop(**shopData1), Shop(**shopData3)] with tdb.TinyDB(self.testDBPath) as db: shopTable: tdb.database.Table = db.table(TinyShopDao._TABLE_NAME) shopTable.insert(shopData1) shopTable.insert(shopData2) shopTable.insert(shopData3) # Setup repo testTinyShopDao = TinyShopDao(path=self.testDBPath) sut = ShopRepo(dao=testTinyShopDao) # When foundShops = sut.findByName("Shop with same name") # Then self.assertIsInstance(foundShops, list) self.assertEqual(2, len(foundShops)) self.assertEqual(expectedShops, foundShops)
def test_run(self): # Given shop = Shop(name="Sneak-a-venue Shop unit test", url=str(self.shopHtmlResponsePath)) product = Product(url=str(self.productHtmlResponsePath)) shop.addProduct(product) # Results will be written to a temp DB file which path is defined in TempShopRepoHelper. repoHelper = TempShopRepoHelper(shops=[shop]) async def runner(): # Given requestMock = RequestMock() messengerMock = MessengerMock(request=requestMock) sut = SneakAvenueShopScraper(scrapee=shop, scrapeeRepo=repoHelper.shopRepo, request=requestMock, messenger=messengerMock) # When / Then try: await sut.run() except Exception as e: self.fail( f"Expected test to run without Exception, but raised: {e}") else: self.assertEqual( 0, sut._failCount, f"Expected fail count to be 0, but is {sut._failCount}") asyncio.run(runner())
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))
def test_getNetloc_shouldRaiseWhenNotExtractable(self): # Given sut = Shop() sut.url = "www.scary-tests.com/url-has-missing-schema.html" # When with self.assertRaises(ValueError): sut.getNetloc()
def test_url_shouldGetAndSet(self): # Given sut = Shop() # When Setter sut.url = "https://something-to-test/shop" # Then Getter self.assertEqual("https://something-to-test/shop", sut.url)
def test_name_shouldGetAndSet(self): # Given sut = Shop() # When Setter sut.name = "My huge shop" # Then Getter self.assertEqual("My huge shop", sut.name)
def _decodeShops(cls, shopItems: List[dict]) -> List[Shop]: decodedShops: List[Shop] = list() for shopItem in shopItems: shopItem: tdb.database.Document shop = Shop(**shopItem) shop.products = cls._decodeProducts(shop.products) decodedShops.append(shop) return decodedShops
def test_lastScanStamp_shouldGetAndSet(self): # Given sut = Shop() # When Setter sut.lastScanStamp = 123456789.987654321 # Then Getter self.assertEqual(123456789.987654321, sut.lastScanStamp)
def test_removeProduct_shouldRaiseTypeErrorOnInvalidType(self): # Given sut = Shop() prodA = mock.Mock() prodA.mockVal = "Some product mock" # When / Then with self.assertRaises(TypeError): sut.removeProduct(prodA)
def test_products_setterShouldRaiseOnInvalidType(self): # Given sut = Shop() prodA = mock.Mock(spec=Product) invalidType = mock.Mock() invalidList = [prodA, invalidType] # When / Then with self.assertRaises(TypeError): sut.products = invalidList
def test_getNetloc(self): # Given sut = Shop() sut.url = "https://www.43einhalb.com/new-balance-m997sob-made-in-usa" expectedNetloc = "www.43einhalb.com" # When result = sut.getNetloc() # Then self.assertEqual(expectedNetloc, result) # Given sut.url = "https://bubblegum.com/en/new-996-vhb-wl996vhb.html" expectedNetloc = "bubblegum.com" # When result = sut.getNetloc() # Then self.assertEqual(expectedNetloc, result) # Given sut.url = "http://www.somewhere.de/bubu/ghj45-3879475?req=nothing" expectedNetloc = "www.somewhere.de" # When result = sut.getNetloc() # Then self.assertEqual(expectedNetloc, result)
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
def setUpClass(cls) -> None: super().setUpClass() shop = Shop(name="BSTN", url="https://www.bstn.com") cls.shopHelper = ConcreteShopHelper( shop, PRODUCTS_URLS_INTEGRATION_TEST_PATH) cls.shopHelper.overwriteRepoTableWithScrapableShop() cls.netHelper = NetworkHelper()
def test_findByUID(self): # Given # Create test data to search for. uidToFind = "b0e2e467-6fd5-4a06-bb1e-9ad60223cafa" shopData1 = dict(uid="ca0f5926-7d55-4973-a8e1-d3e2cc89fca6", name="The name of the first test shop") shopData2 = dict(uid=uidToFind, name="The name of the second test shop") expectedShop = Shop(**shopData2) with tdb.TinyDB(self.testDBPath) as db: shopTable: tdb.database.Table = db.table(TinyShopDao._TABLE_NAME) shopTable.insert(shopData1) shopTable.insert(shopData2) # Setup repo testTinyShopDao = TinyShopDao(path=self.testDBPath) sut = ShopRepo(dao=testTinyShopDao) # When foundShop = sut.findByUID(uidToFind) # Then self.assertIsInstance(foundShop, Shop) self.assertEqual(foundShop.uid, uidToFind) self.assertEqual(expectedShop, foundShop)
def test_products_setterShouldSetCorrectValues(self): # Given sut = Shop() prodA = mock.Mock(spec=Product) prodB = mock.Mock(spec=Product) prodC = mock.Mock(spec=Product) prodA.mockVal = "Some product mock A" prodB.mockVal = "Some product mock B" prodC.mockVal = "Some product mock C" expectedProducts = [prodA, prodB, prodC] # When sut.products = expectedProducts # Then self.assertListEqual(expectedProducts, sut.products)
def test_removeProduct(self): # Given sut = Shop() prodA = mock.Mock(spec=Product) prodB = mock.Mock(spec=Product) prodC = mock.Mock(spec=Product) prodA.mockVal = "Some product mock A" prodB.mockVal = "Some product mock B" prodC.mockVal = "Some product mock C" expectedProducts = [prodA] sut.products = [prodA, prodB, prodC] # When sut.removeProduct(prodB) sut.removeProduct(prodC) # Then self.assertListEqual(expectedProducts, sut.products)
def test_init_shouldSetDefaultProperties(self): # Given sut = Shop() # Then self.assertNotEqual("", sut.uid) self.assertEqual(sut.uid, str(uuid.UUID(hex=sut.uid, version=4))) self.assertEqual("", sut.name) self.assertEqual("", sut.url) self.assertEqual(0.0, sut.lastScanStamp) self.assertEqual(0, len(sut.products))
def setUpClass(cls) -> None: super().setUpClass() # Show some logging while testing, esp. with custom time and sourcecode info LogHelper.activate(level=clog.DEBUG) # Setup cls.shopHtmlResponsePath = TEST_BSTN_SHOP_HTML_RESPONSE.resolve() cls.productHtmlResponsePath = TEST_BSTN_PRODUCT_HTML_RESPONSE.resolve() # Download shop and product response if both not exist. This is a one-time-op. # Note that NetworkHelper is imported from integration tests. product = Product( url="https://www.bstn.com/en/adidas-zx-8000-ef4364-147994") shop = Shop(name="BSTN Shop Download Response", url="https://www.bstn.com", products=[product]) netHelper = NetworkHelper() netHelper.downloadShopResponse( shop=shop, shopResponsePath=cls.shopHtmlResponsePath, productResponsePath=cls.productHtmlResponsePath)
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 setUpClass(cls) -> None: super().setUpClass() # Show some logging while testing, esp. with custom time and sourcecode info LogHelper.activate(level=clog.DEBUG) # Setup cls.shopHtmlResponsePath = TEST_SNEAKAVENUE_SHOP_HTML_RESPONSE.resolve( ) cls.productHtmlResponsePath = TEST_SNEAKAVENUE_PRODUCT_HTML_RESPONSE.resolve( ) # Download shop and product response if both not exist. This is a one-time-op. # Note that NetworkHelper is imported from integration tests. product = Product( url="https://www.sneak-a-venue.de/nike-air-max-95-se-white-229724") shop = Shop(name="Sneak-a-venue Shop Unit Test", url="https://www.sneak-a-venue.de/", products=[product]) netHelper = NetworkHelper() netHelper.downloadShopResponse( shop=shop, shopResponsePath=cls.shopHtmlResponsePath, productResponsePath=cls.productHtmlResponsePath)
def setUpClass(cls) -> None: super().setUpClass() # Show some logging while testing, esp. with custom time and sourcecode info LogHelper.activate(level=clog.DEBUG) # Setup cls.shopHtmlResponsePath = TEST_FOOTDISTRICT_SHOP_HTML_RESPONSE.resolve( ) cls.productHtmlResponsePath = TEST_FOOTDISTRICT_PRODUCT_HTML_RESPONSE.resolve( ) # Download shop and product response if both not exist. This is a one-time-op. # Note that NetworkHelper is imported from integration tests. product = Product( url="https://footdistrict.com/en/new-balance-w990-na5-w990na5.html" ) shop = Shop(name="Footdistrict Shop Unit Test", url="https://footdistrict.com", products=[product]) netHelper = NetworkHelper() netHelper.downloadShopResponse( shop=shop, shopResponsePath=cls.shopHtmlResponsePath, productResponsePath=cls.productHtmlResponsePath)
def setUpClass(cls) -> None: super().setUpClass() # Show some logging while testing, esp. with custom time and sourcecode info LogHelper.activate(level=clog.DEBUG) # Setup cls.shopHtmlResponsePath = TEST_SOLEBOX_SHOP_HTML_RESPONSE.resolve() cls.productHtmlResponsePath = TEST_SOLEBOX_PRODUCT_HTML_RESPONSE.resolve( ) # Download shop and product response if both not exist. This is a one-time-op. # Note that NetworkHelper is imported from integration tests. product = Product( url= "https://www.solebox.com/en_DE/p/adidas_consortium-craig_green_kontuur_ii-mgsogr%2Fmgsogr%2Fmgsogr-01800380.html" ) shop = Shop(name="Solebox Shop Unit Test", url="https://www.solebox.com", products=[product]) netHelper = NetworkHelper() netHelper.downloadShopResponse( shop=shop, shopResponsePath=cls.shopHtmlResponsePath, productResponsePath=cls.productHtmlResponsePath)
def test_addProduct_shouldNotAddProductWithSameUrlAgain(self): # Given sut = Shop() prodA = mock.Mock(spec=Product) prodB = mock.Mock(spec=Product) prodC = mock.Mock(spec=Product) prodA.url = "http://url-value-A.com/" prodB.url = "https://some-other-url.io" prodC.url = "http://url-value-A.com/" expectedProducts = [prodA, prodB] # NOT prodC ! # When sut.addProduct(prodA) sut.addProduct(prodB) sut.addProduct(prodC) # Then self.assertEqual(2, len(sut.products)) self.assertListEqual(expectedProducts, sut.products)
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))
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]
from enemy.enemy_spawner import EnemySpawner from dog.dog_spawner import DogSpawner from shop.shop import Shop from mouse import Mouse BG_COLOR = (125, 125, 0) # 1. init pygame pygame.init() # 2. setup screen size = (1280, 720) canvas = pygame.display.set_mode(size) input_manager = InputManager() hole = Hole(20, 360) shop = Shop(1200, 360) player = Player(2, 3, input_manager) dog_spawner = DogSpawner(25, 25) # enemy_spawner1 = EnemySpawner(200,300) # enemy_spawner2 = EnemySpawner(100,100) enemy = Enemy(500, 500) game_object.add(player) game_object.add(hole) game_object.add(dog_spawner) # game_object.add(enemy_spawner1) game_object.add(enemy) game_object.add(shop) mouse = Mouse(0, 0) game_object.add(mouse) clock = pygame.time.Clock()