Skip to content

Commit

Permalink
feat(pkg.model) add model unit test , isseu: #778 (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
wang1309 committed Nov 27, 2023
1 parent 108fb12 commit 0f2a9c8
Showing 1 changed file with 312 additions and 0 deletions.
312 changes: 312 additions & 0 deletions pkg/config/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
package config_test

import (
"fmt"
"io"
"os"
"testing"
"time"
)

import (
Expand Down Expand Up @@ -129,3 +133,311 @@ func TestUnmarshalText(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, config.MySQL, protocolType)
}

func TestParametersMap_LowerCaseFirstLetter(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
pm config.ParametersMap
args args
want string
}{
{
name: "test lower case first letter",
pm: config.ParametersMap{},
args: args{
str: "Arana",
},
want: "arana",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.pm.LowerCaseFirstLetter(tt.args.str), "LowerCaseFirstLetter(%v)", tt.args.str)
})
}
}

func TestDecoder_Decode(t *testing.T) {
type fields struct {
reader io.Reader
}
type args struct {
v interface{}
}

f, err := os.Open(FakeConfigPath)
if err != nil {
t.Fatal(err)
}
defer func() {
_ = f.Close()
}()

tests := []struct {
name string
fields fields
args args
wantErr assert.ErrorAssertionFunc
}{
{
name: "test decode",
args: args{
v: &config.Configuration{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := config.NewDecoder(f).Decode(tt.args.v); err != nil {
t.Errorf("Decode() error = %v", err)
}
})
}
}

func TestLoad(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
args args
want *config.Configuration
wantErr assert.ErrorAssertionFunc
}{
{
name: "test load",
args: args{
path: FakeConfigPath,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := config.Load(tt.args.path)
if err != nil {
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
return
}

t.Logf("got: %+v", got)
})
}
}

func TestTenant_Empty(t1 *testing.T) {
type fields struct {
Spec config.Spec
Name string
Users []*config.User
SysDB *config.Node
DataSourceClusters []*config.DataSourceCluster
ShardingRule *config.ShardingRule
ShadowRule *config.ShadowRule
Nodes map[string]*config.Node
}
tests := []struct {
name string
fields fields
want bool
}{
{
name: "test empty",
fields: fields{
Spec: config.Spec{},
Name: "",
Users: nil,
SysDB: nil,
DataSourceClusters: nil,
ShardingRule: nil,
ShadowRule: nil,
Nodes: nil,
},
want: true,
},
}
for _, tt := range tests {
t1.Run(tt.name, func(t1 *testing.T) {
t := &config.Tenant{
Spec: tt.fields.Spec,
Name: tt.fields.Name,
Users: tt.fields.Users,
SysDB: tt.fields.SysDB,
DataSourceClusters: tt.fields.DataSourceClusters,
ShardingRule: tt.fields.ShardingRule,
ShadowRule: tt.fields.ShadowRule,
Nodes: tt.fields.Nodes,
}
assert.Equalf(t1, tt.want, t.Empty(), "Empty()")
})
}
}

func TestNode_GetReadAndWriteWeight(t *testing.T) {
type fields *config.Node

node := &config.Node{
Name: "arana-node-1",
Host: "arana-mysql",
Port: 3306,
Username: "root",
Password: "123456",
Database: "employees",
ConnProps: nil,
Weight: "r10w10",
Labels: nil,
}

tests := []struct {
name string
fields fields
want int
want1 int
wantErr assert.ErrorAssertionFunc
}{
{
name: "test get read and write weight",
fields: node,
want: 10,
want1: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := node.GetReadAndWriteWeight()
if err != nil {
t.Errorf("GetReadAndWriteWeight() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(t, tt.want, got)
assert.Equal(t, tt.want1, got1)
})
}
}

func TestGetConnPropCapacity(t *testing.T) {
type args struct {
connProps map[string]interface{}
defaultValue int
}
tests := []struct {
name string
args args
want int
}{
{
name: "test get conn prop capacity",
args: args{
connProps: map[string]interface{}{
"capacity": 10,
},
defaultValue: 1,
},
want: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, config.GetConnPropCapacity(tt.args.connProps, tt.args.defaultValue), "GetConnPropCapacity(%v, %v)", tt.args.connProps, tt.args.defaultValue)
})
}
}

func TestGetConnPropMaxCapacity(t *testing.T) {
type args struct {
connProps map[string]interface{}
defaultValue int
}
tests := []struct {
name string
args args
want int
}{
{
name: "test get conn prop max capacity",
args: args{
connProps: map[string]interface{}{
"maxCapacity": 10,
},
defaultValue: 1,
},
want: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, config.GetConnPropMaxCapacity(tt.args.connProps, tt.args.defaultValue), "GetConnPropMaxCapacity(%v, %v)", tt.args.connProps, tt.args.defaultValue)
})
}
}

func TestGetConnPropIdleTime(t *testing.T) {
type args struct {
connProps map[string]interface{}
defaultValue time.Duration
}
tests := []struct {
name string
args args
want time.Duration
}{
{
name: "test get conn prop idle time",
args: args{
connProps: map[string]interface{}{
"idle_time": "10s",
},
defaultValue: 1 * time.Second,
},
want: 10 * time.Second,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, config.GetConnPropIdleTime(tt.args.connProps, tt.args.defaultValue), "GetConnPropIdleTime(%v, %v)", tt.args.connProps, tt.args.defaultValue)
})
}
}

func TestListener_String(t *testing.T) {
type fields struct {
ProtocolType string
SocketAddress *config.SocketAddress
ServerVersion string
}

socketAddr := fmt.Sprintf("%s:%d", "127.0.0.1", 3306)
want := fmt.Sprintf("Listener protocol_type:%s, socket_address:%s, server_version:%s", "mysql", socketAddr, "5.7.25")

tests := []struct {
name string
fields fields
want string
}{
{
name: "test listener string",
fields: fields{
ProtocolType: "mysql",
SocketAddress: &config.SocketAddress{
Address: "127.0.0.1",
Port: 3306,
},
ServerVersion: "5.7.25",
},
want: want,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &config.Listener{
ProtocolType: tt.fields.ProtocolType,
SocketAddress: tt.fields.SocketAddress,
ServerVersion: tt.fields.ServerVersion,
}
assert.Equalf(t, tt.want, l.String(), "String()")
})
}
}

0 comments on commit 0f2a9c8

Please sign in to comment.