def setUp(self): self._db_connection = DbConnection(in_memory=True) self._user = User(self._db_connection) self.rut1 = Rut.build_rut('2.343.234-k') self.rut2 = Rut.build_rut('12.444.333-4') self.rut3 = Rut.build_rut('18.123.021-5') self.rut4 = Rut.build_rut('12.456.371-2') self.assertNotEqual(None, self.rut1) self.assertNotEqual(None, self.rut2) self.assertNotEqual(None, self.rut3) self.assertNotEqual(None, self.rut4)
def test_normalize_rut(self): self.assertEqual(None, Rut._normalize_rut('18.123.021-1')) self.assertEqual('18123021', Rut._normalize_rut('18.123.021-5')) self.assertEqual(None, Rut._normalize_rut('18.123.021-2')) self.assertEqual('18123021', Rut._normalize_rut('18.123021-5')) self.assertEqual('18123021', Rut._normalize_rut('18123.021-5')) self.assertEqual(None, Rut._normalize_rut('18123.0211')) self.assertEqual(None, Rut._normalize_rut('18.123.0212')) self.assertEqual(None, Rut._normalize_rut('181230212')) self.assertEqual('2343234', Rut._normalize_rut('2343234-k')) self.assertEqual(None, Rut._normalize_rut('12.444.333-3'))
def msg(self, bot, update: telegram.Update): """Handler when a message arrives.""" # Log every msg received. logger.debug("USR:[%s]; MSG:[%s]", update.message.from_user.id, update.message.text) rut = Rut.build_rut(update.message.text) if rut: self.query_the_bank_and_reply(update.message.from_user.id, rut, update.message.reply_text, self.ReplyWhen.ALWAYS) elif Rut.looks_like_rut(update.message.text): update.message.reply_text(Messages.LOOKS_LIKE_RUT) else: self.echo(bot, update)
def step(self, updater, hours=HOURS_TO_UPDATE): """Checks the bank for subscribed users. If useful new data is available, send a message to the user. """ user_conn = User(self._db_connection) users_to_update = user_conn.get_subscribers_to_update(hours) if not users_to_update: return user_to_update = users_to_update[random.randint( 0, len(users_to_update) - 1)] logger.debug("To update queue length: %s. Updating: user_id=%s", len(users_to_update), user_to_update.id) rut = Rut.build_rut_sin_digito(user_to_update.rut) user_chat_id = user_conn.get_chat_id(user_to_update.id) try: self.query_the_bank_and_reply( user_to_update.telegram_id, rut, partial(updater.bot.sendMessage, user_chat_id), ValeVistaBot.ReplyWhen.IS_USEFUL_FOR_USER) except telegram.error.Unauthorized: logger.debug( 'USR[%s]; CHAT_ID[%s] Unauthorized us, unsubscribing...', user_to_update.telegram_id, user_chat_id) user_conn.unsubscribe(user_to_update.telegram_id, user_chat_id)
def test_looks_like_rut(self): self.assertTrue(Rut.looks_like_rut('12.444.333')) self.assertTrue(Rut.looks_like_rut('12444333')) self.assertTrue(Rut.looks_like_rut('2444333')) self.assertTrue(Rut.looks_like_rut('102444333')) self.assertFalse(Rut.looks_like_rut('12.444.333-4')) self.assertTrue(Rut.looks_like_rut('124443334')) self.assertTrue(Rut.looks_like_rut('12444333k'))
def setUp(self): self._db_connection = DbConnection(in_memory=True) self.retriever = web_test.WebPageFromFileRetriever() self.bot = ValeVistaBot(self._db_connection, self.retriever) self.queue = queue.Queue() self.dispatcher = MockDispatcher(self.bot, self.queue) self.user1_telegram_id = get_id() self.user1 = telegram.User(id=self.user1_telegram_id, first_name='john', username='******', is_bot=False) self.chat_id = get_id() self.chat = telegram.Chat(self.chat_id, type='private', username='******', first_name='john') self.bot.add_handlers(self.dispatcher) self.pass_test = True self.rut = Rut.build_rut('2343234-k') self.rut2 = Rut.build_rut('12444333-4')
def setUp(self): self._db_connection = DbConnection(in_memory=True) self.bot = ValeVistaBot(self._db_connection) self.queue = queue.Queue() self.dispatcher = MockDispatcher(self.bot, self.queue) self.user1 = telegram.User(id=get_id(), first_name='john', username='******', is_bot=False) self.chat = telegram.Chat(get_id(), type='private', username='******', first_name='john') self.bot.add_handlers(self.dispatcher) self.pass_test = True self.rut = Rut.build_rut('2343234-k') self.rut_non_std_str = '2343.234-k'
def set_rut(self, unused_bot, update: telegram.Update): """Set a rut to easily query it in the future.""" spl = update.message.text.split(' ') if len(spl) < 2: logger.debug('USR[%s]; EMPTY_RUT', update.message.from_user.id) update.message.reply_text(Messages.SET_EMPTY_RUT) return rut = Rut.build_rut(spl[1]) if rut is None: logger.debug('USR[%s]; INVALID_RUT', update.message.from_user.id) update.message.reply_text(Messages.SET_INVALID_RUT) return User(self._db_connection).set_rut(update.message.from_user.id, rut) logger.debug("USR[%s]; SET_RUT[%s]", update.message.from_user.id, rut) update.message.reply_text(Messages.SET_RUT % rut)
def setUp(self): self.dummy_rut = Rut.build_rut('17325823-2')
def test_digito_verificador(self): self.assertEqual('2', Rut._digito_verificador('12456371')) self.assertEqual('4', Rut._digito_verificador('6786532')) self.assertEqual('8', Rut._digito_verificador('6786530')) self.assertEqual('1', Rut._digito_verificador('6786539')) self.assertEqual('k', Rut._digito_verificador('14123742'))
def test_simple(self): rut = Rut.build_rut('2343234-K') self.assertEqual(2343234, rut.rut_sin_digito) self.assertEqual('k', rut.digito_verificador)
def test_bot_representation(self): self.assertEqual('12.444.333-4', str(Rut.build_rut('12.444.333-4'))) self.assertEqual('12.444.333-4', str(Rut.build_rut('12444.333-4'))) self.assertEqual('12.444.333-4', str(Rut.build_rut('12444333-4'))) self.assertEqual('12.444.333-4', str(Rut.build_rut('12444333-4'))) self.assertEqual('2.343.234-k', str(Rut.build_rut('2343234-K')))
def test_remove_digito_verificador_puntos(self): self.assertEqual('12444333', Rut._remove_digito_verificador_puntos('12.444.333-3')) self.assertEqual('3324324', Rut._remove_digito_verificador_puntos('3.324.324-2')) self.assertEqual('3324324', Rut._remove_digito_verificador_puntos('03.324.324-2')) self.assertEqual('3432345', Rut._remove_digito_verificador_puntos('3.432.345-1')) self.assertEqual(None, Rut._remove_digito_verificador_puntos('3.4323455')) self.assertEqual('3432345', Rut._remove_digito_verificador_puntos('3.432345-5')) self.assertEqual('2343234', Rut._remove_digito_verificador_puntos('2343234-k')) self.assertEqual('2343234', Rut._remove_digito_verificador_puntos('2343234-K')) self.assertEqual(None, Rut._remove_digito_verificador_puntos('2343234k')) self.assertEqual('2343234', Rut._remove_digito_verificador_puntos('2.343.234-K')) self.assertEqual(None, Rut._remove_digito_verificador_puntos('2:343.234-K'))
def setUp(self): self.retriever = WebPageFromFileRetriever() self.base_path = TestFilesBasePath() self.dummy_rut = Rut.build_rut('12444333-3')
def get_rut(self, telegram_id): """Gets the user rut.""" user = self._get_user(telegram_id, True) if user.rut: return Rut.build_rut_sin_digito(user.rut) return None