git reimport

This commit is contained in:
2019-03-15 15:02:19 +04:00
commit 742797309a
90 changed files with 4411 additions and 0 deletions

0
tests/__init__.py Normal file
View File

0
tests/zvk/__init__.py Normal file
View File

View File

22
tests/zvk/bot/test_bot.py Normal file
View File

@@ -0,0 +1,22 @@
import asyncio
import pytest
from zvk.event.consumer import on_startup
@on_startup
async def kamikaze(bot):
bot.die()
@pytest.mark.asyncio
async def test_do_commit_die(bot):
bot.event_queue.register_consumer(kamikaze)
assert await bot.run()
def test_nonasync(bot):
asyncio.run(bot.run())

View File

@@ -0,0 +1,32 @@
import asyncio
import pytest
from zvk.bot.trunk import Trunk
@pytest.mark.asyncio
async def test_trunk():
trunk = Trunk()
trunk.initialize()
counter = [0]
async def f1():
counter[0] += await trunk.get('thing')
async def f2():
await asyncio.sleep(0.1)
trunk.set('thing', 1)
async def f3():
await asyncio.sleep(0.2)
counter[0] += await trunk.get('thing')
await asyncio.gather(
f1(),
f2(),
f3(),
)
assert counter[0] == 2

134
tests/zvk/conftest.py Normal file
View File

@@ -0,0 +1,134 @@
from __future__ import annotations
import json
from dataclasses import dataclass
from datetime import datetime
from logging import DEBUG
from typing import Dict, Any, List
import pytest
from main import read_config
from zvk.bot.bot import Bot
from zvk.event.event import Event
from zvk.plugins.vk.event_type import ParsedEventType
from zvk.plugins.vk.message_parser import Message
from zvk.util.db import Database
from zvk.util.zlogging import logger
from zvk.plugins.vk.api import VKApi
logger.setLevel(DEBUG)
@pytest.fixture(scope='function')
def db(bot) -> Database:
return bot.db
@pytest.fixture(scope='function')
def api() -> TestingVKApi:
api = TestingVKApi()
api.expect('users.get').set_result([{'id': 111, 'first_name': 'testing', 'last_name': 'testing'}])
return api
@pytest.fixture(scope='function')
def bot(api) -> TestingBot:
return TestingBot(api)
class TestingBot(Bot):
def __init__(self, api: TestingVKApi):
test_bot_config = read_config()
test_bot_config['db_url'] = 'sqlite:///:memory:'
test_bot_config['plugins']['whitelist'] = [
'vk.command_parser',
'vk.event_saver',
'vk.message_parser',
'init.identify_self',
'init.permissions'
]
super().__init__(config=test_bot_config)
self.db.create_all()
self.testing_counter = 0
self.api = api
def dummy_message_event(self, text: str, from_id=None):
return Event(ParsedEventType.MESSAGE, message=Message(
message_id=123,
from_id=123 if from_id is None else from_id,
peer_id=123 if from_id is None else from_id,
to_id=0,
flags=0,
timestamp=datetime.utcnow(),
text=text,
extra_fields_json='{}',
attachments_json='{}',
random_id=0,
is_outgoing=False,
is_bot_message=False,
))
@dataclass
class MockVKApiCall:
method_name: str
params: Dict[str, Any]
result: Any = None
single_use: bool = True
def does_match(self, method_name: str, params: Dict[str, Any]) -> bool:
if self.method_name != method_name:
return False
if set(self.params.keys()) != set(params.keys()):
return False
for key in self.params.keys():
expected_value = self.params[key]
actual_value = params[key]
if expected_value == '*':
continue
if expected_value != actual_value:
return False
return True
def set_result(self, result) -> MockVKApiCall:
self.result = result
return self
def set_single_use(self, single_use) -> MockVKApiCall:
self.single_use = single_use
return self
class TestingVKApi(VKApi):
_expectations: List[MockVKApiCall]
# noinspection PyMissingConstructor
def __init__(self):
self._expectations = []
def expect(self, method_name, **params):
mock_vk_api_call = MockVKApiCall(method_name=method_name, params=params)
self._expectations.append(mock_vk_api_call)
return mock_vk_api_call
async def call_method(self, method_name, **params):
for expectation in self._expectations:
if expectation.does_match(method_name, params):
if expectation.single_use:
self._expectations.remove(expectation)
logger.debug(f'Expected api call {method_name}({params}) -> {expectation.result}')
return expectation.result
raise ValueError(f'Unexpected api call {method_name}({params})')

View File

View File

