Пример #1
0
    def test_check_channel_mode_too_much(self):
        """validate exception if too many configs found"""
        test_channel = 'check_too_much_config'

        self.conn[connections.CHANNEL_COLLECTION].insert_one({
            'channel_name':
            test_channel,
            'channel_mode':
            'test',
            'channel_set_time':
            'datetime-not-checked',
            'user_name':
            'DummyUser'
        })
        self.conn[connections.CHANNEL_COLLECTION].insert_one({
            'channel_name':
            test_channel,
            'channel_mode':
            'test',
            'channel_set_time':
            'datetime-not-checked',
            'user_name':
            'DummyUser'
        })
        with pytest.raises(exceptions.TooManyOptions):
            channel_mode = connections.check_channel_mode(
                test_channel, self.conn)
Пример #2
0
    def test_check_channel_mode_default(self):
        """validate happy-path behavior for set_channel_mode"""
        default_mode = connections.check_channel_mode('DEFAULT_CHANNEL',
                                                      self.conn)

        assert isinstance(default_mode, connections.Modes)

        assert default_mode == connections.DEFAULT_MODE
Пример #3
0
def stock_news(message, ticker):
    """fetch relevant article for requested stock"""
    ticker = ticker.upper()
    message_info = platform_utils.parse_slack_message_object(message)
    api_config.LOGGER.info('#%s @%s -- Stock News %s',
                           message_info['channel_name'],
                           message_info['user_name'], ticker)

    mode = connections.check_channel_mode(message_info['channel'],
                                          CONN,
                                          logger=api_config.LOGGER)
    api_config.LOGGER.info('Channel mode: %s', mode.value)
    try:
        if mode == connections.Modes.stocks:
            quote = commands.generic_stock_info(
                ticker,
                CONN,
                cooldown_time=0,
                logger=api_config.LOGGER,
                info_mask=['name', 'current_price', 'change_pct'])
            if not quote:
                raise exceptions.EmptyQuoteReturned

            direction = float(quote.split()[-1].replace('%', ''))
            link, details = commands.stock_news(ticker,
                                                direction,
                                                logger=api_config.LOGGER)

        elif mode == connections.Modes.coins:
            api_config.LOGGER.warning('not supported')
            message.send('mode=coins not supported')
            quote = ''
        else:
            api_config.LOGGER.error('UNEXPECTED CHANNEL MODE -- #%s %s',
                                    message_info['channel_name'],
                                    str(mode),
                                    exc_info=True)
            quote = ''
    except exceptions.ProsperBotException:
        api_config.LOGGER.warning('Unable to resolve basic stock info for %s',
                                  ticker,
                                  exc_info=True)
        quote = 'ERROR - NO QUOTE DATA FOUND FOR {}'.format(ticker)
        link = ''
    except Exception as err:
        api_config.LOGGER.error('Unable to resolve basic stock info for %s',
                                ticker,
                                exc_info=True)
        quote = 'ERROR - UNABLE TO RESOLVE NEWS {} -- {}'.format(
            ticker, repr(err))
        link = ''

    if quote:  # only emit if there is data
        api_config.LOGGER.debug(quote)
        message.send('`' + quote + '`\n' + link  #+ ' ' + details
                     )
Пример #4
0
    def test_set_and_check_loop(self):
        """validate setting/reading channel info works as expected"""
        test_channel = 'SET_AND_CHECK'
        set_mode = connections.set_channel_mode(test_channel, 'TEST',
                                                'DummyUser', self.conn)
        ## check expected outcome ##
        assert isinstance(set_mode, connections.Modes)
        assert set_mode == connections.Modes.test

        check_mode = connections.check_channel_mode(test_channel, self.conn)
        assert isinstance(check_mode, connections.Modes)
        assert set_mode == connections.Modes.test
Пример #5
0
    def test_check_channel_mode_bad_enum(self):
        """validate enums validate as expected"""
        test_channel = 'bad_enum'

        self.conn[connections.CHANNEL_COLLECTION].insert_one({
            'channel_name':
            test_channel,
            'channel_mode':
            'butts',
            'channel_set_time':
            'datetime-not-checked',
            'user_name':
            'DummyUser'
        })
        with pytest.raises(ValueError):
            channel_mode = connections.check_channel_mode(
                test_channel, self.conn)
Пример #6
0
def generic_stock_info(message, ticker):
    """echo basic info about stock"""
    ticker = ticker.upper()
    message_info = platform_utils.parse_slack_message_object(message)
    api_config.LOGGER.info('#%s @%s -- Basic company info %s',
                           message_info['channel_name'],
                           message_info['user_name'], ticker)

    mode = connections.check_channel_mode(message_info['channel'],
                                          CONN,
                                          logger=api_config.LOGGER)
    api_config.LOGGER.info('Channel mode: %s', mode.value)
    try:
        if mode == connections.Modes.stocks:
            data = commands.generic_stock_info(ticker,
                                               CONN,
                                               cooldown_time=CONFIG.get_option(
                                                   'ProsperBot',
                                                   'generic_info', None, 30),
                                               logger=api_config.LOGGER)
        elif mode == connections.Modes.coins:
            data = commands.generic_coin_info(ticker,
                                              CONN,
                                              cooldown_time=CONFIG.get_option(
                                                  'ProsperBot', 'generic_info',
                                                  None, 30),
                                              logger=api_config.LOGGER)
        else:
            api_config.LOGGER.error('UNEXPECTED CHANNEL MODE -- #%s %s',
                                    message_info['channel_name'],
                                    str(mode),
                                    exc_info=True)
            data = ''
    except Exception:  # pramga: no cover
        api_config.LOGGER.error('Unable to resolve basic stock info for %s',
                                ticker,
                                exc_info=True)
        data = ''

    if data:  # only emit if there is data
        api_config.LOGGER.debug(data)
        message.send('`' + data + '`')