2020-02-22 14:41:01 +03:00
|
|
|
package schema_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UserCheck struct {
|
|
|
|
Name string `gorm:"check:name_checker,name <> 'jinzhu'"`
|
|
|
|
Name2 string `gorm:"check:name <> 'jinzhu'"`
|
|
|
|
Name3 string `gorm:"check:,name <> 'jinzhu'"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseCheck(t *testing.T) {
|
2020-02-24 03:51:35 +03:00
|
|
|
user, err := schema.Parse(&UserCheck{}, &sync.Map{}, schema.NamingStrategy{})
|
2020-02-22 14:41:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse user check, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
results := map[string]schema.Check{
|
|
|
|
"name_checker": {
|
|
|
|
Name: "name_checker",
|
|
|
|
Constraint: "name <> 'jinzhu'",
|
|
|
|
},
|
|
|
|
"chk_user_checks_name2": {
|
|
|
|
Name: "chk_user_checks_name2",
|
|
|
|
Constraint: "name <> 'jinzhu'",
|
|
|
|
},
|
|
|
|
"chk_user_checks_name3": {
|
|
|
|
Name: "chk_user_checks_name3",
|
|
|
|
Constraint: "name <> 'jinzhu'",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
checks := user.ParseCheckConstraints()
|
|
|
|
|
|
|
|
for k, result := range results {
|
|
|
|
v, ok := checks[k]
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Failed to found check %v from parsed checks %+v", k, checks)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range []string{"Name", "Constraint"} {
|
|
|
|
if reflect.ValueOf(result).FieldByName(name).Interface() != reflect.ValueOf(v).FieldByName(name).Interface() {
|
|
|
|
t.Errorf(
|
|
|
|
"check %v %v should equal, expects %v, got %v",
|
|
|
|
k, name, reflect.ValueOf(result).FieldByName(name).Interface(), reflect.ValueOf(v).FieldByName(name).Interface(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|