-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathget_by_reference_test.go
220 lines (194 loc) · 6.55 KB
/
get_by_reference_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package beeorm
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type getByReferenceEntity struct {
ID uint64 `orm:"localCache;redisCache"`
Name string
Ref Reference[getByReferenceReference] `orm:"index=Ref"`
RefCached Reference[getByReferenceReference] `orm:"index=RefCached;cached"`
RefCachedNoCache Reference[getByReferenceReferenceNoCache] `orm:"index=RefCachedNoCache;cached"`
}
type getByReferenceReference struct {
ID uint64 `orm:"localCache;redisCache"`
Name string
}
type getByReferenceReferenceNoCache struct {
ID uint64
Name string
}
func TestGetByReferenceNoCache(t *testing.T) {
testGetByReference(t, false, false)
}
func TestGetByReferenceLocalCache(t *testing.T) {
testGetByReference(t, true, false)
}
func TestGetByReferenceRedisCache(t *testing.T) {
testGetByReference(t, false, true)
}
func TestGetByReferenceLocalRedisCache(t *testing.T) {
testGetByReference(t, true, true)
}
func testGetByReference(t *testing.T, local, redis bool) {
var entity *getByReferenceEntity
orm := PrepareTables(t, NewRegistry(), entity, getByReferenceReference{}, getByReferenceReferenceNoCache{})
schema := GetEntitySchema[getByReferenceEntity](orm)
schema.DisableCache(!local, !redis)
loggerDB := &MockLogHandler{}
orm.RegisterQueryLogger(loggerDB, true, false, false)
// getting missing rows
rows := GetByReference[getByReferenceEntity](orm, "RefCached", 1)
assert.Equal(t, 0, rows.Len())
loggerDB.Clear()
rows = GetByReference[getByReferenceEntity](orm, "RefCached", 1)
assert.Equal(t, 0, rows.Len())
assert.Len(t, loggerDB.Logs, 0)
loggerDB.Clear()
var entities []*getByReferenceEntity
ref := NewEntity[getByReferenceReference](orm)
ref.Name = "Ref 1"
ref2 := NewEntity[getByReferenceReference](orm)
ref2.Name = "Ref 2"
refNoCache := NewEntity[getByReferenceReferenceNoCache](orm)
refNoCache.Name = "Ref 1"
refNoCache2 := NewEntity[getByReferenceReferenceNoCache](orm)
refNoCache2.Name = "Ref 2"
for i := 0; i < 10; i++ {
entity = NewEntity[getByReferenceEntity](orm)
entity.Name = fmt.Sprintf("Name %d", i)
entity.Ref = Reference[getByReferenceReference](ref.ID)
entity.RefCached = Reference[getByReferenceReference](ref.ID)
entity.RefCachedNoCache = Reference[getByReferenceReferenceNoCache](refNoCache.ID)
entities = append(entities, entity)
}
err := orm.Flush()
assert.NoError(t, err)
loggerDB.Clear()
rows = GetByReference[getByReferenceEntity](orm, "Ref", ref.ID)
assert.Equal(t, 10, rows.Len())
rows.Next()
e := rows.Entity()
assert.Equal(t, entities[0].ID, e.ID)
assert.Equal(t, entities[0].Name, e.Name)
assert.Len(t, loggerDB.Logs, 1)
loggerDB.Clear()
rows = GetByReference[getByReferenceEntity](orm, "RefCached", ref.ID)
assert.Equal(t, 10, rows.Len())
rows.Next()
e = rows.Entity()
assert.Equal(t, entities[0].ID, e.ID)
assert.Equal(t, entities[0].Name, e.Name)
assert.Len(t, loggerDB.Logs, 1)
loggerDB.Clear()
rows = GetByReference[getByReferenceEntity](orm, "RefCached", ref.ID)
assert.Equal(t, 10, rows.Len())
rows.Next()
e = rows.Entity()
assert.Equal(t, entities[0].ID, e.ID)
assert.Equal(t, entities[0].Name, e.Name)
if local || redis {
assert.Len(t, loggerDB.Logs, 0)
}
loggerDB.Clear()
rows2 := GetByReference[getByReferenceEntity](orm, "RefCachedNoCache", ref.ID)
assert.Equal(t, 10, rows2.Len())
rows2.Next()
e = rows2.Entity()
assert.Equal(t, entities[0].ID, e.ID)
assert.Equal(t, entities[0].Name, e.Name)
assert.Len(t, loggerDB.Logs, 1)
loggerDB.Clear()
rows2 = GetByReference[getByReferenceEntity](orm, "RefCachedNoCache", ref.ID)
assert.Equal(t, 10, rows2.Len())
rows2.Next()
e = rows2.Entity()
assert.Equal(t, entities[0].ID, e.ID)
assert.Equal(t, entities[0].Name, e.Name)
if local || redis {
assert.Len(t, loggerDB.Logs, 0)
}
// Update set to nil
entity = EditEntity(orm, e)
entity.Ref = 0
entity.RefCached = 0
entity.RefCachedNoCache = 0
err = orm.Flush()
assert.NoError(t, err)
loggerDB.Clear()
rows = GetByReference[getByReferenceEntity](orm, "RefCached", ref.ID)
assert.Equal(t, 9, rows.Len())
rows.Next()
e = rows.Entity()
assert.Equal(t, entities[1].ID, e.ID)
assert.Equal(t, entities[1].Name, e.Name)
if local || redis {
assert.Len(t, loggerDB.Logs, 0)
}
loggerDB.Clear()
rows2 = GetByReference[getByReferenceEntity](orm, "RefCachedNoCache", refNoCache.ID)
assert.Equal(t, 9, rows2.Len())
rows2.Next()
e = rows2.Entity()
assert.Equal(t, entities[1].ID, e.ID)
assert.Equal(t, entities[1].Name, e.Name)
if local || redis {
assert.Len(t, loggerDB.Logs, 0)
}
loggerDB.Clear()
// update change id
entity = EditEntity(orm, entities[3])
entity.Ref = Reference[getByReferenceReference](ref2.ID)
entity.RefCached = Reference[getByReferenceReference](ref2.ID)
entity.RefCachedNoCache = Reference[getByReferenceReferenceNoCache](refNoCache2.ID)
err = orm.Flush()
assert.NoError(t, err)
loggerDB.Clear()
rows = GetByReference[getByReferenceEntity](orm, "RefCached", ref.ID)
assert.Equal(t, 8, rows.Len())
if local || redis {
assert.Len(t, loggerDB.Logs, 0)
}
rows = GetByReference[getByReferenceEntity](orm, "RefCached", ref2.ID)
assert.Equal(t, 1, rows.Len())
rows.Next()
e = rows.Entity()
assert.Equal(t, "Name 3", e.Name)
rows2 = GetByReference[getByReferenceEntity](orm, "RefCachedNoCache", refNoCache.ID)
assert.Equal(t, 8, rows2.Len())
rows2 = GetByReference[getByReferenceEntity](orm, "RefCachedNoCache", refNoCache2.ID)
assert.Equal(t, 1, rows2.Len())
rows2.Next()
e = rows2.Entity()
assert.Equal(t, "Name 3", e.Name)
DeleteEntity(orm, entities[7])
err = orm.Flush()
assert.NoError(t, err)
loggerDB.Clear()
rows = GetByReference[getByReferenceEntity](orm, "RefCached", ref.ID)
assert.Equal(t, 7, rows.Len())
if local || redis {
assert.Len(t, loggerDB.Logs, 0)
}
loggerDB.Clear()
rows2 = GetByReference[getByReferenceEntity](orm, "RefCachedNoCache", refNoCache.ID)
assert.Equal(t, 7, rows.Len())
if local || redis {
assert.Len(t, loggerDB.Logs, 0)
}
err = EditEntityField(orm, entities[0], "RefCached", ref2)
assert.NoError(t, err)
assert.NoError(t, orm.Flush())
rows = GetByReference[getByReferenceEntity](orm, "RefCached", ref2.ID)
assert.Equal(t, 2, rows.Len())
rows = GetByReference[getByReferenceEntity](orm, "RefCached", ref.ID)
assert.Equal(t, 7, rows.Len())
err = EditEntityField(orm, entities[0], "RefCached", ref)
assert.NoError(t, err)
assert.NoError(t, orm.Flush())
rows = GetByReference[getByReferenceEntity](orm, "RefCached", ref2.ID)
assert.Equal(t, 1, rows.Len())
rows = GetByReference[getByReferenceEntity](orm, "RefCached", ref.ID)
assert.Equal(t, 8, rows.Len())
}