Repository navigation
Expand file tree
/
Copy pathmodel_query_test.go
More file actions
134 lines (113 loc) · 5 KB
/
Copy pathmodel_query_test.go
File metadata and controls
134 lines (113 loc) · 5 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
package dbx
import (
"context"
"database/sql"
"testing"
"github.com/stretchr/testify/assert"
)
// execRecorder is an Executor that records the executed statements instead of running them.
type execRecorder struct {
sql []string
args [][]interface{}
}
func (r *execRecorder) Exec(query string, args ...interface{}) (sql.Result, error) {
r.sql = append(r.sql, query)
r.args = append(r.args, args)
return nil, nil //nolint:nilnil // test fake: the code under test never reads the Result of these statements
}
func (r *execRecorder) ExecContext(_ context.Context, query string, args ...interface{}) (sql.Result, error) {
return r.Exec(query, args...)
}
func (r *execRecorder) Query(string, ...interface{}) (*sql.Rows, error) {
panic("not expected")
}
func (r *execRecorder) QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) {
panic("not expected")
}
func (r *execRecorder) Prepare(string) (*sql.Stmt, error) {
panic("not expected")
}
func newRecordingBuilder(driver string, newBuilder BuilderFunc) (Builder, *execRecorder) {
r := &execRecorder{}
db := NewFromDB(nil, driver)
b := newBuilder(db, r)
db.Builder = b
return b, r
}
type upsertCustomer struct {
ID int
Email string
Name string
Created string
}
func (upsertCustomer) TableName() string { return "customer" }
type upsertOrderItem struct {
OrderID int `db:"pk"`
ItemID int `db:"pk"`
Quantity int
}
type upsertNoPK struct {
Name string
}
func TestModelQuery_Upsert(t *testing.T) {
newCustomer := func(id int) *upsertCustomer {
return &upsertCustomer{ID: id, Email: "e@example.com", Name: "n", Created: "c"}
}
t.Run("mysql", func(t *testing.T) {
b, r := newRecordingBuilder("mysql", NewMysqlBuilder)
// existing PK: PK inserted and used as the target, never updated
assert.Nil(t, b.Model(newCustomer(5)).Upsert())
// Exclude applies to both the insertion and the update
assert.Nil(t, b.Model(newCustomer(5)).Exclude("Created").Upsert())
// zero auto-increment PK is left out of the insertion
assert.Nil(t, b.Model(newCustomer(0)).Upsert())
// attrs select the fields; the PK is added regardless
assert.Nil(t, b.Model(newCustomer(5)).Upsert("Name"))
// Exclude does not apply to the PK
assert.Nil(t, b.Model(newCustomer(5)).Exclude("ID", "Created").Upsert())
assert.Equal(t, []string{
"INSERT INTO `customer` (`created`, `email`, `id`, `name`) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE `created`=VALUES(`created`), `email`=VALUES(`email`), `name`=VALUES(`name`)",
"INSERT INTO `customer` (`email`, `id`, `name`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `email`=VALUES(`email`), `name`=VALUES(`name`)",
"INSERT INTO `customer` (`created`, `email`, `name`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `created`=VALUES(`created`), `email`=VALUES(`email`), `name`=VALUES(`name`)",
"INSERT INTO `customer` (`id`, `name`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `name`=VALUES(`name`)",
"INSERT INTO `customer` (`email`, `id`, `name`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `email`=VALUES(`email`), `name`=VALUES(`name`)",
}, r.sql)
assert.Equal(t, []interface{}{"c", "e@example.com", 5, "n"}, r.args[0])
assert.Equal(t, []interface{}{5, "n"}, r.args[3])
})
t.Run("pgsql", func(t *testing.T) {
b, r := newRecordingBuilder("postgres", NewPgsqlBuilder)
assert.Nil(t, b.Model(newCustomer(5)).Exclude("Created").Upsert())
assert.Nil(t, b.Model(newCustomer(0)).Exclude("Created").Upsert())
assert.Nil(t, b.Model(&upsertOrderItem{OrderID: 1, ItemID: 0, Quantity: 3}).Upsert())
assert.Equal(t, []string{
`INSERT INTO "customer" ("email", "id", "name") VALUES ($1, $2, $3) ON CONFLICT ("id") DO UPDATE SET "email"=EXCLUDED."email", "name"=EXCLUDED."name"`,
`INSERT INTO "customer" ("email", "name") VALUES ($1, $2) ON CONFLICT ("id") DO UPDATE SET "email"=EXCLUDED."email", "name"=EXCLUDED."name"`,
// composite PK: all PK columns are the target, a zero value is kept
`INSERT INTO "upsert_order_item" ("item_id", "order_id", "quantity") VALUES ($1, $2, $3) ON CONFLICT ("item_id", "order_id") DO UPDATE SET "quantity"=EXCLUDED."quantity"`,
}, r.sql)
})
t.Run("sqlite", func(t *testing.T) {
b, r := newRecordingBuilder("sqlite", NewSqliteBuilder)
assert.Nil(t, b.Model(newCustomer(5)).Upsert("Name"))
assert.Equal(t, []string{
"INSERT INTO `customer` (`id`, `name`) VALUES (?, ?) ON CONFLICT (`id`) DO UPDATE SET `name`=EXCLUDED.`name`",
}, r.sql)
})
t.Run("errors", func(t *testing.T) {
b, r := newRecordingBuilder("mysql", NewMysqlBuilder)
// nothing left to update besides the PK
assert.NotNil(t, b.Model(newCustomer(5)).Upsert("ID"))
assert.Equal(t, MissingPKError, b.Model(&upsertNoPK{Name: "n"}).Upsert())
var a int
assert.NotNil(t, b.Model(&a).Upsert())
assert.Empty(t, r.sql)
// databases without Builder.Upsert
b, r = newRecordingBuilder("mssql", NewMssqlBuilder)
assert.NotNil(t, b.Model(newCustomer(5)).Upsert())
b2, r2 := newRecordingBuilder("oci8", NewOciBuilder)
assert.NotNil(t, b2.Model(newCustomer(5)).Upsert())
assert.Empty(t, r.sql)
assert.Empty(t, r2.sql)
})
}