-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_values_test.go
More file actions
136 lines (124 loc) · 3.9 KB
/
insert_values_test.go
File metadata and controls
136 lines (124 loc) · 3.9 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
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
package squildx
import (
"errors"
"reflect"
"testing"
)
func TestInsertValues(t *testing.T) {
q := NewInsert().Into("users").Columns("name", "email").
Values(":name, :email", Params{"name": "Alice", "email": "a@b.com"})
ib := q.(*insertBuilder)
if len(ib.valueRows) != 1 {
t.Fatalf("expected 1 value row, got %d", len(ib.valueRows))
}
if ib.valueRows[0].sql != ":name, :email" {
t.Errorf("unexpected sql: %s", ib.valueRows[0].sql)
}
}
func TestInsertValues_MultiRow(t *testing.T) {
q := NewInsert().Into("users").Columns("name", "email").
Values(":n1, :e1", Params{"n1": "Alice", "e1": "a@b.com"}).
Values(":n2, :e2", Params{"n2": "Bob", "e2": "b@b.com"})
ib := q.(*insertBuilder)
if len(ib.valueRows) != 2 {
t.Fatalf("expected 2 value rows, got %d", len(ib.valueRows))
}
}
func TestInsertValues_Immutability(t *testing.T) {
base := NewInsert().Into("users").Columns("name").
Values(":name", Params{"name": "Alice"})
_ = base.Values(":name2", Params{"name2": "Bob"})
ib := base.(*insertBuilder)
if len(ib.valueRows) != 1 {
t.Errorf("base should have 1 value row, got %d", len(ib.valueRows))
}
}
func TestInsertValues_MissingParam(t *testing.T) {
q := NewInsert().Into("users").Columns("name").
Values(":name", Params{})
_, _, err := q.Build()
if !errors.Is(err, ErrMissingParam) {
t.Errorf("expected ErrMissingParam, got: %v", err)
}
}
func TestInsertValues_ExtraParam(t *testing.T) {
q := NewInsert().Into("users").Columns("name").
Values(":name", Params{"name": "Alice", "extra": "oops"})
_, _, err := q.Build()
if !errors.Is(err, ErrExtraParam) {
t.Errorf("expected ErrExtraParam, got: %v", err)
}
}
func TestInsertValuesObject(t *testing.T) {
type User struct {
Name string `db:"name"`
Email string `db:"email"`
}
q := NewInsert().Into("users").
ValuesObject(User{Name: "Alice", Email: "a@b.com"})
ib := q.(*insertBuilder)
wantCols := []string{"name", "email"}
if !reflect.DeepEqual(ib.columns, wantCols) {
t.Errorf("columns = %v, want %v", ib.columns, wantCols)
}
if len(ib.valueRows) != 1 {
t.Fatalf("expected 1 value row, got %d", len(ib.valueRows))
}
if ib.valueRows[0].sql != ":name, :email" {
t.Errorf("unexpected sql: %s", ib.valueRows[0].sql)
}
assertParam(t, ib.valueRows[0].params, "name", "Alice")
assertParam(t, ib.valueRows[0].params, "email", "a@b.com")
}
func TestInsertValuesObject_NotAStruct(t *testing.T) {
q := NewInsert().Into("users").ValuesObject(42)
_, _, err := q.Build()
if !errors.Is(err, ErrNotAStruct) {
t.Errorf("expected ErrNotAStruct, got: %v", err)
}
}
func TestInsertValuesObject_ColumnMismatch(t *testing.T) {
type User struct {
Name string `db:"name"`
}
q := NewInsert().Into("users").Columns("email").
ValuesObject(User{Name: "Alice"})
_, _, err := q.Build()
if !errors.Is(err, ErrColumnMismatch) {
t.Errorf("expected ErrColumnMismatch on column mismatch, got: %v", err)
}
}
func TestInsertValuesObject_SetsColumnsOnce(t *testing.T) {
type User struct {
Name string `db:"name"`
Email string `db:"email"`
}
q := NewInsert().Into("users").
ValuesObject(User{Name: "Alice", Email: "a@b.com"}).
ValuesObject(User{Name: "Bob", Email: "b@b.com"})
ib := q.(*insertBuilder)
if len(ib.valueRows) != 2 {
t.Fatalf("expected 2 value rows, got %d", len(ib.valueRows))
}
wantCols := []string{"name", "email"}
if !reflect.DeepEqual(ib.columns, wantCols) {
t.Errorf("columns = %v, want %v", ib.columns, wantCols)
}
}
func TestInsertValuesObject_EmbeddedStruct(t *testing.T) {
type Base struct {
ID int `db:"id"`
}
type User struct {
Base
Name string `db:"name"`
}
q := NewInsert().Into("users").ValuesObject(User{Base: Base{ID: 1}, Name: "Alice"})
ib := q.(*insertBuilder)
wantCols := []string{"id", "name"}
if !reflect.DeepEqual(ib.columns, wantCols) {
t.Errorf("columns = %v, want %v", ib.columns, wantCols)
}
assertParam(t, ib.valueRows[0].params, "id", 1)
assertParam(t, ib.valueRows[0].params, "name", "Alice")
}