schema_helper_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package schema_test
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. "testing"
  7. "github.com/jinzhu/gorm/schema"
  8. "github.com/jinzhu/gorm/tests"
  9. )
  10. func checkSchema(t *testing.T, s *schema.Schema, v schema.Schema, primaryFields []string) {
  11. t.Run("CheckSchema/"+s.Name, func(t *testing.T) {
  12. tests.AssertEqual(t, s, v, "Name", "Table")
  13. for idx, field := range primaryFields {
  14. var found bool
  15. for _, f := range s.PrimaryFields {
  16. if f.Name == field {
  17. found = true
  18. }
  19. }
  20. if idx == 0 {
  21. if field != s.PrioritizedPrimaryField.Name {
  22. t.Errorf("schema %v prioritized primary field should be %v, but got %v", s, field, s.PrioritizedPrimaryField.Name)
  23. }
  24. }
  25. if !found {
  26. t.Errorf("schema %v failed to found priamry key: %v", s, field)
  27. }
  28. }
  29. })
  30. }
  31. func checkSchemaField(t *testing.T, s *schema.Schema, f *schema.Field, fc func(*schema.Field)) {
  32. t.Run("CheckField/"+f.Name, func(t *testing.T) {
  33. if fc != nil {
  34. fc(f)
  35. }
  36. if f.TagSettings == nil {
  37. if f.Tag != "" {
  38. f.TagSettings = schema.ParseTagSetting(f.Tag)
  39. } else {
  40. f.TagSettings = map[string]string{}
  41. }
  42. }
  43. if parsedField, ok := s.FieldsByName[f.Name]; !ok {
  44. t.Errorf("schema %v failed to look up field with name %v", s, f.Name)
  45. } else {
  46. tests.AssertEqual(t, parsedField, f, "Name", "DBName", "BindNames", "DataType", "DBDataType", "PrimaryKey", "AutoIncrement", "Creatable", "Updatable", "HasDefaultValue", "DefaultValue", "NotNull", "Unique", "Comment", "Size", "Precision", "Tag", "TagSettings")
  47. if field, ok := s.FieldsByDBName[f.DBName]; !ok || parsedField != field {
  48. t.Errorf("schema %v failed to look up field with dbname %v", s, f.DBName)
  49. }
  50. for _, name := range []string{f.DBName, f.Name} {
  51. if field := s.LookUpField(name); field == nil || parsedField != field {
  52. t.Errorf("schema %v failed to look up field with dbname %v", s, f.DBName)
  53. }
  54. }
  55. if f.PrimaryKey {
  56. var found bool
  57. for _, primaryField := range s.PrimaryFields {
  58. if primaryField == parsedField {
  59. found = true
  60. }
  61. }
  62. if !found {
  63. t.Errorf("schema %v doesn't include field %v", s, f.Name)
  64. }
  65. }
  66. }
  67. })
  68. }
  69. type Relation struct {
  70. Name string
  71. Type schema.RelationshipType
  72. Schema string
  73. FieldSchema string
  74. Polymorphic Polymorphic
  75. JoinTable JoinTable
  76. References []Reference
  77. }
  78. type Polymorphic struct {
  79. ID string
  80. Type string
  81. Value string
  82. }
  83. type JoinTable struct {
  84. Name string
  85. Table string
  86. Fields []schema.Field
  87. }
  88. type Reference struct {
  89. PrimaryKey string
  90. PrimarySchema string
  91. ForeignKey string
  92. ForeignSchema string
  93. PrimaryValue string
  94. OwnPrimaryKey bool
  95. }
  96. func checkSchemaRelation(t *testing.T, s *schema.Schema, relation Relation) {
  97. t.Run("CheckRelation/"+relation.Name, func(t *testing.T) {
  98. if r, ok := s.Relationships.Relations[relation.Name]; ok {
  99. if r.Name != relation.Name {
  100. t.Errorf("schema %v relation name expects %v, but got %v", s, r.Name, relation.Name)
  101. }
  102. if r.Type != relation.Type {
  103. t.Errorf("schema %v relation name expects %v, but got %v", s, r.Type, relation.Type)
  104. }
  105. if r.Schema.Name != relation.Schema {
  106. t.Errorf("schema %v relation's schema expects %v, but got %v", s, relation.Schema, r.Schema.Name)
  107. }
  108. if r.FieldSchema.Name != relation.FieldSchema {
  109. t.Errorf("schema %v relation's schema expects %v, but got %v", s, relation.Schema, r.Schema.Name)
  110. }
  111. if r.Polymorphic != nil {
  112. if r.Polymorphic.PolymorphicID.Name != relation.Polymorphic.ID {
  113. t.Errorf("schema %v relation's polymorphic id field expects %v, but got %v", s, relation.Polymorphic.ID, r.Polymorphic.PolymorphicID.Name)
  114. }
  115. if r.Polymorphic.PolymorphicType.Name != relation.Polymorphic.Type {
  116. t.Errorf("schema %v relation's polymorphic type field expects %v, but got %v", s, relation.Polymorphic.Type, r.Polymorphic.PolymorphicType.Name)
  117. }
  118. if r.Polymorphic.Value != relation.Polymorphic.Value {
  119. t.Errorf("schema %v relation's polymorphic value expects %v, but got %v", s, relation.Polymorphic.Value, r.Polymorphic.Value)
  120. }
  121. }
  122. if r.JoinTable != nil {
  123. if r.JoinTable.Name != relation.JoinTable.Name {
  124. t.Errorf("schema %v relation's join table name expects %v, but got %v", s, relation.JoinTable.Name, r.JoinTable.Name)
  125. }
  126. if r.JoinTable.Table != relation.JoinTable.Table {
  127. t.Errorf("schema %v relation's join table tablename expects %v, but got %v", s, relation.JoinTable.Table, r.JoinTable.Table)
  128. }
  129. for _, f := range relation.JoinTable.Fields {
  130. checkSchemaField(t, r.JoinTable, &f, nil)
  131. }
  132. }
  133. if len(relation.References) != len(r.References) {
  134. t.Errorf("schema %v relation's reference's count doesn't match, expects %v, but got %v", s, len(relation.References), len(r.References))
  135. }
  136. for _, ref := range relation.References {
  137. var found bool
  138. for _, rf := range r.References {
  139. 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) {
  140. found = true
  141. }
  142. }
  143. if !found {
  144. var refs []string
  145. for _, rf := range r.References {
  146. var primaryKey, primaryKeySchema string
  147. if rf.PrimaryKey != nil {
  148. primaryKey, primaryKeySchema = rf.PrimaryKey.Name, rf.PrimaryKey.Schema.Name
  149. }
  150. refs = append(refs, fmt.Sprintf(
  151. "{PrimaryKey: %v PrimaryKeySchame: %v ForeignKey: %v ForeignKeySchema: %v PrimaryValue: %v OwnPrimaryKey: %v}",
  152. primaryKey, primaryKeySchema, rf.ForeignKey.Name, rf.ForeignKey.Schema.Name, rf.PrimaryValue, rf.OwnPrimaryKey,
  153. ))
  154. }
  155. t.Errorf("schema %v relation %v failed to found reference %+v, has %v", s, relation.Name, ref, strings.Join(refs, ", "))
  156. }
  157. }
  158. } else {
  159. t.Errorf("schema %v failed to find relations by name %v", s, relation.Name)
  160. }
  161. })
  162. }
  163. func checkField(t *testing.T, s *schema.Schema, value reflect.Value, values map[string]interface{}) {
  164. for k, v := range values {
  165. t.Run("CheckField/"+k, func(t *testing.T) {
  166. field := s.FieldsByDBName[k]
  167. fv := field.ValueOf(value)
  168. if reflect.ValueOf(fv).Kind() == reflect.Ptr {
  169. if reflect.ValueOf(v).Kind() == reflect.Ptr {
  170. if fv != v {
  171. t.Errorf("pointer expects: %p, but got %p", v, fv)
  172. }
  173. } else if fv == nil {
  174. if v != nil {
  175. t.Errorf("expects: %+v, but got nil", v)
  176. }
  177. } else if reflect.ValueOf(fv).Elem().Interface() != v {
  178. t.Errorf("expects: %+v, but got %+v", v, fv)
  179. }
  180. } else if reflect.ValueOf(v).Kind() == reflect.Ptr {
  181. if reflect.ValueOf(v).Elem().Interface() != fv {
  182. t.Errorf("expects: %+v, but got %+v", v, fv)
  183. }
  184. } else if reflect.ValueOf(v).Type().ConvertibleTo(field.FieldType) {
  185. if reflect.ValueOf(v).Convert(field.FieldType).Interface() != fv {
  186. t.Errorf("expects: %+v, but got %+v", v, fv)
  187. }
  188. }
  189. })
  190. }
  191. }