@@ -153,3 +153,74 @@ func TestUpsert_EmitErrors(t *testing.T) {
153
153
}
154
154
})
155
155
}
156
+
157
+ func TestUpsert_WithoutReplacePlaceHolder (t * testing.T ) {
158
+ t .Run ("PostgreSQL" , func (t * testing.T ) {
159
+ sql , args , err := bob .
160
+ Upsert ("users" , bob .PostgreSQL ).
161
+ Columns ("name" , "email" ).
162
+ Values ("John Doe" , "john@doe.com" ).
163
+ Key ("email" ).
164
+ Replace ("name" , "John Does" ).
165
+ ToSql ()
166
+ if err != nil {
167
+ t .Error (err )
168
+ }
169
+
170
+ desiredSql := "INSERT INTO \" users\" (\" name\" , \" email\" ) VALUES ($1, $2) ON CONFLICT (\" email\" ) DO UPDATE SET \" name\" = $3;"
171
+ desiredArgs := []interface {}{"John Doe" , "john@doe.com" , "John Does" }
172
+
173
+ if sql != desiredSql {
174
+ t .Error ("sql is not the same as result: " , sql )
175
+ }
176
+ if ! reflect .DeepEqual (args , desiredArgs ) {
177
+ t .Error ("args is not the same as result: " , args )
178
+ }
179
+ })
180
+
181
+ t .Run ("MSSQL" , func (t * testing.T ) {
182
+ sql , args , err := bob .
183
+ Upsert ("users" , bob .MSSQL ).
184
+ Columns ("name" , "email" ).
185
+ Values ("John Doe" , "john@doe.com" ).
186
+ Key ("email" , "john@doe.com" ).
187
+ Replace ("name" , "John Does" ).
188
+ ToSql ()
189
+ if err != nil {
190
+ t .Error (err )
191
+ }
192
+
193
+ desiredSql := "IF NOT EXISTS (SELECT * FROM \" users\" WHERE \" email\" = @p1) INSERT INTO \" users\" (\" name\" , \" email\" ) VALUES (@p2, @p3) ELSE UPDATE \" users\" SET \" name\" = @p4 WHERE \" email\" = @p5;"
194
+ desiredArgs := []interface {}{"john@doe.com" , "John Doe" , "john@doe.com" , "John Does" , "john@doe.com" }
195
+
196
+ if sql != desiredSql {
197
+ t .Error ("sql is not the same as result: " , sql )
198
+ }
199
+ if ! reflect .DeepEqual (args , desiredArgs ) {
200
+ t .Error ("args is not the same as result: " , args )
201
+ }
202
+ })
203
+
204
+ t .Run ("SQLite" , func (t * testing.T ) {
205
+ sql , args , err := bob .
206
+ Upsert ("users" , bob .SQLite ).
207
+ Columns ("name" , "email" ).
208
+ Values ("John Doe" , "john@doe.com" ).
209
+ Key ("email" ).
210
+ Replace ("name" , "John Does" ).
211
+ ToSql ()
212
+ if err != nil {
213
+ t .Error (err )
214
+ }
215
+
216
+ desiredSql := "INSERT INTO \" users\" (\" name\" , \" email\" ) VALUES (?, ?) ON CONFLICT (\" email\" ) DO UPDATE SET \" name\" = ?;"
217
+ desiredArgs := []interface {}{"John Doe" , "john@doe.com" , "John Does" }
218
+
219
+ if sql != desiredSql {
220
+ t .Error ("sql is not the same as result: " , sql )
221
+ }
222
+ if ! reflect .DeepEqual (args , desiredArgs ) {
223
+ t .Error ("args is not the same as result: " , args )
224
+ }
225
+ })
226
+ }
0 commit comments