forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
181 lines (150 loc) · 4.43 KB
/
cache_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
// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xorm
import (
"testing"
"time"
"xorm.io/xorm/caches"
"github.com/stretchr/testify/assert"
)
func TestCacheFind(t *testing.T) {
assert.NoError(t, prepareEngine())
type MailBox struct {
Id int64 `xorm:"pk"`
Username string
Password string
}
oldCacher := testEngine.GetDefaultCacher()
cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour, 10000)
testEngine.SetDefaultCacher(cacher)
assert.NoError(t, testEngine.Sync2(new(MailBox)))
var inserts = []*MailBox{
{
Id: 0,
Username: "user1",
Password: "pass1",
},
{
Id: 1,
Username: "user2",
Password: "pass2",
},
}
_, err := testEngine.Insert(inserts[0], inserts[1])
assert.NoError(t, err)
var boxes []MailBox
assert.NoError(t, testEngine.Find(&boxes))
assert.EqualValues(t, 2, len(boxes))
for i, box := range boxes {
assert.Equal(t, inserts[i].Id, box.Id)
assert.Equal(t, inserts[i].Username, box.Username)
assert.Equal(t, inserts[i].Password, box.Password)
}
boxes = make([]MailBox, 0, 2)
assert.NoError(t, testEngine.Find(&boxes))
assert.EqualValues(t, 2, len(boxes))
for i, box := range boxes {
assert.Equal(t, inserts[i].Id, box.Id)
assert.Equal(t, inserts[i].Username, box.Username)
assert.Equal(t, inserts[i].Password, box.Password)
}
boxes = make([]MailBox, 0, 2)
assert.NoError(t, testEngine.Alias("a").Where("a.id > -1").Asc("a.id").Find(&boxes))
assert.EqualValues(t, 2, len(boxes))
for i, box := range boxes {
assert.Equal(t, inserts[i].Id, box.Id)
assert.Equal(t, inserts[i].Username, box.Username)
assert.Equal(t, inserts[i].Password, box.Password)
}
type MailBox4 struct {
Id int64
Username string
Password string
}
boxes2 := make([]MailBox4, 0, 2)
assert.NoError(t, testEngine.Table("mail_box").Where("mail_box.id > -1").Asc("mail_box.id").Find(&boxes2))
assert.EqualValues(t, 2, len(boxes2))
for i, box := range boxes2 {
assert.Equal(t, inserts[i].Id, box.Id)
assert.Equal(t, inserts[i].Username, box.Username)
assert.Equal(t, inserts[i].Password, box.Password)
}
testEngine.SetDefaultCacher(oldCacher)
}
func TestCacheFind2(t *testing.T) {
assert.NoError(t, prepareEngine())
type MailBox2 struct {
Id uint64 `xorm:"pk"`
Username string
Password string
}
oldCacher := testEngine.GetDefaultCacher()
cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour, 10000)
testEngine.SetDefaultCacher(cacher)
assert.NoError(t, testEngine.Sync2(new(MailBox2)))
var inserts = []*MailBox2{
{
Id: 0,
Username: "user1",
Password: "pass1",
},
{
Id: 1,
Username: "user2",
Password: "pass2",
},
}
_, err := testEngine.Insert(inserts[0], inserts[1])
assert.NoError(t, err)
var boxes []MailBox2
assert.NoError(t, testEngine.Find(&boxes))
assert.EqualValues(t, 2, len(boxes))
for i, box := range boxes {
assert.Equal(t, inserts[i].Id, box.Id)
assert.Equal(t, inserts[i].Username, box.Username)
assert.Equal(t, inserts[i].Password, box.Password)
}
boxes = make([]MailBox2, 0, 2)
assert.NoError(t, testEngine.Find(&boxes))
assert.EqualValues(t, 2, len(boxes))
for i, box := range boxes {
assert.Equal(t, inserts[i].Id, box.Id)
assert.Equal(t, inserts[i].Username, box.Username)
assert.Equal(t, inserts[i].Password, box.Password)
}
testEngine.SetDefaultCacher(oldCacher)
}
func TestCacheGet(t *testing.T) {
assert.NoError(t, prepareEngine())
type MailBox3 struct {
Id uint64
Username string
Password string
}
oldCacher := testEngine.GetDefaultCacher()
cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour, 10000)
testEngine.SetDefaultCacher(cacher)
assert.NoError(t, testEngine.Sync2(new(MailBox3)))
var inserts = []*MailBox3{
{
Username: "user1",
Password: "pass1",
},
}
_, err := testEngine.Insert(inserts[0])
assert.NoError(t, err)
var box1 MailBox3
has, err := testEngine.Where("id = ?", inserts[0].Id).Get(&box1)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, "user1", box1.Username)
assert.EqualValues(t, "pass1", box1.Password)
var box2 MailBox3
has, err = testEngine.Where("id = ?", inserts[0].Id).Get(&box2)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, "user1", box2.Username)
assert.EqualValues(t, "pass1", box2.Password)
testEngine.SetDefaultCacher(oldCacher)
}