Sfoglia il codice sorgente

article test case nexted structure worked.

master
Patrick Peng Sun 8 anni fa
parent
commit
5e2606bbf8
4 ha cambiato i file con 173 aggiunte e 20 eliminazioni
  1. +54
    -12
      material.go
  2. +88
    -0
      material_test.go
  3. +6
    -6
      sample_data/material_list.json
  4. +25
    -2
      sample_data/sample_news_list.json

+ 54
- 12
material.go Vedi File

@@ -9,12 +9,14 @@ import (
"net/http"
)

//get a list of material
type materialQuery struct {
Type string `json:"type"`
Offiset int `json:"offset"`
Count int `json:"count"`
}

//overall material count
type materialCount struct {
VoiceCount int64 `json:"voice_count"`
VideoCount int64 `json:"video_count"`
@@ -22,6 +24,54 @@ type materialCount struct {
NewsCount int64 `json:"news_count"`
}

//Material Image Item
type materialItemImage struct {
MediaID string `json:"media_id"`
Name string `json:"name"`
UpdateTime int `json:"update_time"`
URL string `json:"url"`
}

type materialImageList struct {
TotalCount int `json:"total_count"`
ItemCount int `json:"item_count"`
Items []materialItemImage `json:"item"`
}

//Material News Item - Article
type materialArticle struct {
Title string `json:"title"`
Author string `json:"author"`
Digest string `json:"digest"`
Content string `json:"content"`
ContentSourceURL string `json:"content_source_url"`
ThumbMediaID string `json:"thumb_media_id"`
ShowCoverPic int `json:"show_cover_pic"`
URL string `json:"url"`
ThumbURL string `json:"thumb_url"`
NeedOpenComment int `json:"need_open_comment"`
OnlyFansCanComment int `json:"only_fans_can_comment"`
}

type materialNewsItem struct {
MediaID string `json:"media_id"`
UpdateTime int `json:"update_time"`
Content struct {
CreateTime int `json:"create_time"`
UpdateTime int `json:"update_time"`
NewsItem []materialArticle `json:"news_item"`
} `json:"content"`
}

type materialNewsList struct {
TotalCount int `json:"total_count"`
ItemCount int `json:"item_count"`
Items []materialNewsItem `json:"item"`
}

//
//-- function definition start
//
func getNewList() (jstr string) {
jstr = getMediaList("news")
return
@@ -88,20 +138,12 @@ func getMaterialCount() (materialCount, error) {
return mc, nil
}

type materialItemImage struct {
MediaID string `json:"media_id"`
Name string `json:"name"`
UpdateTime int `json:"update_time"`
URL string `json:"url"`
}

type materialListImage struct {
TotalCount int `json:"total_count"`
ItemCount int `json:"item_count"`
Items []materialItemImage `json:"item"`
func unmarshalImageList(jstr string) (r materialImageList) {
json.Unmarshal([]byte(jstr), &r)
return
}

func unmarshalImageList(jstr string) (r materialListImage) {
func unmarshalNewsList(jstr string) (r materialNewsList) {
json.Unmarshal([]byte(jstr), &r)
return
}

+ 88
- 0
material_test.go Vedi File

@@ -76,3 +76,91 @@ func TestUnmarshalImageList(t *testing.T) {
AssertEqual(t, r.Items[2].URL, "http://mmbiz.qpic.cn/mmbiz_png/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw/0?wx_fmt=png", "Item[2] URL wrong")

}

func TestUnmarshalNewsList(t *testing.T) {
msg := `
{
"item": [
{
"media_id": "e2iNEiSxCX5TV1WbFd0TQP4_MKtjcY1q-4UPIZFrHhw",
"content": {
"news_item": [
{
"title": "新人入会前几周推荐",
"author": "孙鹏",
"digest": "摘要",
"content": "<p>&nbsp;&nbsp;&nbsp;&nbsp;新人入会,前几周推荐做如下几件事情<\/p><p><img data-s=\"300,640\" data-type=\"jpeg\" data-src=\"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUs0YQBiatN97ibIgOWE4picXg49jj1k5TRibswd7DzLC1CCvJco0Kic9T90g\/0?wx_fmt=jpeg\" data-ratio=\"0.9035087719298246\" data-w=\"342\" \/><\/p><p><br \/><\/p>",
"content_source_url": "",
"thumb_media_id": "SCqYVM0F3OSBIqptPWQ5ZjDYlLvKsEtHHaYnHLyXbeM",
"show_cover_pic": 0,
"url": "http:\/\/mp.weixin.qq.com\/s?__biz=MzUxMDAzNDAwMA==&mid=100000008&idx=1&sn=cbfd3f5f5367ca67f9a1184eb394f788&chksm=790868374e7fe121ac5e1d27f6d082c3f8e8033eb4e7158615d0a69dc76ac2971e5650b28fac#rd",
"thumb_url": "http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUs0YQBiatN97ibIgOWE4picXg49jj1k5TRibswd7DzLC1CCvJco0Kic9T90g\/0?wx_fmt=jpeg",
"need_open_comment": 0,
"only_fans_can_comment": 0
}
],
"create_time": 1494488254,
"update_time": 1494488268
},
"update_time": 1494488268
},
{
"media_id": "e2iNEiSxCX5TV1WbFd0TQMqvVrqFDbDOacdjgQ-OAuE",
"content": {
"news_item": [
{
"title": "测试消息",
"author": "孙鹏",
"digest": "加入校友会",
"content": "<p>加入校友会<\/p><p><img data-s=\"300,640\" data-type=\"png\" data-src=\"http:\/\/mmbiz.qpic.cn\/mmbiz_png\/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw\/0?wx_fmt=png\" data-ratio=\"0.6765327695560254\" data-w=\"946\" \/><\/p><p><br \/><\/p>",
"content_source_url": "http:\/\/www.hitxy.org.au\/join",
"thumb_media_id": "SCqYVM0F3OSBIqptPWQ5ZjDYlLvKsEtHHaYnHLyXbeM",
"show_cover_pic": 0,
"url": "http:\/\/mp.weixin.qq.com\/s?__biz=MzUxMDAzNDAwMA==&mid=100000002&idx=1&sn=723bf16b52f88345e5a73ccde1c79bdb&chksm=7908683d4e7fe12bcaefa9c563809db5546ab97af7184ec648f20930fab5c1d95ee825409c0d#rd",
"thumb_url": "http:\/\/mmbiz.qpic.cn\/mmbiz_png\/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw\/0?wx_fmt=png",
"need_open_comment": 0,
"only_fans_can_comment": 0
}
],
"create_time": 1492696196,
"update_time": 1492696243
},
"update_time": 1492696243
}
],
"total_count": 20,
"item_count": 2
}
`
r := unmarshalNewsList(msg)
AssertEqual(t, r.TotalCount, 20, "Total count should be 20")
AssertEqual(t, r.ItemCount, 2, "Item count should be 2")
cnt := len(r.Items)
AssertEqual(t, cnt, 2, "")

//first item
item := r.Items[0]
AssertEqual(t, item.MediaID, "e2iNEiSxCX5TV1WbFd0TQP4_MKtjcY1q-4UPIZFrHhw", "Item[0] media ID wrong")
//AssertEqual(t, item.CreateTime, 1494488254, "Item[0] create time wrong")
AssertEqual(t, item.UpdateTime, 1494488268, "Item[0] update time wrong")

//second item
item = r.Items[1]
AssertEqual(t, item.MediaID, "e2iNEiSxCX5TV1WbFd0TQMqvVrqFDbDOacdjgQ-OAuE", "Item[1] media ID wrong")
//AssertEqual(t, item.CreateTime, 1494488254, "Item[1] create time wrong")
AssertEqual(t, item.UpdateTime, 1492696243, "Item[1] update time wrong")
AssertEqual(t, item.Content.CreateTime, 1492696196, "Item[1] update time wrong")
AssertEqual(t, item.Content.UpdateTime, 1492696243, "Item[1] update time wrong")
AssertEqual(t, item.Content.NewsItem[0].Title, "测试消息", "Item[1] update time wrong")
AssertEqual(t, item.Content.NewsItem[0].Author, "孙鹏", "Item[1] update time wrong")
AssertEqual(t, item.Content.NewsItem[0].Digest, "加入校友会", "Item[1] update time wrong")
AssertEqual(t, item.Content.NewsItem[0].Content, "<p>加入校友会</p><p><img data-s=\"300,640\" data-type=\"png\" data-src=\"http://mmbiz.qpic.cn/mmbiz_png/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw/0?wx_fmt=png\" data-ratio=\"0.6765327695560254\" data-w=\"946\" /></p><p><br /></p>", "article content wrong")
AssertEqual(t, item.Content.NewsItem[0].ContentSourceURL, "http://www.hitxy.org.au/join", "Item[1] update time wrong")
AssertEqual(t, item.Content.NewsItem[0].ThumbMediaID, "SCqYVM0F3OSBIqptPWQ5ZjDYlLvKsEtHHaYnHLyXbeM", "Item[1] update time wrong")
AssertEqual(t, item.Content.NewsItem[0].ShowCoverPic, 0, "Item[1] update time wrong")
AssertEqual(t, item.Content.NewsItem[0].URL, "http://mp.weixin.qq.com/s?__biz=MzUxMDAzNDAwMA==&mid=100000002&idx=1&sn=723bf16b52f88345e5a73ccde1c79bdb&chksm=7908683d4e7fe12bcaefa9c563809db5546ab97af7184ec648f20930fab5c1d95ee825409c0d#rd", "Article URL is not right")
AssertEqual(t, item.Content.NewsItem[0].ThumbURL, "http://mmbiz.qpic.cn/mmbiz_png/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw/0?wx_fmt=png", "Article Thumb URL wrong")
AssertEqual(t, item.Content.NewsItem[0].NeedOpenComment, 0, "Need Open Comment should be 0")
AssertEqual(t, item.Content.NewsItem[0].OnlyFansCanComment, 0, "OnlyFansCanComment should be 0")

}

+ 6
- 6
sample_data/material_list.json Vedi File

@@ -1,5 +1,5 @@
2017/05/11 11: 27: 17 https: //api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=RB8viLdrGw91XDjX29crjt9Dz242CWL6QqXQmyYcr2Pb012ZkdzdJR17uLwqjZApTnSVZYfofLWdVMN2wmszwk_1ACT29Xev1PwnKUuZEXpoChl8U-MteAgNBa34kLANUMNhABALDE 2017/05/11 11: 27: 18 news is: {
"item": [
//2017/05/11 11: 27: 17 https: //api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=RB8viLdrGw91XDjX29crjt9Dz242CWL6QqXQmyYcr2Pb012ZkdzdJR17uLwqjZApTnSVZYfofLWdVMN2wmszwk_1ACT29Xev1PwnKUuZEXpoChl8U-MteAgNBa34kLANUMNhABALDE 2017/05/11 11: 27: 18 news is: {
{ "item": [
{
"media_id": "e2iNEiSxCX5TV1WbFd0TQMqvVrqFDbDOacdjgQ-OAuE",
"content": {
@@ -27,13 +27,13 @@
"total_count": 1,
"item_count": 1
}
2017/05/11 11: 27: 18 read authtoke from /tmp/wechat_hitxy_access_token2017/05/11 11: 27: 18 https: //api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=RB8viLdrGw91XDjX29crjt9Dz242CWL6QqXQmyYcr2Pb012ZkdzdJR17uLwqjZApTnSVZYfofLWdVMN2wmszwk_1ACT29Xev1PwnKUuZEXpoChl8U-MteAgNBa34kLANUMNhABALDE 2017/05/11 11: 27: 18 voice is: {
"item": [],
//2017/05/11 11: 27: 18 read authtoke from /tmp/wechat_hitxy_access_token2017/05/11 11: 27: 18 https: //api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=RB8viLdrGw91XDjX29crjt9Dz242CWL6QqXQmyYcr2Pb012ZkdzdJR17uLwqjZApTnSVZYfofLWdVMN2wmszwk_1ACT29Xev1PwnKUuZEXpoChl8U-MteAgNBa34kLANUMNhABALDE 2017/05/11 11: 27: 18 voice is: {
{ "item": [],
"total_count": 0,
"item_count": 0
}
2017/05/11 11: 27: 18 read authtoke from /tmp/wechat_hitxy_access_token2017/05/11 11: 27: 18 https: //api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=RB8viLdrGw91XDjX29crjt9Dz242CWL6QqXQmyYcr2Pb012ZkdzdJR17uLwqjZApTnSVZYfofLWdVMN2wmszwk_1ACT29Xev1PwnKUuZEXpoChl8U-MteAgNBa34kLANUMNhABALDE 2017/05/11 11: 27: 19 video is: {
"item": [
//2017/05/11 11: 27: 18 read authtoke from /tmp/wechat_hitxy_access_token2017/05/11 11: 27: 18 https: //api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=RB8viLdrGw91XDjX29crjt9Dz242CWL6QqXQmyYcr2Pb012ZkdzdJR17uLwqjZApTnSVZYfofLWdVMN2wmszwk_1ACT29Xev1PwnKUuZEXpoChl8U-MteAgNBa34kLANUMNhABALDE 2017/05/11 11: 27: 19 video is: {
{ "item": [
{
"media_id": "e2iNEiSxCX5TV1WbFd0TQPZtC5Oz_9VZmIKEyC7WW8w",
"name": "哈工大校歌",

+ 25
- 2
sample_data/sample_news_list.json Vedi File

@@ -1,5 +1,28 @@
{
"item": [
{
"media_id": "e2iNEiSxCX5TV1WbFd0TQP4_MKtjcY1q-4UPIZFrHhw",
"content": {
"news_item": [
{
"title": "新人入会前几周推荐",
"author": "孙鹏",
"digest": "摘要",
"content": "<p>&nbsp;&nbsp;&nbsp;&nbsp;新人入会,前几周推荐做如下几件事情<\/p><p><img data-s=\"300,640\" data-type=\"jpeg\" data-src=\"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUs0YQBiatN97ibIgOWE4picXg49jj1k5TRibswd7DzLC1CCvJco0Kic9T90g\/0?wx_fmt=jpeg\" data-ratio=\"0.9035087719298246\" data-w=\"342\" \/><\/p><p><br \/><\/p>",
"content_source_url": "",
"thumb_media_id": "SCqYVM0F3OSBIqptPWQ5ZjDYlLvKsEtHHaYnHLyXbeM",
"show_cover_pic": 0,
"url": "http:\/\/mp.weixin.qq.com\/s?__biz=MzUxMDAzNDAwMA==&mid=100000008&idx=1&sn=cbfd3f5f5367ca67f9a1184eb394f788&chksm=790868374e7fe121ac5e1d27f6d082c3f8e8033eb4e7158615d0a69dc76ac2971e5650b28fac#rd",
"thumb_url": "http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUs0YQBiatN97ibIgOWE4picXg49jj1k5TRibswd7DzLC1CCvJco0Kic9T90g\/0?wx_fmt=jpeg",
"need_open_comment": 0,
"only_fans_can_comment": 0
}
],
"create_time": 1494488254,
"update_time": 1494488268
},
"update_time": 1494488268
},
{
"media_id": "e2iNEiSxCX5TV1WbFd0TQMqvVrqFDbDOacdjgQ-OAuE",
"content": {
@@ -24,6 +47,6 @@
"update_time": 1492696243
}
],
"total_count": 1,
"item_count": 1
"total_count": 2,
"item_count": 2
}

Loading…
Annulla
Salva