-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_test.go
177 lines (171 loc) · 5.89 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package querybuilder
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestUpdateQuery_Build(t *testing.T) {
sampleStruct := struct {
Name string `json:"name,omitempty" db:"name"`
Email string `json:"email,omitempty" db:"email"`
ID interface{} `json:"id,omitempty"`
Order float32 `json:"order" db:"-"`
}{
Name: "Omid",
Email: "[email protected]",
ID: 74639876,
}
tests := []struct {
name string
query *UpdateQuery
wantQuery string
wantArgs []interface{}
wantErr error
}{
{
name: "test1",
query: Update("table1").MapValues(nil),
wantQuery: "",
wantArgs: []interface{}(nil),
wantErr: errors.New(ErrColumnValueMapIsEmpty),
},
{
name: "test2",
query: Update(""),
wantQuery: "",
wantArgs: []interface{}(nil),
wantErr: errors.New(ErrTableIsEmpty),
},
{
name: "test3",
query: Update("table1").MapValues(map[string]interface{}{"field1": 10, "field2": "test"}).Where("id=?", 5000),
wantQuery: "UPDATE table1 SET field1=?,field2=? WHERE (id=?)",
wantArgs: []interface{}{10, "test", 5000},
wantErr: nil,
},
{
name: "test4 - non pointer struct",
query: Update("table1").StructValues(sampleStruct).Where("id=?", 5000),
wantQuery: "UPDATE table1 SET name=?,email=?,ID=? WHERE (id=?)",
wantArgs: []interface{}{"Omid", "[email protected]", 74639876, 5000},
wantErr: nil,
},
{
name: "test5 -with pointer struct",
query: Update("table1").StructValues(&sampleStruct).Where("id=?", 5000),
wantQuery: "UPDATE table1 SET name=?,email=?,ID=? WHERE (id=?)",
wantArgs: []interface{}{"Omid", "[email protected]", 74639876, 5000},
wantErr: nil,
},
{
name: "test6 -wrong number of arguments",
query: Update("table1").StructValues(&sampleStruct).Where("id=?"),
wantQuery: "",
wantArgs: []interface{}(nil),
wantErr: errors.New(ErrWrongNumberOfArgs),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t1 := time.Now()
gotQuery, gotArgs, err := tt.query.Build()
t.Logf("duration: %s", time.Since(t1))
require.Equal(t, tt.wantQuery, gotQuery, "Build() gotQuery = %v, wantQuery %v", gotQuery, tt.wantQuery)
require.Equal(t, tt.wantArgs, gotArgs, "Build() gotArgs = %v, wantQuery %v", gotArgs, tt.wantArgs)
require.Equal(t, tt.wantErr, err)
})
}
}
func TestUpdatePanicNotStruct(t *testing.T) {
require.Panics(t, func() {
Update("table1").StructValues(123)
}, "should panic with non struct types")
}
func TestUpdateQuery_Rebind(t *testing.T) {
tests := []struct {
name string
driver DriverName
updateQuery *UpdateQuery
want string
}{
{
name: "test Postgres",
driver: DriverPostgres,
updateQuery: Update("table1").MapValues(map[string]interface{}{"field1": 10, "field2": "test"}).Where("id=?", 5000),
want: "UPDATE table1 SET field1=$1,field2=$2 WHERE (id=$3)",
},
{
name: "test PGX",
driver: DriverPGX,
updateQuery: Update("table1").MapValues(map[string]interface{}{"field1": 10, "field2": "test"}).Where("id=?", 5000),
want: "UPDATE table1 SET field1=$1,field2=$2 WHERE (id=$3)",
},
{
name: "test pq-timeouts",
driver: DriverPqTimeout,
updateQuery: Update("table1").MapValues(map[string]interface{}{"field1": 10, "field2": "test"}).Where("id=?", 5000),
want: "UPDATE table1 SET field1=$1,field2=$2 WHERE (id=$3)",
},
{
name: "test CloudSqlPostgres",
driver: DriverCloudSqlPostgres,
updateQuery: Update("table1").MapValues(map[string]interface{}{"field1": 10, "field2": "test"}).Where("id=?", 5000),
want: "UPDATE table1 SET field1=$1,field2=$2 WHERE (id=$3)",
},
{
name: "test MySQL",
driver: DriverMySQL,
updateQuery: Update("table1").MapValues(map[string]interface{}{"field1": 10, "field2": "test"}).Where("id=?", 5000),
want: "UPDATE table1 SET field1=?,field2=? WHERE (id=?)",
},
{
name: "test Sqlite3",
driver: DriverSqlite3,
updateQuery: Update("table1").MapValues(map[string]interface{}{"field1": 10, "field2": "test"}).Where("id=?", 5000),
want: "UPDATE table1 SET field1=?,field2=? WHERE (id=?)",
},
{
name: "test oci8",
driver: DriverOCI8,
updateQuery: Update("table1").MapValues(map[string]interface{}{"field1": 10, "field2": "test"}).Where("id=?", 5000),
want: "UPDATE table1 SET field1=:arg1,field2=:arg2 WHERE (id=:arg3)",
},
{
name: "test ora",
driver: DriverORA,
updateQuery: Update("table1").MapValues(map[string]interface{}{"field1": 10, "field2": "test"}).Where("id=?", 5000),
want: "UPDATE table1 SET field1=:arg1,field2=:arg2 WHERE (id=:arg3)",
},
{
name: "test goracle",
driver: DriverGORACLE,
updateQuery: Update("table1").MapValues(map[string]interface{}{"field1": 10, "field2": "test"}).Where("id=?", 5000),
want: "UPDATE table1 SET field1=:arg1,field2=:arg2 WHERE (id=:arg3)",
},
{
name: "test SqlServer",
driver: DriverSqlServer,
updateQuery: Update("table1").MapValues(map[string]interface{}{"field1": 10, "field2": "test"}).Where("id=?", 5000),
want: "UPDATE table1 SET field1=@p1,field2=@p2 WHERE (id=@p3)",
},
{
name: "test unknown",
driver: "abcdefg",
updateQuery: Update("table1").MapValues(map[string]interface{}{"field1": 10, "field2": "test"}).Where("id=?", 5000),
want: "UPDATE table1 SET field1=?,field2=? WHERE (id=?)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
query, _, err := tt.updateQuery.Build()
if err != nil {
t.Errorf("can't build the query: %s", err)
}
Driver = tt.driver
if got := Rebind(query); got != tt.want {
require.Equal(t, tt.want, got, "Rebind() got = %v, want %v", got, tt.want)
}
})
}
}