github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/toolkit/sqlext/logger/logger_test.go (about) 1 package logger_test 2 3 import ( 4 "context" 5 "github.com/ascarter/requestid" 6 "github.com/google/uuid" 7 "github.com/opentracing/opentracing-go" 8 "github.com/opentracing/opentracing-go/mocktracer" 9 "github.com/unionj-cloud/go-doudou/framework/tracing" 10 "github.com/unionj-cloud/go-doudou/toolkit/sqlext/logger" 11 "os" 12 "testing" 13 ) 14 15 func TestMain(m *testing.M) { 16 os.Setenv("GDD_SERVICE_NAME", "TestSqlLogger") 17 os.Setenv("GDD_SQL_LOG_ENABLE", "true") 18 19 tracer, closer := tracing.Init() 20 defer closer.Close() 21 opentracing.SetGlobalTracer(tracer) 22 23 m.Run() 24 } 25 26 func TestSqlLogger_Log(t *testing.T) { 27 type fields struct { 28 ctx func() context.Context 29 } 30 type args struct { 31 query string 32 args []interface{} 33 } 34 tests := []struct { 35 name string 36 fields fields 37 args args 38 }{ 39 { 40 name: "", 41 fields: fields{ 42 ctx: func() context.Context { 43 return requestid.NewContext(context.Background(), uuid.NewString()) 44 }, 45 }, 46 args: args{ 47 query: "select * from ddl_user where (`school` = 'Shanxi Univ.' and `delete_at` is null) order by `create_at` desc limit ?,?", 48 args: []interface{}{0, 2}, 49 }, 50 }, 51 { 52 name: "", 53 fields: fields{ 54 ctx: func() context.Context { 55 _, ctx := opentracing.StartSpanFromContext(requestid.NewContext(context.Background(), uuid.NewString()), "TestSqlLogger") 56 return ctx 57 }, 58 }, 59 args: args{ 60 query: "select * from ddl_user where (`school` = 'Shanxi Univ.' and `delete_at` is null) order by `create_at` desc limit ?", 61 args: []interface{}{20}, 62 }, 63 }, 64 { 65 name: "", 66 fields: fields{ 67 ctx: func() context.Context { 68 return opentracing.ContextWithSpan(context.Background(), mocktracer.New().StartSpan("TestSqlLogger")) 69 }, 70 }, 71 args: args{ 72 query: "select * from ddl_user where (`school` = ? and `delete_at` is null) order by `create_at` desc", 73 args: []interface{}{"Shanxi Univ."}, 74 }, 75 }, 76 } 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 receiver := logger.NewSqlLogger() 80 receiver.Log(tt.fields.ctx(), tt.args.query, tt.args.args...) 81 }) 82 } 83 }