Skip to content

Commit

Permalink
update unit test (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohrusky authored Aug 25, 2023
1 parent 3b940a1 commit 5143807
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
10 changes: 2 additions & 8 deletions dao/favorite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@ var userID, videoID int64 = 114, 514

func setup() {
err := SetFavoriteByUserIDAndVideoID(userID, videoID)
if err != nil {
log.Logger.Error(err.Error())
return
}
log.HandleError(err)
}

func cleanup() {
err := SetFavoriteCancelByUserIDAndVideoID(userID, videoID)
if err != nil {
log.Logger.Error(err.Error())
return
}
log.HandleError(err)
}

func Test_GetFavoriteCountByVideoID(t *testing.T) {
Expand Down
19 changes: 16 additions & 3 deletions module/log/log.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package log

import (
"os"
"time"

"fmt"
"github.com/Biu-X/TikTok/module/config"
"github.com/Biu-X/TikTok/module/util"
"github.com/charmbracelet/log"
"os"
"runtime"
"strconv"
"time"
)

var Logger *log.Logger
Expand Down Expand Up @@ -33,3 +36,13 @@ func Init() {
}
Logger.Debugf("log level: %v", level)
}

// HandleError 封装了错误打印,高亮错误信息,单元测试时使用!!!
// 相当于 if err != nil { Logger.Error(err) }
func HandleError(err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
logMessage := fmt.Sprintf("<%s:%s> %s", file, util.HighlightString(util.RED, strconv.Itoa(line)), err.Error())
Logger.Errorf(logMessage)
}
}
4 changes: 2 additions & 2 deletions module/middleware/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ func DefaultLogger() gin.HandlerFunc {
Print: func(layout LogLayout) {
StatusMessage := layout.RequestMethod + ": " + strconv.Itoa(layout.StatusCode)
if layout.Error == "" {
log.Logger.Info(util.HighlightString("green", StatusMessage) + " - " + util.StructToString(layout))
log.Logger.Info(util.HighlightString(util.GREEN, StatusMessage) + " - " + util.StructToString(layout))
} else {
log.Logger.Error(util.HighlightString("red", StatusMessage) + " - " + util.StructToString(layout))
log.Logger.Error(util.HighlightString(util.RED, StatusMessage) + " - " + util.StructToString(layout))
}
},
Source: "TikTok",
Expand Down
7 changes: 1 addition & 6 deletions module/util/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package util
import (
"strconv"

"github.com/Biu-X/TikTok/module/log"
"github.com/gin-gonic/gin"
)

Expand All @@ -15,11 +14,7 @@ func GetUserIDFromGinContext(c *gin.Context) int64 {
return 0
}
// 已登录
userID, err := strconv.ParseInt(userIDstr, 10, 64)
if err != nil {
log.Logger.Errorf("strconv.ParseInt failed, err: %v", err)
return 0
}
userID, _ := strconv.ParseInt(userIDstr, 10, 64)

return userID
}
Expand Down
17 changes: 15 additions & 2 deletions module/util/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package util

import "encoding/json"

type Color string

// 高亮颜色map
var colorMap = map[string]string{
var colorMap = map[Color]string{
"green": "\033[97;42m",
"white": "\033[90;47m",
"yellow": "\033[90;43m",
Expand All @@ -14,8 +16,19 @@ var colorMap = map[string]string{
"reset": "\033[0m",
}

const (
GREEN Color = "green"
WHITE Color = "white"
YELLOW Color = "yellow"
RED Color = "red"
BLUE Color = "blue"
MAGENTA Color = "magenta"
CYAN Color = "cyan"
RESET Color = "reset"
)

// HighlightString 高亮字符串
func HighlightString(color string, str string) string {
func HighlightString(color Color, str string) string {
// 判断是否存在颜色,不存在返回绿色
if _, ok := colorMap[color]; !ok {
return colorMap["green"] + str + colorMap["reset"]
Expand Down

0 comments on commit 5143807

Please sign in to comment.