check_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package schema_test
  2. import (
  3. "reflect"
  4. "sync"
  5. "testing"
  6. "github.com/jinzhu/gorm/schema"
  7. )
  8. type UserCheck struct {
  9. Name string `gorm:"check:name_checker,name <> 'jinzhu'"`
  10. Name2 string `gorm:"check:name <> 'jinzhu'"`
  11. Name3 string `gorm:"check:,name <> 'jinzhu'"`
  12. }
  13. func TestParseCheck(t *testing.T) {
  14. user, _, err := schema.Parse(&UserCheck{}, &sync.Map{}, schema.NamingStrategy{})
  15. if err != nil {
  16. t.Fatalf("failed to parse user check, got error %v", err)
  17. }
  18. results := map[string]schema.Check{
  19. "name_checker": {
  20. Name: "name_checker",
  21. Constraint: "name <> 'jinzhu'",
  22. },
  23. "chk_user_checks_name2": {
  24. Name: "chk_user_checks_name2",
  25. Constraint: "name <> 'jinzhu'",
  26. },
  27. "chk_user_checks_name3": {
  28. Name: "chk_user_checks_name3",
  29. Constraint: "name <> 'jinzhu'",
  30. },
  31. }
  32. checks := user.ParseCheckConstraints()
  33. for k, result := range results {
  34. v, ok := checks[k]
  35. if !ok {
  36. t.Errorf("Failed to found check %v from parsed checks %+v", k, checks)
  37. }
  38. for _, name := range []string{"Name", "Constraint"} {
  39. if reflect.ValueOf(result).FieldByName(name).Interface() != reflect.ValueOf(v).FieldByName(name).Interface() {
  40. t.Errorf(
  41. "check %v %v should equal, expects %v, got %v",
  42. k, name, reflect.ValueOf(result).FieldByName(name).Interface(), reflect.ValueOf(v).FieldByName(name).Interface(),
  43. )
  44. }
  45. }
  46. }
  47. }