forked from jschaf/pggen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.sql_test.go
93 lines (85 loc) · 2.51 KB
/
query.sql_test.go
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
package composite
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/jschaf/pggen/internal/errs"
"github.com/jschaf/pggen/internal/pgtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestNewQuerier_SearchScreenshots(t *testing.T) {
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()
q := NewQuerier(conn)
screenshotID := 99
screenshot1 := insertScreenshotBlock(t, q, screenshotID, "body1")
screenshot2 := insertScreenshotBlock(t, q, screenshotID, "body2")
want := []SearchScreenshotsRow{
{
ID: screenshotID,
Blocks: []Blocks{
{
ID: screenshot1.ID,
ScreenshotID: screenshotID,
Body: screenshot1.Body,
},
{
ID: screenshot2.ID,
ScreenshotID: screenshotID,
Body: screenshot2.Body,
},
},
},
}
t.Run("SearchScreenshots", func(t *testing.T) {
rows, err := q.SearchScreenshots(context.Background(), SearchScreenshotsParams{
Body: "body",
Limit: 5,
Offset: 0,
})
require.NoError(t, err)
assert.Equal(t, want, rows)
})
t.Run("SearchScreenshotsBatch", func(t *testing.T) {
batch := &pgx.Batch{}
q.SearchScreenshotsBatch(batch, SearchScreenshotsParams{
Body: "body",
Limit: 5,
Offset: 0,
})
results := conn.SendBatch(context.Background(), batch)
defer errs.CaptureT(t, results.Close, "close batch results")
rows, err := q.SearchScreenshotsScan(results)
require.NoError(t, err)
assert.Equal(t, want, rows)
})
t.Run("SearchScreenshotsOneCol", func(t *testing.T) {
rows, err := q.SearchScreenshotsOneCol(context.Background(), SearchScreenshotsOneColParams{
Body: "body",
Limit: 5,
Offset: 0,
})
require.NoError(t, err)
assert.Equal(t, [][]Blocks{want[0].Blocks}, rows)
})
t.Run("SearchScreenshotsOneColBatch", func(t *testing.T) {
batch := &pgx.Batch{}
q.SearchScreenshotsOneColBatch(batch, SearchScreenshotsOneColParams{
Body: "body",
Limit: 5,
Offset: 0,
})
results := conn.SendBatch(context.Background(), batch)
defer errs.CaptureT(t, results.Close, "close batch results")
rows, err := q.SearchScreenshotsOneColScan(results)
require.NoError(t, err)
assert.Equal(t, [][]Blocks{want[0].Blocks}, rows)
})
}
func insertScreenshotBlock(t *testing.T, q *DBQuerier, screenID int, body string) InsertScreenshotBlocksRow {
t.Helper()
row, err := q.InsertScreenshotBlocks(context.Background(), screenID, body)
require.NoError(t, err, "insert screenshot blocks")
return row
}