Skip to content

Commit a68ab4b

Browse files
authored
Merge pull request #6 from bramca/master
Add filter replacement for OR conditions
2 parents 965e83d + 13c169f commit a68ab4b

File tree

5 files changed

+102
-6
lines changed

5 files changed

+102
-6
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
run: go test -json > TestResults-${{ matrix.go-version }}.json
2222

2323
- name: Upload Go test results for ${{ matrix.go-version }}
24-
uses: actions/upload-artifact@v3
24+
uses: actions/upload-artifact@v4
2525
with:
2626
name: Go-results-${{ matrix.go-version }}
2727
path: TestResults-${{ matrix.go-version }}.json

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/stretchr/testify v1.8.1
99
github.com/survivorbat/go-tsyncmap v0.0.0
1010
gorm.io/driver/sqlite v1.5.2
11-
gorm.io/gorm v1.25.6
11+
gorm.io/gorm v1.25.12
1212
)
1313

1414
require (
@@ -18,5 +18,6 @@ require (
1818
github.com/kr/text v0.2.0 // indirect
1919
github.com/mattn/go-sqlite3 v1.14.17 // indirect
2020
github.com/pmezard/go-difflib v1.0.0 // indirect
21+
golang.org/x/text v0.22.0 // indirect
2122
gopkg.in/yaml.v3 v3.0.1 // indirect
2223
)

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
2727
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
2828
github.com/survivorbat/go-tsyncmap v0.0.0 h1:XTc1+uXyuw//1Hhpg4IxW6tEe3Tvd2d5vM/6IPqmkeg=
2929
github.com/survivorbat/go-tsyncmap v0.0.0/go.mod h1:zKe2CuXEo+c1d9DVT5L7AG2jPTdWi7QQN/Gk+26Vecg=
30+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
31+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
3032
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3133
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
3234
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3335
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
3436
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3537
gorm.io/driver/sqlite v1.5.2 h1:TpQ+/dqCY4uCigCFyrfnrJnrW9zjpelWVoEVNy5qJkc=
3638
gorm.io/driver/sqlite v1.5.2/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
37-
gorm.io/gorm v1.25.6 h1:V92+vVda1wEISSOMtodHVRcUIOPYa2tgQtyF+DfFx+A=
38-
gorm.io/gorm v1.25.6/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
39+
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
40+
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=

plugin.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ func createDeepFilterRecursively(exprs []clause.Expression, db *gorm.DB) {
4141
switch cond := cond.(type) {
4242
case clause.AndConditions:
4343
createDeepFilterRecursively(exprs[index].(clause.AndConditions).Exprs, db)
44-
44+
case clause.OrConditions:
45+
createDeepFilterRecursively(exprs[index].(clause.OrConditions).Exprs, db)
4546
case clause.Eq:
4647
switch value := cond.Value.(type) {
4748
case map[string]any:

plugin_test.go

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package deepgorm
22

33
import (
4+
"testing"
5+
46
"github.com/google/uuid"
57
"github.com/ing-bank/gormtestutil"
68
"github.com/stretchr/testify/assert"
79
"gorm.io/gorm/clause"
8-
"testing"
910
)
1011

1112
func TestDeepGorm_Name_ReturnsExpectedName(t *testing.T) {
@@ -227,3 +228,94 @@ func TestDeepGorm_Initialize_TriggersFilteringCorrectly(t *testing.T) {
227228
})
228229
}
229230
}
231+
232+
func TestDeepGorm_Initialize_TriggersFilteringCorrectlyWithOrQuery(t *testing.T) {
233+
t.Parallel()
234+
filter1 := map[string]any{
235+
"object_bs": map[string]any{
236+
"name": "def",
237+
},
238+
}
239+
filter2 := map[string]any{
240+
"object_bs": map[string]any{
241+
"name": "abc",
242+
},
243+
}
244+
existing := []ObjectA{
245+
{
246+
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
247+
Name: "ghi",
248+
ObjectBs: []ObjectB{
249+
{
250+
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
251+
Name: "def",
252+
},
253+
},
254+
},
255+
{
256+
ID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"),
257+
Name: "nope",
258+
ObjectBs: []ObjectB{
259+
{
260+
ID: uuid.MustParse("83aaf47d-a167-4a49-8b7c-3516ced56e8a"),
261+
Name: "abc",
262+
},
263+
},
264+
},
265+
{
266+
ID: uuid.MustParse("383e9a9b-ef95-421d-a89e-60f0344ee29d"),
267+
Name: "Maybe",
268+
ObjectBs: []ObjectB{
269+
{
270+
ID: uuid.MustParse("3b35e207-c544-424e-b029-be31d5fe8bad"),
271+
Name: "cba",
272+
},
273+
},
274+
},
275+
}
276+
expected := []ObjectA{
277+
{
278+
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
279+
Name: "ghi",
280+
ObjectBs: []ObjectB{
281+
{
282+
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
283+
Name: "def",
284+
ObjectAID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
285+
},
286+
},
287+
},
288+
{
289+
ID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"),
290+
Name: "nope",
291+
ObjectBs: []ObjectB{
292+
{
293+
ID: uuid.MustParse("83aaf47d-a167-4a49-8b7c-3516ced56e8a"),
294+
Name: "abc",
295+
ObjectAID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"),
296+
},
297+
},
298+
},
299+
}
300+
301+
db := gormtestutil.NewMemoryDatabase(t, gormtestutil.WithName(t.Name()))
302+
_ = db.AutoMigrate(&ObjectA{}, &ObjectB{})
303+
plugin := New()
304+
305+
if err := db.CreateInBatches(existing, 10).Error; err != nil {
306+
t.Error(err)
307+
t.FailNow()
308+
}
309+
310+
// Act
311+
err := db.Use(plugin)
312+
313+
// Assert
314+
assert.Nil(t, err)
315+
316+
var actual []ObjectA
317+
err = db.Where(filter1).Or(filter2).Preload(clause.Associations).Find(&actual).Error
318+
assert.Nil(t, err)
319+
320+
assert.Equal(t, expected, actual)
321+
}

0 commit comments

Comments
 (0)