fixing issues that broke tests

This commit is contained in:
Rob Rodriguez 2018-12-10 17:43:50 -08:00
parent 25674faa7d
commit 2cc2a1555d
4 changed files with 23 additions and 24 deletions

View File

@ -75,18 +75,16 @@ func updateCallback(scope *Scope) {
} else { } else {
for _, field := range scope.Fields() { for _, field := range scope.Fields() {
if scope.changeableField(field) { if scope.changeableField(field) {
if !field.IsPrimaryKey { if !field.IsPrimaryKey && field.IsNormal {
if field.IsNormal { if !field.IsForeignKey || !field.IsBlank || !field.HasDefaultValue {
if !field.IsForeignKey || !field.IsBlank || !field.HasDefaultValue { sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) }
} } else if field.UseEncoder {
} else if field.UseEncoder { if enc, ok := scope.Value.(Encoder); ok {
if enc, ok := scope.Value.(Encoder); ok { if val, err := enc.EncodeField(field.DBName); err == nil {
if val, err := enc.EncodeField(field.DBName); err == nil { sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(val)))
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(val))) } else {
} else { scope.Err(err)
scope.Err(err)
}
} }
} }
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" { } else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {

View File

@ -4,6 +4,8 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"testing" "testing"
"github.com/kr/pretty"
) )
type ( type (
@ -78,8 +80,6 @@ func (m *WidgetUser) DecodeField(column string, value interface{}) error {
} }
func TestEncoder(t *testing.T) { func TestEncoder(t *testing.T) {
DB.AutoMigrate(&WidgetUser{})
user := &WidgetUser{ user := &WidgetUser{
User: User{ User: User{
Id: 1, Id: 1,
@ -110,4 +110,5 @@ func TestEncoder(t *testing.T) {
t.Errorf("user widget is not valid") t.Errorf("user widget is not valid")
} }
} }
pretty.Log(user1.Widget)
} }

View File

@ -292,7 +292,7 @@ func runMigration() {
DB.Exec(fmt.Sprintf("drop table %v;", table)) DB.Exec(fmt.Sprintf("drop table %v;", table))
} }
values := []interface{}{&Short{}, &ReallyLongThingThatReferencesShort{}, &ReallyLongTableNameToTestMySQLNameLengthLimit{}, &NotSoLongTableName{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Hamster{}, &Toy{}, &ElementWithIgnoredField{}, &Place{}} values := []interface{}{&Short{}, &ReallyLongThingThatReferencesShort{}, &ReallyLongTableNameToTestMySQLNameLengthLimit{}, &NotSoLongTableName{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Hamster{}, &Toy{}, &ElementWithIgnoredField{}, &Place{}, &WidgetUser{}}
for _, value := range values { for _, value := range values {
DB.DropTable(value) DB.DropTable(value)
} }

View File

@ -606,16 +606,16 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
} }
} }
} }
// Even it is ignored, also possible to decode db value into the field
if value, ok := field.TagSettingsGet("COLUMN"); ok {
field.DBName = value
} else {
field.DBName = ToColumnName(fieldStruct.Name)
}
modelStruct.StructFields = append(modelStruct.StructFields, field)
} }
// Even it is ignored, also possible to decode db value into the field
if value, ok := field.TagSettingsGet("COLUMN"); ok {
field.DBName = value
} else {
field.DBName = ToColumnName(fieldStruct.Name)
}
modelStruct.StructFields = append(modelStruct.StructFields, field)
} }
if len(modelStruct.PrimaryFields) == 0 { if len(modelStruct.PrimaryFields) == 0 {