-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin_test.go
229 lines (210 loc) · 5.37 KB
/
plugin_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
223
224
225
226
227
228
229
package deepgorm
import (
"github.com/google/uuid"
"github.com/ing-bank/gormtestutil"
"github.com/stretchr/testify/assert"
"gorm.io/gorm/clause"
"testing"
)
func TestDeepGorm_Name_ReturnsExpectedName(t *testing.T) {
t.Parallel()
// Arrange
plugin := New()
// Act
result := plugin.Name()
// Assert
assert.Equal(t, "deepgorm", result)
}
func TestDeepGorm_Initialize_RegistersCallback(t *testing.T) {
t.Parallel()
// Arrange
db := gormtestutil.NewMemoryDatabase(t, gormtestutil.WithName(t.Name()))
plugin := New()
// Act
err := plugin.Initialize(db)
// Assert
assert.Nil(t, err)
assert.NotNil(t, db.Callback().Query().Get("deepgorm:query"))
}
type ObjectB struct {
ID uuid.UUID
Name string
ObjectA *ObjectA `gorm:"foreignKey:ObjectAID"`
ObjectAID uuid.UUID
}
type ObjectA struct {
ID uuid.UUID
Name string
ObjectBs []ObjectB `gorm:"foreignKey:ObjectAID"`
}
func TestDeepGorm_Initialize_TriggersFilteringCorrectly(t *testing.T) {
t.Parallel()
tests := map[string]struct {
filter map[string]any
existing []ObjectA
expected []ObjectA
}{
"nothing": {
expected: []ObjectA{},
},
"no filter": {
filter: map[string]any{},
existing: []ObjectA{
{ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688")},
{ID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460")},
{ID: uuid.MustParse("383e9a9b-ef95-421d-a89e-60f0344ee29d")},
},
expected: []ObjectA{
{ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"), ObjectBs: []ObjectB{}},
{ID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"), ObjectBs: []ObjectB{}},
{ID: uuid.MustParse("383e9a9b-ef95-421d-a89e-60f0344ee29d"), ObjectBs: []ObjectB{}},
},
},
"simple filter": {
filter: map[string]any{
"id": uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"),
},
existing: []ObjectA{
{ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688")},
{ID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460")},
{ID: uuid.MustParse("383e9a9b-ef95-421d-a89e-60f0344ee29d")},
},
expected: []ObjectA{
{ID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"), ObjectBs: []ObjectB{}},
},
},
"deep filter": {
filter: map[string]any{
"object_bs": map[string]any{
"name": "abc",
},
},
existing: []ObjectA{
{
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
Name: "def",
},
},
},
{
ID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"),
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("83aaf47d-a167-4a49-8b7c-3516ced56e8a"),
Name: "abc",
},
},
},
{
ID: uuid.MustParse("383e9a9b-ef95-421d-a89e-60f0344ee29d"),
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("3b35e207-c544-424e-b029-be31d5fe8bad"),
Name: "abc",
},
},
},
},
expected: []ObjectA{
{
ID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"),
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("83aaf47d-a167-4a49-8b7c-3516ced56e8a"),
Name: "abc",
ObjectAID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"),
},
},
},
{
ID: uuid.MustParse("383e9a9b-ef95-421d-a89e-60f0344ee29d"),
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("3b35e207-c544-424e-b029-be31d5fe8bad"),
Name: "abc",
ObjectAID: uuid.MustParse("383e9a9b-ef95-421d-a89e-60f0344ee29d"),
},
},
},
},
},
"multi filter": {
filter: map[string]any{
"name": "ghi",
"object_bs": map[string]any{
"name": "def",
},
},
existing: []ObjectA{
{
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
Name: "ghi",
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
Name: "def",
},
},
},
{
ID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"),
Name: "nope",
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("83aaf47d-a167-4a49-8b7c-3516ced56e8a"),
Name: "abc",
},
},
},
{
ID: uuid.MustParse("383e9a9b-ef95-421d-a89e-60f0344ee29d"),
Name: "Maybe",
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("3b35e207-c544-424e-b029-be31d5fe8bad"),
Name: "abc",
},
},
},
},
expected: []ObjectA{
{
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
Name: "ghi",
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
Name: "def",
ObjectAID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
},
},
},
},
},
}
for name, testData := range tests {
testData := testData
t.Run(name, func(t *testing.T) {
t.Parallel()
// Arrange
db := gormtestutil.NewMemoryDatabase(t, gormtestutil.WithName(t.Name()))
_ = db.AutoMigrate(&ObjectA{}, &ObjectB{})
plugin := New()
if err := db.CreateInBatches(testData.existing, 10).Error; err != nil {
t.Error(err)
t.FailNow()
}
// Act
err := db.Use(plugin)
// Assert
assert.Nil(t, err)
var actual []ObjectA
err = db.Where(testData.filter).Preload(clause.Associations).Find(&actual).Error
assert.Nil(t, err)
assert.Equal(t, testData.expected, actual)
})
}
}