-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
101 lines (88 loc) · 3.53 KB
/
Copy pathtest.py
File metadata and controls
101 lines (88 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from tools import EventProcessor, UnexpectedEventException
import unittest
from message_new_handler import MessageNewHandler
import types
class EventProcessorTest(unittest.TestCase):
def setUp(self):
self.group_id = 123
self.event_processor = EventProcessor(self.group_id)
#def tearDown(self):
# pass
def test_must_confirm(self):
data = {"type": "confirmation", "group_id": self.group_id, "object": None}
expected = data
actual = None
@self.event_processor.confirmation()
def confirm_handler(event):
nonlocal actual
actual = event
self.event_processor.process(data)
self.assertEqual(actual, expected)
def test_must_assept_secret(self):
secret = "1234567890"
need_secret = True
data = {"type": "confirmation", "group_id": self.group_id, "object": None, "secret": secret}
expected = data
actual = None
event_processor = EventProcessor(self.group_id, need_secret, secret)
@event_processor.confirmation()
def confirm_handler(event):
nonlocal actual
actual = event
event_processor.process(data)
self.assertEqual(actual, expected)
@unittest.expectedFailure
def test_must_not_assept_secret(self):
secret = "1234567890"
remote_secret = "4234567894"
need_secret = True
data = {"type": "confirmation", "group_id": self.group_id, "object": None, "secret": remote_secret}
expected = data
actual = None
event_processor = EventProcessor(self.group_id, need_secret, secret)
@event_processor.confirmation()
def confirm_handler(event):
nonlocal actual
actual = event
event_processor.process(data)
def test_message_new(self):
data = {"type": "message_new", "group_id": self.group_id, "object": None}
expected = data
actual = None
@self.event_processor.message_new()
def message_new_handler(event):
nonlocal actual
actual = event
self.event_processor.process(data)
self.assertEqual(actual, expected)
def test_unexpected_message(self):
data = {"type": "any_other_type", "group_id": self.group_id, "object": None}
expected = "UnexpectedEventException('Unexpected event: ', event)"
actual = None
@self.event_processor.error()
def error_fn_handler(error):
nonlocal actual
actual = error
self.event_processor.process(data)
self.assertIsInstance(actual, UnexpectedEventException)
class VkStub:
result = None
def __init__(self, response):
self.response = response
def vk_method_get(self, message_type, value):
Test_response = type('TestResponse', (), {"text": self.response})
self.result = Test_response()
return self.result
class MessageNewHandlerTest(unittest.TestCase):
def setUp(self):
self.message_send_response_stub = '{"response":35}'
self.vk_stub = VkStub(self.message_send_response_stub)
self.message_new_handler = MessageNewHandler(self.vk_stub)
#def tearDown(self):
# pass
def test_must_handle(self):
event = {"type": "message_new", "group_id": 123, "object": {"text": "test text", "from_id": 10}}
self.message_new_handler.handle(event)
self.assertEqual(self.vk_stub.result.text, self.message_send_response_stub)
if __name__ == '__main__':
unittest.main()