Skip to content

Commit c34fa65

Browse files
Igorpython273
authored andcommitted
Fix pytest, fix docs, streaming docs (#159)
1 parent b81c1eb commit c34fa65

8 files changed

Lines changed: 58 additions & 42 deletions

File tree

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import os
2+
import pytest
3+
4+
from vk_api import VkApi
5+
6+
7+
@pytest.fixture
8+
def vk():
9+
login = os.environ['LOGIN']
10+
password = os.environ['PASSWORD']
11+
vk = VkApi(login, password)
12+
vk.auth(token_only=True)
13+
return vk

tests/test_api.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import os
2-
3-
import vk_api
4-
5-
vk = vk_api.VkApi(login=os.environ['LOGIN'], password=os.environ['PASSWORD'])
6-
vk.auth(token_only=True)
7-
api = vk.get_api()
8-
9-
10-
def test_api():
1+
def test_api(vk):
2+
api = vk.get_api()
113
user_info = api.users.get(user_ids=1)
124
assert isinstance(user_info, list)
135
assert user_info[0]['id'] == 1

tests/test_execute.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
import os
2-
3-
from vk_api import VkApi
41
from vk_api.execute import VkFunction
52

6-
vk = VkApi(login=os.environ['LOGIN'], password=os.environ['PASSWORD'])
7-
vk.auth(token_only=True)
8-
93

10-
def test_execute():
4+
def test_execute(vk):
115
func_add = VkFunction('return %(x)s + %(y)s;', args=('x', 'y'))
12-
func_get = VkFunction('return API.users.get(%(values)s)[0]["id"];',
13-
args=('values',))
6+
func_get = VkFunction(
7+
'return API.users.get(%(values)s)[0]["id"];',
8+
args=('values',)
9+
)
1410

1511
assert func_add(vk, 2, 6) == 8
1612
assert func_get(vk, {'user_ids': 'durov'}) == 1

tests/test_keyboard.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from vk_api import VkKeyboard
2-
from vk_api.keyboard import KeyboardColor
1+
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
32
from vk_api.utils import sjson_dumps
43

54
KEYBOARD_TEST = {
@@ -26,9 +25,11 @@
2625

2726

2827
def test_keyboard():
29-
keyboard.add_button('Test-1',
30-
color=KeyboardColor.DEFAULT,
31-
payload={'test': 'some_payload'})
28+
keyboard.add_button(
29+
'Test-1',
30+
color=VkKeyboardColor.DEFAULT,
31+
payload={'test': 'some_payload'}
32+
)
3233
keyboard.add_line()
3334
assert keyboard.get_keyboard() == sjson_dumps(KEYBOARD_TEST)
3435

tests/test_requests_pool.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import os
1+
from vk_api import VkRequestsPool, vk_request_one_param_pool
22

3-
from vk_api import VkApi, VkRequestsPool, vk_request_one_param_pool
43

5-
vk = VkApi(login=os.environ['LOGIN'], password=os.environ['PASSWORD'])
6-
vk.auth(token_only=True)
7-
8-
9-
def test_requests_pool():
4+
def test_requests_pool(vk):
105
with VkRequestsPool(vk) as pool:
116
users = pool.method('users.get', {'user_ids': 'durov'})
127
error_request = pool.method('invalid.method')
@@ -18,12 +13,14 @@ def test_requests_pool():
1813
assert not error_request.ok
1914

2015

21-
def test_requests_pool_one_param():
22-
users, error = vk_request_one_param_pool(vk,
23-
'users.get',
24-
key='user_ids',
25-
values=['durov', 'python273'],
26-
default_values={'fields': 'city'})
16+
def test_requests_pool_one_param(vk):
17+
users, error = vk_request_one_param_pool(
18+
vk,
19+
'users.get',
20+
key='user_ids',
21+
values=['durov', 'python273'],
22+
default_values={'fields': 'city'}
23+
)
2724

2825
assert error == {}
2926
assert isinstance(users, dict)

vk_api/longpoll.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class VkMessageFlag(IntEnum):
209209

210210

211211
class VkPeerFlag(IntEnum):
212-
"""Флаги диалогов"""
212+
""" Флаги диалогов """
213213

214214
#: Важный диалог
215215
IMPORTANT = 1

vk_api/streaming.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212
import json
1313

1414

15-
1615
class VkStreaming(object):
16+
""" Класс для работы с Streaming API
17+
18+
`Подробнее в документации VK API <https://vk.com/dev/streaming_api_docs>`__.
19+
20+
:param vk: объект :class:`VkApi`
21+
"""
1722

1823
__slots__ = ('vk', 'url', 'key', 'server')
1924

2025
URL_TEMPLATE = '{schema}://{server}/{method}?key={key}'
2126

2227
def __init__(self, vk):
23-
"""
24-
:param vk: объект VkApi
25-
"""
2628
self.vk = vk
2729

2830
self.url = None
@@ -38,6 +40,7 @@ def update_streaming_server(self):
3840
self.server = response['endpoint']
3941

4042
def get_rules(self):
43+
""" Получить список добавленных правил """
4144
response = self.vk.http.get(self.URL_TEMPLATE.format(
4245
schema='https',
4346
server=self.server,
@@ -51,6 +54,14 @@ def get_rules(self):
5154
raise VkStreamingError(response['error'])
5255

5356
def add_rule(self, value, tag):
57+
""" Добавить правило
58+
59+
:param value: Строковое представление правила
60+
:type value: str
61+
62+
:param tag: Тег правила
63+
:type tag: str
64+
"""
5465
response = self.vk.http.post(self.URL_TEMPLATE.format(
5566
schema='https',
5667
server=self.server,
@@ -65,6 +76,11 @@ def add_rule(self, value, tag):
6576
raise VkStreamingError(response['error'])
6677

6778
def delete_rule(self, tag):
79+
""" Удалить правило
80+
81+
:param tag: Тег правила
82+
:type tag: str
83+
"""
6884
response = self.vk.http.delete(self.URL_TEMPLATE.format(
6985
schema='https',
7086
server=self.server,
@@ -79,6 +95,7 @@ def delete_rule(self, tag):
7995
raise VkStreamingError(response['error'])
8096

8197
def listen(self):
98+
""" Слушать сервер """
8299
ws = websocket.create_connection(self.URL_TEMPLATE.format(
83100
schema='wss',
84101
server=self.server,

0 commit comments

Comments
 (0)