コード例 #1
0
ファイル: conftest.py プロジェクト: starcraftman/cogBot
def f_asheet():
    """
    Return a mocked AsyncGSheet object.

    This is a VERY fake object with real methods that can do simple things:
        - pull in data from a filename
        - determine last row/column
        - fake the transmissions but store sent
    """
    asheet = aiomock.Mock()
    asheet.worksheet = aiomock.Mock()
    asheet.filename = None

    async def batch_update_send_(dicts, value):
        asheet.batch_update_sent = dicts

    async def batch_update_get_(*args, dim='', value_render=''):
        return asheet.batch_get.async_return_value

    async def init_():
        asheet.init_called = True

    async def values_col_(ind):
        async with aiofiles.open(asheet.filename, 'r') as fin:
            cells = eval(await fin.read())
            return cog.util.transpose_table(cells)[ind]

    async def values_row_(ind):
        async with aiofiles.open(asheet.filename, 'r') as fin:
            cells = eval(await fin.read())
            return cells[ind]

    async def whole_sheet_():
        async with aiofiles.open(asheet.filename, 'r') as fin:
            return eval(await fin.read())

    async def update_cells_():
        return True

    asheet.batch_get.async_return_value = None
    asheet.batch_update = batch_update_send_
    asheet.batch_get = batch_update_get_
    asheet.init_sheet = init_
    asheet.cells_get_range.async_return_value = None
    asheet.cells_updatea.async_return_value = None
    asheet.values_col = values_col_
    asheet.values_row = values_row_
    asheet.whole_sheet = whole_sheet_
    asheet.update_cells = update_cells_

    yield asheet
コード例 #2
0
ファイル: conftest.py プロジェクト: starcraftman/cogBot
    def __init__(self, name, *, id=None, user=None, message=None, component=None, values=None, comp_label=None):
        super().__init__(name, id)
        self.message = message
        self.user = user
        self.sent = []

        if comp_label:
            component = aiomock.Mock()
            component.label = comp_label

        self.component = component
        self.values = [component.label] if component and not values else values
コード例 #3
0
def f_bot():
    """
    Return a mocked bot.

    Bot must have methods:
        bot.send_message
        bot.send_long_message
        bot.send_ttl_message
        bot.delete_message
        bot.emoji.fix - EmojiResolver tested elsewhere
        bot.loop.run_in_executor, None, func, *args
        bot.get_member_by_substr

    Bot must have attributes:
        bot.uptime
        bot.prefix
    """
    member = aiomock.Mock()
    member.mention = "@Gears"
    fake_bot = aiomock.AIOMock(uptime=5, prefix="!")
    fake_bot.send_message.async_return_value = fake_msg_gears(
        "A message to send.")
    fake_bot.send_ttl_message.async_return_value = fake_msg_gears(
        "A ttl message to send.")
    fake_bot.send_long_message.async_return_value = fake_msg_gears(
        "A long message to send.")
    fake_bot.get_member_by_substr.return_value = member
    fake_bot.wait_for.async_return_value = None  # Whenever wait_for needed, put message here.
    fake_bot.emoji.fix = lambda x, y: x
    fake_bot.guilds = fake_servers()
    fake_bot.get_channel_by_name.return_value = 'private_dev'

    def fake_exec(_, func, *args):
        return func(*args)

    fake_bot.loop.run_in_executor.async_side_effect = fake_exec

    tick.util.BOT = fake_bot

    yield fake_bot

    tick.util.BOT = None
コード例 #4
0
ファイル: test_inara.py プロジェクト: starcraftman/cogBot
async def test_reply_with_api_result(f_bot):
    api = cog.inara.InaraApi()
    msg = fake_msg_gears('stop')
    comp = aiomock.Mock()
    comp.label = cog.inara.BUT_CANCEL
    f_bot.wait_for.async_return_value = Interaction('reply_api',
                                                    message=msg,
                                                    user=msg.author,
                                                    component=comp)

    f_bot.send_message.async_return_value = fake_msg_gears(
        "A message to send.")
    cog.util.BOT = f_bot
    f_msg = fake_msg_gears('!whois gearsandcogs')
    cmdr = await api.search_with_api('gearsandcogs', f_msg)
    await api.reply_with_api_result(cmdr["req_id"], cmdr["event_data"], f_msg)

    # TODO: Very lazy check :/
    assert 'Should the CMDR GearsandCogs be added as friendly or hostile?' in str(
        f_bot.send_message.call_args)