2015-09-11 02:35:57 +03:00
|
|
|
package gorm_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-11-03 17:14:39 +03:00
|
|
|
"github.com/gofrs/uuid"
|
2015-09-11 02:35:57 +03:00
|
|
|
"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
|
2016-03-07 07:15:15 +03:00
|
|
|
var scope = DB.NewScope(&field)
|
|
|
|
if field, ok := scope.FieldByName("Children"); !ok || field.Relationship == nil {
|
2015-09-11 02:35:57 +03:00
|
|
|
t.Errorf("Should calculate fields correctly for the first time")
|
|
|
|
}
|
2016-01-08 04:02:01 +03:00
|
|
|
|
2016-03-07 07:15:15 +03:00
|
|
|
if field, ok := scope.FieldByName("Category"); !ok || field.Relationship == nil {
|
|
|
|
t.Errorf("Should calculate fields correctly for the first time")
|
|
|
|
}
|
|
|
|
|
|
|
|
if field, ok := scope.FieldByName("embedded_name"); !ok {
|
2016-01-08 04:02:01 +03:00
|
|
|
t.Errorf("should find embedded field")
|
2018-09-10 02:11:00 +03:00
|
|
|
} else if _, ok := field.TagSettingsGet("NOT NULL"); !ok {
|
2016-01-08 04:02:01 +03:00
|
|
|
t.Errorf("should find embedded field's tag settings")
|
|
|
|
}
|
2015-09-11 02:35:57 +03:00
|
|
|
}
|
2018-11-03 17:14:39 +03:00
|
|
|
|
|
|
|
func TestFieldSet(t *testing.T) {
|
|
|
|
type TestFieldSetNullUUID struct {
|
|
|
|
NullUUID uuid.NullUUID
|
|
|
|
}
|
|
|
|
scope := DB.NewScope(&TestFieldSetNullUUID{})
|
|
|
|
field := scope.Fields()[0]
|
|
|
|
err := field.Set(uuid.FromStringOrNil("3034d44a-da03-11e8-b366-4a00070b9f00"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if id, ok := field.Field.Addr().Interface().(*uuid.NullUUID); !ok {
|
|
|
|
t.Fatal()
|
|
|
|
} else if !id.Valid || id.UUID.String() != "3034d44a-da03-11e8-b366-4a00070b9f00" {
|
|
|
|
t.Fatal(id)
|
|
|
|
}
|
|
|
|
}
|