|
| 1 | +package boilingcore |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/volatiletech/sqlboiler/drivers" |
| 8 | +) |
| 9 | + |
| 10 | +func TestAliasesTables(t *testing.T) { |
| 11 | + t.Parallel() |
| 12 | + |
| 13 | + tables := []drivers.Table{ |
| 14 | + { |
| 15 | + Name: "videos", |
| 16 | + Columns: []drivers.Column{ |
| 17 | + {Name: "id"}, |
| 18 | + {Name: "name"}, |
| 19 | + }, |
| 20 | + }, |
| 21 | + } |
| 22 | + |
| 23 | + t.Run("Automatic", func(t *testing.T) { |
| 24 | + expect := TableAlias{ |
| 25 | + UpPlural: "Videos", |
| 26 | + UpSingular: "Video", |
| 27 | + DownPlural: "videos", |
| 28 | + DownSingular: "video", |
| 29 | + Columns: map[string]string{ |
| 30 | + "id": "ID", |
| 31 | + "name": "Name", |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + a := Aliases{} |
| 36 | + FillAliases(&a, tables) |
| 37 | + |
| 38 | + if got := a.Tables["videos"]; !reflect.DeepEqual(expect, got) { |
| 39 | + t.Errorf("it should fill in the blanks: %#v", got) |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + t.Run("UserOverride", func(t *testing.T) { |
| 44 | + expect := TableAlias{ |
| 45 | + UpPlural: "NotVideos", |
| 46 | + UpSingular: "NotVideo", |
| 47 | + DownPlural: "notVideos", |
| 48 | + DownSingular: "notVideo", |
| 49 | + Columns: map[string]string{ |
| 50 | + "id": "NotID", |
| 51 | + "name": "NotName", |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + a := Aliases{} |
| 56 | + a.Tables = map[string]TableAlias{"videos": expect} |
| 57 | + FillAliases(&a, tables) |
| 58 | + |
| 59 | + if !reflect.DeepEqual(expect, a.Tables["videos"]) { |
| 60 | + t.Error("it should not alter things that were specified by user") |
| 61 | + } |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +func TestAliasesRelationships(t *testing.T) { |
| 66 | + t.Parallel() |
| 67 | + |
| 68 | + tables := []drivers.Table{ |
| 69 | + { |
| 70 | + Name: "videos", |
| 71 | + Columns: []drivers.Column{ |
| 72 | + {Name: "id"}, |
| 73 | + {Name: "name"}, |
| 74 | + }, |
| 75 | + FKeys: []drivers.ForeignKey{ |
| 76 | + { |
| 77 | + Name: "fkey_1", |
| 78 | + Table: "videos", |
| 79 | + Column: "user_id", |
| 80 | + ForeignTable: "users", |
| 81 | + ForeignColumn: "id", |
| 82 | + }, |
| 83 | + { |
| 84 | + Name: "fkey_2", |
| 85 | + Table: "videos", |
| 86 | + Column: "publisher_id", |
| 87 | + ForeignTable: "users", |
| 88 | + ForeignColumn: "id", |
| 89 | + }, |
| 90 | + { |
| 91 | + Name: "fkey_3", |
| 92 | + Table: "videos", |
| 93 | + Column: "one_id", |
| 94 | + Unique: true, |
| 95 | + ForeignTable: "ones", |
| 96 | + ForeignColumn: "one", |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + t.Run("Automatic", func(t *testing.T) { |
| 103 | + expect1 := RelationshipAlias{ |
| 104 | + Local: "User", |
| 105 | + Foreign: "Videos", |
| 106 | + } |
| 107 | + expect2 := RelationshipAlias{ |
| 108 | + Local: "Publisher", |
| 109 | + Foreign: "PublisherVideos", |
| 110 | + } |
| 111 | + expect3 := RelationshipAlias{ |
| 112 | + Local: "One", |
| 113 | + Foreign: "Video", |
| 114 | + } |
| 115 | + |
| 116 | + a := Aliases{} |
| 117 | + FillAliases(&a, tables) |
| 118 | + |
| 119 | + if got := a.Relationships["fkey_1"]; !reflect.DeepEqual(expect1, got) { |
| 120 | + t.Errorf("bad values: %#v", got) |
| 121 | + } |
| 122 | + if got := a.Relationships["fkey_2"]; !reflect.DeepEqual(expect2, got) { |
| 123 | + t.Errorf("bad values: %#v", got) |
| 124 | + } |
| 125 | + if got := a.Relationships["fkey_3"]; !reflect.DeepEqual(expect3, got) { |
| 126 | + t.Errorf("bad values: %#v", got) |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + t.Run("UserOverride", func(t *testing.T) { |
| 131 | + expect1 := RelationshipAlias{ |
| 132 | + Local: "TheUser", |
| 133 | + Foreign: "Videos", |
| 134 | + } |
| 135 | + expect2 := RelationshipAlias{ |
| 136 | + Local: "Publisher", |
| 137 | + Foreign: "PublishedVideos", |
| 138 | + } |
| 139 | + expect3 := RelationshipAlias{ |
| 140 | + Local: "TheOne", |
| 141 | + Foreign: "AwesomeOneVideo", |
| 142 | + } |
| 143 | + |
| 144 | + a := Aliases{ |
| 145 | + Relationships: map[string]RelationshipAlias{ |
| 146 | + "fkey_1": {Local: "TheUser"}, |
| 147 | + "fkey_2": {Foreign: "PublishedVideos"}, |
| 148 | + "fkey_3": {Local: "TheOne", Foreign: "AwesomeOneVideo"}, |
| 149 | + }, |
| 150 | + } |
| 151 | + FillAliases(&a, tables) |
| 152 | + |
| 153 | + if got := a.Relationships["fkey_1"]; !reflect.DeepEqual(expect1, got) { |
| 154 | + t.Errorf("bad values: %#v", got) |
| 155 | + } |
| 156 | + if got := a.Relationships["fkey_2"]; !reflect.DeepEqual(expect2, got) { |
| 157 | + t.Errorf("bad values: %#v", got) |
| 158 | + } |
| 159 | + if got := a.Relationships["fkey_3"]; !reflect.DeepEqual(expect3, got) { |
| 160 | + t.Errorf("bad values: %#v", got) |
| 161 | + } |
| 162 | + }) |
| 163 | +} |
| 164 | + |
| 165 | +func TestAliasesRelationshipsJoinTable(t *testing.T) { |
| 166 | + t.Parallel() |
| 167 | + |
| 168 | + tables := []drivers.Table{ |
| 169 | + { |
| 170 | + Name: "videos", |
| 171 | + ToManyRelationships: []drivers.ToManyRelationship{ |
| 172 | + { |
| 173 | + Table: "videos", |
| 174 | + ForeignTable: "tags", |
| 175 | + Column: "id", |
| 176 | + ForeignColumn: "id", |
| 177 | + |
| 178 | + ToJoinTable: true, |
| 179 | + JoinTable: "video_tags", |
| 180 | + |
| 181 | + JoinLocalFKeyName: "fk_video_id", |
| 182 | + JoinLocalColumn: "video_id", |
| 183 | + JoinForeignFKeyName: "fk_tag_id", |
| 184 | + JoinForeignColumn: "tag_id", |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + } |
| 189 | + |
| 190 | + t.Run("Automatic", func(t *testing.T) { |
| 191 | + expect1 := RelationshipAlias{ |
| 192 | + Local: "Tags", |
| 193 | + Foreign: "Videos", |
| 194 | + } |
| 195 | + expect2 := RelationshipAlias{ |
| 196 | + Local: "Videos", |
| 197 | + Foreign: "Tags", |
| 198 | + } |
| 199 | + |
| 200 | + a := Aliases{} |
| 201 | + FillAliases(&a, tables) |
| 202 | + |
| 203 | + if got := a.Relationships["fk_video_id"]; !reflect.DeepEqual(expect1, got) { |
| 204 | + t.Errorf("bad values: %#v", got) |
| 205 | + } |
| 206 | + if got := a.Relationships["fk_tag_id"]; !reflect.DeepEqual(expect2, got) { |
| 207 | + t.Errorf("bad values: %#v", got) |
| 208 | + } |
| 209 | + }) |
| 210 | + |
| 211 | + t.Run("UserOverride", func(t *testing.T) { |
| 212 | + expect1 := RelationshipAlias{ |
| 213 | + Local: "NotTags", |
| 214 | + Foreign: "NotVideos", |
| 215 | + } |
| 216 | + expect2 := RelationshipAlias{ |
| 217 | + Local: "NotVideos", |
| 218 | + Foreign: "NotTags", |
| 219 | + } |
| 220 | + |
| 221 | + a := Aliases{ |
| 222 | + Relationships: map[string]RelationshipAlias{ |
| 223 | + "fk_video_id": {Local: "NotTags", Foreign: "NotVideos"}, |
| 224 | + }, |
| 225 | + } |
| 226 | + FillAliases(&a, tables) |
| 227 | + |
| 228 | + if got := a.Relationships["fk_video_id"]; !reflect.DeepEqual(expect1, got) { |
| 229 | + t.Errorf("bad values: %#v", got) |
| 230 | + } |
| 231 | + if got := a.Relationships["fk_tag_id"]; !reflect.DeepEqual(expect2, got) { |
| 232 | + t.Errorf("bad values: %#v", got) |
| 233 | + } |
| 234 | + }) |
| 235 | +} |
0 commit comments