Skip to content

Commit

Permalink
调整单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Dec 17, 2023
1 parent b4a7814 commit f2252d4
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 13 deletions.
8 changes: 4 additions & 4 deletions flog/logData.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func newLogData(logLevel eumLogLevel.Enum, content string, component string) *lo
return &logData{Content: content, CreateAt: dateTime.Now(), LogLevel: logLevel, Component: component, newLine: true}
}

// 清除颜色
func (receiver *logData) clearColor() {
receiver.Content = mustCompile.ReplaceAllString(receiver.Content, "")
}
//// 清除颜色
//func (receiver *logData) clearColor() {
// receiver.Content = mustCompile.ReplaceAllString(receiver.Content, "")
//}
15 changes: 15 additions & 0 deletions test/asyncLocal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package test

import (
"github.com/farseer-go/fs/asyncLocal"
"github.com/stretchr/testify/assert"
"testing"
)

func TestAsyncLocal(t *testing.T) {
al := asyncLocal.New[string]()
al.Set("A")
assert.Equal(t, "A", al.Get())
al.Remove()
assert.Equal(t, "", al.Get())
}
7 changes: 7 additions & 0 deletions test/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"testing"
)

func TestConfigureFSEnv(t *testing.T) {
_ = os.Setenv("fsenv", "test")
fs.Initialize[modules.FarseerKernelModule]("unit test")
val := configure.GetString("Test.Name")
assert.Equal(t, "test", val)
}

