|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
from __future__ import absolute_import, unicode_literals |
|
3
|
|
|
import unittest |
|
4
|
|
|
|
|
5
|
|
|
from wechatpy import replies |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
def create_reply(cls, **kwargs): |
|
9
|
|
|
return cls( |
|
10
|
|
|
source="source", |
|
11
|
|
|
target="target", |
|
12
|
|
|
time=123456789, |
|
13
|
|
|
**kwargs |
|
14
|
|
|
) |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class DeserializeReplyTestCase(unittest.TestCase): |
|
18
|
|
|
|
|
19
|
|
|
def test_empty_reply_deserialize(self): |
|
20
|
|
|
reply = replies.EmptyReply() |
|
21
|
|
|
self._test_deserialize(reply) |
|
22
|
|
|
|
|
23
|
|
|
def test_text_reply_deserialize(self): |
|
24
|
|
|
reply = create_reply(replies.TextReply, content="test") |
|
25
|
|
|
self._test_deserialize(reply) |
|
26
|
|
|
|
|
27
|
|
|
def test_image_reply_deserialize(self): |
|
28
|
|
|
reply = create_reply(replies.ImageReply, media_id="media_id") |
|
29
|
|
|
self._test_deserialize(reply) |
|
30
|
|
|
|
|
31
|
|
|
def test_voice_reply_deserialize(self): |
|
32
|
|
|
reply = create_reply(replies.VoiceReply, media_id="media_id") |
|
33
|
|
|
self._test_deserialize(reply) |
|
34
|
|
|
|
|
35
|
|
|
def test_video_reply_deserialize(self): |
|
36
|
|
|
reply = create_reply( |
|
37
|
|
|
replies.VideoReply, media_id="media_id", |
|
38
|
|
|
title="title", description="说个中文呗") |
|
39
|
|
|
self._test_deserialize(reply) |
|
40
|
|
|
# 非必填字段 |
|
41
|
|
|
reply = create_reply( |
|
42
|
|
|
replies.VideoReply, media_id="media_id", title="无描述") |
|
43
|
|
|
self._test_deserialize(reply) |
|
44
|
|
|
|
|
45
|
|
|
def test_music_reply_deserialize(self): |
|
46
|
|
|
reply = create_reply( |
|
47
|
|
|
replies.MusicReply, title="title", description="desc", |
|
48
|
|
|
music_url="music_url", hq_music_url="hq_music_url", |
|
49
|
|
|
thumb_media_id="thumb_media_id") |
|
50
|
|
|
self._test_deserialize(reply) |
|
51
|
|
|
# 非必填字段 |
|
52
|
|
|
reply = create_reply(replies.MusicReply, thumb_media_id="media_id") |
|
53
|
|
|
self._test_deserialize(reply) |
|
54
|
|
|
|
|
55
|
|
|
def test_article_reply_deserialize(self): |
|
56
|
|
|
articles = [ |
|
57
|
|
|
{ |
|
58
|
|
|
'title': 'test 1', |
|
59
|
|
|
'description': 'test 1', |
|
60
|
|
|
'image': 'http://www.qq.com/1.png', |
|
61
|
|
|
'url': 'http://www.qq.com/1' |
|
62
|
|
|
}, |
|
63
|
|
|
{ |
|
64
|
|
|
'title': 'test 2', |
|
65
|
|
|
'description': 'test 2', |
|
66
|
|
|
'image': 'http://www.qq.com/2.png', |
|
67
|
|
|
'url': 'http://www.qq.com/2' |
|
68
|
|
|
}, |
|
69
|
|
|
{ |
|
70
|
|
|
'title': 'test 3', |
|
71
|
|
|
'description': 'test 3', |
|
72
|
|
|
'image': 'http://www.qq.com/3.png', |
|
73
|
|
|
'url': 'http://www.qq.com/3' |
|
74
|
|
|
}, |
|
75
|
|
|
] |
|
76
|
|
|
reply = create_reply(replies.ArticlesReply, articles=articles) |
|
77
|
|
|
self._test_deserialize(reply) |
|
78
|
|
|
|
|
79
|
|
|
def _test_deserialize(self, reply): |
|
80
|
|
|
xml = reply.render() |
|
81
|
|
|
deserialized = replies.deserialize_reply(xml) |
|
82
|
|
|
xml2 = deserialized.render() |
|
83
|
|
|
self.assertEqual(xml, xml2) |
|
84
|
|
|
|