-
Notifications
You must be signed in to change notification settings - Fork 11
/
wizard_test.go
187 lines (150 loc) · 5.1 KB
/
wizard_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package wizard
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewWizard(t *testing.T) {
assert := assert.New(t)
wiz := NewWizard()
assert.NotNil(wiz)
assert.Empty(wiz.clusters)
}
func TestSetDefault(t *testing.T) {
assert := assert.New(t)
wiz := NewWizard()
assert.Nil(wiz.defaultCluster)
c := NewCluster("db")
wiz.SetDefault(c)
assert.Equal(c, wiz.defaultCluster, "It can set StandardCluster")
s := &ShardCluster{}
wiz.SetDefault(s)
assert.Equal(s, wiz.defaultCluster, "It can set ShardCluster")
}
func TestHasDefault(t *testing.T) {
assert := assert.New(t)
wiz := NewWizard()
assert.False(wiz.HasDefault())
c := NewCluster("db")
wiz.SetDefault(c)
assert.True(wiz.HasDefault())
}
func TestGetCluster(t *testing.T) {
assert := assert.New(t)
wiz := NewWizard()
assert.Nil(wiz.getCluster("table name"))
c := NewCluster("db")
wiz.clusters["table name"] = c
assert.Equal(c, wiz.getCluster("table name"))
}
func TestSetCluster(t *testing.T) {
assert := assert.New(t)
wiz := NewWizard()
assert.Nil(wiz.clusters["table name"])
c := NewCluster("db")
wiz.setCluster(c, "table name")
assert.Equal(c, wiz.clusters["table name"])
}
func TestCreateCluster(t *testing.T) {
assert := assert.New(t)
wiz := NewWizard()
c := wiz.CreateCluster("table name", "db-master")
assert.NotNil(c)
assert.NotNil(c.master)
assert.Empty(c.slaves)
assert.Equal("db-master", c.master.db)
}
func TestCreateShardCluster(t *testing.T) {
assert := assert.New(t)
var s *ShardCluster
wiz := NewWizard()
var slotsize int64 = 99
s = wiz.CreateShardCluster("table name", slotsize)
assert.NotNil(s)
assert.Empty(s.List)
assert.Equal(int64(99), s.slotsize)
assert.Equal(s, wiz.clusters["table name"])
var slotsizeZero int64
s = wiz.CreateShardCluster("table name2", slotsizeZero)
assert.NotNil(s)
assert.Empty(s.List)
assert.Equal(int64(1), s.slotsize)
assert.Equal(s, wiz.clusters["table name2"])
var slotsizeMinus int64 = -99
s = wiz.CreateShardCluster("table name", slotsizeMinus)
assert.NotNil(s)
assert.Empty(s.List)
assert.Equal(int64(1), s.slotsize)
assert.Equal(s, wiz.clusters["table name"])
}
func TestSelect(t *testing.T) {
assert := assert.New(t)
wiz := NewWizard()
// shard test
type myStruct struct {
ID int64 `shard_key:"true"`
}
s := wiz.CreateShardCluster(myStruct{}, 100)
shardSet1 := NewCluster("shard01-master")
shardSet2 := NewCluster("shard02-master")
s.RegisterShard(0, 49, shardSet1)
s.RegisterShard(50, 99, shardSet2)
assert.Equal(shardSet1, wiz.Select(&myStruct{ID: 1}))
assert.Equal(shardSet1, wiz.Select(&myStruct{ID: 49}))
assert.Equal(shardSet2, wiz.Select(&myStruct{ID: 50}))
assert.Equal(shardSet2, wiz.Select(&myStruct{ID: 99}))
assert.Equal(shardSet1, wiz.Select(&myStruct{ID: 100}))
assert.Equal(shardSet1, wiz.Select(&myStruct{ID: 149}))
assert.Equal(shardSet2, wiz.Select(&myStruct{ID: 150}))
assert.Equal(shardSet2, wiz.Select(&myStruct{ID: 199}))
assert.Equal(shardSet1, wiz.Select(&myStruct{ID: 200}))
// standard test
c1 := wiz.CreateCluster("standard table", "db-master")
c2 := wiz.Select("standard table")
assert.Equal(c1, c2)
c3 := wiz.CreateCluster(myStruct{}, "db-master")
c4 := wiz.Select(&myStruct{ID: 100})
assert.Equal(c3, c4)
// error
_ = wiz.CreateShardCluster("shard table", 100)
nilShard := wiz.Select("shard table")
assert.Nil(nilShard, "Select() returns nil for shardcluster when obj does not contain shardkey")
nilTable := wiz.Select("not registered")
assert.Nil(nilTable, "Select() returns nil when table name does not registered")
}
func TestSelectByKey(t *testing.T) {
assert := assert.New(t)
wiz := NewWizard()
c1 := wiz.CreateCluster("standard table", "db-master")
c2 := wiz.SelectByKey("standard table", 1)
c3 := wiz.SelectByKey("standard table", 99)
assert.Equal(c1, c2)
assert.Equal(c1, c3)
// object test
type myStruct struct {
ID int64 `shard_key:"true"`
}
s1 := wiz.CreateShardCluster(myStruct{}, 100)
shardSet1 := NewCluster("shard01-master")
shardSet2 := NewCluster("shard02-master")
s1.RegisterShard(0, 49, shardSet1)
s1.RegisterShard(50, 99, shardSet2)
assert.Equal(shardSet1, wiz.SelectByKey(&myStruct{ID: 99}, 1))
assert.Equal(shardSet1, wiz.SelectByKey(&myStruct{ID: 99}, 49))
assert.Equal(shardSet2, wiz.SelectByKey(&myStruct{ID: 99}, 50))
assert.Equal(shardSet2, wiz.SelectByKey(&myStruct{ID: 99}, 99))
assert.Equal(shardSet1, wiz.SelectByKey(&myStruct{ID: 99}, 100))
assert.Equal(shardSet1, wiz.SelectByKey(&myStruct{ID: 99}, 149))
assert.Equal(shardSet2, wiz.SelectByKey(&myStruct{ID: 99}, 150))
assert.Equal(shardSet2, wiz.SelectByKey(&myStruct{ID: 99}, 199))
assert.Equal(shardSet1, wiz.SelectByKey(&myStruct{ID: 99}, 200))
// non object test
s2 := wiz.CreateShardCluster("shard table", 100)
s2.RegisterShard(0, 49, NewCluster("x01-master"))
s2.RegisterShard(50, 99, NewCluster("x02-master"))
c4 := s2.SelectByKey(5000)
c5 := wiz.SelectByKey("shard table", 5000)
assert.Equal(c4, c5, "Select() returns nil for shardcluster when obj does not contain shardkey")
// error
nilTable := wiz.SelectByKey("not registered", 99)
assert.Nil(nilTable, "Select() returns nil when table name does not registered")
}