From f694aea59084ab65df31ed2efe801f042e2c55cb Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Sun, 18 Aug 2019 13:09:37 +0800 Subject: [PATCH 1/4] updated Jenkinsfile --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index a6f5f9ab..774975db 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -46,7 +46,7 @@ pipeline { steps { echo 'Deploying....' sh """ - if [ ${ENV:GIT_BRANCH} -eq master ] || [ ${ENV:GIT_BRANCH} -eq develop ]; then + if [[ ${ENV:GIT_BRANCH} = master ]] || [[ ${ENV:GIT_BRANCH} = develop ]]; then # 重启docker compose cd ./jenkins/${ENV:GIT_BRANCH} docker-compose stop | true From c516596e1d4c577183fefb5cfe5877080f9bdd89 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Sun, 18 Aug 2019 13:12:53 +0800 Subject: [PATCH 2/4] updated README --- README-zh.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README-zh.md b/README-zh.md index fa9d45be..8b47f402 100644 --- a/README-zh.md +++ b/README-zh.md @@ -4,7 +4,7 @@ ![](https://img.shields.io/github/release/tikazyq/crawlab.svg) ![](https://img.shields.io/github/last-commit/tikazyq/crawlab.svg) ![](https://img.shields.io/github/issues/tikazyq/crawlab.svg) -![](https://img.shields.io/github/contributers/tikazyq/crawlab.svg) +![](https://img.shields.io/github/contributors/tikazyq/crawlab.svg) ![](https://img.shields.io/docker/pulls/tikazyq/crawlab) ![](https://img.shields.io/github/license/tikazyq/crawlab.svg) diff --git a/README.md b/README.md index 689a224d..027c8bd7 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ![](https://img.shields.io/github/release/tikazyq/crawlab.svg) ![](https://img.shields.io/github/last-commit/tikazyq/crawlab.svg) ![](https://img.shields.io/github/issues/tikazyq/crawlab.svg) -![](https://img.shields.io/github/contributers/tikazyq/crawlab.svg) +![](https://img.shields.io/github/contributors/tikazyq/crawlab.svg) ![](https://img.shields.io/docker/pulls/tikazyq/crawlab) ![](https://img.shields.io/github/license/tikazyq/crawlab.svg) From 98470b87e3e11af8930d759b50ee9b1bc44b09cf Mon Sep 17 00:00:00 2001 From: hantmac Date: Sun, 18 Aug 2019 14:20:37 +0800 Subject: [PATCH 3/4] Add Unit Test for backend/utils/chan.go --- backend/utils/chan_test.go | 76 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 backend/utils/chan_test.go diff --git a/backend/utils/chan_test.go b/backend/utils/chan_test.go new file mode 100644 index 00000000..7b5f1bba --- /dev/null +++ b/backend/utils/chan_test.go @@ -0,0 +1,76 @@ +package utils + +import ( + . "github.com/smartystreets/goconvey/convey" + "testing" +) + +func TestNewChanMap(t *testing.T) { + mapTest := make(map[string]chan string) + chanTest := make(chan string) + test := "test" + + Convey("Call NewChanMap to generate ChanMap", t, func() { + mapTest[test] = chanTest + chanMapTest := ChanMap{mapTest} + chanMap := NewChanMap() + chanMap.m[test] = chanTest + + Convey(test, func() { + So(chanMap, ShouldResemble, &chanMapTest) + }) + + }) +} + +func TestChan(t *testing.T) { + mapTest := make(map[string]chan string) + chanTest := make(chan string) + mapTest["test"] = chanTest + chanMapTest := ChanMap{mapTest} + + Convey("Test Chan use exist key", t, func() { + ch1 := chanMapTest.Chan( + "test") + Convey("ch1 should equal chanTest", func() { + So(ch1, ShouldEqual, chanTest) + }) + + }) + Convey("Test Chan use no-exist key", t, func() { + ch2 := chanMapTest.Chan("test2") + Convey("ch2 should equal chanMapTest.m[test2]", func() { + + So(chanMapTest.m["test2"], ShouldEqual, ch2) + }) + Convey("Cap of chanMapTest.m[test2] should equal 10", func() { + So(10, ShouldEqual, cap(chanMapTest.m["test2"])) + }) + }) +} + +func TestChanBlocked(t *testing.T) { + mapTest := make(map[string]chan string) + chanTest := make(chan string) + mapTest["test"] = chanTest + chanMapTest := ChanMap{mapTest} + + Convey("Test Chan use exist key", t, func() { + ch1 := chanMapTest.ChanBlocked( + "test") + Convey("ch1 should equal chanTest", func() { + So(ch1, ShouldEqual, chanTest) + }) + + }) + Convey("Test Chan use no-exist key", t, func() { + ch2 := chanMapTest.ChanBlocked("test2") + Convey("ch2 should equal chanMapTest.m[test2]", func() { + + So(chanMapTest.m["test2"], ShouldEqual, ch2) + }) + Convey("Cap of chanMapTest.m[test2] should equal 10", func() { + So(0, ShouldEqual, cap(chanMapTest.m["test2"])) + }) + }) +} From 0fe20c07f45a1d4d4a2fd5fb5fa4d5e796ae2cfa Mon Sep 17 00:00:00 2001 From: hantmac Date: Sun, 18 Aug 2019 19:07:59 +0800 Subject: [PATCH 4/4] Complete Unit Test for package utils --- backend/utils/file_test.go | 72 +++++++++++++++++++++++++++++++++++++ backend/utils/model_test.go | 49 +++++++++++++++++++++++++ backend/utils/user_test.go | 14 ++++++++ 3 files changed, 135 insertions(+) create mode 100644 backend/utils/file_test.go create mode 100644 backend/utils/model_test.go create mode 100644 backend/utils/user_test.go diff --git a/backend/utils/file_test.go b/backend/utils/file_test.go new file mode 100644 index 00000000..484366f5 --- /dev/null +++ b/backend/utils/file_test.go @@ -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) + }) + +} diff --git a/backend/utils/model_test.go b/backend/utils/model_test.go new file mode 100644 index 00000000..d641865c --- /dev/null +++ b/backend/utils/model_test.go @@ -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, "") + }) + }) + +} diff --git a/backend/utils/user_test.go b/backend/utils/user_test.go new file mode 100644 index 00000000..68cf4d65 --- /dev/null +++ b/backend/utils/user_test.go @@ -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) + }) +}