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
|
// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0
package oblast_test
import (
"database/sql"
"strconv"
"testing"
"go.xyrillian.de/oblast"
"go.xyrillian.de/oblast/internal/assert"
"go.xyrillian.de/oblast/internal/mock"
"go.xyrillian.de/oblast/internal/must"
)
func TestInsertBasicUsingLastInsertId(t *testing.T) {
md := mock.NewDriver()
db := sql.OpenDB(md)
type basicRecord struct {
ID int64 `db:"id,auto"`
Name string `db:"name"`
}
store := oblast.MustNewStore[basicRecord](
oblast.SqliteDialect(),
oblast.TableNameIs("basic_records"),
oblast.PrimaryKeyIs("id"),
)
for _, batchSize := range []int{1, oblast.PrepareThreshold - 1, oblast.PrepareThreshold + 1} {
t.Run("N="+strconv.Itoa(batchSize), func(t *testing.T) {
records := make([]basicRecord, batchSize)
for idx := range batchSize {
records[idx] = basicRecord{Name: "new"}
md.ForQuery(`INSERT INTO "basic_records" ("name") VALUES (?)`).
ExpectExecWithArgs("new").
AndReturnLastInsertId(int64(42 + idx)).
AndReturnRowsAffected(1)
}
records = must.Return(store.Insert(db, records...))(t)
for idx, r := range records {
assert.Equal(t, r.ID, int64(42+idx))
}
})
}
}
func TestInsertBasicUsingReturningClause(t *testing.T) {
md := mock.NewDriver()
db := sql.OpenDB(md)
type basicRecord struct {
ID int64 `db:"id,auto"`
Name string `db:"name"`
}
store := oblast.MustNewStore[basicRecord](
oblast.PostgresDialect(),
oblast.TableNameIs("basic_records"),
oblast.PrimaryKeyIs("id"),
)
for _, batchSize := range []int{1, oblast.PrepareThreshold - 1, oblast.PrepareThreshold + 1} {
t.Run("N="+strconv.Itoa(batchSize), func(t *testing.T) {
records := make([]basicRecord, batchSize)
for idx := range batchSize {
records[idx] = basicRecord{Name: "new"}
md.ForQuery(`INSERT INTO "basic_records" ("name") VALUES ($1) RETURNING "id"`).
ExpectQueryWithArgs("new").
AndReturnColumns("id").
WithRow(int64(42 + idx))
}
records = must.Return(store.Insert(db, records...))(t)
for idx, r := range records {
assert.Equal(t, r.ID, int64(42+idx))
}
})
}
}
func TestUpdateBasic(t *testing.T) {
md := mock.NewDriver()
db := sql.OpenDB(md)
type basicRecord struct {
ID int64 `db:"id,auto"`
Name string `db:"name"`
}
store := oblast.MustNewStore[basicRecord](
oblast.PostgresDialect(),
oblast.TableNameIs("basic_records"),
oblast.PrimaryKeyIs("id"),
)
for _, batchSize := range []int{1, oblast.PrepareThreshold - 1, oblast.PrepareThreshold + 1} {
t.Run("N="+strconv.Itoa(batchSize), func(t *testing.T) {
records := make([]basicRecord, batchSize)
for idx := range batchSize {
r := basicRecord{ID: int64(42 + idx), Name: "updated"}
records[idx] = r
md.ForQuery(`UPDATE "basic_records" SET "name" = $1 WHERE "id" = $2`).
ExpectExecWithArgs(r.Name, r.ID).
AndReturnRowsAffected(1)
}
must.Succeed(t, store.Update(db, records...))
})
}
}
func TestDeleteBasic(t *testing.T) {
md := mock.NewDriver()
db := sql.OpenDB(md)
type basicRecord struct {
ID int64 `db:"id,auto"`
Name string `db:"name"`
}
store := oblast.MustNewStore[basicRecord](
oblast.PostgresDialect(),
oblast.TableNameIs("basic_records"),
oblast.PrimaryKeyIs("id"),
)
for _, batchSize := range []int{1, oblast.PrepareThreshold - 1, oblast.PrepareThreshold + 1} {
t.Run("N="+strconv.Itoa(batchSize), func(t *testing.T) {
records := make([]basicRecord, batchSize)
for idx := range batchSize {
r := basicRecord{ID: int64(42 + idx), Name: "removed"}
records[idx] = r
md.ForQuery(`DELETE FROM "basic_records" WHERE "id" = $1`).
ExpectExecWithArgs(r.ID).
AndReturnRowsAffected(1)
}
must.Succeed(t, store.Delete(db, records...))
})
}
}
// TODO: more test coverage for query.go
|