-
Notifications
You must be signed in to change notification settings - Fork 6
/
column_test.go
307 lines (280 loc) · 7.17 KB
/
column_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
package pqt_test
import (
"testing"
"go/types"
"github.com/piotrkowalczuk/pqt"
"github.com/piotrkowalczuk/pqt/pqtgo"
)
func TestNewDynamicColumn(t *testing.T) {
c := pqt.NewDynamicColumn("dynamic", &pqt.Function{
Name: "sum",
Type: pqt.TypeIntegerBig(),
Body: "select $1 + $2",
Behaviour: pqt.FunctionBehaviourStable,
Args: []*pqt.FunctionArg{
{
Name: "A",
Type: pqt.TypeIntegerBig(),
},
{
Name: "B",
Type: pqt.TypeIntegerBig(),
},
},
})
if !c.IsDynamic {
t.Error("column should be dynamic")
}
if c.Name != "dynamic" {
t.Errorf("wrong name: %s", c.Name)
}
if len(c.Func.Args) != 2 {
t.Errorf("wrong number of function arguments, expected %d got %d", 2, len(c.Func.Args))
}
}
func TestNewColumn(t *testing.T) {
collate := "UTF-7"
check := "username = 'random'"
r := pqt.NewColumn("username", pqt.TypeText())
c := pqt.NewColumn(
"user_username",
pqt.TypeText(),
pqt.WithCollate(collate),
pqt.WithCheck(check),
pqt.WithDefault("janusz"),
pqt.WithUnique(),
pqt.WithTypeMapping(pqtgo.BuiltinType(types.Byte)),
pqt.WithNotNull(),
pqt.WithPrimaryKey(),
pqt.WithReference(r),
)
if c.Type.String() != pqt.TypeText().String() {
t.Errorf("wrong column type, expected %s but got %s", pqt.TypeText().String(), c.Type.String())
}
if c.Collate != collate {
t.Errorf("wrong column collate, expected %s but got %s", collate, c.Collate)
}
if c.Check != check {
t.Errorf("wrong column check, expected %s but got %s", check, c.Check)
}
if d, ok := c.Default[pqt.EventInsert]; ok && d != "janusz" {
t.Errorf("wrong column default, expected %s but got %s", "janusz", d)
}
if !c.Unique {
t.Error("wrong column unique, expected true but got false")
}
if !c.NotNull {
t.Error("wrong column not null, expected true but got false")
}
if !c.PrimaryKey {
t.Error("wrong column primary key, expected true but got false")
}
if c.Reference != r {
t.Errorf("wrong column reference, expected %p but got %p", r, c.Reference)
}
constraints := c.Constraints()
if len(constraints) != 3 {
t.Errorf("wrong number of constraints, expected 3 but got %d", len(constraints))
}
var hasPK, hasFK, hasCH bool
for _, constraint := range constraints {
switch constraint.Type {
case pqt.ConstraintTypePrimaryKey:
hasPK = true
case pqt.ConstraintTypeForeignKey:
hasFK = true
case pqt.ConstraintTypeCheck:
hasCH = true
}
}
if !hasPK {
t.Errorf("mising primary key constraint")
}
if !hasFK {
t.Errorf("mising foreign key constraint")
}
if !hasCH {
t.Errorf("mising check constraint")
}
}
func TestColumn_DefaultOn(t *testing.T) {
success := []struct {
d string
e []pqt.Event
}{
{
d: "NOW()",
e: []pqt.Event{pqt.EventUpdate},
},
}
for _, data := range success {
c := pqt.NewColumn("column", pqt.TypeTimestampTZ(), pqt.WithDefault(data.d, data.e...))
EventLoop:
for _, e := range data.e {
d, ok := c.DefaultOn(e)
if !ok {
t.Errorf("missing default value for %s", e)
continue EventLoop
}
if d != data.d {
t.Errorf("wrong value, expected %s but got %s", data.d, d)
}
}
}
}
func TestWithIndex(t *testing.T) {
c := pqt.NewColumn("with_index", pqt.TypeText(), pqt.WithIndex())
if !c.Index {
t.Fatal("index expected to be true")
}
}
func TestWithDefault(t *testing.T) {
def := "0"
c := pqt.NewColumn("with_index", pqt.TypeText(), pqt.WithDefault(def, pqt.EventInsert, pqt.EventUpdate))
if len(c.Default) != 2 {
t.Fatal("expected default value for 2 events")
}
if d, ok := c.Default[pqt.EventInsert]; ok {
if d != def {
t.Errorf("insert event wrong value, expected %s but got %s", def, d)
}
} else {
t.Error("insert event expected")
}
if d, ok := c.Default[pqt.EventUpdate]; ok {
if d != def {
t.Errorf("update event wrong value, expected %s but got %s", def, d)
}
} else {
t.Error("update event expected")
}
}
func TestWithOnDelete(t *testing.T) {
c := pqt.NewColumn("on_delete", pqt.TypeBool(), pqt.WithOnDelete(pqt.Cascade))
if c.OnDelete != pqt.Cascade {
t.Errorf("wrong on delete event: %d", c.OnDelete)
}
}
func TestWithOnUpdate(t *testing.T) {
c := pqt.NewColumn("on_update", pqt.TypeBool(), pqt.WithOnUpdate(pqt.SetNull))
if c.OnUpdate != pqt.SetNull {
t.Errorf("wrong on update event: %d", c.OnUpdate)
}
}
func TestWithColumnShortName(t *testing.T) {
given := "short-name"
c := pqt.NewColumn("short_name", pqt.TypeBool(), pqt.WithColumnShortName(given))
if c.ShortName != given {
t.Errorf("wrong short name: %s", c.ShortName)
}
}
func TestColumns_String(t *testing.T) {
given := pqt.Columns{
&pqt.Column{Name: "1"},
&pqt.Column{Name: "2"},
&pqt.Column{Name: "3"},
}
exp := "1,2,3"
if given.String() != exp {
t.Errorf("wrong output, expected %s but got %s", exp, given.String())
}
}
func TestJoinColumns(t *testing.T) {
got := pqt.JoinColumns(pqt.Columns{
&pqt.Column{Name: "1"},
&pqt.Column{Name: "2"},
&pqt.Column{Name: "3"},
}, ".")
exp := "1.2.3"
if got != exp {
t.Errorf("wrong output, expected %s but got %s", exp, got)
}
}
func TestColumn_Constraints(t *testing.T) {
check := "column > 0"
col := pqt.NewColumn(
"column",
pqt.TypeSerial(),
pqt.WithPrimaryKey(),
pqt.WithUnique(),
pqt.WithCheck(check),
pqt.WithIndex(),
)
tbl := pqt.NewTable("table").AddColumn(col)
got := col.Constraints()
var nb int
for _, g := range got {
switch g.Type {
case pqt.ConstraintTypePrimaryKey:
nb++
if len(g.PrimaryColumns) != 1 {
t.Errorf("wrong number of columns, expected 1 got %d", len(g.PrimaryColumns))
}
if g.PrimaryTable != tbl {
t.Error("wrong table")
}
case pqt.ConstraintTypeIndex, pqt.ConstraintTypeUnique:
t.Errorf("unexpected constraint type, if column has primary key index and unique should be ignored, got %s", g.Type)
case pqt.ConstraintTypeCheck:
nb++
if len(g.PrimaryColumns) != 1 {
t.Errorf("pk: wrong number of columns, expected 1 got %d", len(g.PrimaryColumns))
}
if g.PrimaryTable != tbl {
t.Error("wrong table")
}
if g.Check != check {
t.Error("wrong check")
}
case pqt.ConstraintTypeForeignKey:
nb++
}
}
if nb != 2 {
t.Errorf("wrong number of constraints, expected 2 got %d", nb)
}
col = pqt.NewColumn(
"column",
pqt.TypeSerial(),
pqt.WithUnique(),
pqt.WithIndex(),
)
nb = 0
tbl = pqt.NewTable("table").AddColumn(col)
for _, g := range col.Constraints() {
switch g.Type {
case pqt.ConstraintTypeUnique:
nb++
if len(g.PrimaryColumns) != 1 {
t.Errorf("wrong number of columns, expected 1 got %d", len(g.PrimaryColumns))
}
if g.PrimaryTable != tbl {
t.Error("wrong table")
}
case pqt.ConstraintTypeIndex:
t.Errorf("unexpected constraint type, if column has unique index regular index be ignored, got %s", g.Type)
}
}
if nb != 1 {
t.Errorf("wrong number of constraints, expected 1 got %d", nb)
}
col = pqt.NewColumn(
"column",
pqt.TypeSerial(),
pqt.WithIndex(),
)
nb = 0
tbl = pqt.NewTable("table").AddColumn(col)
for _, g := range col.Constraints() {
switch g.Type {
case pqt.ConstraintTypeIndex:
nb++
if len(g.PrimaryColumns) != 1 {
t.Errorf("wrong number of columns, expected 1 got %d", len(g.PrimaryColumns))
}
if g.PrimaryTable != tbl {
t.Error("wrong table")
}
}
}
}