def wrapper(update: Update, context: CallbackContext): session = bot.Session() try: result = func(update, context, session) except Exception as e: session.rollback() raise e finally: bot.Session.remove() return result
def test_givenAliasExists_whenDefinitionNotSpecified_thenDelete(self): msg = Mock() session = bot.Session() for authorName, authorId, content, name, definition in ( ("Bill", 0, "slam=", "slam", "slams for d6"), ("Bert", 86400, " rapier = ", "rapier", "d20adv + 5 then hit for d8"), ): msg.author.display_name = authorName msg.author.id = authorId session.add(Alias(user=authorId, name=name, definition=definition)) session.commit() reply = handleAlias(msg, content) self.assertEqual(reply, f"{authorName} -- {name.strip()} is no longer aliased to {definition.strip()}")
def test_storesAlias_whenDefinedByUser(self): msg = Mock() session = bot.Session() for userId, content, name, definition in ( (0, "slam = slams for d6", "slam", "slams for d6"), (86400, "rapier = d20adv + 5 then hit for d8", "rapier", "d20adv + 5 then hit for d8"), ): msg.author.display_name = "Bob" msg.author.id = userId reply = handleAlias(msg, content) self.assertEqual(reply, f"stored alias for {msg.author.display_name} = {definition}") res = session.query(Alias).filter_by(user=userId, name=name) self.assertEqual(res.count(), 1) self.assertEqual(res[0].definition, definition)
def handle_start(update: Update, context: CallbackContext): del context # Not used update.effective_chat.send_message( "Здравствуй! Я помогу распознать QR коды и другие баркоды.\n\n" "*Отправьте фото для распознования*", parse_mode=ParseMode.MARKDOWN, ) session = bot.Session() user = (session.query( model.User).filter_by(ext_user_id=update.effective_user.id).first()) if not user: user = model.User(ext_user_id=update.effective_user.id) session.add(user) session.commit() bot.Session.remove()
def test_whenCommandIsAnAlias_thenParseDefinition(self): msg = Mock() session = bot.Session() for authorName, authorId, name, definition, definitionRegex in ( ("Bill", 0, "slam", "slams for d6", r"slams for \d"), ("Bert", 86400, "rapier", "d20adv + 5 then hit for d8", r"\[\d{1,2}, \d{1,2}\] \+ 5 = \d{1,2} then hit for \d"), ): msg.author.display_name = authorName msg.author.id = authorId session.add(Alias(user=authorId, name=name, definition=definition)) session.commit() reply = handleCommand(msg, name) expectedReply = f"{authorName} -- {definitionRegex}" self.assertTrue(reply, f"Alias {name} was not executed") self.assertTrue(re.match(expectedReply, reply), f"{reply=} does not match desired {expectedReply=}")
def test_whenNoArgumentsGiven_thenPrintAllAliases(self): session = bot.Session() msg = Mock() msg.author.id = 21027 msg.author.display_name = "Dymorius" expectedHeader = f"{msg.author.display_name} has the following aliases defined:" expectedAliases = [] for content, name, definition in ( ("", "slam", "slams for d6"), (" ", "rapier", "d20adv + 5 then hit for d8"), ): expectedAliases.append(f"{name} = {definition.strip()}") expectedAliases.sort() session.add(Alias(user=msg.author.id, name=name, definition=definition)) session.commit() reply = handleAlias(msg, content) aliases = reply.split("\n")[1:] aliases.sort() self.assertEqual(reply.split("\n")[0], expectedHeader) self.assertListEqual(aliases, expectedAliases)
def tearDown(self): session = bot.Session() session.query(Alias).delete()