@@ -0,0 +1,28 @@
import pytest
from zvk.util.zlogging import logger
@pytest.mark.asyncio
async def test_async_gen():
async def f1():
logger.info('hi 1')
async def f2():
logger.info('hi 2')
yield 1
async def f3():
logger.info('hi 3')
if False:
yield 1
with pytest.raises(Exception):
async for i in f1():
assert i == 1
async for i in f2():
assert i == 1
async for i in f3():
assert i == 1

View File

@@ -0,0 +1,74 @@
import pytest
from zvk.event.consumer import event_consumer
from zvk.event.event import Event
from zvk.event.queue import EventQueue
from zvk.util.zlogging import logger
@event_consumer(consumes=['event1'])
async def consumer_simple(counter):
logger.debug('simple_consumer')
counter[0] += 1
@event_consumer(consumes=['event1'])
async def consumer_defaults(counter, default_inc=1):
logger.debug('simple_consumer_with_defaults')
counter[0] += default_inc
@event_consumer(consumes=['event1'])
async def consumer_defaults_overridden(counter, inc=100):
logger.debug('simple_consumer_with_overridden_defaults')
counter[0] += inc
@event_consumer(consumes=['event1'])
async def consumer_producer(counter):
logger.debug('consumer_producer')
counter[0] += 1
yield Event('event2')
@event_consumer(consumes=['event2'])
async def consumer_new_event(counter):
logger.debug('another_simple_consumer')
counter[0] += 1
yield Event('event3', new_inc=1)
@event_consumer(consumes=['event3'])
async def consumer_finisher(counter, new_inc):
logger.debug('finisher')
counter[0] += new_inc
# await event_queue.omae_wa_mou_shindeiru()
@pytest.mark.asyncio
async def test_queue():
event_queue = EventQueue()
counter = [0]
event_queue.register_consumer(consumer_simple)
event_queue.register_consumer(consumer_defaults)
event_queue.register_consumer(consumer_defaults_overridden)
event_queue.register_consumer(consumer_producer)
event_queue.register_consumer(consumer_new_event)
event_queue.register_consumer(consumer_finisher)
starting_events = [Event('event1', counter=counter, inc=1)]
await event_queue.run(starting_events)
assert counter[0] == 6
await event_queue.run(starting_events)
await event_queue.run(starting_events)
assert counter[0] == 18
event_queue.deregister_consumer(consumer_producer)
await event_queue.run(starting_events)
assert counter[0] == 21

View File

@@ -0,0 +1,35 @@
import asyncio
import pytest
from zvk.event.consumer import event_consumer
from zvk.event.event import Event
from zvk.event.queue import EventQueue
@event_consumer(consumes=['event_loop'])
async def consumer_loop(event, counter):
counter[0] += 1
await asyncio.sleep(0.1)
yield event
@pytest.mark.asyncio
async def test_event_cancellation():
event_queue = EventQueue()
counter = [0]
event_queue.register_consumer(consumer_loop)
starting_events = [Event('event_loop', counter=counter)]
queue_task = asyncio.create_task(event_queue.run(starting_events))
await asyncio.sleep(0.25)
event_queue.omae_wa_mou_shindeiru()
await queue_task
assert counter[0] == 3

View File

@@ -0,0 +1,36 @@
import asyncio
import pytest
from zvk.bot.event_type import BotEventType
from zvk.event.event import Event
from zvk.event.periodic import periodic
from zvk.event.queue import EventQueue
from zvk.util.zlogging import logger
@periodic(period_secs=0.1)
async def periodic_f(counter):
counter[0] += 1
logger.debug('tick')
@pytest.mark.asyncio
async def test_periodic():
event_queue = EventQueue()
event_queue.register_consumer(periodic_f)
counter = [0]
starting_events = [Event(BotEventType.STARTUP, counter=counter)]
queue_task = asyncio.create_task(event_queue.run(starting_events))
await asyncio.sleep(0.45)
assert counter[0] == 5
event_queue.omae_wa_mou_shindeiru()
assert counter[0] == 5
await queue_task

View File

@@ -0,0 +1,39 @@
import pytest
from zvk.event.reflection import run_with_env
def test_reflection():
calls = 0
def no_args():
nonlocal calls
calls += 1
run_with_env(None, no_args)
assert calls == 1
def some_args(a, b=1):
nonlocal calls
calls += 1
assert a == 1
assert b == 2
run_with_env(dict(a=1, b=2), some_args)
assert calls == 2
def default_check(a, b=1):
nonlocal calls
calls += 1
assert a == 1
assert b == 1
run_with_env(dict(a=1), default_check)
assert calls == 3
with pytest.raises(TypeError):
run_with_env(None, default_check)
assert calls == 3

