def get_wishlist_info(self, wishlist_id): self.cursor.execute("SELECT wishlist_id, name, price, url FROM wishlists WHERE wishlist_id=?;", [str(wishlist_id)]) wishlist = self.cursor.fetchone() if wishlist is not None: wl_id, name, price, url = wishlist return Wishlist(entity_id=wl_id, name=name, price=price, url=url) return None
def get_all_wishlists(self): self.cursor.execute("SELECT wishlist_id, name, price, url FROM wishlists;") wishlist_l = self.cursor.fetchall() wishlists = [] for line in wishlist_l: wishlists.append(Wishlist(entity_id=line[0], name=line[1], price=line[2], url=line[3])) return wishlists
def test_from_url(self): """Test to check if creating a wishlist by url works as intended""" # Create a wishlist by url - needs a network connection my_wl = Wishlist.from_url(self.wl.url) self.assertEqual(type(my_wl), Wishlist) self.assertEqual(my_wl.id, self.wl.entity_id) self.assertEqual(my_wl.name, self.wl.name) self.assertEqual(my_wl.url, self.wl.url) # The price obviously can't be checked by a precise value self.assertEqual(type(my_wl.price), float) self.assertGreater(my_wl.price, 0.1) # Make sure that wrong urls lead to exceptions with self.assertRaises(InvalidWishlistURLException): failed_wl = Wishlist.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_wishlist_subscriber(self): """Check if checking for wishlist subscribers works as intended""" user1 = self.user user2 = self.user2 wl1 = Wishlist(123123, "Wishlist", "https://geizhals.de/?cat=WL-123123", 123.45) wl2 = Wishlist(987123, "Wishlist2", "https://geizhals.de/?cat=WL-987123", 1.23) # Add user1, add user2 self.helper_add_user(user1) self.helper_add_user(user2) # Add wishlist1, add wishlist2 self.db.add_wishlist(wl1.entity_id, wl1.name, wl1.price, wl1.url) self.db.add_wishlist(wl2.entity_id, wl2.name, wl2.price, wl2.url) # subscribe user1 to wishlist1 self.db.subscribe_wishlist(wl1.entity_id, user1.get("user_id")) # subscribe user2 to wishlist2 self.db.subscribe_wishlist(wl2.entity_id, user2.get("user_id")) # check if user1 is subscribed to wishlist1 -> True self.assertTrue( self.db.is_user_wishlist_subscriber(user1.get("user_id"), wl1.entity_id)) # check if user2 is subscribed to wishlist1 -> False self.assertFalse( self.db.is_user_wishlist_subscriber(user2.get("user_id"), wl1.entity_id)) # check if user1 is subscribed to wishlist2 -> False self.assertFalse( self.db.is_user_wishlist_subscriber(user1.get("user_id"), wl2.entity_id)) # check if user2 is subscribed to wishlist2 -> True self.assertTrue( self.db.is_user_wishlist_subscriber(user2.get("user_id"), wl2.entity_id))
def get_all_subscribed_wishlists(self): self.cursor.execute("SELECT w.wishlist_id, name, price, url " "FROM wishlists w " "INNER JOIN wishlist_subscribers ws ON ws.wishlist_id=w.wishlist_id " "GROUP BY w.wishlist_id;") wishlist_l = self.cursor.fetchall() wishlists = [] for line in wishlist_l: wishlists.append(Wishlist(entity_id=line[0], name=line[1], price=line[2], url=line[3])) return wishlists
def test_get_wishlists_for_user(self): """Test to check if getting wishlists for a user works as intended""" user = self.user wl1 = Wishlist(123123, "Wishlist", "https://geizhals.de/?cat=WL-123123", 123.45) wl2 = Wishlist(987123, "Wishlist2", "https://geizhals.de/?cat=WL-987123", 1.23) wl3 = Wishlist(4567418, "Wishlist3", "https://geizhals.de/?cat=WL-987123", 154.00) local_wishlists = [wl1, wl2] # Add user self.db.add_user(user.get("user_id"), user.get("first_name"), user.get("username"), user.get("lang_code")) # Add wishlist1 & wishlist2 & wishlist3 self.db.add_wishlist(wl1.entity_id, wl1.name, wl1.price, wl1.url) self.db.add_wishlist(wl2.entity_id, wl2.name, wl2.price, wl2.url) self.db.add_wishlist(wl3.entity_id, wl3.name, wl3.price, wl3.url) # Subscribe user to wishlist1 & wishlist2 self.db.subscribe_wishlist(wl1.entity_id, user.get("user_id")) self.db.subscribe_wishlist(wl2.entity_id, user.get("user_id")) # Check if wishlists are in the return value wishlists = self.db.get_wishlists_for_user(user.get("user_id")) # Make sure that both lists are the same length self.assertEqual(len(wishlists), len(local_wishlists)) for local_wishlist in local_wishlists: for wishlist in wishlists: if wishlist.entity_id == local_wishlist.entity_id: break else: # Make sure that each subscribed wishlist is in the list self.fail("Subscribed wishlist is not in the list")
def get_wishlists_for_user(self, user_id): """Return all wishlists a user subscribed to""" self.cursor.execute( "SELECT wishlists.wishlist_id, wishlists.name, wishlists.price, wishlists.url \ FROM 'wishlist_subscribers' AS ws \ INNER JOIN wishlists on wishlists.wishlist_id=ws.wishlist_id \ WHERE ws.user_id=?;", [str(user_id)]) wishlist_l = self.cursor.fetchall() wishlists = [] for line in wishlist_l: wishlists.append(Wishlist(entity_id=line[0], name=line[1], price=line[2], url=line[3])) return wishlists
class WishlistTest(unittest.TestCase): def setUp(self): self.wl = Wishlist(entity_id=676328, name="NAS", url="https://geizhals.de/?cat=WL-676328", price=617.90) def tearDown(self): del self.wl # Depending on the environment this test might fail and that's okay - skipping for the moment @unittest.skip("This test might fail depending on the environment") def test_from_url(self): """Test to check if creating a wishlist by url works as intended""" # Create a wishlist by url - needs a network connection my_wl = Wishlist.from_url(self.wl.url) self.assertEqual(type(my_wl), Wishlist) self.assertEqual(my_wl.entity_id, self.wl.entity_id) self.assertEqual(my_wl.name, self.wl.name) self.assertEqual(my_wl.url, self.wl.url) # The price obviously can't be checked by a precise value self.assertEqual(type(my_wl.price), float) self.assertGreater(my_wl.price, 0.1) # Make sure that wrong urls lead to exceptions with self.assertRaises(InvalidWishlistURLException): Wishlist.from_url("http://example.com") def test_get_wishlist_products(self): """Test to check if getting the products of a wishlist works as intended""" # Since this is not implemented yet, there should be a exception with self.assertRaises(NotImplementedError): self.wl.get_wishlist_products()
def test_get_subscribed_wishlist_count(self): """Test to check if the subscribed wishlist count is correct""" user_id = 11223344 first_name = "John" last_name = "Doe" username = "******" wl2 = Wishlist("9922113", "TestName", "https://geizhals.de/?cat=WL-123456", 12.12) self.db.add_wishlist(wishlist_id=self.wl.entity_id, name=self.wl.name, url=self.wl.url, price=self.wl.price) self.db.add_wishlist(wishlist_id=wl2.entity_id, name=wl2.name, url=wl2.url, price=wl2.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_wishlist_count(user_id) self.assertEqual(count, 0) # Subscribe to first wishlist and check that count equals to 1 self.db.subscribe_wishlist(self.wl.entity_id, user_id) count = self.db.get_subscribed_wishlist_count(user_id) self.assertEqual(count, 1) # Subscribe to second wishlist and check that count equals to 2 self.db.subscribe_wishlist(wl2.entity_id, user_id) count = self.db.get_subscribed_wishlist_count(user_id) self.assertEqual(count, 2) # Check that after unsubscribing the count decreases self.db.unsubscribe_wishlist(user_id, self.wl.entity_id) count = self.db.get_subscribed_wishlist_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 setUp(self): self.wl = Wishlist(entity_id=676328, name="NAS", url="https://geizhals.de/?cat=WL-676328", price=617.90)
def add_wishlist(bot, update): text = update.message.text user = update.message.from_user reply_markup = InlineKeyboardMarkup([[cancel_button]]) add_user_if_new(user) try: url = get_wl_url(text) except InvalidURLException: logger.debug("Invalid url '{}'!".format(text)) bot.sendMessage(chat_id=user.id, text="Die URL ist ungültig!", reply_markup=reply_markup) return # Check if website is parsable! try: wishlist = Wishlist.from_url(url) except HTTPError as e: logger.error(e) if e.code == 403: bot.sendMessage( chat_id=user.id, text= "Wunschliste ist nicht öffentlich! Wunschliste nicht hinzugefügt!" ) 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! Preisagent wurde nicht erstellt!" ) else: add_wishlist_if_new(wishlist) try: logger.debug("Subscribing to wishlist.") subscribe_entity(user, wishlist) bot.sendMessage( user.id, "Preisagent für die Wunschliste {link_name} erstellt! Aktueller Preis: {price}" .format(link_name=link(wishlist.url, wishlist.name), price=bold(price(wishlist.price, signed=False))), parse_mode="HTML", disable_web_page_preview=True) rm_state(user.id) except AlreadySubscribedException as ase: logger.debug("User already subscribed!") bot.sendMessage( user.id, "Du hast bereits einen Preisagenten für diese Wunschliste! Bitte sende mir eine andere URL.", reply_markup=InlineKeyboardMarkup([[cancel_button]]))