-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_references_test.go
107 lines (96 loc) · 3.49 KB
/
load_references_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
package orm
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type loadReferenceSub struct {
Sub1 Reference[loadSubReferenceEntity1]
Sub2 Reference[loadSubReferenceEntity2]
}
type loadReferenceEntity struct {
ID uint64 `orm:"localCache;redisCache"`
Name string `orm:"required"`
Sub loadReferenceSub
Ref1a Reference[loadSubReferenceEntity1]
Ref1b Reference[loadSubReferenceEntity1]
Ref2 Reference[loadSubReferenceEntity2]
Ref1Array [2]Reference[loadSubReferenceEntity1]
}
type loadSubReferenceEntity1 struct {
ID uint64 `orm:"localCache;redisCache"`
Name string `orm:"required"`
SubRef2 Reference[loadSubReferenceEntity2]
}
type loadSubReferenceEntity2 struct {
ID uint64 `orm:"localCache;redisCache"`
Name string `orm:"required"`
}
func TestLoadReferencesLocal(t *testing.T) {
testLoadReferences(t, true, false)
}
func testLoadReferences(t *testing.T, local, redis bool) {
var entity *loadReferenceEntity
var ref1 *loadSubReferenceEntity1
var ref2 *loadSubReferenceEntity2
orm := PrepareTables(t, NewRegistry(), entity, ref1, ref2)
schema := GetEntitySchema[loadReferenceEntity](orm)
schema.DisableCache(!local, !redis)
GetEntitySchema[loadSubReferenceEntity1](orm).DisableCache(!local, !redis)
GetEntitySchema[loadSubReferenceEntity2](orm).DisableCache(!local, !redis)
for i := 1; i <= 10; i++ {
entity = NewEntity[loadReferenceEntity](orm)
entity.Name = fmt.Sprintf("Entity %d", i)
ref1 = NewEntity[loadSubReferenceEntity1](orm)
ref1.Name = fmt.Sprintf("Ref1 %d", i)
entity.Ref1a = Reference[loadSubReferenceEntity1](ref1.ID)
entity.Ref1Array[0] = Reference[loadSubReferenceEntity1](ref1.ID)
sub1 := NewEntity[loadSubReferenceEntity1](orm)
sub1.Name = fmt.Sprintf("Sub1 %d", i)
entity.Sub.Sub1 = Reference[loadSubReferenceEntity1](sub1.ID)
ref2 = NewEntity[loadSubReferenceEntity2](orm)
ref2.Name = fmt.Sprintf("Ref2 %d", i)
entity.Ref2 = Reference[loadSubReferenceEntity2](ref2.ID)
if i > 5 {
ref1.SubRef2 = Reference[loadSubReferenceEntity2](ref2.ID)
ref1 = NewEntity[loadSubReferenceEntity1](orm)
ref1.Name = fmt.Sprintf("Ref1b %d", i)
entity.Ref1b = Reference[loadSubReferenceEntity1](ref1.ID)
entity.Ref1Array[1] = Reference[loadSubReferenceEntity1](ref1.ID)
sub2 := NewEntity[loadSubReferenceEntity2](orm)
sub2.Name = fmt.Sprintf("Sub2 %d", i)
entity.Sub.Sub2 = Reference[loadSubReferenceEntity2](sub2.ID)
} else {
sub2 := NewEntity[loadSubReferenceEntity2](orm)
sub2.Name = fmt.Sprintf("SubSub %d", i)
ref1.SubRef2 = Reference[loadSubReferenceEntity2](sub2.ID)
}
}
err := orm.Flush()
assert.NoError(t, err)
iterator := Search[loadReferenceEntity](orm, NewWhere("1"), nil)
assert.Equal(t, 10, iterator.Len())
if local {
schema.(*entitySchema).localCache.Clear(orm)
GetEntitySchema[loadSubReferenceEntity1](orm).(*entitySchema).localCache.Clear(orm)
GetEntitySchema[loadSubReferenceEntity2](orm).(*entitySchema).localCache.Clear(orm)
}
for iterator.Next() {
iterator.Entity()
}
loggerDB := &MockLogHandler{}
orm.RegisterQueryLogger(loggerDB, true, false, false)
loggerLocal := &MockLogHandler{}
orm.RegisterQueryLogger(loggerLocal, false, false, true)
loggerRedis := &MockLogHandler{}
orm.RegisterQueryLogger(loggerRedis, false, true, false)
iterator.LoadReference("Ref1a")
assert.Len(t, loggerDB.Logs, 1)
i := 0
for iterator.Next() {
entity = iterator.Entity()
assert.Equal(t, fmt.Sprintf("Ref1 %d", i+1), entity.Ref1a.GetEntity(orm).Name)
i++
}
assert.Equal(t, 10, i)
}