forked from yunionio/sqlchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_test.go
More file actions
65 lines (59 loc) · 1.41 KB
/
insert_test.go
File metadata and controls
65 lines (59 loc) · 1.41 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
package sqlchemy
import (
"reflect"
"testing"
"yunion.io/x/pkg/util/reflectutils"
)
func insertSqlPrep(v interface{}) (string, []interface{}, error) {
vvvalue := reflect.ValueOf(v).Elem()
vv := vvvalue.Interface()
vvFields := reflectutils.FetchStructFieldValueSet(vvvalue)
ts := NewTableSpecFromStruct(vv, "vv")
sql, vals, err := ts.insertSqlPrep(vvFields)
return sql, vals, err
}
func TestInsertAutoIncrement(t *testing.T) {
sql, vals, err := insertSqlPrep(&struct {
RowId int `auto_increment:true`
}{})
if err != nil {
t.Errorf("prepare sql failed: %s", err)
return
}
wantSql := "INSERT INTO `vv` () VALUES()"
if sql != wantSql {
t.Errorf("sql want: %s\ngot: %s", wantSql, sql)
return
}
if len(vals) != 0 {
t.Errorf("vals want %d, got %d", 0, len(vals))
return
}
}
func TestInsertMultiAutoIncrement(t *testing.T) {
defer func() {
v := recover()
if v == nil {
t.Errorf("should panic with multiple auto_increment fields")
}
}()
_, _, err := insertSqlPrep(&struct {
RowId int `auto_increment:true`
RowId2 int `auto_increment:true`
}{})
t.Errorf("should panic but it continues: err: %s", err)
}
func TestInsertWithPointerValue(t *testing.T) {
sql, vals, err := insertSqlPrep(&struct {
RowId int `auto_increment:true`
ColT1 *int
ColT2 int
ColT3 string
ColT4 *string
}{})
if err != nil {
t.Errorf("prepare sql failed: %s", err)
return
}
t.Logf("%s values: %v", sql, vals)
}