forked from xormplus/xorm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
session_delete_test.go
222 lines (173 loc) · 5.85 KB
/
session_delete_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
221
222
// 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"
"github.com/stretchr/testify/assert"
)
func TestDelete(t *testing.T) {
assert.NoError(t, prepareEngine())
type UserinfoDelete struct {
Uid int64 `xorm:"id pk not null autoincr"`
IsMan bool
}
assert.NoError(t, testEngine.Sync2(new(UserinfoDelete)))
user := UserinfoDelete{Uid: 1}
cnt, err := testEngine.Insert(&user)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
cnt, err = testEngine.Delete(&UserinfoDelete{Uid: user.Uid})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
user.Uid = 0
user.IsMan = true
has, err := testEngine.ID(1).Get(&user)
assert.NoError(t, err)
assert.False(t, has)
cnt, err = testEngine.Insert(&user)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
cnt, err = testEngine.Where("id=?", user.Uid).Delete(&UserinfoDelete{})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
user.Uid = 0
user.IsMan = true
has, err = testEngine.ID(2).Get(&user)
assert.NoError(t, err)
assert.False(t, has)
}
func TestDeleted(t *testing.T) {
assert.NoError(t, prepareEngine())
type Deleted struct {
Id int64 `xorm:"pk"`
Name string
DeletedAt time.Time `xorm:"deleted"`
}
err := testEngine.DropTables(&Deleted{})
assert.NoError(t, err)
err = testEngine.CreateTables(&Deleted{})
assert.NoError(t, err)
_, err = testEngine.InsertOne(&Deleted{Id: 1, Name: "11111"})
assert.NoError(t, err)
_, err = testEngine.InsertOne(&Deleted{Id: 2, Name: "22222"})
assert.NoError(t, err)
_, err = testEngine.InsertOne(&Deleted{Id: 3, Name: "33333"})
assert.NoError(t, err)
// Test normal Find()
var records1 []Deleted
err = testEngine.Where("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` > 0").Find(&records1, &Deleted{})
assert.EqualValues(t, 3, len(records1))
// Test normal Get()
record1 := &Deleted{}
has, err := testEngine.ID(1).Get(record1)
assert.NoError(t, err)
assert.True(t, has)
// Test Delete() with deleted
affected, err := testEngine.ID(1).Delete(&Deleted{})
assert.NoError(t, err)
assert.EqualValues(t, 1, affected)
has, err = testEngine.ID(1).Get(&Deleted{})
assert.NoError(t, err)
assert.False(t, has)
var records2 []Deleted
err = testEngine.Where("`" + testEngine.GetColumnMapper().Obj2Table("Id") + "` > 0").Find(&records2)
assert.NoError(t, err)
assert.EqualValues(t, 2, len(records2))
// Test no rows affected after Delete() again.
affected, err = testEngine.ID(1).Delete(&Deleted{})
assert.NoError(t, err)
assert.EqualValues(t, 0, affected)
// Deleted.DeletedAt must not be updated.
affected, err = testEngine.ID(2).Update(&Deleted{Name: "2", DeletedAt: time.Now()})
assert.NoError(t, err)
assert.EqualValues(t, 1, affected)
record2 := &Deleted{}
has, err = testEngine.ID(2).Get(record2)
assert.NoError(t, err)
assert.True(t, record2.DeletedAt.IsZero())
// Test find all records whatever `deleted`.
var unscopedRecords1 []Deleted
err = testEngine.Unscoped().Where("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` > 0").Find(&unscopedRecords1, &Deleted{})
assert.NoError(t, err)
assert.EqualValues(t, 3, len(unscopedRecords1))
// Delete() must really delete a record with Unscoped()
affected, err = testEngine.Unscoped().ID(1).Delete(&Deleted{})
assert.NoError(t, err)
assert.EqualValues(t, 1, affected)
var unscopedRecords2 []Deleted
err = testEngine.Unscoped().Where("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` > 0").Find(&unscopedRecords2, &Deleted{})
assert.NoError(t, err)
assert.EqualValues(t, 2, len(unscopedRecords2))
var records3 []Deleted
err = testEngine.Where("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` > 0").And("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"`> 1").
Or("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` = ?", 3).Find(&records3)
assert.NoError(t, err)
assert.EqualValues(t, 2, len(records3))
}
func TestCacheDelete(t *testing.T) {
assert.NoError(t, prepareEngine())
oldCacher := testEngine.GetDefaultCacher()
cacher := NewLRUCacher(NewMemoryStore(), 1000)
testEngine.SetDefaultCacher(cacher)
type CacheDeleteStruct struct {
Id int64
}
err := testEngine.CreateTables(&CacheDeleteStruct{})
assert.NoError(t, err)
_, err = testEngine.Insert(&CacheDeleteStruct{})
assert.NoError(t, err)
aff, err := testEngine.Delete(&CacheDeleteStruct{
Id: 1,
})
assert.NoError(t, err)
assert.EqualValues(t, aff, 1)
aff, err = testEngine.Unscoped().Delete(&CacheDeleteStruct{
Id: 1,
})
assert.NoError(t, err)
assert.EqualValues(t, aff, 0)
testEngine.SetDefaultCacher(oldCacher)
}
func TestUnscopeDelete(t *testing.T) {
assert.NoError(t, prepareEngine())
type UnscopeDeleteStruct struct {
Id int64
Name string
DeletedAt time.Time `xorm:"deleted"`
}
assertSync(t, new(UnscopeDeleteStruct))
cnt, err := testEngine.Insert(&UnscopeDeleteStruct{
Name: "test",
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var nowUnix = time.Now().Unix()
var s UnscopeDeleteStruct
cnt, err = testEngine.ID(1).Delete(&s)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
assert.EqualValues(t, nowUnix, s.DeletedAt.Unix())
var s1 UnscopeDeleteStruct
has, err := testEngine.ID(1).Get(&s1)
assert.NoError(t, err)
assert.False(t, has)
var s2 UnscopeDeleteStruct
has, err = testEngine.ID(1).Unscoped().Get(&s2)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, "test", s2.Name)
assert.EqualValues(t, nowUnix, s2.DeletedAt.Unix())
cnt, err = testEngine.ID(1).Unscoped().Delete(new(UnscopeDeleteStruct))
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var s3 UnscopeDeleteStruct
has, err = testEngine.ID(1).Get(&s3)
assert.NoError(t, err)
assert.False(t, has)
var s4 UnscopeDeleteStruct
has, err = testEngine.ID(1).Unscoped().Get(&s4)
assert.NoError(t, err)
assert.False(t, has)
}