-
Notifications
You must be signed in to change notification settings - Fork 27
/
insert_statement_test.go
144 lines (121 loc) · 3.59 KB
/
insert_statement_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
package godb
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestInsertInto(t *testing.T) {
Convey("TestInsertInto creates an insert into statement", t, func() {
db := &DB{}
q := db.InsertInto("dummies")
So(q.intoTable, ShouldEqual, "dummies")
})
}
func TestInsertColumns(t *testing.T) {
Convey("Given an insert statement", t, func() {
db := &DB{}
q := db.InsertInto("dummies")
Convey("Columns add columns after the existing list", func() {
q.Columns("foo")
q.Columns("bar", "baz")
So(len(q.columns), ShouldEqual, 3)
So(q.columns[0], ShouldEqual, "foo")
So(q.columns[1], ShouldEqual, "bar")
})
})
}
func TestInsertReturning(t *testing.T) {
Convey("Given an insert statement", t, func() {
db := &DB{}
q := db.InsertInto("dummies")
Convey("Calling Returning will add the given string to the returning list", func() {
q.Returning("id")
So(len(q.returningColumns), ShouldEqual, 1)
So(q.returningColumns[0], ShouldEqual, "id")
})
})
}
func TestInsertSuffix(t *testing.T) {
Convey("Given an insert statement", t, func() {
db := &DB{}
q := db.InsertInto("dummies")
Convey("Calling Suffix will add the given string to the suffixes list", func() {
suffix := "ON CONFLICT DO UPDATE"
q.Suffix(suffix)
So(len(q.suffixes), ShouldEqual, 1)
So(q.suffixes[0], ShouldEqual, suffix)
})
})
}
func TestInsertToSQL(t *testing.T) {
Convey("Given a valid insert statement with table, columns and values", t, func() {
db := &DB{}
q := db.InsertInto("dummies")
q.Columns("foo", "bar", "baz")
q.Values(1, 2, 3)
Convey("ToSQL create a SQL request", func() {
sql, _, err := q.ToSQL()
So(err, ShouldBeNil)
So(sql, ShouldEqual, "INSERT INTO dummies (foo, bar, baz) VALUES (?, ?, ?)")
})
Convey("Calling Values multiple times create a SQL with more values", func() {
q.Values(4, 5, 6)
sql, _, err := q.ToSQL()
So(err, ShouldBeNil)
So(sql, ShouldEqual, "INSERT INTO dummies (foo, bar, baz) VALUES (?, ?, ?), (?, ?, ?)")
})
Convey("Given values are in returned arguments", func() {
q.Values(4, 5, 6)
_, args, err := q.ToSQL()
So(err, ShouldBeNil)
So(len(args), ShouldEqual, 6)
})
Convey("Calling Suffix will add the given clause to SQL", func() {
q.Suffix("RETURNING id")
sql, _, _ := q.ToSQL()
So(sql, ShouldEndWith, " RETURNING id")
})
})
}
func TestInsertToSQLErrors(t *testing.T) {
db := &DB{}
Convey("Table name is mandatory", t, func() {
q := db.InsertInto("").Columns("foo").Values(1, 2, 3)
_, _, err := q.ToSQL()
So(err, ShouldNotBeNil)
})
Convey("Columns are mandatory", t, func() {
q := db.InsertInto("dummies").Values(1, 2, 3)
_, _, err := q.ToSQL()
So(err, ShouldNotBeNil)
})
Convey("Values are mandatory", t, func() {
q := db.InsertInto("dummies").Columns("foo")
_, _, err := q.ToSQL()
So(err, ShouldNotBeNil)
})
Convey("The count of values have to match the columns count", t, func() {
q := db.InsertInto("dummies").
Columns("foo", "bar", "baz").
Values(1, 2)
_, _, err := q.ToSQL()
So(err, ShouldNotBeNil)
})
}
func TestDoInsert(t *testing.T) {
Convey("Given a test database", t, func() {
db := fixturesSetup(t)
defer db.Close()
Convey("Do execute the query and return the Id", func() {
lastID, err := db.InsertInto("dummies").
Columns("a_text", "another_text", "an_integer").
Values("Foo", "Bar", 123).Do()
So(err, ShouldBeNil)
So(lastID, ShouldBeGreaterThan, 0)
Convey("The data are in the database", func() {
dummy := Dummy{}
db.Select(&dummy).Where("id = ?", lastID).Do()
So(dummy.ID, ShouldEqual, lastID)
})
})
})
}