Skip to content

Commit

Permalink
optimmize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Dec 14, 2024
1 parent 755a429 commit 01269ad
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 10 deletions.
2 changes: 1 addition & 1 deletion log/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (h *Hook) Fire(entry *logrus.Entry) error {
ctx: entry.Context,
data: map[string]any(entry.Data),
level: log.Level(entry.Level),
time: entry.Time,
message: entry.Message,
time: entry.Time,
}

data := entry.Data
Expand Down
149 changes: 149 additions & 0 deletions log/hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package log

import (
"context"
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

"github.com/goravel/framework/contracts/log"
mockslog "github.com/goravel/framework/mocks/log"
)

func TestHook_Fire(t *testing.T) {
var (
mockHook *mockslog.Hook

ctx = context.Background()
code = "123"
domain = "example.com"
hint = "hint"
level = logrus.InfoLevel
message = "message"
owner = "owner"
request = map[string]any{"key": "value"}
response = map[string]any{"key": "value"}
stacktrace = map[string]any{"key": "value"}
now = time.Now()
tags = []string{"tag1", "tag2"}
user = "user"
with = map[string]any{"key": "value"}
)

tests := []struct {
name string
entry *logrus.Entry
setup func()
expectError error
}{
{
name: "full data",
entry: &logrus.Entry{
Context: ctx,
Data: logrus.Fields{
"root": map[string]any{
"code": code,
"domain": domain,
"hint": hint,
"owner": owner,
"request": request,
"response": response,
"stacktrace": stacktrace,
"tags": tags,
"user": user,
"with": with,
},
},
Level: level,
Time: now,
Message: message,
},
setup: func() {
mockHook.EXPECT().Fire(&Entry{
ctx: ctx,
code: code,
data: map[string]any{
"root": map[string]any{
"code": code,
"domain": domain,
"hint": hint,
"owner": owner,
"request": request,
"response": response,
"stacktrace": stacktrace,
"tags": tags,
"user": user,
"with": with,
},
},
domain: domain,
hint: hint,
level: log.Level(level),
message: message,
owner: owner,
request: request,
response: response,
stacktrace: stacktrace,
tags: tags,
time: now,
user: user,
with: with,
}).Return(nil).Once()
},
},
{
name: "empty data",
entry: &logrus.Entry{
Context: ctx,
Data: logrus.Fields{},
Level: level,
Time: now,
Message: message,
},
setup: func() {
mockHook.EXPECT().Fire(&Entry{
ctx: ctx,
data: map[string]any{},
level: log.Level(level),
message: message,
time: now,
}).Return(nil).Once()
},
},
{
name: "Fire returns error",
entry: &logrus.Entry{
Context: ctx,
Data: logrus.Fields{},
Level: level,
Time: now,
Message: message,
},
setup: func() {
mockHook.EXPECT().Fire(&Entry{
ctx: ctx,
data: map[string]any{},
level: log.Level(level),
message: message,
time: now,
}).Return(assert.AnError).Once()
},
expectError: assert.AnError,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockHook = mockslog.NewHook(t)
tt.setup()

hook := &Hook{instance: mockHook}

err := hook.Fire(tt.entry)

assert.Equal(t, tt.expectError, err)
})
}
}
5 changes: 3 additions & 2 deletions log/logrus_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"io"
"os"

"github.com/rotisserie/eris"
"github.com/sirupsen/logrus"

"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/log"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/log/logger"
"github.com/rotisserie/eris"
"github.com/sirupsen/logrus"
)

func NewLogrus() *logrus.Logger {
Expand Down
2 changes: 1 addition & 1 deletion log/logrus_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestLogrus(t *testing.T) {

log, err = NewApplication(mockConfig, j)
ctx := context.Background()
ctx = context.WithValue(ctx, "key", "value")
ctx = context.WithValue(ctx, testContextKey("key"), "value")
log.WithContext(ctx).Info("Goravel")
},
assert: func() {
Expand Down
10 changes: 6 additions & 4 deletions log/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ import (
"github.com/gookit/goutil/testutil/assert"
)

type testContextKey any

func TestGetContextValues(t *testing.T) {
ctx := context.Background()
values := make(map[any]any)
getContextValues(ctx, values)
assert.Equal(t, make(map[any]any), values)

ctx = context.WithValue(ctx, "a", "b")
ctx = context.WithValue(ctx, 1, 2)
ctx = context.WithValue(ctx, "c", map[string]any{"d": "e"})
ctx = context.WithValue(ctx, testContextKey("a"), "b")
ctx = context.WithValue(ctx, testContextKey(1), 2)
ctx = context.WithValue(ctx, testContextKey("c"), map[string]any{"d": "e"})

type T struct {
A string
}
ctx = context.WithValue(ctx, "d", T{A: "a"})
ctx = context.WithValue(ctx, testContextKey("d"), T{A: "a"})

values = make(map[any]any)
getContextValues(ctx, values)
Expand Down
2 changes: 0 additions & 2 deletions support/file/file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package file

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -19,7 +18,6 @@ func ClientOriginalExtension(file string) string {
func Contain(file string, search string) bool {
if Exists(file) {
data, err := os.ReadFile(file)
fmt.Println(string(data))
if err != nil {
return false
}
Expand Down

0 comments on commit 01269ad

Please sign in to comment.