def setUp(self): self.p = Product( entity_id=1756905, name="Samsung SSD 860 EVO 1TB, SATA (MZ-76E1T0B)", url= "https://geizhals.de/samsung-ssd-860-evo-1tb-mz-76e1t0b-a1756905.html", price=195.85)
def get_product_info(self, product_id): self.cursor.execute("SELECT product_id, name, price, url FROM products WHERE product_id=?;", [str(product_id)]) product = self.cursor.fetchone() if product is None: return None p_id, name, price, url = product return Product(entity_id=p_id, name=name, price=price, url=url)
def get_all_products(self): self.cursor.execute("SELECT product_id, name, price, url FROM products;") product_l = self.cursor.fetchall() products = [] for line in product_l: products.append(Product(entity_id=line[0], name=line[1], price=line[2], url=line[3])) return products
def test_from_url(self): """Test to check if creating a product by url works as intended""" # Create a product by url - needs a network connection my_p = Product.from_url(self.p.url) self.assertEqual(type(my_p), Product) self.assertEqual(my_p.id, self.p.entity_id) self.assertEqual(my_p.name, self.p.name) self.assertEqual(my_p.url, self.p.url) # The price obviously can't be checked by a precise value self.assertEqual(type(my_p.price), float) self.assertGreater(my_p.price, 0.1) # Make sure that wrong urls lead to exceptions with self.assertRaises(InvalidWishlistURLException): failed_p = Product.from_url("http://example.com")
def add_entity(update, context): msg = update.message text = update.message.text t_user = update.effective_user logger.info("Adding new entity for user '{}'".format(t_user.id)) reply_markup = InlineKeyboardMarkup([[cancel_button]]) user = User(user_id=t_user.id, first_name=t_user.first_name, last_name=t_user.last_name, username=t_user.username, lang_code=t_user.language_code) core.add_user_if_new(user) try: entity_type = core.get_type_by_url(text) url = core.get_e_url(text, entity_type) logger.info("Valid URL for new entity is '{}'".format(url)) except InvalidURLException: logger.warning("Invalid url '{}' sent by user {}!".format(text, t_user)) msg.reply_text(text="Die URL ist ungültig!", reply_markup=reply_markup) return try: if entity_type == EntityType.WISHLIST: entity = Wishlist.from_url(url) elif entity_type == EntityType.PRODUCT: entity = Product.from_url(url) else: raise ValueError("EntityType '{}' not found!".format(entity_type)) except HTTPError as e: logger.error(e) if e.response.status_code == 403: msg.reply_text(text="Die URL ist nicht öffentlich einsehbar, daher wurde kein neuer Preisagent erstellt!") elif e.response.status_code == 429: msg.reply_text(text="Entschuldige, ich bin temporär bei Geizhals blockiert und kann keine Preise auslesen. Bitte probiere es später noch einmal.") except (ValueError, Exception) as e: # Raised when price could not be parsed logger.error(e) msg.reply_text(text="Name oder Preis konnte nicht ausgelesen werden! Preisagent wurde nicht erstellt!") else: core.add_entity_if_new(entity) try: logger.debug("Subscribing to entity.") core.subscribe_entity(user, entity) entity_data = EntityType.get_type_article_name(entity_type) msg.reply_html("Preisagent für {article} {type} {link_name} erstellt! Aktueller Preis: {price}".format( article=entity_data.get("article"), type=entity_data.get("name"), link_name=link(entity.url, entity.name), price=bold(price(entity.price, signed=False))), disable_web_page_preview=True) context.user_data["state"] = State.IDLE except AlreadySubscribedException: logger.debug("User already subscribed!") msg.reply_text("Du hast bereits einen Preisagenten für diese URL! Bitte sende mir eine andere URL.", reply_markup=InlineKeyboardMarkup([[cancel_button]]))
def test_is_user_product_subscriber(self): """Check if checking for product subscribers works as intended""" user1 = self.user user2 = self.user2 p1 = Product(123123, "Product", "https://geizhals.de/?cat=WL-123123", 123.45) p2 = Product(987123, "Product2", "https://geizhals.de/?cat=WL-987123", 1.23) # Add user1, add user2 self.helper_add_user(user1) self.helper_add_user(user2) # Add product1, add product2 self.db.add_product(p1.entity_id, p1.name, p1.price, p1.url) self.db.add_product(p2.entity_id, p2.name, p2.price, p2.url) # subscribe user1 to product1 self.db.subscribe_product(p1.entity_id, user1.get("user_id")) # subscribe user2 to product2 self.db.subscribe_product(p2.entity_id, user2.get("user_id")) # check if user1 is subscribed to product1 -> True self.assertTrue( self.db.is_user_product_subscriber(user1.get("user_id"), p1.entity_id)) # check if user2 is subscribed to product1 -> False self.assertFalse( self.db.is_user_product_subscriber(user2.get("user_id"), p1.entity_id)) # check if user1 is subscribed to product2 -> False self.assertFalse( self.db.is_user_product_subscriber(user1.get("user_id"), p2.entity_id)) # check if user2 is subscribed to product2 -> True self.assertTrue( self.db.is_user_product_subscriber(user2.get("user_id"), p2.entity_id))
def get_all_subscribed_products(self): self.cursor.execute("SELECT p.product_id, name, price, url " "FROM products p " "INNER JOIN product_subscribers ps ON ps.product_id=p.product_id " "GROUP BY p.product_id;") product_l = self.cursor.fetchall() products = [] for line in product_l: products.append(Product(entity_id=line[0], name=line[1], price=line[2], url=line[3])) return products
def test_get_products_for_user(self): """Test to check if getting products for a user works as intended""" user = self.user p1 = Product(123123, "Product", "https://geizhals.de/?cat=WL-123123", 123.45) p2 = Product(987123, "Product2", "https://geizhals.de/?cat=WL-987123", 1.23) p3 = Product(4567418, "Product3", "https://geizhals.de/?cat=WL-987123", 154.00) local_products = [p1, p2] # Add user self.db.add_user(user.get("user_id"), user.get("first_name"), user.get("last_name"), user.get("username"), user.get("lang_code")) # Add product1 & product2 & product3 self.db.add_product(p1.entity_id, p1.name, p1.price, p1.url) self.db.add_product(p2.entity_id, p2.name, p2.price, p2.url) self.db.add_product(p3.entity_id, p3.name, p3.price, p3.url) # Subscribe user to product1 & product2 self.db.subscribe_product(p1.entity_id, user.get("user_id")) self.db.subscribe_product(p2.entity_id, user.get("user_id")) # Check if products are in the return value products = self.db.get_products_for_user(user.get("user_id")) # Make sure that both lists are the same length self.assertEqual(len(products), len(local_products)) for local_product in local_products: for product in products: if product.entity_id == local_product.entity_id: break else: # Make sure that each subscribed product is in the list self.fail("Subscribed product is not in the list")
def get_products_for_user(self, user_id): """Returns all products a user subscribed to""" self.cursor.execute( "SELECT products.product_id, products.name, products.price, products.url \ FROM 'product_subscribers' AS ps \ INNER JOIN products on products.product_id=ps.product_id \ WHERE ps.user_id=?;", [str(user_id)]) product_l = self.cursor.fetchall() products = [] for line in product_l: products.append(Product(entity_id=line[0], name=line[1], price=line[2], url=line[3])) return products
def test_get_subscribed_product_count(self): """Test to check if the subscribed product count is correct""" user_id = 11223344 first_name = "John" last_name = "Doe" username = "******" p2 = Product("9922113", "TestName", "https://geizhals.de/?cat=WL-123456", 12.12) self.db.add_product(product_id=self.p.entity_id, name=self.p.name, url=self.p.url, price=self.p.price) self.db.add_product(product_id=p2.entity_id, name=p2.name, url=p2.url, price=p2.price) # Make sure that count is 0 in the beginning self.db.add_user(user_id, first_name, last_name, username) count = self.db.get_subscribed_product_count(user_id) self.assertEqual(count, 0) # Subscribe to first product and check that count equals to 1 self.db.subscribe_product(self.p.entity_id, user_id) count = self.db.get_subscribed_product_count(user_id) self.assertEqual(count, 1) # Subscribe to second product and check that count equals to 2 self.db.subscribe_product(p2.entity_id, user_id) count = self.db.get_subscribed_product_count(user_id) self.assertEqual(count, 2) # Check that after unsubscribing the count decreases self.db.unsubscribe_product(user_id, self.p.entity_id) count = self.db.get_subscribed_product_count(user_id) self.assertEqual(count, 1)
def setUp(self): self.db_name = "test.db" self.db_name_test_create = "test_create.db" self.db = DBwrapper.get_instance(self.db_name) # Define sample wishlist and product self.wl = Wishlist(123456, "Wishlist", "https://geizhals.de/?cat=WL-123456", 123.45) self.p = Product(123456, "Product", "https://geizhals.de/a123456", 123.45) self.user = { "user_id": 415641, "first_name": "Peter", "last_name": "Müller", "username": "******", "lang_code": "en_US" } self.user2 = { "user_id": 123456, "first_name": "John", "last_name": "Doe", "username": "******", "lang_code": "de" }
def add_product(bot, update): text = update.message.text user = update.message.from_user logger.info("Adding new product for user '{}'".format(user.id)) reply_markup = InlineKeyboardMarkup([[cancel_button]]) add_user_if_new(user) try: url = get_p_url(text) logger.info("Valid URL for new product is '{}'".format(url)) except InvalidURLException: logger.warning("Invalid url '{}' sent by user {}!".format(text, user)) bot.sendMessage(chat_id=user.id, text="Die URL ist ungültig!", reply_markup=reply_markup) return try: product = Product.from_url(url) except HTTPError as e: logger.error(e) if e.code == 403: bot.sendMessage( chat_id=user.id, text= "Das Produkt ist nicht zugänglich! Preisagent wurde nicht erstellt!" ) elif e.code == 429: bot.sendMessage( chat_id=user.id, text= "Entschuldige, ich bin temporär bei Geizhals blockiert und kann keine Preise auslesen. Bitte probiere es später noch einmal." ) except ValueError as valueError: # Raised when price could not be parsed logger.error(valueError) bot.sendMessage( chat_id=user.id, text= "Name oder Preis konnte nicht ausgelesen werden! Preisagent wurde nicht erstellt!" ) except Exception as e: logger.error(e) bot.sendMessage( chat_id=user.id, text= "Name oder Preis konnte nicht ausgelesen werden! Wunschliste nicht erstellt!" ) else: add_product_if_new(product) try: logger.debug("Subscribing to product.") subscribe_entity(user, product) bot.sendMessage( user.id, "Preisagent für das Produkt {link_name} erstellt! Aktueller Preis: {price}" .format(link_name=link(product.url, product.name), price=bold(price(product.price, signed=False))), parse_mode="HTML", disable_web_page_preview=True) rm_state(user.id) except AlreadySubscribedException: logger.debug("User already subscribed!") bot.sendMessage( user.id, "Du hast bereits einen Preisagenten für dieses Produkt! Bitte sende mir eine andere URL.", reply_markup=InlineKeyboardMarkup([[cancel_button]]))