-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_test.go
119 lines (102 loc) · 3.25 KB
/
update_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
package mogi_test
import (
"testing"
"time"
"github.com/guregu/mogi"
)
func TestUpdate(t *testing.T) {
defer mogi.Reset()
db := openDB()
// naked update
mogi.Update().StubResult(-1, 1)
_, err := db.Exec(`UPDATE beer
SET name = "Mikkel’s Dream", brewery = "Mikkeller", pct = 4.6
WHERE id = 3`)
checkNil(t, err)
// update with cols
mogi.Reset()
mogi.Update("name", "brewery", "pct").StubResult(-1, 1)
_, err = db.Exec(`UPDATE beer
SET name = "Mikkel’s Dream", brewery = "Mikkeller", pct = 4.6
WHERE id = 3`)
checkNil(t, err)
// with wrong cols
mogi.Reset()
mogi.Update("犬", "🐱", "かっぱ").StubResult(-1, 1)
_, err = db.Exec(`UPDATE beer
SET name = "Mikkel’s Dream", brewery = "Mikkeller", pct = 4.6
WHERE id = 3`)
if err != mogi.ErrUnstubbed {
t.Error("err should be ErrUnstubbed but is", err)
}
}
func TestUpdateTable(t *testing.T) {
defer mogi.Reset()
db := openDB()
// table
mogi.Update().Table("beer").StubResult(-1, 1)
_, err := db.Exec(`UPDATE beer
SET name = "Mikkel’s Dream", brewery = "Mikkeller", pct = 4.6
WHERE id = 3`)
checkNil(t, err)
// with wrong table
mogi.Reset()
mogi.Update().Table("酒").StubResult(-1, 1)
_, err = db.Exec(`UPDATE beer
SET name = "Mikkel’s Dream", brewery = "Mikkeller", pct = 4.6
WHERE id = 3`)
if err != mogi.ErrUnstubbed {
t.Error("err should be ErrUnstubbed but is", err)
}
}
func TestUpdateValues(t *testing.T) {
defer mogi.Reset()
db := openDB()
mogi.Update().Value("name", "Mikkel’s Dream").Value("brewery", "Mikkeller").Value("pct", 4.6).StubRowsAffected(1)
_, err := db.Exec(`UPDATE beer
SET name = "Mikkel’s Dream", brewery = "Mikkeller", pct = ?
WHERE id = 3`, 4.6)
checkNil(t, err)
// time.Time
mogi.Reset()
mogi.ParseTime(time.RFC3339)
now := time.Now()
mogi.Update().Value("updated_at", now).Value("brewery", "Mikkeller").Value("pct", 4.6).StubRowsAffected(1)
_, err = db.Exec(`UPDATE beer
SET updated_at = ?, brewery = "Mikkeller", pct = ?
WHERE id = 3`, now, 4.6)
checkNil(t, err)
// boolean as 1 vs true
mogi.Reset()
mogi.Update().Value("awesome", true).StubRowsAffected(1)
_, err = db.Exec(`UPDATE beer
SET awesome = 1
WHERE id = 3`)
checkNil(t, err)
// with wrong values
mogi.Reset()
mogi.Update().Value("name", "7-Premium THE BREW").Value("brewery", "Suntory").Value("pct", 5.0).StubResult(-1, 1)
_, err = db.Exec(`UPDATE beer
SET name = "Mikkel’s Dream", brewery = "Mikkeller", pct = ?
WHERE id = 3`, 4.6)
if err != mogi.ErrUnstubbed {
t.Error("err should be ErrUnstubbed but is", err)
}
}
func TestUpdateWhere(t *testing.T) {
defer mogi.Reset()
db := openDB()
mogi.Update().Where("id", 3).Where("moon", "full").StubResult(-1, 1)
_, err := db.Exec(`UPDATE beer
SET name = "Mikkel’s Dream", brewery = "Mikkeller", pct = ?
WHERE id = ? AND moon = "full"`, 4.6, 3)
checkNil(t, err)
mogi.Reset()
mogi.Update().Where("foo", 555).Where("bar", "qux").StubResult(-1, 1)
_, err = db.Exec(`UPDATE beer
SET name = "Mikkel’s Dream", brewery = "Mikkeller", pct = ?
WHERE id = 3 AND moon = "full"`, 4.6)
if err != mogi.ErrUnstubbed {
t.Error("err should be ErrUnstubbed but is", err)
}
}