4
4
"testing"
5
5
6
6
"gopkg.in/stretchr/testify.v1/assert"
7
+ "fmt"
8
+ "strings"
7
9
)
8
10
9
11
func BenchmarkUpdateValuesSql (b * testing.B ) {
@@ -23,45 +25,45 @@ func BenchmarkUpdateValueMapSql(b *testing.B) {
23
25
func TestUpdateAllToSql (t * testing.T ) {
24
26
sql , args := Update ("a" ).Set ("b" , 1 ).Set ("c" , 2 ).ToSQL ()
25
27
26
- assert .Equal (t , sql , quoteSQL (`UPDATE "a" SET %s = $1, %s = $2` , "b" , "c" ))
27
- assert .Equal (t , args , []interface {}{1 , 2 })
28
+ assert .Equal (t , quoteSQL (`UPDATE "a" SET %s = $1, %s = $2` , "b" , "c" ), sql )
29
+ assert .Equal (t , []interface {}{1 , 2 }, args )
28
30
}
29
31
30
32
func TestUpdateSingleToSql (t * testing.T ) {
31
33
sql , args := Update ("a" ).Set ("b" , 1 ).Set ("c" , 2 ).Where ("id = $1" , 1 ).ToSQL ()
32
34
33
- assert .Equal (t , sql , quoteSQL (`UPDATE "a" SET %s = $1, %s = $2 WHERE (id = $3)` , "b" , "c" ))
34
- assert .Equal (t , args , []interface {}{1 , 2 , 1 })
35
+ assert .Equal (t , quoteSQL (`UPDATE "a" SET %s = $1, %s = $2 WHERE (id = $3)` , "b" , "c" ), sql )
36
+ assert .Equal (t , []interface {}{1 , 2 , 1 }, args )
35
37
}
36
38
37
39
func TestUpdateSetMapToSql (t * testing.T ) {
38
40
sql , args := Update ("a" ).SetMap (map [string ]interface {}{"b" : 1 , "c" : 2 }).Where ("id = $1" , 1 ).ToSQL ()
39
41
40
42
if sql == quoteSQL (`UPDATE "a" SET %s = $1, %s = $2 WHERE (id = $3)` , "b" , "c" ) {
41
- assert .Equal (t , args , []interface {}{1 , 2 , 1 })
43
+ assert .Equal (t , []interface {}{1 , 2 , 1 }, args )
42
44
} else {
43
- assert .Equal (t , sql , quoteSQL (`UPDATE "a" SET %s = $1, %s = $2 WHERE (id = $3)` , "c" , "b" ))
44
- assert .Equal (t , args , []interface {}{2 , 1 , 1 })
45
+ assert .Equal (t , quoteSQL (`UPDATE "a" SET %s = $1, %s = $2 WHERE (id = $3)` , "c" , "b" ), sql )
46
+ assert .Equal (t , []interface {}{2 , 1 , 1 }, args )
45
47
}
46
48
}
47
49
48
50
func TestUpdateSetExprToSql (t * testing.T ) {
49
51
sql , args := Update ("a" ).Set ("foo" , 1 ).Set ("bar" , Expr ("COALESCE(bar, 0) + 1" )).Where ("id = $1" , 9 ).ToSQL ()
50
52
51
- assert .Equal (t , sql , quoteSQL (`UPDATE "a" SET %s = $1, %s = COALESCE(bar, 0) + 1 WHERE (id = $2)` , "foo" , "bar" ))
52
- assert .Equal (t , args , []interface {}{1 , 9 })
53
+ assert .Equal (t , quoteSQL (`UPDATE "a" SET %s = $1, %s = COALESCE(bar, 0) + 1 WHERE (id = $2)` , "foo" , "bar" ), sql )
54
+ assert .Equal (t , []interface {}{1 , 9 }, args )
53
55
54
56
sql , args = Update ("a" ).Set ("foo" , 1 ).Set ("bar" , Expr ("COALESCE(bar, 0) + $1" , 2 )).Where ("id = $1" , 9 ).ToSQL ()
55
57
56
- assert .Equal (t , sql , quoteSQL (`UPDATE "a" SET %s = $1, %s = COALESCE(bar, 0) + $2 WHERE (id = $3)` , "foo" , "bar" ))
57
- assert .Equal (t , args , []interface {}{1 , 2 , 9 })
58
+ assert .Equal (t , quoteSQL (`UPDATE "a" SET %s = $1, %s = COALESCE(bar, 0) + $2 WHERE (id = $3)` , "foo" , "bar" ), sql )
59
+ assert .Equal (t , []interface {}{1 , 2 , 9 }, args )
58
60
}
59
61
60
62
func TestUpdateTenStaringFromTwentyToSql (t * testing.T ) {
61
63
sql , args := Update ("a" ).Set ("b" , 1 ).Limit (10 ).Offset (20 ).ToSQL ()
62
64
63
- assert .Equal (t , sql , quoteSQL (`UPDATE "a" SET %s = $1 LIMIT 10 OFFSET 20` , "b" ))
64
- assert .Equal (t , args , []interface {}{1 })
65
+ assert .Equal (t , quoteSQL (`UPDATE "a" SET %s = $1 LIMIT 10 OFFSET 20` , "b" ), sql )
66
+ assert .Equal (t , []interface {}{1 }, args )
65
67
}
66
68
67
69
func TestUpdateWhitelist (t * testing.T ) {
@@ -75,8 +77,8 @@ func TestUpdateWhitelist(t *testing.T) {
75
77
SetWhitelist (sr , "user_id" , "other" ).
76
78
ToSQL ()
77
79
78
- assert .Equal (t , sql , quoteSQL (`UPDATE "a" SET %s = $1, %s = $2` , "user_id" , "other" ))
79
- checkSliceEqual (t , args , []interface {}{2 , false })
80
+ assert .Equal (t , quoteSQL (`UPDATE "a" SET %s = $1, %s = $2` , "user_id" , "other" ), sql )
81
+ checkSliceEqual (t , []interface {}{2 , false }, args )
80
82
}
81
83
82
84
func TestUpdateBlacklist (t * testing.T ) {
@@ -85,13 +87,31 @@ func TestUpdateBlacklist(t *testing.T) {
85
87
SetBlacklist (sr , "something_id" ).
86
88
ToSQL ()
87
89
88
- assert .Equal (t , sql , quoteSQL (`UPDATE "a" SET %s = $1, %s = $2` , "user_id" , "other" ))
89
- checkSliceEqual (t , args , []interface {}{2 , false })
90
+ assert .Equal (t , quoteSQL (`UPDATE "a" SET %s = $1, %s = $2` , "user_id" , "other" ), sql )
91
+ checkSliceEqual (t , []interface {}{2 , false }, args )
90
92
}
91
93
92
94
func TestUpdateWhereExprSql (t * testing.T ) {
93
95
expr := Expr ("id=$1" , 100 )
94
96
sql , args := Update ("a" ).Set ("b" , 10 ).Where (expr ).ToSQL ()
95
- assert .Equal (t , sql , `UPDATE "a" SET "b" = $1 WHERE (id=$2)` )
96
- assert .Exactly (t , args , []interface {}{10 , 100 })
97
+ assert .Equal (t , `UPDATE "a" SET "b" = $1 WHERE (id=$2)` , sql )
98
+ assert .Exactly (t , []interface {}{10 , 100 }, args )
99
+ }
100
+
101
+ func TestUpdateBeyondMaxLookup (t * testing.T ) {
102
+ sqlBuilder := Update ("a" )
103
+ setClauses := []string {}
104
+ expectedArgs := []interface {}{}
105
+ for i := 1 ; i < maxLookup + 1 ; i ++ {
106
+ sqlBuilder = sqlBuilder .Set ("b" , i )
107
+ setClauses = append (setClauses , fmt .Sprintf (" %s = $%d" , quoteSQL ("%s" , "b" ), i ))
108
+ expectedArgs = append (expectedArgs , i )
109
+ }
110
+ sql , args := sqlBuilder .Where ("id = $1" , maxLookup + 1 ).ToSQL ()
111
+ expectedSQL := fmt .Sprintf (`UPDATE "a" SET%s WHERE (id = $%d)` , strings .Join (setClauses , "," ), maxLookup + 1 )
112
+ expectedArgs = append (expectedArgs , maxLookup + 1 )
113
+
114
+ assert .Equal (t , expectedSQL , sql )
115
+ assert .Equal (t , expectedArgs , args )
116
+
97
117
}
0 commit comments