|
@@ -1,7 +1,6 @@
|
|
|
package schema_test
|
|
|
|
|
|
import (
|
|
|
- "reflect"
|
|
|
"sync"
|
|
|
"testing"
|
|
|
|
|
@@ -11,68 +10,40 @@ import (
|
|
|
|
|
|
func TestParseSchema(t *testing.T) {
|
|
|
cacheMap := sync.Map{}
|
|
|
- user, err := schema.Parse(&tests.User{}, &cacheMap, schema.NamingStrategy{})
|
|
|
|
|
|
+ user, err := schema.Parse(&tests.User{}, &cacheMap, schema.NamingStrategy{})
|
|
|
if err != nil {
|
|
|
t.Fatalf("failed to parse user, got error %v", err)
|
|
|
}
|
|
|
|
|
|
- checkSchemaFields(t, user)
|
|
|
-}
|
|
|
+ // check schema
|
|
|
+ checkSchema(t, user, schema.Schema{Name: "User", Table: "users"}, []string{"ID"})
|
|
|
|
|
|
-func checkSchemaFields(t *testing.T, s *schema.Schema) {
|
|
|
+ // check fields
|
|
|
fields := []schema.Field{
|
|
|
- schema.Field{
|
|
|
- Name: "ID", DBName: "id", BindNames: []string{"Model", "ID"}, DataType: schema.Uint,
|
|
|
- PrimaryKey: true, Tag: `gorm:"primarykey"`, TagSettings: map[string]string{"PRIMARYKEY": "PRIMARYKEY"},
|
|
|
- },
|
|
|
- schema.Field{Name: "CreatedAt", DBName: "created_at", BindNames: []string{"Model", "CreatedAt"}, DataType: schema.Time},
|
|
|
- schema.Field{Name: "UpdatedAt", DBName: "updated_at", BindNames: []string{"Model", "UpdatedAt"}, DataType: schema.Time},
|
|
|
- schema.Field{Name: "DeletedAt", DBName: "deleted_at", BindNames: []string{"Model", "DeletedAt"}, Tag: `gorm:"index"`, DataType: schema.Time},
|
|
|
- schema.Field{Name: "Name", DBName: "name", BindNames: []string{"Name"}, DataType: schema.String},
|
|
|
- schema.Field{Name: "Age", DBName: "age", BindNames: []string{"Age"}, DataType: schema.Uint},
|
|
|
- schema.Field{Name: "Birthday", DBName: "birthday", BindNames: []string{"Birthday"}, DataType: schema.Time},
|
|
|
- schema.Field{Name: "CompanyID", DBName: "company_id", BindNames: []string{"CompanyID"}, DataType: schema.Int},
|
|
|
- schema.Field{Name: "ManagerID", DBName: "manager_id", BindNames: []string{"ManagerID"}, DataType: schema.Uint},
|
|
|
+ {Name: "ID", DBName: "id", BindNames: []string{"Model", "ID"}, DataType: schema.Uint, PrimaryKey: true, Tag: `gorm:"primarykey"`, TagSettings: map[string]string{"PRIMARYKEY": "PRIMARYKEY"}},
|
|
|
+ {Name: "CreatedAt", DBName: "created_at", BindNames: []string{"Model", "CreatedAt"}, DataType: schema.Time},
|
|
|
+ {Name: "UpdatedAt", DBName: "updated_at", BindNames: []string{"Model", "UpdatedAt"}, DataType: schema.Time},
|
|
|
+ {Name: "DeletedAt", DBName: "deleted_at", BindNames: []string{"Model", "DeletedAt"}, Tag: `gorm:"index"`, DataType: schema.Time},
|
|
|
+ {Name: "Name", DBName: "name", BindNames: []string{"Name"}, DataType: schema.String},
|
|
|
+ {Name: "Age", DBName: "age", BindNames: []string{"Age"}, DataType: schema.Uint},
|
|
|
+ {Name: "Birthday", DBName: "birthday", BindNames: []string{"Birthday"}, DataType: schema.Time},
|
|
|
+ {Name: "CompanyID", DBName: "company_id", BindNames: []string{"CompanyID"}, DataType: schema.Int},
|
|
|
+ {Name: "ManagerID", DBName: "manager_id", BindNames: []string{"ManagerID"}, DataType: schema.Uint},
|
|
|
}
|
|
|
|
|
|
for _, f := range fields {
|
|
|
- f.Creatable = true
|
|
|
- f.Updatable = true
|
|
|
- if f.TagSettings == nil {
|
|
|
- if f.Tag != "" {
|
|
|
- f.TagSettings = schema.ParseTagSetting(f.Tag)
|
|
|
- } else {
|
|
|
- f.TagSettings = map[string]string{}
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if foundField, ok := s.FieldsByName[f.Name]; !ok {
|
|
|
- t.Errorf("schema %v failed to look up field with name %v", s, f.Name)
|
|
|
- } else {
|
|
|
- checkSchemaField(t, foundField, f)
|
|
|
-
|
|
|
- if field, ok := s.FieldsByDBName[f.DBName]; !ok || foundField != field {
|
|
|
- t.Errorf("schema %v failed to look up field with dbname %v", s, f.DBName)
|
|
|
- }
|
|
|
-
|
|
|
- for _, name := range []string{f.DBName, f.Name} {
|
|
|
- if field := s.LookUpField(name); field == nil || foundField != field {
|
|
|
- t.Errorf("schema %v failed to look up field with dbname %v", s, f.DBName)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ checkSchemaField(t, user, &f, func(f *schema.Field) {
|
|
|
+ f.Creatable = true
|
|
|
+ f.Updatable = true
|
|
|
+ })
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-func checkSchemaField(t *testing.T, parsedField *schema.Field, field schema.Field) {
|
|
|
- equalFieldNames := []string{"Name", "DBName", "BindNames", "DataType", "DBDataType", "PrimaryKey", "AutoIncrement", "Creatable", "Updatable", "HasDefaultValue", "DefaultValue", "NotNull", "Unique", "Comment", "Size", "Precision", "Tag", "TagSettings"}
|
|
|
-
|
|
|
- for _, name := range equalFieldNames {
|
|
|
- got := reflect.ValueOf(parsedField).Elem().FieldByName(name).Interface()
|
|
|
- expects := reflect.ValueOf(field).FieldByName(name).Interface()
|
|
|
- if !reflect.DeepEqual(got, expects) {
|
|
|
- t.Errorf("%v is not equal, expects: %v, got %v", name, expects, got)
|
|
|
- }
|
|
|
+ // check relations
|
|
|
+ relations := []Relation{
|
|
|
+ {Name: "Pets", Type: schema.HasMany, Schema: "User", FieldSchema: "Pet", References: []Reference{{"ID", "User", "UserID", "Pet", true}}},
|
|
|
+ }
|
|
|
+ for _, relation := range relations {
|
|
|
+ checkSchemaRelation(t, user, relation)
|
|
|
}
|
|
|
}
|