-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconstraint_test.go
106 lines (93 loc) · 3.12 KB
/
constraint_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
package pqt_test
import (
"testing"
"reflect"
"github.com/piotrkowalczuk/pqt"
)
func TestConstraint_Name(t *testing.T) {
id := pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey())
success := map[string]*pqt.Constraint{
"public.user_id_pkey": pqt.PrimaryKey(pqt.NewTable("user"), id),
"custom_schema.user_id_pkey": pqt.PrimaryKey(func() *pqt.Table {
t := pqt.NewTable("user")
s := pqt.NewSchema("custom_schema")
s.AddTable(t)
return t
}(), id),
"<missing table>": pqt.Check(nil, "a > b", id),
"public.news_key": pqt.Unique(pqt.NewTable("news")),
}
for expected, given := range success {
got := given.Name()
if got != expected {
t.Errorf("wrong name, expected %s got %s", expected, got)
}
}
}
func TestForeignKey(t *testing.T) {
t1 := pqt.NewTable("left")
c11 := pqt.NewColumn("id", pqt.TypeSerialBig())
c12 := pqt.NewColumn("name", pqt.TypeText())
t1.AddColumn(c11)
t1.AddColumn(c12)
t2 := pqt.NewTable("right")
c21 := pqt.NewColumn("id", pqt.TypeSerialBig())
c22 := pqt.NewColumn("name", pqt.TypeText())
t2.AddColumn(c21)
t2.AddColumn(c22)
cstr := pqt.ForeignKey(pqt.Columns{c11, c12}, pqt.Columns{c21, c22})
if cstr.Type != pqt.ConstraintTypeForeignKey {
t.Errorf("wrong type, expected %s but got %s", pqt.ConstraintTypeForeignKey, cstr.Type)
}
if len(cstr.PrimaryColumns) != 2 {
t.Errorf("wrong number of columns, expected %d but got %d", 2, len(cstr.PrimaryColumns))
}
if len(cstr.Columns) != 2 {
t.Errorf("wrong number of columns, expected %d but got %d", 2, len(cstr.Columns))
}
if !reflect.DeepEqual(cstr.PrimaryTable, t1) {
t.Errorf("table does not match, expected %v but got %v", t1, cstr.PrimaryTable)
}
if !reflect.DeepEqual(cstr.Table, t2) {
t.Errorf("reference table does not match, expected %v but got %v", t2, cstr.Table)
}
}
func TestConstraints_CountOf(t *testing.T) {
idx := pqt.NewColumn("index", pqt.TypeIntegerBig())
uidx := pqt.NewColumn("unique_index", pqt.TypeIntegerBig())
unq := pqt.NewColumn("unique", pqt.TypeIntegerBig())
chk := pqt.NewColumn("check", pqt.TypeIntegerBig())
tbl := pqt.NewTable("table").
AddColumn(idx).
AddColumn(uidx).
AddColumn(unq).
AddColumn(chk)
given := pqt.Constraints{
pqt.Index(tbl, idx),
pqt.UniqueIndex(tbl, "Name", "check > 0", uidx),
pqt.Unique(tbl, unq),
pqt.Check(tbl, "check > 0", unq),
}
if given.CountOf() != len(given) {
t.Errorf("expected %d but got %d", len(given), given.CountOf())
}
if given.CountOf(pqt.ConstraintTypeForeignKey) != 0 {
t.Errorf("foreign key does not exists")
}
got := given.CountOf(pqt.ConstraintTypeIndex, pqt.ConstraintTypePrimaryKey)
if got != 1 {
t.Errorf("expected %d but got %d", 1, got)
}
got = given.CountOf(pqt.ConstraintTypeIndex, pqt.ConstraintTypeUnique)
if got != 2 {
t.Errorf("expected %d but got %d", 2, got)
}
got = given.CountOf(pqt.ConstraintTypeIndex, pqt.ConstraintTypeUnique, pqt.ConstraintTypeCheck)
if got != 3 {
t.Errorf("expected %d but got %d", 3, got)
}
got = given.CountOf(pqt.ConstraintTypeUniqueIndex, pqt.ConstraintTypeIndex, pqt.ConstraintTypeUnique, pqt.ConstraintTypeCheck)
if got != 4 {
t.Errorf("expected %d but got %d", 4, got)
}
}