func TestGetArray(t *testing.T) {
fs.Initialize[modules.FarseerKernelModule]("unit test")
assert.Equal(t, "", configure.GetString("a.b.c"))
Expand Down
5 changes: 5 additions & 0 deletions test/dateTime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ func TestDateTime_ToString(t *testing.T) {
assert.Equal(t, time.UnixMilli(100000).String(), dateTime.NewUnixMilli(100000).ToTime().String())
dateTime.Since(dateTime.Now())
dateTime.Now().Duration().String()
assert.Equal(t, true, dateTime.Now().After(dt))
dateTime.Now().Value()
now := dateTime.Now()
now.Scan("2021-09-06 21:14:25")
assert.Equal(t, "2021-09-06 21:14:25", now.ToString("yyyy-MM-dd HH:mm:ss"))
}
2 changes: 2 additions & 0 deletions test/farseer.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Test:
Name: "test"
2 changes: 1 addition & 1 deletion test/farseer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Log:
RollingInterval: "Hour" # 滚动间隔(Hour|Day|Week|Month|Year)
FileSizeLimitMb: 1 # 文件大小限制
FileCountLimit: 20 # 文件数量限制
RefreshInterval: 1 # 写入到文件的时间间隔,秒单位,最少为1
RefreshInterval: 0 # 写入到文件的时间间隔,秒单位,最少为1
Component:
task: true
cacheManage: true
Expand Down
11 changes: 11 additions & 0 deletions test/flog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestFlog(t *testing.T) {
_ = flog.Error("")
_ = flog.Errorf("")
flog.ErrorIfExists(fmt.Errorf("test error"))
flog.ErrorIfExists(nil)
flog.Critical("")
flog.Criticalf("")
flog.Print("")
Expand All @@ -29,6 +30,16 @@ func TestFlog(t *testing.T) {
flog.ComponentInfo("task", "")
flog.ComponentInfof("task", "")

assert.Equal(t, "aaa"+flog.Red("b")+"aaa", flog.ReplaceRed("aaabaaa", "b"))
assert.Equal(t, "aaa"+flog.Blue("b")+"aaa", flog.ReplaceBlue("aaabaaa", "b"))
assert.Equal(t, "aaa"+flog.Green("b")+"aaa", flog.ReplaceGreen("aaabaaa", "b"))
assert.Equal(t, "aaa"+flog.Yellow("b")+"aaa", flog.ReplaceYellow("aaabaaa", "b"))

assert.Equal(t, "aaa"+flog.Red("b")+flog.Red("c")+"aaa", flog.ReplaceReds("aaabcaaa", "b", "c"))
assert.Equal(t, "aaa"+flog.Blue("b")+flog.Blue("c")+"aaa", flog.ReplaceBlues("aaabcaaa", "b", "c"))
assert.Equal(t, "aaa"+flog.Green("b")+flog.Green("c")+"aaa", flog.ReplaceGreens("aaabcaaa", "b", "c"))
assert.Equal(t, "aaa"+flog.Yellow("b")+flog.Yellow("c")+"aaa", flog.ReplaceYellows("aaabcaaa", "b", "c"))

flog.Red("")
flog.Blue("")
flog.Green("")
Expand Down
7 changes: 1 addition & 6 deletions test/system_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import (
)

func TestResourceResource(t *testing.T) {
//ticker := time.NewTicker(time.Second)
//for range ticker.C {
// fmt.Println(system.GetResource().ToString())
//}

//fmt.Println(system.GetResource().ToString())
resource := system.GetResource()
assert.Greater(t, resource.CpuCores, 0)
resource.ToString()
}
75 changes: 75 additions & 0 deletions test/trace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package test

import (
"fmt"
"github.com/farseer-go/fs"
"github.com/farseer-go/fs/container"
"github.com/farseer-go/fs/modules"
"github.com/farseer-go/fs/trace"
"github.com/farseer-go/fs/trace/eumCallType"
"github.com/stretchr/testify/assert"
"testing"
)

func TestTrace(t *testing.T) {
fs.Initialize[modules.FarseerKernelModule]("unit test")

assert.Equal(t, "Grpc", eumCallType.Grpc.ToString())
assert.Equal(t, "Http", eumCallType.Http.ToString())
assert.Equal(t, "Database", eumCallType.Database.ToString())
assert.Equal(t, "Redis", eumCallType.Redis.ToString())
assert.Equal(t, "Mq", eumCallType.Mq.ToString())
assert.Equal(t, "Elasticsearch", eumCallType.Elasticsearch.ToString())
assert.Equal(t, "Hand", eumCallType.Hand.ToString())
assert.Equal(t, "KeyLocation", eumCallType.KeyLocation.ToString())
assert.Equal(t, "Etcd", eumCallType.Etcd.ToString())
assert.Equal(t, "", eumCallType.Enum(9).ToString())

baseTraceDetail := trace.BaseTraceDetail{}
baseTraceDetail.SetSql("", "", "", 0)
baseTraceDetail.Ignore()
assert.Equal(t, true, baseTraceDetail.IsIgnore())
baseTraceDetail.GetLevel()
testErr(baseTraceDetail)

// EmptyManager
iManager := container.Resolve[trace.IManager]()
iManager.EntryWebApi("", "", "", "", nil, "", "")
iManager.EntryFSchedule("", 0, 0)
iManager.EntryMqConsumer("", "", "")
iManager.EntryQueueConsumer("")
iManager.EntryTask("")
iManager.EntryWatchKey("")
iManager.TraceMq("", "", "iManager")
iManager.GetCurTrace()
iManager.TraceDatabase()
iManager.TraceDatabaseOpen("", "")
iManager.TraceElasticsearch("", "", "")
iManager.TraceEtcd("", "", 0)
iManager.TraceHand("")
iManager.TraceHttp("", "")
iManager.TraceKeyLocation("")
iManager.TraceMqSend("", "", "", "")
iManager.TraceRedis("", "", "")

iManager.TraceHand("").ToString()
iManager.TraceHand("").GetTraceDetail()
iManager.TraceHand("").End(nil)
iManager.TraceHand("").Ignore()
iManager.TraceHand("").IsIgnore()
iManager.TraceHand("").GetLevel()
iManager.TraceHand("").SetSql("", "", "", 0)

iManager.EntryQueueConsumer("").End()
iManager.EntryQueueConsumer("").Ignore()
iManager.EntryQueueConsumer("").GetList()
iManager.EntryQueueConsumer("").GetTraceId()
iManager.EntryQueueConsumer("").AddDetail(nil)
iManager.EntryQueueConsumer("").Error(nil)
iManager.EntryQueueConsumer("").GetStartTs()
iManager.EntryQueueConsumer("").SetBody("", 0, "")
}

func testErr(baseTraceDetail trace.BaseTraceDetail) {
baseTraceDetail.End(fmt.Errorf(""))
}
10 changes: 10 additions & 0 deletions test/try_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"fmt"
"github.com/farseer-go/fs/exception"
"github.com/stretchr/testify/assert"
"os"
Expand All @@ -18,6 +19,15 @@ func TestTry(t *testing.T) {
assert.Panics(t, func() {
exception.ThrowWebExceptionf(502, "%d", 123)
})
assert.Panics(t, func() {
exception.ThrowWebExceptionBool(true, 403, "")
})
assert.Panics(t, func() {
exception.ThrowWebExceptionfBool(true, 403, "%s", "")
})
assert.Panics(t, func() {
exception.ThrowWebExceptionError(403, fmt.Errorf(""))
})
}).CatchWebException(func(exp exception.WebException) {
os.Exit(-1)
}).CatchRefuseException(func(exp exception.RefuseException) {
Expand Down
19 changes: 19 additions & 0 deletions test/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,23 @@ func TestType(t *testing.T) {

assert.False(t, types.IsDtoModelIgnoreInterface([]reflect.Type{reflect.TypeOf(sqlserver{}), reflect.TypeOf(sqlserver{})}))
assert.True(t, types.IsDtoModelIgnoreInterface([]reflect.Type{reflect.TypeOf(sqlserver{})}))

assert.Panics(t, func() {
types.ListNew(nil)
})
assert.Panics(t, func() {
types.ListAdd(reflect.ValueOf(""), nil)
})
assert.Panics(t, func() {
types.GetListItemArrayType(nil)
})
assert.Panics(t, func() {
types.GetListItemType(nil)
})
assert.Panics(t, func() {
types.ListToArray(reflect.ValueOf(""))
})
assert.Panics(t, func() {
types.GetPageList(nil)
})
}
2 changes: 1 addition & 1 deletion trace/eumCallType/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const (
Redis // Redis
Mq // Mq
Elasticsearch // Elasticsearch
Etcd // Elasticsearch
Etcd // Etcd
Hand // Hand
KeyLocation // KeyLocation
)
Expand Down
2 changes: 1 addition & 1 deletion types/isType.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func IsList(val reflect.Value) (reflect.Type, bool) {
return realType, strings.HasPrefix(realType.String(), "collections.List[")
}

// IsList 判断类型是否为List
// IsListByType 判断类型是否为List
func IsListByType(realType reflect.Type) (reflect.Type, bool) {
return realType, strings.HasPrefix(realType.String(), "collections.List[")
}
Expand Down

0 comments on commit f2252d4

Please sign in to comment.