-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathget_by_ids_test.go
167 lines (153 loc) · 3.71 KB
/
get_by_ids_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
package beeorm
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type getByIdsEntity struct {
ID uint64 `orm:"localCache;redisCache"`
Name string `orm:"max=100"`
}
func TestLoadByIdsNoCache(t *testing.T) {
testLoadByIds(t, false, false)
}
func TestLoadByIdsLocalCache(t *testing.T) {
testLoadByIds(t, true, false)
}
func TestLoadByIdsRedisCache(t *testing.T) {
testLoadByIds(t, false, true)
}
func TestLoadByIdsLocalRedisCache(t *testing.T) {
testLoadByIds(t, true, true)
}
func testLoadByIds(t *testing.T, local, redis bool) {
var entity *getByIdsEntity
orm := PrepareTables(t, NewRegistry(), entity)
schema := GetEntitySchema[getByIdsEntity](orm)
schema.DisableCache(!local, !redis)
var ids []uint64
for i := 0; i < 10; i++ {
entity = NewEntity[getByIdsEntity](orm)
entity.Name = fmt.Sprintf("Name %d", i)
ids = append(ids, entity.ID)
}
err := orm.Flush()
assert.NoError(t, err)
loggerDB := &MockLogHandler{}
orm.RegisterQueryLogger(loggerDB, true, false, false)
loggerRedis := &MockLogHandler{}
orm.RegisterQueryLogger(loggerRedis, false, true, false)
loggerLocal := &MockLogHandler{}
orm.RegisterQueryLogger(loggerLocal, false, false, false)
rows := GetByIDs[getByIdsEntity](orm, ids...)
assert.Equal(t, 10, rows.Len())
i := 0
for rows.Next() {
e := rows.Entity()
assert.NotNil(t, e)
assert.Equal(t, fmt.Sprintf("Name %d", i), e.Name)
i++
}
assert.Equal(t, 10, i)
if !local && !redis {
assert.Len(t, loggerDB.Logs, 1)
}
loggerDB.Clear()
if local {
lc, _ := schema.GetLocalCache()
lc.Clear(orm)
}
if redis {
rc, _ := schema.GetRedisCache()
rc.FlushDB(orm)
}
rows = GetByIDs[getByIdsEntity](orm, ids...)
assert.Equal(t, 10, rows.Len())
i = 0
for rows.Next() {
e := rows.Entity()
assert.NotNil(t, e)
assert.Equal(t, fmt.Sprintf("Name %d", i), e.Name)
i++
}
if local {
assert.Len(t, loggerDB.Logs, 1)
} else {
assert.Len(t, loggerDB.Logs, 1)
}
loggerDB.Clear()
if local || redis {
rows = GetByIDs[getByIdsEntity](orm, ids...)
assert.Equal(t, 10, rows.Len())
i = 0
for rows.Next() {
e := rows.Entity()
assert.NotNil(t, e)
assert.Equal(t, fmt.Sprintf("Name %d", i), e.Name)
i++
}
assert.Len(t, loggerDB.Logs, 0)
}
loggerDB.Clear()
// invalid ids
rows = GetByIDs[getByIdsEntity](orm, 7777, 8888, 9999)
assert.Equal(t, 3, rows.Len())
i = 0
for rows.Next() {
assert.Nil(t, rows.Entity())
i++
}
assert.Equal(t, 3, i)
if local {
assert.Len(t, loggerDB.Logs, 1)
} else {
assert.Len(t, loggerDB.Logs, 1)
}
loggerDB.Clear()
if local || redis {
rows = GetByIDs[getByIdsEntity](orm, 7777, 8888, 9999)
assert.Equal(t, 3, rows.Len())
for rows.Next() {
assert.Nil(t, rows.Entity())
}
assert.Len(t, loggerDB.Logs, 0)
}
if local && redis {
lc, _ := schema.GetLocalCache()
lc.Clear(orm)
loggerDB.Clear()
rows = GetByIDs[getByIdsEntity](orm, 7777, 8888, 9999)
assert.Equal(t, 3, rows.Len())
for rows.Next() {
assert.Nil(t, rows.Entity())
}
assert.Len(t, loggerDB.Logs, 0)
loggerLocal.Clear()
loggerRedis.Clear()
rows = GetByIDs[getByIdsEntity](orm, 7777, 8888, 9999)
assert.Equal(t, 3, rows.Len())
for rows.Next() {
assert.Nil(t, rows.Entity())
}
assert.Len(t, loggerDB.Logs, 0)
assert.Len(t, loggerRedis.Logs, 0)
}
// missing one
rows = GetByIDs[getByIdsEntity](orm, ids[0], 7777, ids[1])
assert.Equal(t, 3, rows.Len())
rows.Next()
assert.NotNil(t, rows.Entity())
rows.Next()
assert.Nil(t, rows.Entity())
rows.Next()
assert.NotNil(t, rows.Entity())
// duplicated
rows = GetByIDs[getByIdsEntity](orm, ids[0], ids[0], ids[0])
assert.Equal(t, 3, rows.Len())
for rows.Next() {
e := rows.Entity()
assert.NotNil(t, e)
assert.Equal(t, ids[0], e.ID)
assert.Equal(t, "Name 0", e.Name)
}
}