Просмотр исходного кода

using global setup for all test cases introducing TestMain

master
Patrick Peng Sun 8 лет назад
Родитель
Сommit
4c17c09220
18 измененных файлов: 63 добавлений и 54 удалений
  1. +2
    -2
      accessToken_test.go
  2. +11
    -0
      common_test.go
  3. +2
    -2
      crmAttachment_test.go
  4. +6
    -8
      crmEntity_test.go
  5. +1
    -1
      crmLeadStream_test.go
  6. +2
    -2
      crmLead_test.go
  7. +1
    -1
      customMenu_test.go
  8. +1
    -1
      encrypt_test.go
  9. +1
    -1
      eventSubscribe_test.go
  10. +1
    -1
      inMsg_test.go
  11. +7
    -7
      kfsend_test.go
  12. +1
    -1
      kfuser_test.go
  13. +5
    -5
      material_test.go
  14. +1
    -1
      outMsg_test.go
  15. +12
    -12
      outTemplateMsg_test.go
  16. +2
    -2
      qrcode_test.go
  17. +3
    -3
      server_test.go
  18. +4
    -4
      upload_test.go

+ 2
- 2
accessToken_test.go Просмотреть файл



//get accesstoken from wechat server or local cache file //get accesstoken from wechat server or local cache file
func TestAccessToken(t *testing.T) { func TestAccessToken(t *testing.T) {
SetupConfig()
token, err := GetAccessToken() token, err := GetAccessToken()
if err != nil { if err != nil {
t.Error("Cannot get AccessToken From Tencent Wechat Server") t.Error("Cannot get AccessToken From Tencent Wechat Server")


//read config, make sure its not empty //read config, make sure its not empty
func TestReadConfig(t *testing.T) { func TestReadConfig(t *testing.T) {
SetupConfig()
if APIConfig.AppSecret == "" { if APIConfig.AppSecret == "" {
t.Error("AppSecret not available") t.Error("AppSecret not available")
} }

+ 11
- 0
common_test.go Просмотреть файл



import ( import (
"fmt" "fmt"
"log"
"os"
"runtime" "runtime"
"testing" "testing"
) )


func TestMain(m *testing.M) {
log.Println("global setup for all test cases ... ")
SetupConfig()
code := m.Run()
//Shutdown
log.Println("global shutdown for all test cases ... ")
os.Exit(code)
}

func SetupConfig() { func SetupConfig() {
APIConfig = WechatAPIConfig{ APIConfig = WechatAPIConfig{
"skdq8vklaurfqemfszuif", "skdq8vklaurfqemfszuif",

+ 2
- 2
crmAttachment_test.go Просмотреть файл

import "encoding/json" import "encoding/json"


func TestCRMAttachFile(t *testing.T) { func TestCRMAttachFile(t *testing.T) {
SetupConfig()
r, e := crmCreateAttachment("media_for_test/200x200.png") r, e := crmCreateAttachment("media_for_test/200x200.png")
log.Println(r) log.Println(r)
log.Println(e) log.Println(e)
} }


func TestCRMDownloadAttachment(t *testing.T) { func TestCRMDownloadAttachment(t *testing.T) {
SetupConfig()
id := "59054884ef1b0c04f" id := "59054884ef1b0c04f"
//crmDownloadAttachment(id) //crmDownloadAttachment(id)
crmDownloadAttachmentAs(id, "/tmp/wechat_hitxy_测试") crmDownloadAttachmentAs(id, "/tmp/wechat_hitxy_测试")

+ 6
- 8
crmEntity_test.go Просмотреть файл

) )


func TestCrmCreateEntity(t *testing.T) { func TestCrmCreateEntity(t *testing.T) {
SetupConfig()
e := crmdLead{} e := crmdLead{}
e.FirstName = "ff" + time.Now().Format("2006-jan-02 03:04:05") e.FirstName = "ff" + time.Now().Format("2006-jan-02 03:04:05")
e.LastName = "ll" e.LastName = "ll"
} }


func TestCreateDuplicate(t *testing.T) { func TestCreateDuplicate(t *testing.T) {
SetupConfig()
e := crmdLead{} e := crmdLead{}
e.FirstName = "ff" + time.Now().Format("2006-jan-02 03:04:05") e.FirstName = "ff" + time.Now().Format("2006-jan-02 03:04:05")
e.LastName = "ll" e.LastName = "ll"
} }


func TestCrmCreateEntityServerNotFound(t *testing.T) { func TestCrmCreateEntityServerNotFound(t *testing.T) {
SetupConfig()


e := crmdLead{} e := crmdLead{}
e.FirstName = "ff" + time.Now().Format("2006-jan-02 03:04:05") e.FirstName = "ff" + time.Now().Format("2006-jan-02 03:04:05")
e.WechatHitxyID = "someopenid" e.WechatHitxyID = "someopenid"
b, _ := json.Marshal(e) b, _ := json.Marshal(e)


//quickly temporarily change base url
oldUrl := CRMConfig.BaseURL
//temporarily change base url
oldURL := CRMConfig.BaseURL
CRMConfig.BaseURL = "https://not-exist.hitxy.org.au/" // CRMConfig.BaseURL = "https://not-exist.hitxy.org.au/" //
entity, err := crmCreateEntity("Lead", b) entity, err := crmCreateEntity("Lead", b)
CRMConfig.BaseURL = oldUrl
CRMConfig.BaseURL = oldURL


AssertEqual(t, isErrIndicateDuplicate(err), false, "this should not be a duplicate error") AssertEqual(t, isErrIndicateDuplicate(err), false, "this should not be a duplicate error")
AssertEqual(t, entity, nil, "should not be able to create entity, it should be nil") AssertEqual(t, entity, nil, "should not be able to create entity, it should be nil")
} }


func TestCrmReplaceEntity(t *testing.T) { func TestCrmReplaceEntity(t *testing.T) {
SetupConfig()
e := crmdLead{} e := crmdLead{}
e.FirstName = "ff1" + time.Now().Format("2006-jan-02 03:04:05") e.FirstName = "ff1" + time.Now().Format("2006-jan-02 03:04:05")
e.LastName = "ll1" e.LastName = "ll1"
AssertEqual(t, entity.(crmdLead).WechatHitxyID, "newid", "wechat_hitxy_id should be updated") AssertEqual(t, entity.(crmdLead).WechatHitxyID, "newid", "wechat_hitxy_id should be updated")
AssertEqual(t, entity.(crmdLead).Password, "newpass", "password should have been changed to newpass") AssertEqual(t, entity.(crmdLead).Password, "newpass", "password should have been changed to newpass")


return
//delete //delete
deleted, _ := crmDeleteEntity("Lead", id) deleted, _ := crmDeleteEntity("Lead", id)
AssertEqual(t, deleted, true, "test record should have been deleted.") AssertEqual(t, deleted, true, "test record should have been deleted.")

+ 1
- 1
crmLeadStream_test.go Просмотреть файл

} }


func TestAddNewStream(t *testing.T) { func TestAddNewStream(t *testing.T) {
SetupConfig()


parentid := "595071f8450974b72" parentid := "595071f8450974b72"
parentType := "Lead" parentType := "Lead"

+ 2
- 2
crmLead_test.go Просмотреть файл

} }


func TestFindOpenIDOnline(t *testing.T) { func TestFindOpenIDOnline(t *testing.T) {
SetupConfig()
info, found, err := crmFindOpenID("weid1") info, found, err := crmFindOpenID("weid1")
log.Println(info) log.Println(info)
log.Println(found) log.Println(found)
} }


func TestGetLead(t *testing.T) { func TestGetLead(t *testing.T) {
SetupConfig()
id := "595071f8450974b72" id := "595071f8450974b72"
r, err := crmGetLead(id) r, err := crmGetLead(id)
AssertEqual(t, err, nil, "decode json should be nil") AssertEqual(t, err, nil, "decode json should be nil")

+ 1
- 1
customMenu_test.go Просмотреть файл



//This test retrieve current menu //This test retrieve current menu
func TestGetMenu(t *testing.T) { func TestGetMenu(t *testing.T) {
SetupConfig()
CreateDefaultMenu() CreateDefaultMenu()
//check menu from Wechat //check menu from Wechat
} }

+ 1
- 1
encrypt_test.go Просмотреть файл

) )


func TestEncodingMessage(t *testing.T) { func TestEncodingMessage(t *testing.T) {
SetupConfig()


//明文: //明文:
var msg = `<xml><ToUserName><![CDATA[gh_f09231355c68]]></ToUserName> var msg = `<xml><ToUserName><![CDATA[gh_f09231355c68]]></ToUserName>

+ 1
- 1
eventSubscribe_test.go Просмотреть файл

} }


func TestRegisterNewUser(t *testing.T) { func TestRegisterNewUser(t *testing.T) {
SetupConfig()
msg := ` msg := `
{ {
"subscribe": 1, "subscribe": 1,

+ 1
- 1
inMsg_test.go Просмотреть файл

) )


func TestReadCommonHeader(t *testing.T) { func TestReadCommonHeader(t *testing.T) {
SetupConfig()
var msg = `<xml><ToUserName><![CDATA[gh_f09231355c68]]></ToUserName> var msg = `<xml><ToUserName><![CDATA[gh_f09231355c68]]></ToUserName>
<FromUserName><![CDATA[oUN420bxqFqlx0ZQHciUOesZO3PE]]></FromUserName> <FromUserName><![CDATA[oUN420bxqFqlx0ZQHciUOesZO3PE]]></FromUserName>
<CreateTime>1492972518</CreateTime> <CreateTime>1492972518</CreateTime>

+ 7
- 7
kfsend_test.go Просмотреть файл

var toUser = "oUN420Wj78vnkNeAJY7RMPXA28oc" // "oUN420bxqFqlx0ZQHciUOesZO3PE" var toUser = "oUN420Wj78vnkNeAJY7RMPXA28oc" // "oUN420bxqFqlx0ZQHciUOesZO3PE"


func TestSendTxt(t *testing.T) { func TestSendTxt(t *testing.T) {
SetupConfig()
msg := fmt.Sprintf("测试消息 & < >, %s ", time.Now().String()) msg := fmt.Sprintf("测试消息 & < >, %s ", time.Now().String())


// randinit() // randinit()
} }


func TestSendPic(t *testing.T) { func TestSendPic(t *testing.T) {
SetupConfig()
kfSendPic(toUser, "media_for_test/200x200.png") kfSendPic(toUser, "media_for_test/200x200.png")
} }


func TestSendVoice(t *testing.T) { func TestSendVoice(t *testing.T) {
SetupConfig()
kfSendVoice(toUser, "media_for_test/example.amr") kfSendVoice(toUser, "media_for_test/example.amr")
//kfSendVoiceByMediaID(toUser, "8Tc-pcFxEMtbO6T71AaL0A16taJUpwolXpB06mBBfaPZ68R3__1nN7HuZAUyW8xR") //kfSendVoiceByMediaID(toUser, "8Tc-pcFxEMtbO6T71AaL0A16taJUpwolXpB06mBBfaPZ68R3__1nN7HuZAUyW8xR")
} }


func TestSendVideo(t *testing.T) { func TestSendVideo(t *testing.T) {
SetupConfig()
kfSendVideo(toUser, "media_for_test/video.mp4", "测试时品", "普通描述", "media_for_test/music-thumb.jpg") kfSendVideo(toUser, "media_for_test/video.mp4", "测试时品", "普通描述", "media_for_test/music-thumb.jpg")


// kfSendVideoByMediaID(toUser, // kfSendVideoByMediaID(toUser,
} }


func TestSendMusic(t *testing.T) { func TestSendMusic(t *testing.T) {
SetupConfig()
tID := uploadThumb("media_for_test/music-thumb.jpg") tID := uploadThumb("media_for_test/music-thumb.jpg")
description := fmt.Sprintf("音乐描述 %s", time.Now().String()) description := fmt.Sprintf("音乐描述 %s", time.Now().String())
kfSendMusic(toUser, kfSendMusic(toUser,
} }


func TestSendArticle(t *testing.T) { func TestSendArticle(t *testing.T) {
SetupConfig()
articles := []sendNewsArticle{} articles := []sendNewsArticle{}
for i := 1; i <= 8; i++ { for i := 1; i <= 8; i++ {
a := sendNewsArticle{} a := sendNewsArticle{}
} }


func TestSendArticleByMediaID(t *testing.T) { func TestSendArticleByMediaID(t *testing.T) {
SetupConfig()
kfSendMPNews(toUser, "e2iNEiSxCX5TV1WbFd0TQP4_MKtjcY1q-4UPIZFrHhw") kfSendMPNews(toUser, "e2iNEiSxCX5TV1WbFd0TQP4_MKtjcY1q-4UPIZFrHhw")
} }

+ 1
- 1
kfuser_test.go Просмотреть файл

//response data is from server, makesure the data is as expected before run this test. //response data is from server, makesure the data is as expected before run this test.
// otherwise, the test should fail // otherwise, the test should fail
func TestKfList(t *testing.T) { func TestKfList(t *testing.T) {
SetupConfig()
now := int32(time.Now().Unix()) now := int32(time.Now().Unix())


s := kfCache{} s := kfCache{}

+ 5
- 5
material_test.go Просмотреть файл

) )


func TestGetMaterialNewsList(t *testing.T) { func TestGetMaterialNewsList(t *testing.T) {
SetupConfig()
l := getNewList() l := getNewList()
log.Printf("news is : %s", l) log.Printf("news is : %s", l)
nl := unmarshalNewsList(l) nl := unmarshalNewsList(l)


} }
func TestGetMaterialVoiceList(t *testing.T) { func TestGetMaterialVoiceList(t *testing.T) {
SetupConfig()
l := getVoiceList() l := getVoiceList()
log.Printf("voice is : %s", l) log.Printf("voice is : %s", l)
} }


func TestGetMaterialVideoList(t *testing.T) { func TestGetMaterialVideoList(t *testing.T) {
SetupConfig()
l := getVideoList() l := getVideoList()
log.Printf("voice is : %s", l) log.Printf("voice is : %s", l)
} }


func TestGetMaterialImageList(t *testing.T) { func TestGetMaterialImageList(t *testing.T) {
SetupConfig()
l := getImageList() l := getImageList()
log.Printf("voice is : %s", l) log.Printf("voice is : %s", l)


} }
func TestGetMaterialCount(t *testing.T) { func TestGetMaterialCount(t *testing.T) {
SetupConfig()
mc, err := getMaterialCount() mc, err := getMaterialCount()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

+ 1
- 1
outMsg_test.go Просмотреть файл

import "testing" import "testing"


func TestBuildTxtMsg(t *testing.T) { func TestBuildTxtMsg(t *testing.T) {
SetupConfig()
s, _ := BuildTextMsg("你好", "oUN420bxqFqlx0ZQHciUOesZO3PE") s, _ := BuildTextMsg("你好", "oUN420bxqFqlx0ZQHciUOesZO3PE")
sd := decryptToXML(s) sd := decryptToXML(s)
h := ReadCommonHeader(sd) h := ReadCommonHeader(sd)

+ 12
- 12
outTemplateMsg_test.go Просмотреть файл

) )


func TestSendTemplateJoinVolunteer(t *testing.T) { func TestSendTemplateJoinVolunteer(t *testing.T) {
SetupConfig()
url := "http://www.google.com.au/" url := "http://www.google.com.au/"
first := "很高兴有你参加志愿者" first := "很高兴有你参加志愿者"
remark := "明天给你发1万块钱" remark := "明天给你发1万块钱"
} }


func TestSendTemplateJoinTeam(t *testing.T) { func TestSendTemplateJoinTeam(t *testing.T) {
SetupConfig()
url := "http://www.google.com.au/" url := "http://www.google.com.au/"
first := "欢迎加入校友会服务团队" first := "欢迎加入校友会服务团队"
remark := "给你发2万张证书" remark := "给你发2万张证书"
} }


func TestSendTemplateCheckinSuccess(t *testing.T) { func TestSendTemplateCheckinSuccess(t *testing.T) {
SetupConfig()
url := "http://www.google.com.au/" url := "http://www.google.com.au/"
first := "恭喜签到成功,你是第五位签到者" first := "恭喜签到成功,你是第五位签到者"
remark := "团结向上,一起发展" remark := "团结向上,一起发展"
} }


func TestSendTemplateMsgFormFillupDone(t *testing.T) { func TestSendTemplateMsgFormFillupDone(t *testing.T) {
SetupConfig()
url := "http://www.google.com.au" url := "http://www.google.com.au"
first := "您的录入信息已经收到,正在核实" first := "您的录入信息已经收到,正在核实"
remark := "若信息有误,会影响您校友会会员的资格" remark := "若信息有误,会影响您校友会会员的资格"
} }


func TestSendTemplateEnrollSuccess(t *testing.T) { func TestSendTemplateEnrollSuccess(t *testing.T) {
SetupConfig()
url := "http://www.google.com.au/" url := "http://www.google.com.au/"
first := "即将到来的活动,欢迎你的到来" first := "即将到来的活动,欢迎你的到来"
remark := "怎么才能让校友们积极互动是一个问题" remark := "怎么才能让校友们积极互动是一个问题"
} }


func TestSendTemplateMeetingReminder(t *testing.T) { func TestSendTemplateMeetingReminder(t *testing.T) {
SetupConfig()
url := "http://www.google.com.au/" url := "http://www.google.com.au/"
first := "恭喜签到成功,你是第五位签到者" first := "恭喜签到成功,你是第五位签到者"
remark := "怎么才能让校友们积极互动是一个问题" remark := "怎么才能让校友们积极互动是一个问题"
} }


func TestSendTemplateCheckinReminder(t *testing.T) { func TestSendTemplateCheckinReminder(t *testing.T) {
SetupConfig()
url := "http://www.google.com.au/" url := "http://www.google.com.au/"
first := "即将到来的活动,欢迎你的到来" first := "即将到来的活动,欢迎你的到来"
remark := "来晚了罚钱,你大爷的" remark := "来晚了罚钱,你大爷的"
} }


func TestSendTemplateMemberFeeReminder(t *testing.T) { func TestSendTemplateMemberFeeReminder(t *testing.T) {
SetupConfig()
url := "http://www.google.com.au/" url := "http://www.google.com.au/"
first := "校友会会员费\n" first := "校友会会员费\n"
remark := "逾期不缴费将会被列出来" remark := "逾期不缴费将会被列出来"
} }


func TestSendTemplatePaymentAck(t *testing.T) { func TestSendTemplatePaymentAck(t *testing.T) {
SetupConfig()
url := "http://www.google.com.au/" url := "http://www.google.com.au/"
first := "即将到来的活动,欢迎你的到来" first := "即将到来的活动,欢迎你的到来"
remark := "怎么才能让校友们积极互动是一个问题" remark := "怎么才能让校友们积极互动是一个问题"
} }


func TestSendTemplateCheckinFail(t *testing.T) { func TestSendTemplateCheckinFail(t *testing.T) {
SetupConfig()
url := "http://www.google.com.au/" url := "http://www.google.com.au/"
first := "--" first := "--"
remark := "checkin failed, due to your location is incorrect" remark := "checkin failed, due to your location is incorrect"
} }


func TestSendTemplateInformCollection(t *testing.T) { func TestSendTemplateInformCollection(t *testing.T) {
SetupConfig()
url := "http://www.google.com.au/" url := "http://www.google.com.au/"
first := "恭喜你获得本次Lucky Draw奖金 $200澳币" first := "恭喜你获得本次Lucky Draw奖金 $200澳币"
remark := "请你点击详情,通知你已收到奖励" remark := "请你点击详情,通知你已收到奖励"
} }


func TestSendTemplateMsgJoinCommunity(t *testing.T) { func TestSendTemplateMsgJoinCommunity(t *testing.T) {
SetupConfig()
url := "http://www.google.com.au" url := "http://www.google.com.au"
communityName := "小大同会" communityName := "小大同会"
first := "欢迎加入Community" + communityName first := "欢迎加入Community" + communityName

+ 2
- 2
qrcode_test.go Просмотреть файл

) )


func TestCreatePermQR(t *testing.T) { func TestCreatePermQR(t *testing.T) {
SetupConfig()
r, e := CreateProfileEditQR() r, e := CreateProfileEditQR()
AssertEqual(t, e, nil, "Create QR code should not have error") AssertEqual(t, e, nil, "Create QR code should not have error")
log.Println("qr ticket = " + r.Ticket) log.Println("qr ticket = " + r.Ticket)
} }


func TestCreateTempQr(t *testing.T) { func TestCreateTempQr(t *testing.T) {
SetupConfig()
r, e := CreateTempQr(000, 2592001) r, e := CreateTempQr(000, 2592001)
AssertEqual(t, e, nil, "Create Temp QR should be success") AssertEqual(t, e, nil, "Create Temp QR should be success")
log.Println("qr ticket = " + r.Ticket) log.Println("qr ticket = " + r.Ticket)

+ 3
- 3
server_test.go Просмотреть файл

//when we setup wechate parameters,we chat will verify us //when we setup wechate parameters,we chat will verify us
func TestInitialSetup(t *testing.T) { func TestInitialSetup(t *testing.T) {


SetupConfig()


req := buildReqWechatAPISetup() req := buildReqWechatAPISetup()


} }


func TestWebRootHandler(t *testing.T) { func TestWebRootHandler(t *testing.T) {
SetupConfig()
req := buildReqWechatWebRoot() req := buildReqWechatWebRoot()
rr, _ := getHTTPResponse(req, webrootHandler) rr, _ := getHTTPResponse(req, webrootHandler)


//we only check decrypted ToUserName should be the one we sent out. //we only check decrypted ToUserName should be the one we sent out.
//as decrypt itself is already a good proof of its working state. //as decrypt itself is already a good proof of its working state.
func TestPostTxtMsg(t *testing.T) { func TestPostTxtMsg(t *testing.T) {
SetupConfig()
req := buildReqWechatPostTxtMsg() req := buildReqWechatPostTxtMsg()
rr, _ := getHTTPResponse(req, apiV1Main) rr, _ := getHTTPResponse(req, apiV1Main)



+ 4
- 4
upload_test.go Просмотреть файл

) )


func TestUploadImage(t *testing.T) { func TestUploadImage(t *testing.T) {
SetupConfig()
src := "media_for_test/640x480.jpg" src := "media_for_test/640x480.jpg"
mediaID := uploadImage(src) mediaID := uploadImage(src)
fmt.Printf("get MediaID: %s \n", mediaID) fmt.Printf("get MediaID: %s \n", mediaID)
} }


func TestUploadVideo(t *testing.T) { func TestUploadVideo(t *testing.T) {
SetupConfig()
src := "media_for_test/video.mp4" src := "media_for_test/video.mp4"
mediaID := uploadVideo(src) mediaID := uploadVideo(src)
fmt.Printf("get MediaID: %s \n", mediaID) fmt.Printf("get MediaID: %s \n", mediaID)
} }


func TestUploadVoice(t *testing.T) { func TestUploadVoice(t *testing.T) {
SetupConfig()
src := "media_for_test/example.amr" src := "media_for_test/example.amr"
mediaID := uploadVoice(src) mediaID := uploadVoice(src)
log.Println(mediaID) log.Println(mediaID)
} }


func TestUploadThumb(t *testing.T) { func TestUploadThumb(t *testing.T) {
SetupConfig()
src := "media_for_test/music-thumb.jpg" src := "media_for_test/music-thumb.jpg"
mediaID := uploadThumb(src) mediaID := uploadThumb(src)
log.Println(mediaID) log.Println(mediaID)

Загрузка…
Отмена
Сохранить