Complete Unit Test for package utils

This commit is contained in:
hantmac
2019-08-18 19:07:59 +08:00
parent 099eb86340
commit 419c667e83
3 changed files with 135 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
package utils
import (
. "github.com/smartystreets/goconvey/convey"
"os"
"testing"
)
func TestExists(t *testing.T) {
var pathString = "../config"
var wrongPathString = "test"
Convey("Test path or file is Exists or not", t, func() {
res := Exists(pathString)
Convey("The result should be true", func() {
So(res, ShouldEqual, true)
})
wrongRes := Exists(wrongPathString)
Convey("The result should be false", func() {
So(wrongRes, ShouldEqual, false)
})
})
}
func TestIsDir(t *testing.T) {
var pathString = "../config"
var fileString = "../config/config.go"
var wrongString = "test"
Convey("Test path is folder or not", t, func() {
res := IsDir(pathString)
So(res, ShouldEqual, true)
fileRes := IsDir(fileString)
So(fileRes, ShouldEqual, false)
wrongRes := IsDir(wrongString)
So(wrongRes, ShouldEqual, false)
})
}
func TestCompress(t *testing.T) {
var pathString = "../utils"
var files []*os.File
var disPath = "../utils/test"
file, err := os.Open(pathString)
if err != nil {
t.Error("open source path failed")
}
files = append(files, file)
Convey("Verify dispath is valid path", t, func() {
er := Compress(files, disPath)
Convey("err should be nil", func() {
So(er, ShouldEqual, nil)
})
})
}
// 测试之前需存在有效的test(.zip)文件
func TestDeCompress(t *testing.T) {
var tmpFilePath = "./test"
tmpFile, err := os.OpenFile(tmpFilePath, os.O_RDONLY, 0777)
if err != nil {
t.Fatal("open zip file failed")
}
var dstPath = "./testDeCompress"
Convey("Test DeCopmress func", t, func() {
err := DeCompress(tmpFile, dstPath)
So(err, ShouldEqual, nil)
})
}

View File

@@ -0,0 +1,49 @@
package utils
import (
"github.com/globalsign/mgo/bson"
. "github.com/smartystreets/goconvey/convey"
"strconv"
"testing"
"time"
)
func TestIsObjectIdNull(t *testing.T) {
var id bson.ObjectId = "123455"
Convey("Test Object ID is null or not", t, func() {
res := IsObjectIdNull(id)
So(res, ShouldEqual, false)
})
}
func TestInterfaceToString(t *testing.T) {
var valueBson bson.ObjectId = "12345"
var valueString = "12345"
var valueInt = 12345
var valueTime = time.Now().Add(60 * time.Second)
var valueOther = []string{"a", "b"}
Convey("Test InterfaceToString", t, func() {
resBson := InterfaceToString(valueBson)
Convey("resBson should be string value", func() {
So(resBson, ShouldEqual, valueBson.Hex())
})
resString := InterfaceToString(valueString)
Convey("resString should be string value", func() {
So(resString, ShouldEqual, valueString)
})
resInt := InterfaceToString(valueInt)
Convey("resInt should be string value", func() {
So(resInt, ShouldEqual, strconv.Itoa(valueInt))
})
resTime := InterfaceToString(valueTime)
Convey("resTime should be string value", func() {
So(resTime, ShouldEqual, valueTime.String())
})
resOther := InterfaceToString(valueOther)
Convey("resOther should be empty string", func() {
So(resOther, ShouldEqual, "")
})
})
}

View File

@@ -0,0 +1,14 @@
package utils
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestEncryptPassword(t *testing.T) {
var passwd = "test"
Convey("Test EncryptPassword", t, func() {
res := EncryptPassword(passwd)
t.Log(res)
})
}