-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
insert_test.go
121 lines (103 loc) · 3.59 KB
/
insert_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
package mogi_test
import (
"testing"
"github.com/guregu/mogi"
)
func TestInsert(t *testing.T) {
//mogi.Insert().Into("device_tokens").Expect("device_type", "gunosy_lite").StubResult(1337, 1)
defer mogi.Reset()
db := openDB()
// naked INSERT stub
mogi.Insert().StubResult(3, 1)
_, err := db.Exec("INSERT INTO beer (name, brewery, pct) VALUES (?, ?, ?)", "Mikkel’s Dream", "Mikkeller", 4.6)
checkNil(t, err)
// INSERT with columns
mogi.Reset()
mogi.Insert("name", "brewery", "pct").Into("beer").StubResult(3, 1)
_, err = db.Exec("INSERT INTO beer (name, brewery, pct) VALUES (?, ?, ?)", "Mikkel’s Dream", "Mikkeller", 4.6)
checkNil(t, err)
// INSERT with wrong columns
mogi.Reset()
mogi.Insert("犬", "🐱", "かっぱ").Into("beer").StubResult(3, 1)
_, err = db.Exec("INSERT INTO beer (name, brewery, pct) VALUES (?, ?, ?)", "Mikkel’s Dream", "Mikkeller", 4.6)
if err != mogi.ErrUnstubbed {
t.Error("err should be ErrUnstubbed but is", err)
}
}
func TestInsertArgs(t *testing.T) {
defer mogi.Reset()
db := openDB()
mogi.Insert().Args("Mikkel’s Dream", "Mikkeller", 4.6).StubResult(3, 1)
_, err := db.Exec("INSERT INTO beer (name, brewery, pct) VALUES (?, ?, ?)", "Mikkel’s Dream", "Mikkeller", 4.6)
checkNil(t, err)
// wrong args
mogi.Reset()
mogi.Insert().Args("Nodogoshi", "Kirin", 5).StubResult(4, 1)
_, err = db.Exec("INSERT INTO beer (name, brewery, pct) VALUES (?, ?, ?)", "Mikkel’s Dream", "Mikkeller", 4.6)
if err != mogi.ErrUnstubbed {
t.Error("err should be ErrUnstubbed but is", err)
}
}
func TestInsertInto(t *testing.T) {
defer mogi.Reset()
db := openDB()
mogi.Insert().Into("beer").StubResult(3, 1)
_, err := db.Exec("INSERT INTO beer (name, brewery, pct) VALUES (?, ?, ?)", "Mikkel’s Dream", "Mikkeller", 4.6)
checkNil(t, err)
// make sure .Into() and .Table() are the same
mogi.Reset()
mogi.Insert().Table("beer").StubResult(3, 1)
_, err = db.Exec("INSERT INTO beer (name, brewery, pct) VALUES (?, ?, ?)", "Mikkel’s Dream", "Mikkeller", 4.6)
checkNil(t, err)
}
func TestStubResult(t *testing.T) {
defer mogi.Reset()
db := openDB()
mogi.Insert().StubResult(3, 1)
res, err := db.Exec("INSERT INTO beer (name, brewery, pct) VALUES (?, ?, ?)", "Mikkel’s Dream", "Mikkeller", 4.6)
checkNil(t, err)
lastID, err := res.LastInsertId()
checkNil(t, err)
if lastID != 3 {
t.Error("LastInsertId() should be 3 but is", lastID)
}
affected, err := res.RowsAffected()
checkNil(t, err)
if affected != 1 {
t.Error("RowsAffected() should be 1 but is", affected)
}
}
func TestStubResultWithErrors(t *testing.T) {
defer mogi.Reset()
db := openDB()
mogi.Insert().StubResult(-1, -1)
res, err := db.Exec("INSERT INTO beer (name, brewery, pct) VALUES (?, ?, ?)", "Mikkel’s Dream", "Mikkeller", 4.6)
checkNil(t, err)
_, err = res.LastInsertId()
if err == nil {
t.Error("error is nil but shouldn't be:", err)
}
_, err = res.RowsAffected()
if err == nil {
t.Error("error is nil but shouldn't be:", err)
}
}
func TestInsertValues(t *testing.T) {
defer mogi.Reset()
db := openDB()
// single row
mogi.Insert().Value("brewery", "Mikkeller").Value("pct", 4.6).StubResult(3, 1)
_, err := db.Exec("INSERT INTO beer (name, brewery, pct) VALUES (?, ?, ?)", "Mikkel’s Dream", "Mikkeller", 4.6)
checkNil(t, err)
// multiple rows
mogi.Reset()
mogi.Insert().
ValueAt(0, "brewery", "Mikkeller").ValueAt(0, "pct", 4.6).
ValueAt(1, "brewery", "BrewDog").ValueAt(1, "pct", 18.2).
StubResult(4, 2)
_, err = db.Exec(`INSERT INTO beer (name, brewery, pct) VALUES (?, "Mikkeller", 4.6), (?, ?, ?)`,
"Mikkel’s Dream",
"Tokyo*", "BrewDog", 18.2,
)
checkNil(t, err)
}