1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from __future__ import absolute_import, unicode_literals |
3
|
|
|
import unittest |
4
|
|
|
import six |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
from wechatpy.enterprise.replies import TextReply, create_reply |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class CreateReplyTestCase(unittest.TestCase): |
11
|
|
|
|
12
|
|
|
def test_create_reply_with_text_not_render(self): |
13
|
|
|
text = 'test' |
14
|
|
|
reply = create_reply(text, render=False) |
15
|
|
|
self.assertEqual('text', reply.type) |
16
|
|
|
self.assertEqual(text, reply.content) |
17
|
|
|
self.assertEqual(0, reply.agent) |
18
|
|
|
|
19
|
|
|
def test_create_reply_with_text_render(self): |
20
|
|
|
text = 'test' |
21
|
|
|
reply = create_reply(text, render=True) |
22
|
|
|
self.assertTrue(isinstance(reply, six.text_type)) |
23
|
|
|
|
24
|
|
|
def test_create_reply_should_return_none(self): |
25
|
|
|
reply = create_reply(None) |
26
|
|
|
self.assertTrue(reply is None) |
27
|
|
|
|
28
|
|
|
def test_create_reply_with_message(self): |
29
|
|
|
from wechatpy.enterprise.messages import TextMessage |
30
|
|
|
|
31
|
|
|
msg = TextMessage({ |
32
|
|
|
'FromUserName': 'user1', |
33
|
|
|
'ToUserName': 'user2', |
34
|
|
|
'AgentID': 1, |
35
|
|
|
}) |
36
|
|
|
reply = create_reply('test', msg, render=False) |
37
|
|
|
|
38
|
|
|
self.assertEqual('user1', reply.target) |
39
|
|
|
self.assertEqual('user2', reply.source) |
40
|
|
|
self.assertEqual(1, reply.agent) |
41
|
|
|
|
42
|
|
|
def test_create_reply_with_reply(self): |
43
|
|
|
_reply = TextReply(content='test') |
44
|
|
|
reply = create_reply(_reply, render=False) |
45
|
|
|
|
46
|
|
|
self.assertEqual(_reply, reply) |
47
|
|
|
|
48
|
|
View Code Duplication |
def test_create_reply_with_articles(self): |
|
|
|
|
49
|
|
|
articles = [ |
50
|
|
|
{ |
51
|
|
|
'title': 'test 1', |
52
|
|
|
'description': 'test 1', |
53
|
|
|
'image': 'http://www.qq.com/1.png', |
54
|
|
|
'url': 'http://www.qq.com/1' |
55
|
|
|
}, |
56
|
|
|
{ |
57
|
|
|
'title': 'test 2', |
58
|
|
|
'description': 'test 2', |
59
|
|
|
'image': 'http://www.qq.com/2.png', |
60
|
|
|
'url': 'http://www.qq.com/2' |
61
|
|
|
}, |
62
|
|
|
{ |
63
|
|
|
'title': 'test 3', |
64
|
|
|
'description': 'test 3', |
65
|
|
|
'image': 'http://www.qq.com/3.png', |
66
|
|
|
'url': 'http://www.qq.com/3' |
67
|
|
|
}, |
68
|
|
|
] |
69
|
|
|
reply = create_reply(articles, render=False) |
70
|
|
|
self.assertEqual('news', reply.type) |
71
|
|
|
|
72
|
|
View Code Duplication |
def test_create_reply_with_more_than_ten_articles(self): |
|
|
|
|
73
|
|
|
articles = [ |
74
|
|
|
{ |
75
|
|
|
'title': 'test 1', |
76
|
|
|
'description': 'test 1', |
77
|
|
|
'image': 'http://www.qq.com/1.png', |
78
|
|
|
'url': 'http://www.qq.com/1' |
79
|
|
|
}, |
80
|
|
|
{ |
81
|
|
|
'title': 'test 2', |
82
|
|
|
'description': 'test 2', |
83
|
|
|
'image': 'http://www.qq.com/2.png', |
84
|
|
|
'url': 'http://www.qq.com/2' |
85
|
|
|
}, |
86
|
|
|
{ |
87
|
|
|
'title': 'test 3', |
88
|
|
|
'description': 'test 3', |
89
|
|
|
'image': 'http://www.qq.com/3.png', |
90
|
|
|
'url': 'http://www.qq.com/3' |
91
|
|
|
}, |
92
|
|
|
{ |
93
|
|
|
'title': 'test 4', |
94
|
|
|
'description': 'test 4', |
95
|
|
|
'image': 'http://www.qq.com/4.png', |
96
|
|
|
'url': 'http://www.qq.com/4' |
97
|
|
|
}, |
98
|
|
|
{ |
99
|
|
|
'title': 'test 5', |
100
|
|
|
'description': 'test 5', |
101
|
|
|
'image': 'http://www.qq.com/5.png', |
102
|
|
|
'url': 'http://www.qq.com/5' |
103
|
|
|
}, |
104
|
|
|
{ |
105
|
|
|
'title': 'test 6', |
106
|
|
|
'description': 'test 6', |
107
|
|
|
'image': 'http://www.qq.com/6.png', |
108
|
|
|
'url': 'http://www.qq.com/6' |
109
|
|
|
}, |
110
|
|
|
{ |
111
|
|
|
'title': 'test 7', |
112
|
|
|
'description': 'test 7', |
113
|
|
|
'image': 'http://www.qq.com/7.png', |
114
|
|
|
'url': 'http://www.qq.com/7' |
115
|
|
|
}, |
116
|
|
|
{ |
117
|
|
|
'title': 'test 8', |
118
|
|
|
'description': 'test 8', |
119
|
|
|
'image': 'http://www.qq.com/8.png', |
120
|
|
|
'url': 'http://www.qq.com/8' |
121
|
|
|
}, |
122
|
|
|
{ |
123
|
|
|
'title': 'test 9', |
124
|
|
|
'description': 'test 9', |
125
|
|
|
'image': 'http://www.qq.com/9.png', |
126
|
|
|
'url': 'http://www.qq.com/9' |
127
|
|
|
}, |
128
|
|
|
{ |
129
|
|
|
'title': 'test 10', |
130
|
|
|
'description': 'test 10', |
131
|
|
|
'image': 'http://www.qq.com/10.png', |
132
|
|
|
'url': 'http://www.qq.com/10' |
133
|
|
|
}, |
134
|
|
|
{ |
135
|
|
|
'title': 'test 11', |
136
|
|
|
'description': 'test 11', |
137
|
|
|
'image': 'http://www.qq.com/11.png', |
138
|
|
|
'url': 'http://www.qq.com/11' |
139
|
|
|
}, |
140
|
|
|
] |
141
|
|
|
self.assertRaises(AttributeError, create_reply, articles) |
142
|
|
|
|