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

Function checkSchemaRelation

schema/schema_helper_test.go:124–202  ·  view source on GitHub ↗
(t *testing.T, s *schema.Schema, relation Relation)

Source from the content-addressed store, hash-verified

122}
123
124func 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 }

Callers 3

checkStructRelationFunction · 0.85
checkUserSchemaFunction · 0.85
checkEmbeddedRelationsFunction · 0.85

Calls 1

checkSchemaFieldFunction · 0.85

Tested by

no test coverage detected