From 2242ac6c0ea490f7fa7c60c61126be0fdee0d72f Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 9 Sep 2020 10:31:48 +0800 Subject: [PATCH] Fix tests & refactor for PR #3429 --- clause/expression.go | 11 ++++++----- tests/go.mod | 4 ++++ tests/query_test.go | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/clause/expression.go b/clause/expression.go index 55599571..dde236d3 100644 --- a/clause/expression.go +++ b/clause/expression.go @@ -39,12 +39,13 @@ func (expr Expr) Build(builder Builder) { case reflect.Slice, reflect.Array: if rv.Len() == 0 { builder.AddVar(builder, nil) - } - for i := 0; i < rv.Len(); i++ { - if i > 0 { - builder.WriteByte(',') + } else { + for i := 0; i < rv.Len(); i++ { + if i > 0 { + builder.WriteByte(',') + } + builder.AddVar(builder, rv.Index(i).Interface()) } - builder.AddVar(builder, rv.Index(i).Interface()) } default: builder.AddVar(builder, expr.Vars[idx]) diff --git a/tests/go.mod b/tests/go.mod index 76db6764..4ddb0b69 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -6,6 +6,10 @@ require ( github.com/google/uuid v1.1.1 github.com/jinzhu/now v1.1.1 github.com/lib/pq v1.6.0 + gorm.io/driver/mysql v1.0.1 + gorm.io/driver/postgres v1.0.0 + gorm.io/driver/sqlite v1.1.1 + gorm.io/driver/sqlserver v1.0.3 gorm.io/gorm v1.9.19 ) diff --git a/tests/query_test.go b/tests/query_test.go index e695e825..14150038 100644 --- a/tests/query_test.go +++ b/tests/query_test.go @@ -204,7 +204,7 @@ func TestFind(t *testing.T) { }) var models []User - if err := DB.Where("name in ?", []string{"find"}).Find(&models).Error; err != nil || len(models) != 3 { + if err := DB.Where("name in (?)", []string{"find"}).Find(&models).Error; err != nil || len(models) != 3 { t.Errorf("errors happened when query find with in clause: %v, length: %v", err, len(models)) } else { for idx, user := range users { @@ -215,7 +215,7 @@ func TestFind(t *testing.T) { } var none []User - if err := DB.Where("name in ?", []string{}).Find(&none).Error; err != nil || len(none) != 0 { + if err := DB.Where("name in (?)", []string{}).Find(&none).Error; err != nil || len(none) != 0 { t.Errorf("errors happened when query find with in clause and zero length parameter: %v, length: %v", err, len(none)) } }