forked from go-xorm/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder_update_test.go
52 lines (41 loc) · 2.04 KB
/
builder_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
// Copyright 2018 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package builder
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuilderUpdate(t *testing.T) {
sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL()
assert.NoError(t, err)
assert.EqualValues(t, "UPDATE table1 SET a=? WHERE a=?", sql)
assert.EqualValues(t, []interface{}{2, 1}, args)
sql, args, err = Update(Eq{"a": 2, "b": 1}).From("table1").Where(Eq{"a": 1}).ToSQL()
assert.NoError(t, err)
assert.EqualValues(t, "UPDATE table1 SET a=?,b=? WHERE a=?", sql)
assert.EqualValues(t, []interface{}{2, 1, 1}, args)
sql, args, err = Update(Eq{"a": 2}, Eq{"b": 1}).From("table1").Where(Eq{"a": 1}).ToSQL()
assert.NoError(t, err)
assert.EqualValues(t, "UPDATE table1 SET a=?,b=? WHERE a=?", sql)
assert.EqualValues(t, []interface{}{2, 1, 1}, args)
sql, args, err = Update(Eq{"a": 2, "b": Incr(1)}).From("table2").Where(Eq{"a": 1}).ToSQL()
assert.NoError(t, err)
assert.EqualValues(t, "UPDATE table2 SET a=?,b=b+? WHERE a=?", sql)
assert.EqualValues(t, []interface{}{2, 1, 1}, args)
sql, args, err = Update(Eq{"a": 2, "b": Incr(1), "c": Decr(1), "d": Expr("select count(*) from table2")}).From("table2").Where(Eq{"a": 1}).ToSQL()
assert.NoError(t, err)
assert.EqualValues(t, "UPDATE table2 SET a=?,b=b+?,c=c-?,d=(select count(*) from table2) WHERE a=?", sql)
assert.EqualValues(t, []interface{}{2, 1, 1, 1}, args)
sql, args, err = Update(Eq{"a": 2}).Where(Eq{"a": 1}).ToSQL()
assert.Error(t, err)
assert.EqualValues(t, ErrNoTableName, err)
sql, args, err = Update(Eq{}).From("table1").Where(Eq{"a": 1}).ToSQL()
assert.Error(t, err)
assert.EqualValues(t, ErrNoColumnToUpdate, err)
var builder = Builder{cond: NewCond()}
sql, args, err = builder.Update(Eq{"a": 2, "b": 1}).From("table1").Where(Eq{"a": 1}).ToSQL()
assert.NoError(t, err)
assert.EqualValues(t, "UPDATE table1 SET a=?,b=? WHERE a=?", sql)
assert.EqualValues(t, []interface{}{2, 1, 1}, args)
}