diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4c16ab6..34b008e 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -21,7 +21,7 @@ jobs: run: go test -json > TestResults-${{ matrix.go-version }}.json - name: Upload Go test results for ${{ matrix.go-version }} - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: Go-results-${{ matrix.go-version }} path: TestResults-${{ matrix.go-version }}.json diff --git a/go.mod b/go.mod index 530a52e..281f8ba 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/stretchr/testify v1.8.1 github.com/survivorbat/go-tsyncmap v0.0.0 gorm.io/driver/sqlite v1.5.2 - gorm.io/gorm v1.25.6 + gorm.io/gorm v1.25.12 ) require ( @@ -18,5 +18,6 @@ require ( github.com/kr/text v0.2.0 // indirect github.com/mattn/go-sqlite3 v1.14.17 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + golang.org/x/text v0.22.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 61b9298..a2c58f7 100644 --- a/go.sum +++ b/go.sum @@ -27,6 +27,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/survivorbat/go-tsyncmap v0.0.0 h1:XTc1+uXyuw//1Hhpg4IxW6tEe3Tvd2d5vM/6IPqmkeg= github.com/survivorbat/go-tsyncmap v0.0.0/go.mod h1:zKe2CuXEo+c1d9DVT5L7AG2jPTdWi7QQN/Gk+26Vecg= +golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= +golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= @@ -34,5 +36,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/sqlite v1.5.2 h1:TpQ+/dqCY4uCigCFyrfnrJnrW9zjpelWVoEVNy5qJkc= gorm.io/driver/sqlite v1.5.2/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4= -gorm.io/gorm v1.25.6 h1:V92+vVda1wEISSOMtodHVRcUIOPYa2tgQtyF+DfFx+A= -gorm.io/gorm v1.25.6/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= diff --git a/plugin.go b/plugin.go index 448502f..e306aa8 100644 --- a/plugin.go +++ b/plugin.go @@ -41,7 +41,8 @@ func createDeepFilterRecursively(exprs []clause.Expression, db *gorm.DB) { switch cond := cond.(type) { case clause.AndConditions: createDeepFilterRecursively(exprs[index].(clause.AndConditions).Exprs, db) - + case clause.OrConditions: + createDeepFilterRecursively(exprs[index].(clause.OrConditions).Exprs, db) case clause.Eq: switch value := cond.Value.(type) { case map[string]any: diff --git a/plugin_test.go b/plugin_test.go index b574f0c..0eedd3c 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -1,11 +1,12 @@ package deepgorm import ( + "testing" + "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) { @@ -227,3 +228,94 @@ func TestDeepGorm_Initialize_TriggersFilteringCorrectly(t *testing.T) { }) } } + +func TestDeepGorm_Initialize_TriggersFilteringCorrectlyWithOrQuery(t *testing.T) { + t.Parallel() + filter1 := map[string]any{ + "object_bs": map[string]any{ + "name": "def", + }, + } + filter2 := map[string]any{ + "object_bs": map[string]any{ + "name": "abc", + }, + } + 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: "cba", + }, + }, + }, + } + 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"), + }, + }, + }, + { + ID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"), + Name: "nope", + ObjectBs: []ObjectB{ + { + ID: uuid.MustParse("83aaf47d-a167-4a49-8b7c-3516ced56e8a"), + Name: "abc", + ObjectAID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"), + }, + }, + }, + } + + db := gormtestutil.NewMemoryDatabase(t, gormtestutil.WithName(t.Name())) + _ = db.AutoMigrate(&ObjectA{}, &ObjectB{}) + plugin := New() + + if err := db.CreateInBatches(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(filter1).Or(filter2).Preload(clause.Associations).Find(&actual).Error + assert.Nil(t, err) + + assert.Equal(t, expected, actual) +}