git reimport
This commit is contained in:
0
tests/zvk/plugins/vk/__init__.py
Normal file
0
tests/zvk/plugins/vk/__init__.py
Normal file
33
tests/zvk/plugins/vk/test_command.py
Normal file
33
tests/zvk/plugins/vk/test_command.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import pytest
|
||||
|
||||
from zvk.event.consumer import on_startup
|
||||
from zvk.plugins.vk.command import Argument, command
|
||||
|
||||
|
||||
@on_startup
|
||||
async def event_emitter(bot):
|
||||
yield bot.dummy_message_event(',a --inc=100', from_id=111)
|
||||
yield bot.dummy_message_event(',a --inc=10', from_id=111)
|
||||
yield bot.dummy_message_event(',a --inc=a', from_id=111)
|
||||
yield bot.dummy_message_event(',a --inc=1', from_id=123)
|
||||
|
||||
|
||||
@command('a', Argument('--inc', type=int, default=0), permissions=['admin'])
|
||||
async def command_a(bot, inc):
|
||||
bot.testing_counter += inc
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_command(bot, api):
|
||||
api.expect(method_name='messages.send', peer_id=111,
|
||||
message="🤖: Command .a argument --inc: invalid int value: 'a'")
|
||||
|
||||
api.expect(method_name='messages.send', peer_id=123,
|
||||
message="🤖: Access denied")
|
||||
|
||||
bot.event_queue.register_consumer(event_emitter)
|
||||
bot.event_queue.register_consumer(command_a)
|
||||
|
||||
assert await bot.run()
|
||||
|
||||
assert bot.testing_counter == 110
|
59
tests/zvk/plugins/vk/test_command_parser.py
Normal file
59
tests/zvk/plugins/vk/test_command_parser.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import pytest
|
||||
|
||||
from zvk.bot.bot import Bot
|
||||
from zvk.event.consumer import event_consumer, on_startup
|
||||
from zvk.event.event import Event
|
||||
from zvk.plugins.vk.api import VKApi
|
||||
from zvk.plugins.vk.command_parser import CommandEventType
|
||||
from zvk.plugins.vk.event_type import VKEventType
|
||||
|
||||
|
||||
@on_startup
|
||||
async def vk_event_emitter():
|
||||
yield Event(VKEventType.MESSAGE_NEW,
|
||||
vk_event_args=[528220, 33, 50951365, 1539933254, 'Я в автобусе щас ваще', {'title': ' ... '}, {}, 0])
|
||||
yield Event(VKEventType.MESSAGE_NEW,
|
||||
vk_event_args=[528392, 532481, 2000000049, 1539947094, ',command1', {'from': '363656437'}, {}, 0])
|
||||
yield Event(VKEventType.MESSAGE_NEW,
|
||||
vk_event_args=[528393, 532481, 2000000049, 1539947094, ',command2', {'from': '363656437'}, {}, 0])
|
||||
yield Event(VKEventType.MESSAGE_NEW,
|
||||
vk_event_args=[528397, 33, 173489181, 1539955700, 'Я литералли ходил на перекур с преподом',
|
||||
{'fwd_all_count': '0', 'fwd_count': '1', 'title': ' ... '}, {'fwd': '0_0'}, 0])
|
||||
|
||||
yield Event(VKEventType.MESSAGE_NEW,
|
||||
vk_event_args=[540583, 35, 50951365, 1541763627, ',command2 sponge bob square pants', {'title': ' ... '},
|
||||
{}, 431864521])
|
||||
|
||||
yield Event(VKEventType.MESSAGE_NEW,
|
||||
vk_event_args=[540974, 8227, 2000000055, 1541779800, ',command3 thinking stock imagte',
|
||||
{'from': '9002294'},
|
||||
{}, 1729925714])
|
||||
|
||||
|
||||
@event_consumer(consumes=[CommandEventType(command_name='command1')])
|
||||
async def consumer1(bot):
|
||||
bot.testing_counter += 1
|
||||
|
||||
|
||||
@event_consumer(consumes=[CommandEventType(command_name='command2')])
|
||||
async def consumer2(bot):
|
||||
bot.testing_counter += 2
|
||||
|
||||
|
||||
@event_consumer(consumes=[CommandEventType(command_name='command3')])
|
||||
async def consumer3(bot, echo):
|
||||
await echo('chat reply')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_command_parser(bot: Bot, api: VKApi):
|
||||
api.expect('messages.send', peer_id=2000000055, message='🤖: chat reply')
|
||||
|
||||
bot.event_queue.register_consumer(vk_event_emitter)
|
||||
bot.event_queue.register_consumer(consumer1)
|
||||
bot.event_queue.register_consumer(consumer2)
|
||||
bot.event_queue.register_consumer(consumer3)
|
||||
|
||||
assert await bot.run()
|
||||
|
||||
assert bot.testing_counter == 5
|
27
tests/zvk/plugins/vk/test_echo.py
Normal file
27
tests/zvk/plugins/vk/test_echo.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import pytest
|
||||
|
||||
from zvk.bot.bot import Bot
|
||||
from zvk.event.consumer import event_consumer, on_startup
|
||||
from zvk.plugins.vk.api import VKApi
|
||||
from zvk.plugins.vk.command_parser import CommandEventType
|
||||
from zvk.util import emoji
|
||||
|
||||
|
||||
@on_startup
|
||||
async def vk_event_emitter(bot):
|
||||
yield bot.dummy_message_event('.command1')
|
||||
|
||||
|
||||
@event_consumer(consumes=[CommandEventType(command_name='command1')])
|
||||
async def command_consumer(echo):
|
||||
assert (await echo('hi')) == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_echo(bot: Bot, api: VKApi):
|
||||
api.expect('messages.send', peer_id=123, message=f'{emoji.ROBOT}: hi').set_result(1)
|
||||
|
||||
bot.event_queue.register_consumer(vk_event_emitter)
|
||||
bot.event_queue.register_consumer(command_consumer)
|
||||
|
||||
assert await bot.run()
|
49
tests/zvk/plugins/vk/test_signatures.py
Normal file
49
tests/zvk/plugins/vk/test_signatures.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import pytest
|
||||
|
||||
from zvk.plugins.vk.command import Argument, CommandEventConsumer, CommandParseException
|
||||
|
||||
|
||||
def test_signatures_easy():
|
||||
a = CommandEventConsumer('a', Argument('n', type=int))
|
||||
|
||||
assert a.parse_argstring('1') == {'n': 1}
|
||||
|
||||
with pytest.raises(Exception):
|
||||
assert a.parse_argstring('dsa') == {'n': 1}
|
||||
|
||||
with pytest.raises(CommandParseException):
|
||||
assert a.parse_argstring('-h')
|
||||
|
||||
with pytest.raises(CommandParseException):
|
||||
assert a.parse_argstring('dsa')
|
||||
|
||||
with pytest.raises(CommandParseException):
|
||||
assert a.parse_argstring('')
|
||||
|
||||
with pytest.raises(CommandParseException):
|
||||
assert a.parse_argstring('--arg=1')
|
||||
|
||||
assert a.parse_argstring('"1"') == {'n': 1}
|
||||
|
||||
with pytest.raises(CommandParseException):
|
||||
assert a.parse_argstring('"')
|
||||
|
||||
|
||||
def test_signatures_complex():
|
||||
a = CommandEventConsumer('a',
|
||||
Argument('n', nargs='?', type=int, default=0),
|
||||
Argument('-n', '--n', dest='m', type=int, default=2),
|
||||
Argument('-v', action='store_true'),
|
||||
Argument('--s', type=str))
|
||||
|
||||
assert a.parse_argstring('1') == {'n': 1, 'm': 2, 'v': False, 's': None}
|
||||
assert a.parse_argstring('--n=1') == {'n': 0, 'm': 1, 'v': False, 's': None}
|
||||
assert a.parse_argstring('--n 1') == {'n': 0, 'm': 1, 'v': False, 's': None}
|
||||
assert a.parse_argstring('-n 1') == {'n': 0, 'm': 1, 'v': False, 's': None}
|
||||
assert a.parse_argstring('-vn1') == {'n': 0, 'm': 1, 'v': True, 's': None}
|
||||
|
||||
|
||||
def test_signatures_whole():
|
||||
a = CommandEventConsumer('a', whole_argstring=True)
|
||||
|
||||
assert a.parse_argstring('d ksja jd j jj jj --n -h 2') == {'argstring': 'd ksja jd j jj jj --n -h 2'}
|
27
tests/zvk/plugins/vk/test_testing_api.py
Normal file
27
tests/zvk/plugins/vk/test_testing_api.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import pytest
|
||||
|
||||
from zvk.event.consumer import on_startup
|
||||
|
||||
|
||||
@on_startup
|
||||
async def inc(api, bot):
|
||||
bot.counter += await api.get_magic.inc(type='test')
|
||||
bot.counter += await api.get_magic.inc(type='test')
|
||||
bot.counter += await api.get_magic.inc(type='test')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
await api.get_magic.inc(type='test')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api(bot):
|
||||
bot.api.expect('get_magic.inc', type='test').set_result(1)
|
||||
bot.api.expect('get_magic.inc', type='test').set_result(2)
|
||||
bot.api.expect('get_magic.inc', type='*').set_result(3)
|
||||
|
||||
bot.counter = 0
|
||||
bot.event_queue.register_consumer(inc)
|
||||
|
||||
assert await bot.run()
|
||||
|
||||
assert bot.counter == 6
|
28
tests/zvk/plugins/vk/test_vk_events.py
Normal file
28
tests/zvk/plugins/vk/test_vk_events.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import pytest
|
||||
|
||||
from zvk.event.consumer import on_startup
|
||||
from zvk.event.event import Event
|
||||
from zvk.plugins.vk.event_saver import VKEvent
|
||||
from zvk.plugins.vk.message_parser import Message
|
||||
from zvk.util.db import Database
|
||||
from zvk.plugins.vk.event_type import VKEventType
|
||||
|
||||
|
||||
@on_startup
|
||||
async def vk_event_emitter():
|
||||
yield Event(VKEventType.UNREAD_COUNTER_UPDATE, vk_event_args=[3, 0])
|
||||
yield Event(VKEventType.MESSAGE_NEW, vk_event_args=[528220, 33, 50951365, 1539933254, 'Я в автобусе щас ваще', {'title': ' ... '}, {}, 0])
|
||||
yield Event(VKEventType.MESSAGE_NEW, vk_event_args=[528392, 532481, 2000000049, 1539947094, 'Где философия?', {'from': '363656437'}, {}, 0])
|
||||
yield Event(VKEventType.MESSAGE_NEW, vk_event_args=[528397, 33, 173489181, 1539955700, 'Я литералли ходил на перекур с преподом', {'fwd_all_count': '0', 'fwd_count': '1', 'title': ' ... '}, {'fwd': '0_0'}, 0])
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test(db: Database, bot, api):
|
||||
bot.event_queue.register_consumer(vk_event_emitter)
|
||||
|
||||
assert await bot.run()
|
||||
|
||||
with db as session:
|
||||
assert session.query(VKEvent).count() == 4
|
||||
|
||||
assert session.query(Message).count() == 3
|
1
tests/zvk/plugins/vk/util.py
Normal file
1
tests/zvk/plugins/vk/util.py
Normal file
@@ -0,0 +1 @@
|
||||
a = 1
|
Reference in New Issue
Block a user