Skip to content

Commit

Permalink
Merge pull request #115 from chuntaojun/fix_unit_test
Browse files Browse the repository at this point in the history
[ISSUE #114] fix wrong unit test under store/boltdb
  • Loading branch information
andrewshan authored Sep 29, 2021
2 parents 3f91b51 + 03f7dec commit 21c0983
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 163 deletions.
118 changes: 62 additions & 56 deletions store/boltdb/circuitbreaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package boltdb

import (
"encoding/json"
"io/ioutil"
"math/rand"
"os"
Expand Down Expand Up @@ -516,62 +517,67 @@ func Test_circuitBreakerStore_DeleteTagCircuitBreaker(t *testing.T) {
})
}

//func Test_circuitBreakerStore_UpdateCircuitBreaker(t *testing.T) {
// CreateCircuitbreakerDBHandlerAndRun(t, func(t *testing.T, handler BoltHandler) {
// type fields struct {
// handler BoltHandler
// ruleLock *sync.RWMutex
// relationLock *sync.RWMutex
// }
// type args struct {
// cb *model.CircuitBreaker
// }
// tests := []struct {
// name string
// fields fields
// args args
// wantErr bool
// }{
// {
// name: "",
// fields: fields{
// handler: handler,
// ruleLock: &sync.RWMutex{},
// relationLock: &sync.RWMutex{},
// },
// args: args{
// cb: createTestCircuitbreaker("", true),
// },
// wantErr: false,
// },
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// c := &circuitBreakerStore{
// handler: tt.fields.handler,
// }
//
// old := createTestCircuitbreaker(tt.args.cb.ID, false)
// if err := c.CreateCircuitBreaker(old); err != nil {
// t.Fatal(err)
// }
//
// if err := c.UpdateCircuitBreaker(tt.args.cb); (err != nil) != tt.wantErr {
// t.Errorf("circuitBreakerStore.UpdateCircuitBreaker() error = %v, wantErr %v", err, tt.wantErr)
// }
//
// newCb, err := c.GetCircuitBreaker(tt.args.cb.ID, tt.args.cb.Version)
// if err != nil {
// t.Fatal(err)
// }
//
// if !reflect.DeepEqual(newCb, tt.args.cb) {
// t.Fatalf("circuitBreakerStore.UpdateCircuitBreaker() expect : %#v, actual : %#v", tt.args.cb, newCb)
// }
// })
// }
// })
//}
func Test_circuitBreakerStore_UpdateCircuitBreaker(t *testing.T) {
CreateCircuitbreakerDBHandlerAndRun(t, func(t *testing.T, handler BoltHandler) {
type fields struct {
handler BoltHandler
ruleLock *sync.RWMutex
relationLock *sync.RWMutex
}
type args struct {
cb *model.CircuitBreaker
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "",
fields: fields{
handler: handler,
ruleLock: &sync.RWMutex{},
relationLock: &sync.RWMutex{},
},
args: args{
cb: createTestCircuitbreaker("", true),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &circuitBreakerStore{
handler: tt.fields.handler,
}

old := createTestCircuitbreaker(tt.args.cb.ID, false)
if err := c.CreateCircuitBreaker(old); err != nil {
t.Fatal(err)
}

if err := c.UpdateCircuitBreaker(tt.args.cb); (err != nil) != tt.wantErr {
t.Errorf("circuitBreakerStore.UpdateCircuitBreaker() error = %v, wantErr %v", err, tt.wantErr)
}

newCb, err := c.GetCircuitBreaker(tt.args.cb.ID, tt.args.cb.Version)
if err != nil {
t.Fatal(err)
}

t.Logf("use time.Time to deep equal : %t", reflect.DeepEqual(newCb, tt.args.cb))

newCbJson, _ := json.Marshal(newCb)
wantCbJson, _ := json.Marshal(tt.args.cb)

if !reflect.DeepEqual(newCbJson, wantCbJson) {
t.Errorf("circuitBreakerStore.UpdateCircuitBreaker() expect : %s, actual : %s", wantCbJson, newCbJson)
}
})
}
})
}

func Test_circuitBreakerStore_GetCircuitBreaker(t *testing.T) {
type fields struct {
Expand Down
239 changes: 132 additions & 107 deletions store/boltdb/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -269,110 +270,134 @@ func Test_platformStore_DeletePlatform(t *testing.T) {
})
}

