49 lines
971 B
Go
49 lines
971 B
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
// TestCommon_Constants 测试common包的常量
|
||
|
|
func TestCommon_Constants(t *testing.T) {
|
||
|
|
if DefaultTimeout != 5*time.Second {
|
||
|
|
t.Errorf("DefaultTimeout = %v, want 5 seconds", DefaultTimeout)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestCommon_JSON 测试JSON变量
|
||
|
|
func TestCommon_JSON(t *testing.T) {
|
||
|
|
// 验证json变量不为nil
|
||
|
|
if json == nil {
|
||
|
|
t.Error("json 变量不应为nil")
|
||
|
|
}
|
||
|
|
|
||
|
|
// 测试JSON序列化
|
||
|
|
testData := map[string]interface{}{
|
||
|
|
"name": "test",
|
||
|
|
"age": 25,
|
||
|
|
}
|
||
|
|
|
||
|
|
bytes, err := json.Marshal(testData)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("json.Marshal() 失败: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if len(bytes) == 0 {
|
||
|
|
t.Error("json.Marshal() 返回的字节不应为空")
|
||
|
|
}
|
||
|
|
|
||
|
|
// 测试JSON反序列化
|
||
|
|
var result map[string]interface{}
|
||
|
|
err = json.Unmarshal(bytes, &result)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("json.Unmarshal() 失败: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if result["name"] != "test" {
|
||
|
|
t.Errorf("反序列化结果 name = %v, want 'test'", result["name"])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|