Skip to content

Commit

Permalink
Bugfixes and bumped version
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkryukov committed Apr 1, 2020
1 parent e14d5aa commit 620686e
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

> Changes to public API is marked as `^`
- v4.1.5
- Fixed `.send_message` with long messages.
- Fixed `.reply` with non-str argument.
- Fixed `.body` with command and multiline body.

- v4.1.4
- Fixed bugs in windows

Expand Down
3 changes: 3 additions & 0 deletions kutana/backends/vkontakte/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ async def resolve_screen_name(self, screen_name):
{"screen_name": screen_name}
)

if not result:
return {}

if len(NAIVE_CACHE) >= 500_000:
NAIVE_CACHE.clear()

Expand Down
4 changes: 3 additions & 1 deletion kutana/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def split_large_text(text, length=4096):
:returns: tuple of chunks
"""

text = str(text)

yield text[0: length]

for i in range(length, len(text), length):
Expand Down Expand Up @@ -159,7 +161,7 @@ async def send_message(self, target_id, message, attachments=(), **kwargs):
target_id,
part,
(),
None,
{},
))

responses.append(await self.backend.perform_send(
Expand Down
2 changes: 1 addition & 1 deletion kutana/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def _populate_cache(self, ctx):
prefixes = ctx.config["prefixes"]

self._cache = re.compile(
pattern=r"({prefix})({command})($|\s.*)".format(
pattern=r"({prefix})({command})(?:$|\s([\s\S]*))".format(
prefix="|".join(re.escape(p) for p in prefixes),
command="|".join(re.escape(c) for c in commands),
),
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import setuptools


VERSION = "4.1.4"
VERSION = "4.1.5"


with open("README.md", "r") as fh:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_context.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import asyncio
import pytest
from asynctest import MagicMock, CoroutineMock
from kutana import Context


def test_reply_non_str():
ctx = Context(backend=MagicMock(perform_send=CoroutineMock()))
ctx.default_target_id = 1
asyncio.get_event_loop().run_until_complete(ctx.reply(123))
ctx.backend.perform_send.assert_called_with(1, '123', (), {})


def test_context():
called = []

Expand Down Expand Up @@ -39,6 +47,16 @@ async def perform_api_call(self, method, kwargs):
]


def test_lont_message_for_kwargs():
ctx = Context(backend=CoroutineMock(perform_send=CoroutineMock()))
ctx.default_target_id = 1

asyncio.get_event_loop().run_until_complete(ctx.reply("a" * (4096 * 2 - 1)))

ctx.backend.perform_send.assert_any_await(1, "a" * 4096, (), {})
ctx.backend.perform_send.assert_any_await(1, "a" * 4095, (), {})


def test_dynamic_attributes():
ctx = Context()

Expand Down
16 changes: 16 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ async def _(msg, ctx):
assert debug.answers[1][2] == (".echo", (), {})
assert debug.answers[1][3] == (".ec\n123", (), {})

def test_command_full_body():
app, debug, hu = make_kutana_no_run()

pl = Plugin("")

@pl.on_commands(["echo"])
async def _(msg, ctx):
await ctx.reply(ctx.body)

app.add_plugin(pl)

hu(Message(None, UpdateType.MSG, ".echo abc\nabc\nabc", (), 1, 0, 0, 0))

assert len(debug.answers[1]) == 1
assert debug.answers[1][0] == ("abc\nabc\nabc", (), {})


def test_attachments():
app, debug, hu = make_kutana_no_run()
Expand Down
10 changes: 10 additions & 0 deletions tests/test_vkontakte.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ async def test():
asyncio.get_event_loop().run_until_complete(test())


@patch("kutana.backends.Vkontakte._request")
def test_resolve_screen_name_empty(mock_request):
mock_request.return_value = []

async def test():
vkontakte = Vkontakte(token="token", session=aiohttp.ClientSession())
assert await vkontakte.resolve_screen_name("asdjfgakyuhagkuf") == {}
asyncio.get_event_loop().run_until_complete(test())


@patch("aiohttp.ClientSession.post")
def test_perform_updates_request(mock_post):
def make_mock(exc):
Expand Down

0 comments on commit 620686e

Please sign in to comment.