View File

@@ -0,0 +1,51 @@
import asyncio
import pytest
from zvk.bot.event_type import BotEventType
from zvk.event.consumer import event_consumer
from zvk.event.event import Event
from zvk.event.periodic import periodic
from zvk.event.queue import EventQueue
@periodic(period_secs=0.1)
async def loop1():
yield Event('inc')
@periodic(period_secs=0.1)
async def loop2():
yield Event('inc')
@event_consumer(consumes=['inc'])
async def inc(counter):
counter[0] += 1
@pytest.mark.asyncio
async def test_event_cancellation():
event_queue = EventQueue()
counter = [0]
event_queue.register_consumer(loop1)
event_queue.register_consumer(loop2)
event_queue.register_consumer(inc)
starting_events = [Event(BotEventType.STARTUP, counter=counter)]
queue_task = asyncio.create_task(event_queue.run(starting_events))
await asyncio.sleep(0.15)
assert counter[0] == 4
event_queue.deregister_consumer(loop2)
await asyncio.sleep(0.2)
assert counter[0] == 6
event_queue.omae_wa_mou_shindeiru()
await queue_task
assert counter[0] == 6

View File

View File

View File

@@ -0,0 +1,56 @@
import json
from datetime import datetime
from zvk.misc.timetable_pb2 import Timetable
import zipfile
import numpy as np
def test_read_timetable_pb():
zfile = zipfile.ZipFile('relics/6407-2.ftt')
timetable_pb_bytes = zfile.read('timetable.pb')
timetable = Timetable.FromString(timetable_pb_bytes)
term_start = datetime.fromtimestamp(timetable.properties.term_start / 1000)
weeks_count = timetable.properties.weeks_count
def convert_weeks(s):
if s == 'a':
return list(range(1, weeks_count + 1))
if s == 'o':
return list(range(1, weeks_count + 1, 2))
if s == 'e':
return list(range(2, weeks_count + 1, 2))
if s.startswith('c'):
return list(map(int, s[1:].split(',')))
raise Exception(f'Bad week identifier {s}')
timetable_dict = {
'term_start': term_start.timestamp(),
'weeks_count': weeks_count,
'lessons': []
}
for lesson in timetable.lesson:
timetable_dict['lessons'].append({
'day': lesson.day,
'time': [lesson.time[:4], lesson.time[4:]],
'weeks': convert_weeks(lesson.weeks),
'subject': timetable.subject[lesson.subject_id - 1].name,
'kind': timetable.kind[lesson.kind_id - 1].name,
'place': timetable.place[lesson.place_id - 1].name,
'teachers': [timetable.teacher[teacher_id - 1].name
for teacher_id in lesson.teacher_id]
})
timetable_json = json.dumps(timetable_dict)
print(timetable_json)
assert 'Дегтярев А. А.' in np.sum([
i['teachers']
for i in timetable_dict['lessons']
])

View File

View 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

View 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

View 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()

View 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'}

View 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

View 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

View File

@@ -0,0 +1 @@
a = 1

View File

23
tests/zvk/util/test_db.py Normal file
View File

@@ -0,0 +1,23 @@
from pytest import fixture
from sqlalchemy import Integer, String, Column
from zvk.util.db import Database, DBBase
class SomeTable(DBBase):
id = Column(Integer, primary_key=True)
s = Column(String)
def test_db(db):
db.create_all()
with db as session:
session.add(SomeTable(s='123'))
session.add(SomeTable(s='132'))
session.add(SomeTable(s='1232132132'))
assert len(session.query(SomeTable).all()) == 3

View File

@@ -0,0 +1,28 @@
import pytest
from zvk.event.consumer import on_startup
from zvk.util.download import download_file
from zvk.util.zlogging import logger
@on_startup
async def test_action(bot, net, db):
filename = await download_file(
db,
net,
'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
)
logger.info(f'{filename}')
filename = await download_file(
db,
net,
'https://yastatic.net/www/_/x/Q/xk8YidkhGjIGOrFm_dL5781YA.svg',
)
logger.info(f'{filename}')
@pytest.mark.asyncio
async def test_download(bot):
bot.event_queue.register_consumer(test_action)
assert await bot.run()

View File

@@ -0,0 +1,19 @@
import pytest
from zvk.util.network import Network
@pytest.mark.asyncio
async def test_request():
net = Network({
'net': {
'timeout': 10
}
})
net.initialize()
response, text = await net.get_text('http://ya.ru')
assert response.status == 200
assert len(text) > 200