MCPcopy
hub / github.com/go-gorm/gorm / checkSchemaField

Function checkSchemaField

schema/schema_helper_test.go:39–91  ·  view source on GitHub ↗
(t *testing.T, s *schema.Schema, f *schema.Field, fc func(*schema.Field))

Source from the content-addressed store, hash-verified

37}
38
39func checkSchemaField(t *testing.T, s *schema.Schema, f *schema.Field, fc func(*schema.Field)) {
40 t.Run("CheckField/"+f.Name, func(t *testing.T) {
41 if fc != nil {
42 fc(f)
43 }
44
45 if f.TagSettings == nil {
46 if f.Tag != "" {
47 f.TagSettings = schema.ParseTagSetting(f.Tag.Get("gorm"), ";")
48 } else {
49 f.TagSettings = map[string]string{}
50 }
51 }
52
53 parsedField, ok := s.FieldsByDBName[f.DBName]
54 if !ok {
55 parsedField, ok = s.FieldsByName[f.Name]
56 }
57
58 if !ok {
59 t.Errorf("schema %v failed to look up field with name %v", s, f.Name)
60 } else {
61 tests.AssertObjEqual(t, parsedField, f, "Name", "DBName", "BindNames", "DataType", "PrimaryKey", "AutoIncrement", "Creatable", "Updatable", "Readable", "HasDefaultValue", "DefaultValue", "NotNull", "Unique", "Comment", "Size", "Precision", "TagSettings")
62
63 if f.DBName != "" {
64 if field, ok := s.FieldsByDBName[f.DBName]; !ok || parsedField != field {
65 t.Errorf("schema %v failed to look up field with dbname %v", s, f.DBName)
66 }
67 }
68
69 for _, name := range []string{f.DBName, f.Name} {
70 if name != "" {
71 if field := s.LookUpField(name); field == nil || (field.Name != name && field.DBName != name) {
72 t.Errorf("schema %v failed to look up field with dbname %v", s, f.DBName)
73 }
74 }
75 }
76
77 if f.PrimaryKey {
78 var found bool
79 for _, primaryField := range s.PrimaryFields {
80 if primaryField == parsedField {
81 found = true
82 }
83 }
84
85 if !found {
86 t.Errorf("schema %v doesn't include field %v", s, f.Name)
87 }
88 }
89 }
90 })
91}
92
93type Relation struct {
94 Name string

Calls 4

ParseTagSettingFunction · 0.92
AssertObjEqualFunction · 0.92
LookUpFieldMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected