Debian Codegraph @

schema/schema_helper_test.go

raw
1 package schema_test
2
3 import (
4 "context"
5 "fmt"
6 "reflect"
7 "strings"
8 "testing"
9
10 "gorm.io/gorm/schema"
11 "gorm.io/gorm/utils/tests"
12 )
13
14 func checkSchema(t *testing.T, s *schema.Schema, v *schema.Schema, primaryFields []string) {
15 t.Run("CheckSchema/"+s.Name, func(t *testing.T) {
16 tests.AssertObjEqual(t, s, v, "Name", "Table")
17
18 for idx, field := range primaryFields {
19 var found bool
20 for _, f := range s.PrimaryFields {
21 if f.Name == field {
22 found = true
23 }
24 }
25
26 if idx == 0 {
27 if field != s.PrioritizedPrimaryField.Name {
28 t.Errorf("schema %v prioritized primary field should be %v, but got %v", s, field, s.PrioritizedPrimaryField.Name)
29 }
30 }
31
32 if !found {
33 t.Errorf("schema %v failed to found primary key: %v", s, field)
34 }
35 }
36 })
37 }
38
39 func 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
93 type Relation struct {
94 Name string
95 Type schema.RelationshipType
96 Schema string
97 FieldSchema string
98 Polymorphic Polymorphic
99 JoinTable JoinTable
100 References []Reference
101 }
102
103 type Polymorphic struct {
104 ID string
105 Type string
106 Value string
107 }
108
109 type JoinTable struct {
110 Name string
111 Table string
112 Fields []schema.Field
113 }
114
115 type Reference struct {
116 PrimaryKey string
117 PrimarySchema string
118 ForeignKey string
119 ForeignSchema string
120 PrimaryValue string
121 OwnPrimaryKey bool
122 }
123
124 func checkSchemaRelation(t *testing.T, s *schema.Schema, relation Relation) {
125 t.Run("CheckRelation/"+relation.Name, func(t *testing.T) {
126 if r, ok := s.Relationships.Relations[relation.Name]; ok {
127 if r.Name != relation.Name {
128 t.Errorf("schema %v relation name expects %v, but got %v", s, r.Name, relation.Name)
129 }
130
131 if r.Type != relation.Type {
132 t.Errorf("schema %v relation name expects %v, but got %v", s, r.Type, relation.Type)
133 }
134
135 if r.Schema.Name != relation.Schema {
136 t.Errorf("schema %v relation's schema expects %v, but got %v", s, relation.Schema, r.Schema.Name)
137 }
138
139 if r.FieldSchema.Name != relation.FieldSchema {
140 t.Errorf("schema %v field relation's schema expects %v, but got %v", s, relation.FieldSchema, r.FieldSchema.Name)
141 }
142
143 if r.Polymorphic != nil {
144 if r.Polymorphic.PolymorphicID.Name != relation.Polymorphic.ID {
145 t.Errorf("schema %v relation's polymorphic id field expects %v, but got %v", s, relation.Polymorphic.ID, r.Polymorphic.PolymorphicID.Name)
146 }
147
148 if r.Polymorphic.PolymorphicType.Name != relation.Polymorphic.Type {
149 t.Errorf("schema %v relation's polymorphic type field expects %v, but got %v", s, relation.Polymorphic.Type, r.Polymorphic.PolymorphicType.Name)
150 }
151
152 if r.Polymorphic.Value != relation.Polymorphic.Value {
153 t.Errorf("schema %v relation's polymorphic value expects %v, but got %v", s, relation.Polymorphic.Value, r.Polymorphic.Value)
154 }
155 }
156
157 if r.JoinTable != nil {
158 if r.JoinTable.Name != relation.JoinTable.Name {
159 t.Errorf("schema %v relation's join table name expects %v, but got %v", s, relation.JoinTable.Name, r.JoinTable.Name)
160 }
161
162 if r.JoinTable.Table != relation.JoinTable.Table {
163 t.Errorf("schema %v relation's join table tablename expects %v, but got %v", s, relation.JoinTable.Table, r.JoinTable.Table)
164 }
165
166 for i := range relation.JoinTable.Fields {
167 checkSchemaField(t, r.JoinTable, &relation.JoinTable.Fields[i], nil)
168 }
169 }
170
171 if len(relation.References) != len(r.References) {
172 t.Errorf("schema %v relation's reference's count doesn't match, expects %v, but got %v", s, len(relation.References), len(r.References))
173 }
174
175 for _, ref := range relation.References {
176 var found bool
177 for _, rf := range r.References {
178 if (rf.PrimaryKey == nil || (rf.PrimaryKey.Name == ref.PrimaryKey && rf.PrimaryKey.Schema.Name == ref.PrimarySchema)) && (rf.PrimaryValue == ref.PrimaryValue) && (rf.ForeignKey.Name == ref.ForeignKey && rf.ForeignKey.Schema.Name == ref.ForeignSchema) && (rf.OwnPrimaryKey == ref.OwnPrimaryKey) {
179 found = true
180 }
181 }
182
183 if !found {
184 var refs []string
185 for _, rf := range r.References {
186 var primaryKey, primaryKeySchema string
187 if rf.PrimaryKey != nil {
188 primaryKey, primaryKeySchema = rf.PrimaryKey.Name, rf.PrimaryKey.Schema.Name
189 }
190 refs = append(refs, fmt.Sprintf(
191 "{PrimaryKey: %v PrimaryKeySchame: %v ForeignKey: %v ForeignKeySchema: %v PrimaryValue: %v OwnPrimaryKey: %v}",
192 primaryKey, primaryKeySchema, rf.ForeignKey.Name, rf.ForeignKey.Schema.Name, rf.PrimaryValue, rf.OwnPrimaryKey,
193 ))
194 }
195 t.Errorf("schema %v relation %v failed to found reference %+v, has %v", s, relation.Name, ref, strings.Join(refs, ", "))
196 }
197 }
198 } else {
199 t.Errorf("schema %v failed to find relations by name %v", s, relation.Name)
200 }
201 })
202 }
203
204 type EmbeddedRelations struct {
205 Relations map[string]Relation
206 EmbeddedRelations map[string]EmbeddedRelations
207 }
208
209 func checkEmbeddedRelations(t *testing.T, actual map[string]*schema.Relationships, expected map[string]EmbeddedRelations) {
210 for name, relations := range actual {
211 rs := expected[name]
212 t.Run("CheckEmbeddedRelations/"+name, func(t *testing.T) {
213 if len(relations.Relations) != len(rs.Relations) {
214 t.Errorf("schema relations count don't match, expects %d, got %d", len(rs.Relations), len(relations.Relations))
215 }
216 if len(relations.EmbeddedRelations) != len(rs.EmbeddedRelations) {
217 t.Errorf("schema embedded relations count don't match, expects %d, got %d", len(rs.EmbeddedRelations), len(relations.EmbeddedRelations))
218 }
219 for n, rel := range relations.Relations {
220 if r, ok := rs.Relations[n]; !ok {
221 t.Errorf("failed to find relation by name %s", n)
222 } else {
223 checkSchemaRelation(t, &schema.Schema{
224 Relationships: schema.Relationships{
225 Relations: map[string]*schema.Relationship{n: rel},
226 },
227 }, r)
228 }
229 }
230 checkEmbeddedRelations(t, relations.EmbeddedRelations, rs.EmbeddedRelations)
231 })
232 }
233 }
234
235 func checkField(t *testing.T, s *schema.Schema, value reflect.Value, values map[string]interface{}) {
236 for k, v := range values {
237 t.Run("CheckField/"+k, func(t *testing.T) {
238 fv, _ := s.FieldsByDBName[k].ValueOf(context.Background(), value)
239 tests.AssertEqual(t, v, fv)
240 })
241 }
242 }