2015-09-11 02:35:57 +03:00
|
|
|
package gorm_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CalculateField struct {
|
|
|
|
gorm.Model
|
|
|
|
Name string
|
|
|
|
Children []CalculateFieldChild
|
|
|
|
Category CalculateFieldCategory
|
2016-01-08 04:02:01 +03:00
|
|
|
EmbeddedField
|
|
|
|
}
|
|
|
|
|
|
|
|
type EmbeddedField struct {
|
|
|
|
EmbeddedName string `sql:"NOT NULL;DEFAULT:'hello'"`
|
2015-09-11 02:35:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type CalculateFieldChild struct {
|
|
|
|
gorm.Model
|
|
|
|
CalculateFieldID uint
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type CalculateFieldCategory struct {
|
|
|
|
gorm.Model
|
|
|
|
CalculateFieldID uint
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCalculateField(t *testing.T) {
|
|
|
|
var field CalculateField
|
|
|
|
fields := DB.NewScope(&field).Fields()
|
|
|
|
if fields["children"].Relationship == nil || fields["category"].Relationship == nil {
|
|
|
|
t.Errorf("Should calculate fields correctly for the first time")
|
|
|
|
}
|
2016-01-08 04:02:01 +03:00
|
|
|
|
|
|
|
if field, ok := fields["embedded_name"]; !ok {
|
|
|
|
t.Errorf("should find embedded field")
|
|
|
|
} else if _, ok := field.TagSettings["NOT NULL"]; !ok {
|
|
|
|
t.Errorf("should find embedded field's tag settings")
|
|
|
|
}
|
2015-09-11 02:35:57 +03:00
|
|
|
}
|