From 157866d39bed1616bc502d4db0f2e0251bc15a6c Mon Sep 17 00:00:00 2001 From: lujinghao Date: Fri, 1 Mar 2024 10:39:39 +0800 Subject: [PATCH] chore: add ConvertToCreateValues ut for Slice case --- callbacks/create_test.go | 66 ++++++++++++++++++++++++++++++++++++++++ go.mod | 7 +++++ go.sum | 10 ++++++ 3 files changed, 83 insertions(+) create mode 100644 callbacks/create_test.go diff --git a/callbacks/create_test.go b/callbacks/create_test.go new file mode 100644 index 00000000..59dc044b --- /dev/null +++ b/callbacks/create_test.go @@ -0,0 +1,66 @@ +package callbacks + +import ( + "reflect" + "sync" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "gorm.io/gorm" + "gorm.io/gorm/clause" + "gorm.io/gorm/schema" +) + +var schemaCache = &sync.Map{} + +func TestConvertToCreateValues_DestType_Slice(t *testing.T) { + type user struct { + ID int `gorm:"primaryKey"` + Name string + Email string `gorm:"default:(-)"` + Age int `gorm:"default:(-)"` + } + + s, err := schema.Parse(&user{}, schemaCache, schema.NamingStrategy{}) + assert.NoError(t, err) + dest := []*user{ + { + ID: 1, + Name: "alice", + Email: "email", + Age: 18, + }, + { + ID: 2, + Name: "bob", + Email: "email", + Age: 19, + }, + } + stmt := &gorm.Statement{ + DB: &gorm.DB{ + Config: &gorm.Config{ + NowFunc: func() time.Time { return time.Time{} }, + }, + Statement: &gorm.Statement{ + Settings: sync.Map{}, + Schema: s, + }, + }, + ReflectValue: reflect.ValueOf(dest), + Dest: dest, + } + + stmt.Schema = s + + values := ConvertToCreateValues(stmt) + assert.EqualValues(t, clause.Values{ + // column has value + defaultValue column has value (which should have a stable order) + Columns: []clause.Column{{Name: "name"}, {Name: "email"}, {Name: "age"}, {Name: "id"}}, + Values: [][]interface{}{ + {"alice", "email", 18, 1}, + {"bob", "email", 19, 2}, + }, + }, values) +} diff --git a/go.mod b/go.mod index deb61b74..d2da2295 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,11 @@ go 1.18 require ( github.com/jinzhu/inflection v1.0.0 github.com/jinzhu/now v1.1.5 + github.com/stretchr/testify v1.8.4 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index bd6104c9..51818555 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,14 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=