Browse Source

Add test cases for utils/utils.go

Jackson Tian 7 years ago
parent
commit
cecf23f105
2 changed files with 67 additions and 14 deletions
  1. 2 14
      sdk/utils/utils.go
  2. 65 0
      sdk/utils/utils_test.go

+ 2 - 14
sdk/utils/utils.go

@@ -18,13 +18,12 @@ import (
 	"crypto/md5"
 	"encoding/base64"
 	"encoding/hex"
-	"encoding/json"
-	"fmt"
-	"github.com/satori/go.uuid"
 	"net/url"
 	"reflect"
 	"strconv"
 	"time"
+
+	"github.com/satori/go.uuid"
 )
 
 // if you use go 1.10 or higher, you can hack this util by these to avoid "TimeZone.zip not found" on Windows
@@ -80,17 +79,6 @@ func GetUrlFormedMap(source map[string]string) (urlEncoded string) {
 	return
 }
 
-func GetFromJsonString(jsonString, key string) (result string, err error) {
-	var responseMap map[string]*json.RawMessage
-	err = json.Unmarshal([]byte(jsonString), &responseMap)
-	if err != nil {
-		return
-	}
-	fmt.Println(string(*responseMap[key]))
-	err = json.Unmarshal(*responseMap[key], &result)
-	return
-}
-
 func InitStructWithDefaultTag(bean interface{}) {
 	configType := reflect.TypeOf(bean)
 	for i := 0; i < configType.Elem().NumField(); i++ {

+ 65 - 0
sdk/utils/utils_test.go

@@ -0,0 +1,65 @@
+package utils
+
+import (
+  "testing"
+  "time"
+  "regexp"
+
+  "github.com/stretchr/testify/assert"
+)
+
+func TestInitStructWithDefaultTag(t *testing.T) {
+  config := &struct {
+    B bool `default:"true"`;
+    S string `default:"default string"`;
+    I int `default:"10"`
+    T time.Duration `default:"100"`
+    E int `default:""`
+  }{}
+  InitStructWithDefaultTag(config)
+  assert.NotNil(t, config)
+  assert.Equal(t, true, config.B)
+  assert.Equal(t, "default string", config.S)
+  assert.Equal(t, 10, config.I)
+  assert.Equal(t, time.Duration(100), config.T)
+  assert.Equal(t, 0, config.E)
+}
+
+func TestGetUUIDV4(t *testing.T) {
+  uuid := GetUUIDV4()
+  assert.Equal(t, 32, len(uuid))
+  assert.NotEqual(t, GetUUIDV4(), GetUUIDV4())
+}
+
+func TestGetMD5Base64(t *testing.T) {
+  assert.Equal(t, "ERIHLmRX2uZmssDdxQnnxQ==",
+    GetMD5Base64([]byte("That's all folks!!")))
+  assert.Equal(t, "GsJRdI3kAbAnHo/0+3wWJw==",
+    GetMD5Base64([]byte("中文也没啥问题")))
+}
+
+func TestGetTimeInFormatRFC2616(t *testing.T) {
+  s := GetTimeInFormatRFC2616()
+  assert.Equal(t, 29, len(s))
+  re := regexp.MustCompile(`^[A-Z][a-z]{2}, [0-9]{2} [A-Z][a-z]{2} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} GMT$`)
+  assert.True(t, re.MatchString(s))
+}
+
+func TestGetTimeInFormatISO8601(t *testing.T) {
+  s := GetTimeInFormatISO8601()
+  assert.Equal(t, 20, len(s))
+  // 2006-01-02T15:04:05Z
+  re := regexp.MustCompile(`^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$`)
+  assert.True(t, re.MatchString(s))
+}
+
+func TestGetUrlFormedMap(t *testing.T) {
+  m := make(map[string]string)
+  m["key"] = "value"
+  s := GetUrlFormedMap(m)
+  assert.Equal(t, "key=value", s)
+  m["key2"] = "http://domain/?key=value&key2=value2"
+  s2 := GetUrlFormedMap(m)
+  assert.Equal(t, "key=value&key2=http%3A%2F%2Fdomain%2F%3Fkey%3Dvalue%26key2%3Dvalue2", s2)
+}
+