forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'go-gorm:master' into master
- Loading branch information
Showing
21 changed files
with
328 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package callbacks | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"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{}) | ||
if err != nil { | ||
t.Errorf("parse schema error: %v, is not expected", err) | ||
return | ||
} | ||
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) | ||
expected := 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}, | ||
}, | ||
} | ||
if !reflect.DeepEqual(expected, values) { | ||
t.Errorf("expected: %v got %v", expected, values) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.