This repository was archived by the owner on Mar 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathupdate_test.go
More file actions
93 lines (85 loc) · 2.06 KB
/
Copy pathupdate_test.go
File metadata and controls
93 lines (85 loc) · 2.06 KB
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
package memeduck_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/genkami/memeduck"
)
func testUpdate(t *testing.T, stmt *memeduck.UpdateStmt, expected string) {
actual, err := stmt.SQL()
assert.Nil(t, err, expected)
assert.Equal(t, expected, actual)
}
func TestUpdate(t *testing.T) {
testUpdate(t,
memeduck.Update("hoge").
Set(memeduck.Ident("a"), 1).
Where(
memeduck.Bool(true),
),
`UPDATE hoge SET a = 1 WHERE TRUE`,
)
testUpdate(t,
memeduck.Update("hoge").
Set(memeduck.Ident("a"), 1).
Set(memeduck.Ident("b"), "foo").
Where(
memeduck.Bool(true),
),
`UPDATE hoge SET a = 1, b = "foo" WHERE TRUE`,
)
testUpdate(t,
memeduck.Update("hoge").
Set(memeduck.Ident("a"), 1).
Set(memeduck.Ident("b"), "foo").
Where(
memeduck.Eq(memeduck.Ident("c"), "bar"),
),
`UPDATE hoge SET a = 1, b = "foo" WHERE c = "bar"`,
)
testUpdate(t,
memeduck.Update("hoge").
Set(memeduck.Ident("a"), memeduck.Param("a")).
Where(
memeduck.Eq(memeduck.Ident("b"), "foo"),
),
`UPDATE hoge SET a = @a WHERE b = "foo"`,
)
testUpdate(t,
memeduck.Update("hoge").
Set(memeduck.Ident("a"), memeduck.Ident("b")).
Set(memeduck.Ident("b"), memeduck.Ident("a")).
Where(
memeduck.Eq(memeduck.Ident("c"), "bar"),
),
`UPDATE hoge SET a = b, b = a WHERE c = "bar"`,
)
testUpdate(t,
memeduck.Update("hoge").
Set(memeduck.Ident("a", "b"), 1).
Where(
memeduck.Eq(memeduck.Ident("c"), "bar"),
),
`UPDATE hoge SET a.b = 1 WHERE c = "bar"`,
)
}
func TestUpdateWithEmptyIdent(t *testing.T) {
_, err := memeduck.Update("hoge").
Set(memeduck.Ident(), 1).
Where(
memeduck.Bool(true),
).SQL()
assert.Error(t, err, "empty ident")
}
func TestUpdateWithNoSet(t *testing.T) {
_, err := memeduck.Update("hoge").
Where(
memeduck.Eq(memeduck.Ident("a"), 1),
).SQL()
assert.Error(t, err, "UPDATE without SET clause")
}
func TestUpdateWithNoWhere(t *testing.T) {
_, err := memeduck.Update("hoge").
Set(memeduck.Ident("a"), 1).
SQL()
assert.Error(t, err, "UPDATE without WHERE clause")
}