-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtable_test.go
340 lines (279 loc) · 10.8 KB
/
table_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package pqt_test
import (
"reflect"
"testing"
"github.com/piotrkowalczuk/pqt"
)
func TestNewTable(t *testing.T) {
tbl := pqt.NewTable("test", pqt.WithTableIfNotExists(), pqt.WithTableSpace("table_space"), pqt.WithTemporary())
if !tbl.IfNotExists {
t.Errorf("table should have field if not exists set to true")
}
if !tbl.Temporary {
t.Errorf("table should have field temporary set to true")
}
if tbl.TableSpace != "table_space" {
t.Errorf("table should have field table space set to table_space")
}
}
func TestWithTableShortName(t *testing.T) {
tbl := pqt.NewTable("table", pqt.WithTableShortName("tbl"))
if tbl.ShortName != "tbl" {
t.Errorf("wrong table short name: %s", tbl.ShortName)
}
}
func TestTable_SetIfNotExists(t *testing.T) {
tbl := pqt.NewTable("table").SetIfNotExists(true)
if !tbl.IfNotExists {
t.Error("if not exists expected to be true")
}
}
func TestTable_SetSchema(t *testing.T) {
sch1 := pqt.NewSchema("schema1")
sch2 := pqt.NewSchema("schema2")
tbl := pqt.NewTable("table").SetSchema(sch1)
if !reflect.DeepEqual(tbl.Schema, sch1) {
t.Error("wrong schema")
}
tbl.SetSchema(sch2)
if !reflect.DeepEqual(tbl.Schema, sch2) {
t.Error("wrong schema")
}
}
func TestTable_AddColumn(t *testing.T) {
c0 := pqt.NewColumn("c0", pqt.TypeSerialBig(), pqt.WithPrimaryKey())
c1 := &pqt.Column{Name: "c1"}
c2 := &pqt.Column{Name: "c2"}
c3 := &pqt.Column{Name: "c3"}
tbl := pqt.NewTable("test").
AddColumn(c0).
AddColumn(c1).
AddColumn(c2).
AddColumn(c3).
AddColumn(pqt.NewColumn("c4", pqt.TypeIntegerBig(), pqt.WithReference(c0))).
AddRelationship(pqt.ManyToOne(pqt.SelfReference()))
if len(tbl.Columns) != 6 {
t.Errorf("wrong number of colums, expected %d but got %d", 6, len(tbl.Columns))
}
if len(tbl.OwnedRelationships) != 2 {
// Reference is not a relationship
t.Errorf("wrong number of owned relationships, expected %d but got %d", 2, len(tbl.OwnedRelationships))
}
for i, c := range tbl.Columns {
if c.Name == "" {
t.Errorf("column #%d table name is empty", i)
}
if c.Table == nil {
t.Errorf("column #%d table nil pointer", i)
}
}
}
func TestTable_AddConstraint(t *testing.T) {
tbl := pqt.Table{}
idx := pqt.Constraint{
Type: pqt.ConstraintTypeIndex,
}
got := tbl.AddConstraint(&idx).Constraints.CountOf(pqt.ConstraintTypeIndex)
if got != 1 {
t.Errorf("wrong number of indexes: %d", got)
}
}
func TestTable_AddRelationship_oneToOneBidirectional(t *testing.T) {
user := pqt.NewTable("user").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey()))
userDetail := pqt.NewTable("user_detail").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey()))
user.AddRelationship(pqt.OneToOne(
userDetail,
pqt.WithInversedName("details"),
pqt.WithOwnerName("user"),
pqt.WithBidirectional(),
))
if len(user.OwnedRelationships) != 1 {
t.Fatalf("user should have 1 relationship, but has %d", len(user.OwnedRelationships))
}
if user.OwnedRelationships[0].OwnerName != "user" {
t.Errorf("user relationship to user_detail should be mapped by user, but is %s", user.OwnedRelationships[0].OwnerName)
}
if user.OwnedRelationships[0].OwnerTable != user {
t.Errorf("user relationship to user_detail should be mapped by user table, but is %v", user.OwnedRelationships[0].OwnerTable)
}
if user.OwnedRelationships[0].Type != pqt.RelationshipTypeOneToOne {
t.Error("user relationship to user_detail should be one to one bidirectional")
}
if len(userDetail.InversedRelationships) != 1 {
t.Fatalf("user_detail should have 1 relationship, but has %d", len(userDetail.InversedRelationships))
}
if userDetail.InversedRelationships[0].InversedName != "details" {
t.Error("user_detail relationship to user should be mapped by user")
}
if userDetail.InversedRelationships[0].InversedTable != userDetail {
t.Error("user_detail relationship to user should be mapped by user_detail table")
}
if userDetail.InversedRelationships[0].Type != pqt.RelationshipTypeOneToOne {
t.Errorf("user_detail relationship to user should be %d, but is %d", pqt.RelationshipTypeOneToOne, userDetail.InversedRelationships[0].Type)
}
}
func TestTable_AddRelationship_oneToOneUnidirectional(t *testing.T) {
user := pqt.NewTable("user").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey()))
userDetail := pqt.NewTable("user_detail").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey())).
AddRelationship(pqt.OneToOne(
user,
pqt.WithInversedName("user"),
pqt.WithOwnerName("details"),
))
if len(user.InversedRelationships) != 0 {
t.Fatalf("user should have 0 relationship, but has %d", len(user.InversedRelationships))
}
if len(userDetail.OwnedRelationships) != 1 {
t.Fatalf("user_detail should have 1 relationship, but has %d", len(userDetail.OwnedRelationships))
}
if userDetail.OwnedRelationships[0].InversedName != "user" {
t.Errorf("user_detail relationship to user should be mapped by user")
}
if userDetail.OwnedRelationships[0].InversedTable != user {
t.Errorf("user_detail relationship to user should be mapped by user table")
}
if userDetail.OwnedRelationships[0].Type != pqt.RelationshipTypeOneToOne {
t.Errorf("user_detail relationship to user should be %d, but is %d", pqt.RelationshipTypeOneToOne, userDetail.OwnedRelationships[0].Type)
}
}
func TestTable_AddRelationship_oneToOneSelfReferencing(t *testing.T) {
user := pqt.NewTable("user").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey()))
user.AddRelationship(pqt.OneToOne(
pqt.SelfReference(),
pqt.WithInversedName("child"),
pqt.WithOwnerName("parent"),
))
if len(user.OwnedRelationships) != 1 {
t.Fatalf("user should have 1 owned relationship, but has %d", len(user.OwnedRelationships))
}
if user.OwnedRelationships[0].OwnerName != "parent" {
t.Errorf("user relationship to user should be mapped by parent")
}
if user.OwnedRelationships[0].OwnerTable != user {
t.Errorf("user relationship to user should be mapped by user table")
}
if user.OwnedRelationships[0].Type != pqt.RelationshipTypeOneToOne {
t.Errorf("user relationship to user should be %d, but is %d", pqt.RelationshipTypeOneToOne, user.OwnedRelationships[0].Type)
}
if len(user.InversedRelationships) != 0 {
t.Fatalf("user should have 0 inversed relationship, but has %d", len(user.InversedRelationships))
}
}
func TestTable_AddRelationship_oneToMany(t *testing.T) {
user := pqt.NewTable("user").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey()))
comment := pqt.NewTable("comment").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey()))
user.AddRelationship(pqt.OneToMany(
comment,
pqt.WithBidirectional(),
pqt.WithInversedName("author"),
pqt.WithOwnerName("comments"),
))
if len(user.InversedRelationships) != 1 {
t.Fatalf("user should have 1 inversed relationship, but has %d", len(user.InversedRelationships))
}
if user.InversedRelationships[0].OwnerName != "comments" {
t.Errorf("user inversed relationship to comment should be mapped by comments")
}
if user.InversedRelationships[0].OwnerTable != comment {
t.Errorf("user inversed relationship to comment should be mapped by comment table")
}
if user.InversedRelationships[0].Type != pqt.RelationshipTypeOneToMany {
t.Errorf("user inversed relationship to comment should be one to many")
}
if len(comment.OwnedRelationships) != 1 {
t.Fatalf("comment should have 1 owned relationship, but has %d", len(comment.OwnedRelationships))
}
if comment.OwnedRelationships[0].InversedName != "author" {
t.Errorf("comment relationship to user should be mapped by author")
}
if comment.OwnedRelationships[0].InversedTable != user {
t.Errorf("comment relationship to user should be mapped by user table")
}
if comment.OwnedRelationships[0].Type != pqt.RelationshipTypeOneToMany {
t.Errorf("comment relationship to user should be %d, but is %d", pqt.RelationshipTypeOneToMany, comment.OwnedRelationships[0].Type)
}
}
func TestTable_AddRelationship_manyToMany(t *testing.T) {
user := pqt.NewTable("user").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey()))
group := pqt.NewTable("group").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey()))
userGroups := pqt.NewTable("user_groups").AddRelationship(pqt.ManyToMany(
user,
group,
pqt.WithInversedName("users"),
pqt.WithOwnerName("groups"),
pqt.WithBidirectional(),
))
if len(userGroups.OwnedRelationships) != 3 {
t.Fatalf("user groups should have 2 relationship, but has %d", len(userGroups.OwnedRelationships))
}
if len(user.ManyToManyRelationships) != 1 {
t.Fatalf("user should have 1 m2m relationship, but has %d", len(user.ManyToManyRelationships))
}
if len(group.ManyToManyRelationships) != 1 {
t.Fatalf("group should have 1 m2m relationship, but has %d", len(group.ManyToManyRelationships))
}
if userGroups.OwnedRelationships[0].OwnerName != "groups" {
t.Errorf("user relationship to group should be mapped by groups")
}
if user.ManyToManyRelationships[0].ThroughTable != userGroups {
t.Errorf("user relationship to group should be mapped by user groups table")
}
if user.ManyToManyRelationships[0].InversedTable != group {
t.Errorf("user should have relationship to group table")
}
if group.ManyToManyRelationships[0].ThroughTable != userGroups {
t.Errorf("group relationship to user should be mapped by user groups table")
}
if group.ManyToManyRelationships[0].OwnerTable != user {
t.Errorf("group should have relationship to user table")
}
if userGroups.OwnedRelationships[0].Type != pqt.RelationshipTypeManyToMany {
t.Errorf("user relationship to group should be many to many")
}
}
func TestTable_FullName(t *testing.T) {
tbl := pqt.NewTable("table")
if tbl.FullName() != "table" {
t.Errorf("wrong full name: %s", tbl.FullName())
}
pqt.NewSchema("schema").AddTable(tbl)
if tbl.FullName() != "schema.table" {
t.Errorf("wrong full name: %s", tbl.FullName())
}
}
func TestTable_AddCheck(t *testing.T) {
a := pqt.NewColumn("a", pqt.TypeInteger())
b := pqt.NewColumn("b", pqt.TypeInteger())
tbl := pqt.NewTable("table").
AddColumn(a).
AddColumn(b).
AddCheck("a > b", a, b)
got := tbl.Constraints.CountOf(pqt.ConstraintTypeCheck)
if got != 1 {
t.Errorf("wrong number of check constraints: %d", got)
}
}
func TestTable_AddUnique(t *testing.T) {
a := pqt.NewColumn("a", pqt.TypeInteger())
b := pqt.NewColumn("b", pqt.TypeInteger())
tbl := pqt.NewTable("table").
AddColumn(a).
AddColumn(b).
AddUnique(a, b)
got := tbl.Constraints.CountOf(pqt.ConstraintTypeUnique)
if got != 1 {
t.Errorf("wrong number of unique constraints: %d", got)
}
}
func TestTable_AddIndex(t *testing.T) {
a := pqt.NewColumn("a", pqt.TypeInteger())
b := pqt.NewColumn("b", pqt.TypeInteger())
tbl := pqt.NewTable("table").
AddColumn(a).
AddColumn(b).
AddIndex(a, b)
got := tbl.Constraints.CountOf(pqt.ConstraintTypeIndex)
if got != 1 {
t.Errorf("wrong number of index constraints: %d", got)
}
}