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

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 Просмотреть файл

@@ -9,7 +9,7 @@ import (

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

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

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

@@ -2,10 +2,21 @@ package main

import (
"fmt"
"log"
"os"
"runtime"
"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() {
APIConfig = WechatAPIConfig{
"skdq8vklaurfqemfszuif",

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

@@ -5,7 +5,7 @@ import "log"
import "encoding/json"

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

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

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

@@ -8,7 +8,7 @@ import (
)

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

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

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

e := crmdLead{}
e.FirstName = "ff" + time.Now().Format("2006-jan-02 03:04:05")
@@ -82,11 +81,11 @@ func TestCrmCreateEntityServerNotFound(t *testing.T) {
e.WechatHitxyID = "someopenid"
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/" //
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, entity, nil, "should not be able to create entity, it should be nil")
@@ -94,7 +93,7 @@ func TestCrmCreateEntityServerNotFound(t *testing.T) {
}

func TestCrmReplaceEntity(t *testing.T) {
SetupConfig()
e := crmdLead{}
e.FirstName = "ff1" + time.Now().Format("2006-jan-02 03:04:05")
e.LastName = "ll1"
@@ -123,7 +122,6 @@ func TestCrmReplaceEntity(t *testing.T) {
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")

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

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

@@ -78,7 +78,7 @@ func TestDecodeStream(t *testing.T) {
}

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

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

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

@@ -391,7 +391,7 @@ func TestSearchResultZero(t *testing.T) {
}

func TestFindOpenIDOnline(t *testing.T) {
SetupConfig()
info, found, err := crmFindOpenID("weid1")
log.Println(info)
log.Println(found)
@@ -405,7 +405,7 @@ func TestLeadJsonCompose(t *testing.T) {
}

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

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

@@ -4,7 +4,7 @@ import "testing"

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

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

@@ -5,7 +5,7 @@ import (
)

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

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

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

@@ -49,7 +49,7 @@ func TestDecodeSubscribeJson(t *testing.T) {
}

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

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

@@ -5,7 +5,7 @@ import (
)

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

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

@@ -9,7 +9,7 @@ import (
var toUser = "oUN420Wj78vnkNeAJY7RMPXA28oc" // "oUN420bxqFqlx0ZQHciUOesZO3PE"

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

// randinit()
@@ -23,18 +23,18 @@ func TestSendTxt(t *testing.T) {
}

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

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

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

// kfSendVideoByMediaID(toUser,
@@ -45,7 +45,7 @@ func TestSendVideo(t *testing.T) {
}

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

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

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

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

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

s := kfCache{}

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

@@ -6,7 +6,7 @@ import (
)

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

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

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

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

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

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

@@ -3,7 +3,7 @@ package main
import "testing"

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

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

@@ -6,7 +6,7 @@ import (
)

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

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

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

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

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

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

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

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

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

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

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

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

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

@@ -7,7 +7,7 @@ import (
)

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

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

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

@@ -12,7 +12,7 @@ import (
//when we setup wechate parameters,we chat will verify us
func TestInitialSetup(t *testing.T) {

SetupConfig()

req := buildReqWechatAPISetup()

@@ -27,7 +27,7 @@ func TestInitialSetup(t *testing.T) {
}

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

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


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

@@ -11,7 +11,7 @@ import (
)

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

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

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

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

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