-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwrite_test.go
More file actions
188 lines (153 loc) · 4.52 KB
/
write_test.go
File metadata and controls
188 lines (153 loc) · 4.52 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//go:build itest
package itests
import (
"testing"
"github.com/georgysavva/scany/v2/pgxscan"
sq "github.com/n-r-w/squirrel"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWriteBuildersWithReturning(t *testing.T) {
t.Parallel()
pool, ctx := newTestPool(t)
setupSQL := `
CREATE TABLE products (
id bigserial PRIMARY KEY,
name text NOT NULL,
stock integer NOT NULL,
price double precision NOT NULL,
active boolean NOT NULL
);
CREATE TABLE sales (
id bigserial PRIMARY KEY,
product_id bigint NOT NULL REFERENCES products(id),
qty integer NOT NULL
);
CREATE TABLE archived_products (
id bigserial PRIMARY KEY,
name text NOT NULL,
stock integer NOT NULL,
price double precision NOT NULL,
active boolean NOT NULL
);
`
execSetup(t, pool, ctx, setupSQL)
insertProducts := sq.Insert("products").
Columns("name", "stock", "price", "active").
Values("Widget", 10, 25.5, true).
Values("Gadget", 3, 15.0, true).
Values("Legacy", 1, 5.0, false).
Suffix("RETURNING id, name, stock").
PlaceholderFormat(sq.Dollar)
sql, args, err := insertProducts.ToSql()
require.NoError(t, err)
type productRow struct {
ID int64 `db:"id"`
Name string `db:"name"`
Stock int `db:"stock"`
}
var products []productRow
err = pgxscan.Select(ctx, pool, &products, sql, args...)
require.NoError(t, err)
require.Len(t, products, 3)
productIDs := make(map[string]int64, len(products))
for _, product := range products {
productIDs[product.Name] = product.ID
}
insertSales := sq.Insert("sales").
Columns("product_id", "qty").
Values(productIDs["Widget"], 4).
Values(productIDs["Widget"], 2).
Values(productIDs["Gadget"], 1).
PlaceholderFormat(sq.Dollar)
sql, args, err = insertSales.ToSql()
require.NoError(t, err)
_, err = pool.Exec(ctx, sql, args...)
require.NoError(t, err)
salesAgg := sq.Select("product_id", "SUM(qty) AS qty_sold").
From("sales").
GroupBy("product_id")
updateQuery := sq.Update("products p").
Set("stock", sq.Expr("p.stock - s.qty_sold")).
FromSelect(salesAgg, "s").
Where("s.product_id = p.id").
Where(sq.Eq{"p.active": true}).
Suffix("RETURNING p.id, p.stock").
PlaceholderFormat(sq.Dollar)
sql, args, err = updateQuery.ToSql()
require.NoError(t, err)
type updatedRow struct {
ID int64 `db:"id"`
Stock int `db:"stock"`
}
var updates []updatedRow
err = pgxscan.Select(ctx, pool, &updates, sql, args...)
require.NoError(t, err)
updated := make(map[int64]int, len(updates))
for _, update := range updates {
updated[update.ID] = update.Stock
}
assert.Len(t, updated, 2)
assert.Equal(t, 4, updated[productIDs["Widget"]])
assert.Equal(t, 2, updated[productIDs["Gadget"]])
_, found := updated[productIDs["Legacy"]]
assert.False(t, found)
insertArchive := sq.Insert("archived_products").
Prefix("/* archive */").
Columns("name", "stock", "price", "active").
Select(
sq.Select("name", "stock", "price", "active").
From("products").
Where(sq.Eq{"active": false}),
).
PlaceholderFormat(sq.Dollar)
sql, args, err = insertArchive.ToSql()
require.NoError(t, err)
_, err = pool.Exec(ctx, sql, args...)
require.NoError(t, err)
var archivedCount int
err = pgxscan.Get(ctx, pool, &archivedCount, "SELECT COUNT(*) FROM archived_products")
require.NoError(t, err)
assert.Equal(t, 1, archivedCount)
deleteTarget := sq.Select("id").
From("products").
Where(sq.Lt{"stock": 5}).
OrderBy("stock ASC").
Limit(1)
deleteQuery := sq.Delete("products").
Where(sq.In("id", deleteTarget)).
Suffix("RETURNING id").
PlaceholderFormat(sq.Dollar)
sql, args, err = deleteQuery.ToSql()
require.NoError(t, err)
var deletedID int64
err = pgxscan.Get(ctx, pool, &deletedID, sql, args...)
require.NoError(t, err)
assert.Equal(t, productIDs["Legacy"], deletedID)
var remaining int
err = pgxscan.Get(ctx, pool, &remaining, "SELECT COUNT(*) FROM products WHERE id = $1", deletedID)
require.NoError(t, err)
assert.Equal(t, 0, remaining)
insertMapped := sq.Insert("products").
SetMap(map[string]any{
"name": "Refill",
"stock": 7,
"price": 9.5,
"active": true,
}).
Suffix("RETURNING id, name, stock").
PlaceholderFormat(sq.Dollar)
sql, args, err = insertMapped.ToSql()
require.NoError(t, err)
type mappedProduct struct {
ID int64 `db:"id"`
Name string `db:"name"`
Stock int `db:"stock"`
}
var mapped mappedProduct
err = pgxscan.Get(ctx, pool, &mapped, sql, args...)
require.NoError(t, err)
assert.NotZero(t, mapped.ID)
assert.Equal(t, "Refill", mapped.Name)
assert.Equal(t, 7, mapped.Stock)
}