+7
insert.go
+7
insert.go
+57
-1
insert_test.go
+57
-1
insert_test.go
···
1
package norm
2
3
import (
4
+
"slices"
5
"testing"
6
+
7
+
_ "github.com/mattn/go-sqlite3"
8
)
9
10
func TestInsertBuild_Success(t *testing.T) {
···
81
t.Errorf("Expected '%v', got '%v' at index %d", test.expectedArgs[i], args[i], i)
82
}
83
}
84
+
})
85
+
}
86
+
}
87
+
88
+
func TestInsertValueSet_Build(t *testing.T) {
89
+
tests := []struct {
90
+
name string
91
+
stmt Compiler
92
+
expectedConfig []struct {
93
+
sql string
94
+
args []any
95
+
}
96
+
}{
97
+
{
98
+
name: "Simple insert",
99
+
stmt: Insert().Into("users").Values(map[string]any{
100
+
"name": "John",
101
+
"age": 25,
102
+
}),
103
+
expectedConfig: []struct {
104
+
sql string
105
+
args []any
106
+
}{
107
+
{
108
+
sql: "INSERT INTO users (name, age) VALUES (?, ?)",
109
+
args: []any{"John", 25},
110
+
},
111
+
{
112
+
sql: "INSERT INTO users (age, name) VALUES (?, ?)",
113
+
args: []any{25, "John"},
114
+
},
115
+
},
116
+
},
117
+
}
118
+
119
+
for _, test := range tests {
120
+
t.Run(test.name, func(t *testing.T) {
121
+
sql, args := test.stmt.MustCompile()
122
+
123
+
any := false
124
+
idx := 0
125
+
for i, config := range test.expectedConfig {
126
+
idx = i
127
+
equalSql := config.sql == sql
128
+
equalArgs := slices.Equal(config.args, args)
129
+
if equalSql && equalArgs {
130
+
any = true
131
+
}
132
+
}
133
+
134
+
if !any {
135
+
t.Errorf("Config did not match: %d: %q; got %q, %q", idx, test.expectedConfig[idx], sql, args)
136
+
}
137
+
138
})
139
}
140
}