示例#1
0
    def test_get_with_update_util(self):
        with ScopedSession(self.connection) as session:
            new_user = User(id=0, name='test')
            session.add(new_user)
            self.assertEqual(1, len(session.query(User).all()))

        # Test existing group with valid information.
        with ScopedSession(self.connection) as session:
            user = get_with_update(session, User, 0, name='test')
            self.assertEqual(1, len(session.query(User).all()))
            self.assertEqual(user.id, 0)
            self.assertEqual(user.name, 'test')

        # Test existing record with updated information.
        with ScopedSession(self.connection) as session:
            user = get_with_update(session, User, 0, name='test_updated')
            self.assertEqual(1, len(session.query(User).all()))
            self.assertEqual(user.id, 0)
            self.assertEqual(user.name, 'test_updated')

        # Test new record.
        with ScopedSession(self.connection) as session:
            user = get_with_update(session, User, 1, name='another_test')
            self.assertEqual(2, len(session.query(User).all()))
            self.assertEqual(user.id, 1)
            self.assertEqual(user.name, 'another_test')
示例#2
0
    def test_session_rollback(self):
        with self.assertRaises(Exception):
            with ScopedSession(self.connection) as session:
                new_user = User(id=0, name='test')
                session.add(new_user)
                self.assertEqual(1, len(session.query(User).all()))
                session.flush()
                raise Exception

        # Make sure, that after exception inside session, session will be rolled back.
        with ScopedSession(self.connection) as session:
            self.assertEqual(0, len(session.query(User).all()))
    def test_user_report_sent(self):
        configuration = Configuration([])
        configuration.set('superuser', 'test')
        bot = MagicMock()
        updater = MagicMock()
        type(updater).bot = PropertyMock(return_value=bot)
        ReportsSender.instance = ReportsSender(bot, configuration)

        with ScopedSession(self.connection) as session:
            superuser = User(id=1234, login='******', name='Super User')
            session.add(superuser)
            user = User(id=0, login='******', name='Reporting User')
            session.add(user)

            context = MagicMock()
            type(context).sender = PropertyMock(return_value=user)
            type(context).session = PropertyMock(return_value=session)
            type(context.update.effective_chat).id = 777
            basic.on_user_report_request(context)
            context.send_response_message.assert_called_once_with(
                'waiting_for_Reporting User_report')
            context.send_response_message.reset_mock()
            session.flush()

            type(context.update.message).message_id = PropertyMock(
                return_value=4321)
            routing.dispatch_bare_message(context)
            context.send_response_message.assert_called_once_with(
                'Reporting User_report_sent')
            bot.forward_message.assert_called_once_with(1234, 777, 4321)
            self.assertEqual(0, session.query(PendingAction).count())
示例#4
0
    def setUp(self):
        super(InBotTestCase, self).setUp()
        with ScopedSession(self.connection) as session:
            session.query(User).delete()

        # Mock translation function, so that localized strings will be returned as their identifiers.
        # This will be overriden in real translation setup, if needed.
        if not getattr(builtins, '_', None):
            builtins._ = lambda x: x
    def test_user_report_cancelled(self):
        with ScopedSession(self.connection) as session:
            user = User(id=0, login='******', name='Reporting User')
            session.add(user)

            context = MagicMock()
            type(context).sender = PropertyMock(return_value=user)
            type(context).session = PropertyMock(return_value=session)
            type(context.update.effective_chat).id = 777
            basic.on_user_report_request(context)
            context.send_response_message.assert_called_once_with(
                'waiting_for_Reporting User_report')
            context.send_response_message.reset_mock()
            session.flush()

            type(context.update.message).id = PropertyMock(return_value=4321)
            basic.on_reset_action(context)
            context.send_response_message.assert_called_once_with(
                'pending_action_cancelled')
示例#6
0
    def test_pending_action_update(self):
        with ScopedSession(self.connection) as session:
            user = User(id=0, name='Test')
            session.add(user)

            # Creating new action returns empty sting.
            self.assertEqual('', user.reset_pending_action('action_1', 25))
            self.assertEqual(1, session.query(PendingAction).count())

            # Different actions for different chats.
            self.assertEqual('', user.reset_pending_action('action_2', 35))
            self.assertEqual(2, session.query(PendingAction).count())

            # Replacing an action should return previous one.
            self.assertEqual('action_1', user.reset_pending_action('action_3', 25))
            self.assertEqual(2, session.query(PendingAction).count())

            # Trying to add the same action should also return nothing.
            self.assertEqual('', user.reset_pending_action('action_3', 25))
            self.assertEqual(2, session.query(PendingAction).count())
示例#7
0
 def test_mention_name(self):
     with ScopedSession(self.connection):
         user = User(id=0, name='only_name')
         self.assertEqual('only_name', user.mention_name())
示例#8
0
 def report_exception(cls, connection: DatabaseConnection):
     with ScopedSession(connection) as session:
         superuser = cls._find_superuser(session)
         if superuser:
             cls.instance.bot.send_message(superuser.id,
                                           traceback.format_exc())