Skip to content

Commit

Permalink
调整单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Oct 31, 2023
1 parent 3c0567c commit 28519c5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 54 deletions.
51 changes: 38 additions & 13 deletions test/apiResponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package test

import (
"bytes"
"encoding/json"
"github.com/farseer-go/fs/core"
"github.com/stretchr/testify/assert"
"testing"
)

func TestApiResponse(t *testing.T) {
api := core.Success("成功", "nice")
assert.Equal(t, "{\"Status\":true,\"StatusCode\":200,\"StatusMessage\":\"成功\",\"Data\":\"nice\"}", api.ToJson())
assert.Equal(t, "{\"Status\":true,\"StatusCode\":200,\"StatusMessage\":\"成功\",\"Data\":\"nice\"}", string(api.ToBytes()))
apiByte, _ := json.Marshal(api)
assert.Equal(t, string(apiByte), api.ToJson())
assert.Equal(t, apiByte, api.ToBytes())

api.SetData("very nice")
assert.Equal(t, "very nice", api.Data)
Expand All @@ -19,35 +21,58 @@ func TestApiResponse(t *testing.T) {
assert.Equal(t, api.ToJson(), byByte.ToJson())

api2 := core.Error403[int]("错误")
assert.Equal(t, "{\"Status\":false,\"StatusCode\":403,\"StatusMessage\":\"错误\",\"Data\":0}", api2.ToJson())
assert.Equal(t, 403, api2.StatusCode)
assert.Equal(t, "错误", api2.StatusMessage)
assert.Equal(t, 0, api2.Data)

api3 := core.Error[bool]("成功", 500)
assert.Equal(t, "{\"Status\":false,\"StatusCode\":500,\"StatusMessage\":\"成功\",\"Data\":false}", api3.ToJson())
assert.Equal(t, 500, api3.StatusCode)
assert.Equal(t, "成功", api3.StatusMessage)
assert.Equal(t, false, api3.Data)

api4 := core.ApiResponse[int](core.ApiResponseIntSuccess("成功", 2))
assert.Equal(t, "{\"Status\":true,\"StatusCode\":200,\"StatusMessage\":\"成功\",\"Data\":2}", api4.ToJson())
assert.Equal(t, 200, api4.StatusCode)
assert.Equal(t, "成功", api4.StatusMessage)
assert.Equal(t, 2, api4.Data)

api5 := core.ApiResponse[int](core.ApiResponseIntError("错误", 100))
assert.Equal(t, "{\"Status\":false,\"StatusCode\":100,\"StatusMessage\":\"错误\",\"Data\":0}", api5.ToJson())
assert.Equal(t, 100, api5.StatusCode)
assert.Equal(t, "错误", api5.StatusMessage)
assert.Equal(t, 0, api5.Data)

api6 := core.ApiResponse[int](core.ApiResponseIntError403("错误"))
assert.Equal(t, "{\"Status\":false,\"StatusCode\":403,\"StatusMessage\":\"错误\",\"Data\":0}", api6.ToJson())
assert.Equal(t, 403, api6.StatusCode)
assert.Equal(t, "错误", api6.StatusMessage)
assert.Equal(t, 0, api6.Data)

api7 := core.ApiResponse[int64](core.ApiResponseLongSuccess("成功", 2))
assert.Equal(t, "{\"Status\":true,\"StatusCode\":200,\"StatusMessage\":\"成功\",\"Data\":2}", api7.ToJson())
assert.Equal(t, 200, api7.StatusCode)
assert.Equal(t, "成功", api7.StatusMessage)
assert.Equal(t, int64(2), api7.Data)

api8 := core.ApiResponse[int64](core.ApiResponseLongError("错误", 100))
assert.Equal(t, "{\"Status\":false,\"StatusCode\":100,\"StatusMessage\":\"错误\",\"Data\":0}", api8.ToJson())
assert.Equal(t, 100, api8.StatusCode)
assert.Equal(t, "错误", api8.StatusMessage)
assert.Equal(t, int64(0), api8.Data)

api9 := core.ApiResponse[int64](core.ApiResponseLongError403("错误"))
assert.Equal(t, "{\"Status\":false,\"StatusCode\":403,\"StatusMessage\":\"错误\",\"Data\":0}", api9.ToJson())
assert.Equal(t, 403, api9.StatusCode)
assert.Equal(t, "错误", api9.StatusMessage)
assert.Equal(t, int64(0), api9.Data)

api10 := core.ApiResponse[string](core.ApiResponseStringSuccess("成功", "steden"))
assert.Equal(t, "{\"Status\":true,\"StatusCode\":200,\"StatusMessage\":\"成功\",\"Data\":\"steden\"}", api10.ToJson())
assert.Equal(t, 200, api10.StatusCode)
assert.Equal(t, "成功", api10.StatusMessage)
assert.Equal(t, "steden", api10.Data)

api11 := core.ApiResponse[string](core.ApiResponseStringError("错误", 100))
assert.Equal(t, "{\"Status\":false,\"StatusCode\":100,\"StatusMessage\":\"错误\",\"Data\":\"\"}", api11.ToJson())
assert.Equal(t, 100, api11.StatusCode)
assert.Equal(t, "错误", api11.StatusMessage)
assert.Equal(t, "", api11.Data)

api12 := core.ApiResponse[string](core.ApiResponseStringError403("错误"))
assert.Equal(t, "{\"Status\":false,\"StatusCode\":403,\"StatusMessage\":\"错误\",\"Data\":\"\"}", api12.ToJson())
assert.Equal(t, 403, api12.StatusCode)
assert.Equal(t, "错误", api12.StatusMessage)
assert.Equal(t, "", api12.Data)

}
74 changes: 37 additions & 37 deletions test/configure_parseConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ type testConfig struct {
ConnectionString string
UsePool bool
}
type rabbitConfig struct {
Server serverConfig
Exchange []exchangeConfig
type testGetSubNodesConfig struct {
Server serverConfig
Test []exchangeConfig
}

type exchangeConfig struct {
ExchangeName string // 交换器名称
RoutingKey string // RoutingKey
ExchangeType string // 交换器类型
UseConfirmModel bool // 是否需要ACK
AutoCreateExchange bool // 交换器不存在,是否自动创建
TestName string // 交换器名称
RoutingKey string // RoutingKey
TestType string // 交换器类型
UseConfirmModel bool // 是否需要ACK
AutoCreateTest bool // 交换器不存在,是否自动创建
}

// rabbitConfig 配置项
// testGetSubNodesConfig 配置项
type serverConfig struct {
Server string // 服务端地址
UserName string // 用户名
Expand All @@ -49,33 +49,33 @@ func TestParseString(t *testing.T) {

func TestParseConfigs(t *testing.T) {
configure.InitConfig()
rabbitConfigs := configure.ParseConfigs[rabbitConfig]("Rabbit")
assert.Equal(t, 2, len(rabbitConfigs))
assert.Equal(t, "rabbit1:5672", rabbitConfigs[0].Server.Server)
assert.Equal(t, "farseer", rabbitConfigs[0].Server.UserName)
assert.Equal(t, "farseer", rabbitConfigs[0].Server.Password)
assert.Equal(t, "Ex1", rabbitConfigs[0].Exchange[0].ExchangeName)
assert.Equal(t, "", rabbitConfigs[0].Exchange[0].RoutingKey)
assert.Equal(t, "fanout", rabbitConfigs[0].Exchange[0].ExchangeType)
assert.Equal(t, false, rabbitConfigs[0].Exchange[0].UseConfirmModel)
assert.Equal(t, true, rabbitConfigs[0].Exchange[0].AutoCreateExchange)
assert.Equal(t, "Ex2", rabbitConfigs[0].Exchange[1].ExchangeName)
assert.Equal(t, "", rabbitConfigs[0].Exchange[1].RoutingKey)
assert.Equal(t, "fanout", rabbitConfigs[0].Exchange[1].ExchangeType)
assert.Equal(t, false, rabbitConfigs[0].Exchange[1].UseConfirmModel)
assert.Equal(t, true, rabbitConfigs[0].Exchange[1].AutoCreateExchange)
testConfigs := configure.ParseConfigs[testGetSubNodesConfig]("TestGetSubNodes")
assert.Equal(t, 2, len(testConfigs))
assert.Equal(t, "test:8888", testConfigs[0].Server.Server)
assert.Equal(t, "farseer", testConfigs[0].Server.UserName)
assert.Equal(t, "farseer", testConfigs[0].Server.Password)
assert.Equal(t, "Ex1", testConfigs[0].Test[0].TestName)
assert.Equal(t, "", testConfigs[0].Test[0].RoutingKey)
assert.Equal(t, "fanout", testConfigs[0].Test[0].TestType)
assert.Equal(t, false, testConfigs[0].Test[0].UseConfirmModel)
assert.Equal(t, true, testConfigs[0].Test[0].AutoCreateTest)
assert.Equal(t, "Ex2", testConfigs[0].Test[1].TestName)
assert.Equal(t, "", testConfigs[0].Test[1].RoutingKey)
assert.Equal(t, "fanout", testConfigs[0].Test[1].TestType)
assert.Equal(t, false, testConfigs[0].Test[1].UseConfirmModel)
assert.Equal(t, true, testConfigs[0].Test[1].AutoCreateTest)

assert.Equal(t, "rabbit2:5672", rabbitConfigs[1].Server.Server)
assert.Equal(t, "farseer", rabbitConfigs[1].Server.UserName)
assert.Equal(t, "farseer", rabbitConfigs[1].Server.Password)
assert.Equal(t, "Ex3", rabbitConfigs[1].Exchange[0].ExchangeName)
assert.Equal(t, "", rabbitConfigs[1].Exchange[0].RoutingKey)
assert.Equal(t, "fanout", rabbitConfigs[1].Exchange[0].ExchangeType)
assert.Equal(t, false, rabbitConfigs[1].Exchange[0].UseConfirmModel)
assert.Equal(t, true, rabbitConfigs[1].Exchange[0].AutoCreateExchange)
assert.Equal(t, "Ex4", rabbitConfigs[1].Exchange[1].ExchangeName)
assert.Equal(t, "", rabbitConfigs[1].Exchange[1].RoutingKey)
assert.Equal(t, "fanout", rabbitConfigs[1].Exchange[1].ExchangeType)
assert.Equal(t, false, rabbitConfigs[1].Exchange[1].UseConfirmModel)
assert.Equal(t, true, rabbitConfigs[1].Exchange[1].AutoCreateExchange)
assert.Equal(t, "test2:8888", testConfigs[1].Server.Server)
assert.Equal(t, "farseer", testConfigs[1].Server.UserName)
assert.Equal(t, "farseer", testConfigs[1].Server.Password)
assert.Equal(t, "Ex3", testConfigs[1].Test[0].TestName)
assert.Equal(t, "", testConfigs[1].Test[0].RoutingKey)
assert.Equal(t, "fanout", testConfigs[1].Test[0].TestType)
assert.Equal(t, false, testConfigs[1].Test[0].UseConfirmModel)
assert.Equal(t, true, testConfigs[1].Test[0].AutoCreateTest)
assert.Equal(t, "Ex4", testConfigs[1].Test[1].TestName)
assert.Equal(t, "", testConfigs[1].Test[1].RoutingKey)
assert.Equal(t, "fanout", testConfigs[1].Test[1].TestType)
assert.Equal(t, false, testConfigs[1].Test[1].UseConfirmModel)
assert.Equal(t, true, testConfigs[1].Test[1].AutoCreateTest)
}
1 change: 0 additions & 1 deletion test/container_panic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func TestContainerPanic_test(t *testing.T) {
})

container.RegisterTransient(func() IDatabase { return nil }, "testNil")
assert.Nil(t, container.Resolve[IDatabase]("testNil"))

assert.Panics(t, func() {
panic("")
Expand Down
13 changes: 11 additions & 2 deletions test/farseer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ Database:
Redis:
default: "Server=192.168.1.8:6379,DB=15,Password=steden@123,ConnectTimeout=600000,SyncTimeout=10000,ResponseTimeout=10000"
Rabbit:
Ex1: "Server=rabbit:5672,UserName=farseer,Password=farseer,Exchange=Ex1,RoutingKey=,Type=fanout,UseConfirm=false,AutoCreate=true"
Ex2: "Server=rabbit:5672,UserName=farseer,Password=farseer,Exchange=Ex2,RoutingKey=,Type=fanout,UseConfirm=false,AutoCreate=true"
Ex1: "Server=rabbit:5672,UserName=farseer,Password=farseer,Test=Ex1,RoutingKey=,Type=fanout,UseConfirm=false,AutoCreate=true"
Ex2: "Server=rabbit:5672,UserName=farseer,Password=farseer,Test=Ex2,RoutingKey=,Type=fanout,UseConfirm=false,AutoCreate=true"
TestGetSubNodes:
- Server: "Server=test:8888,UserName=farseer,Password=farseer"
Test:
- "TestName=Ex1,RoutingKey=,TestType=fanout,UseConfirmModel=false,AutoCreateTest=true"
- "TestName=Ex2,RoutingKey=,TestType=fanout,UseConfirmModel=false,AutoCreateTest=true"
- Server: "Server=test2:8888,UserName=farseer,Password=farseer"
Test:
- "TestName=Ex3,RoutingKey=,TestType=fanout,UseConfirmModel=false,AutoCreateTest=true"
- "TestName=Ex4,RoutingKey=,TestType=fanout,UseConfirmModel=false,AutoCreateTest=true"
WebApi:
Url: ":888"
Log:
Expand Down
1 change: 0 additions & 1 deletion test/try_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func TestTry(t *testing.T) {
})
try.CatchException(func(exp any) {
assert.Equal(t, "aaa", exp)
panic("aaa")
})
assert.Panics(t, func() {
try.ThrowUnCatch()
Expand Down

0 comments on commit 28519c5

Please sign in to comment.