-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_test.go
74 lines (66 loc) · 1.53 KB
/
insert_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
package gosqle
import (
"strings"
"testing"
"github.com/dwethmar/gosqle/mysql"
"github.com/dwethmar/gosqle/postgres"
)
func TestInsert_Write(t *testing.T) {
t.Parallel()
tests := []struct {
name string
insert *Insert
want string
wantErr bool
}{
{
name: "select columns mysql",
insert: NewInsert("users", "id", "username").Values(
mysql.NewArgument(1),
mysql.NewArgument("test"),
),
want: "INSERT INTO users (id, username) VALUES (?, ?);",
wantErr: false,
},
{
name: "select columns postgres",
insert: NewInsert("users", "id", "username").Values(
postgres.NewArgument(1, 1),
postgres.NewArgument("test", 2),
),
want: "INSERT INTO users (id, username) VALUES ($1, $2);",
wantErr: false,
},
{
name: "error on no table",
insert: NewInsert("", "email", "username"),
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sb := new(strings.Builder)
err := tt.insert.Write(sb)
if (err != nil) != tt.wantErr {
t.Errorf("Insert.Write() error = %v, wantErr %v", err, tt.wantErr)
return
}
if query := sb.String(); query != tt.want {
t.Errorf("Insert.Write() query = %q, wantQuery %q", query, tt.want)
}
})
}
}
func BenchmarkInsert_Write(b *testing.B) {
insert := NewInsert("users", "id", "username").Values(
mysql.NewArgument(1),
mysql.NewArgument("test"),
)
sb := new(strings.Builder)
for i := 0; i < b.N; i++ {
if err := insert.Write(sb); err != nil {
b.Fatal(err)
}
}
}