//func Test_platformStore_GetPlatforms(t *testing.T) {
// CreatePlatformDBHandlerAndRun(t, func(t *testing.T, handler BoltHandler) {
//
// p := &platformStore{
// handler: handler,
// }
//
// platNames := []string{"polaris_1", "polaris_2", "polaris_3"}
//
// // create 20 and save
// platforms := make([]*model.Platform, 20)
// platformsOne := make([]*model.Platform, 0)
// platformsTwo := make([]*model.Platform, 0)
// platformsThree := make([]*model.Platform, 0)
//
// for i := 0; i < 20; i++ {
// platforms[i] = createTestPlatform(uuid.NewString(), false)
// tN := time.Now().Add(time.Minute * time.Duration(i+1))
// platforms[i].Name = platNames[rand.Intn(len(platNames))]
// platforms[i].CreateTime = tN
// platforms[i].ModifyTime = tN
//
// if err := p.CreatePlatform(platforms[i]); err != nil {
// t.Errorf("platformStore.DeletePlatform() create error = %v", err)
// }
//
// if strings.Compare(platforms[i].Name, platNames[0]) == 0 {
// platformsOne = append(platformsOne, platforms[i])
// }
// if strings.Compare(platforms[i].Name, platNames[0]) == 0 {
// platformsTwo = append(platformsTwo, platforms[i])
// }
// if strings.Compare(platforms[i].Name, platNames[0]) == 0 {
// platformsThree = append(platformsThree, platforms[i])
// }
// }
//
// type fields struct {
// handler BoltHandler
// lock *sync.RWMutex
// }
// type args struct {
// query map[string]string
// offset uint32
// limit uint32
// }
// tests := []struct {
// name string
// fields fields
// args args
// want uint32
// want1 []*model.Platform
// wantErr bool
// }{
// {
// name: "",
// fields: fields{
// handler: handler,
// },
// args: args{
// query: map[string]string{
// "Name": platNames[0],
// },
// offset: 0,
// limit: 20,
// },
// want: 0,
// want1: []*model.Platform{},
// wantErr: false,
// },
// {
// name: "",
// fields: fields{
// handler: handler,
// },
// args: args{
// query: map[string]string{
// "Name": platNames[1],
// },
// offset: 0,
// limit: 20,
// },
// want: 0,
// want1: []*model.Platform{},
// wantErr: false,
// },
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// p := &platformStore{
// handler: tt.fields.handler,
// }
// got, got1, err := p.GetPlatforms(tt.args.query, tt.args.offset, tt.args.limit)
// if (err != nil) != tt.wantErr {
// t.Errorf("platformStore.GetPlatforms() error = %v, wantErr %v", err, tt.wantErr)
// return
// }
// if got != tt.want {
// t.Errorf("platformStore.GetPlatforms() got = %v, want %v", got, tt.want)
// }
// if !reflect.DeepEqual(got1, tt.want1) {
// t.Errorf("platformStore.GetPlatforms() got1 = %v, want %v", got1, tt.want1)
// }
// })
// }
// })
//}
func Test_platformStore_GetPlatforms(t *testing.T) {
CreatePlatformDBHandlerAndRun(t, func(t *testing.T, handler BoltHandler) {

p := &platformStore{
handler: handler,
}

platNames := []string{"polaris_1", "polaris_2", "polaris_3"}
wantResSlice := make([][]*model.Platform, 2)
wantResSlice[0] = make([]*model.Platform, 0)
wantResSlice[1] = make([]*model.Platform, 0)

// create 20 and save
platforms := make([]*model.Platform, 20)
platformsOne := make([]*model.Platform, 0)
platformsTwo := make([]*model.Platform, 0)
platformsThree := make([]*model.Platform, 0)

for i := 0; i < 20; i++ {
platforms[i] = createTestPlatform(uuid.NewString(), false)
tN := time.Now().Add(time.Minute * time.Duration(i+1))
platforms[i].Name = platNames[rand.Intn(len(platNames))]
platforms[i].CreateTime = tN
platforms[i].ModifyTime = tN

if err := p.CreatePlatform(platforms[i]); err != nil {
t.Errorf("platformStore.DeletePlatform() create error = %v", err)
}

if strings.Compare(platforms[i].Name, platNames[0]) == 0 {
platformsOne = append(platformsOne, platforms[i])
wantResSlice[0] = append(wantResSlice[0], platforms[i])
}
if strings.Compare(platforms[i].Name, platNames[1]) == 0 {
platformsTwo = append(platformsTwo, platforms[i])
wantResSlice[1] = append(wantResSlice[1], platforms[i])
}
if strings.Compare(platforms[i].Name, platNames[2]) == 0 {
platformsThree = append(platformsThree, platforms[i])
}
}

type fields struct {
handler BoltHandler
lock *sync.RWMutex
}
type args struct {
query map[string]string
offset uint32
limit uint32
}
tests := []struct {
name string
fields fields
args args
want uint32
wantRes []*model.Platform
wantErr bool
}{
{
name: "",
fields: fields{
handler: handler,
},
args: args{
query: map[string]string{
"Name": platNames[0],
},
offset: 0,
limit: 20,
},
want: uint32(len(wantResSlice[0])),
wantRes: wantResSlice[0],
wantErr: false,
},
{
name: "",
fields: fields{
handler: handler,
},
args: args{
query: map[string]string{
"Name": platNames[1],
},
offset: 0,
limit: 20,
},
want: uint32(len(wantResSlice[1])),
wantRes: wantResSlice[1],
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &platformStore{
handler: tt.fields.handler,
}
got, got1, err := p.GetPlatforms(tt.args.query, tt.args.offset, tt.args.limit)
if (err != nil) != tt.wantErr {
t.Errorf("platformStore.GetPlatforms() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("platformStore.GetPlatforms() got = %v, want %v", got, tt.want)
}

// The effect of shielding time on results
tN := time.Now()

sort.Slice(got1, func(i, j int) bool {
got1[i].CreateTime = tN
got1[i].ModifyTime = tN
got1[j].CreateTime = tN
got1[j].ModifyTime = tN
return strings.Compare(got1[i].ID, got1[j].ID) < 0
})
sort.Slice(tt.wantRes, func(i, j int) bool {
tt.wantRes[i].CreateTime = tN
tt.wantRes[i].ModifyTime = tN
tt.wantRes[j].CreateTime = tN
tt.wantRes[j].ModifyTime = tN
return strings.Compare(tt.wantRes[i].ID, tt.wantRes[j].ID) < 0
})

if !reflect.DeepEqual(got1, tt.wantRes) {
t.Errorf("platformStore.GetPlatforms() got1 = %v, want %v", got1, tt.wantRes)
}
})
}
})
}

0 comments on commit 21c0983

Please sign in to comment.