-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuilder_test.go
More file actions
90 lines (79 loc) · 2.93 KB
/
builder_test.go
File metadata and controls
90 lines (79 loc) · 2.93 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
package pgkit_test
import (
"testing"
sq "github.com/Masterminds/squirrel"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/goware/pgkit/v2"
)
func TestInsertRecords_ColumnDriftRejected(t *testing.T) {
// ,omitzero produces different column shapes for nil vs non-nil empty
// slices; squirrel would otherwise stitch the mismatched widths into
// malformed multi-row SQL and surface only at exec time.
type Item struct {
ID int `db:"id"`
Tags []string `db:"tags,omitzero"`
}
sb := &pgkit.StatementBuilder{StatementBuilderType: sq.StatementBuilder.PlaceholderFormat(sq.Dollar)}
records := []Item{
{ID: 1, Tags: nil},
{ID: 2, Tags: []string{}},
}
b := sb.InsertRecords(records, "items")
require.Error(t, b.Err())
assert.Contains(t, b.Err().Error(), "differ from record 0")
}
func TestInsertRecords_UniformShape(t *testing.T) {
// Sanity: batches with consistent column shape across rows still build.
type Item struct {
ID int `db:"id"`
Tags []string `db:"tags,omitzero"`
}
sb := &pgkit.StatementBuilder{StatementBuilderType: sq.StatementBuilder.PlaceholderFormat(sq.Dollar)}
records := []Item{
{ID: 1, Tags: []string{"a"}},
{ID: 2, Tags: []string{"b"}},
}
b := sb.InsertRecords(records, "items")
require.NoError(t, b.Err())
}
func TestInsertRecord_EmptyColumnsRejected(t *testing.T) {
// All fields tagged ,omitzero (or ,omitempty) and all zero leaves
// Map with no columns. Squirrel would emit invalid INSERT INTO t
// VALUES (); fail fast at build time and point at sq.Expr as the
// escape for the all-default INSERT case. Tracked in goware/pgkit#51.
type Item struct {
Tags []string `db:"tags,omitzero"`
}
sb := &pgkit.StatementBuilder{StatementBuilderType: sq.StatementBuilder.PlaceholderFormat(sq.Dollar)}
b := sb.InsertRecord(&Item{}, "items")
require.Error(t, b.Err())
assert.Contains(t, b.Err().Error(), "no columns")
assert.Contains(t, b.Err().Error(), "sq.Expr")
}
func TestInsertRecords_EmptyColumnsRejected(t *testing.T) {
type Item struct {
Tags []string `db:"tags,omitzero"`
}
sb := &pgkit.StatementBuilder{StatementBuilderType: sq.StatementBuilder.PlaceholderFormat(sq.Dollar)}
records := []Item{{}, {}}
b := sb.InsertRecords(records, "items")
require.Error(t, b.Err())
assert.Contains(t, b.Err().Error(), "no columns")
}
func TestInsertRecords_OmitEmptyMapDriftRejected(t *testing.T) {
// Latent footgun ,omitzero exposes: legacy ,omitempty on a map already
// produced shape drift (nil map skipped, non-nil empty map kept via the
// DeepEqual fallback). The validation catches this case for free.
type Item struct {
ID int `db:"id"`
Tags map[string]string `db:"tags,omitempty"`
}
sb := &pgkit.StatementBuilder{StatementBuilderType: sq.StatementBuilder.PlaceholderFormat(sq.Dollar)}
records := []Item{
{ID: 1, Tags: nil},
{ID: 2, Tags: map[string]string{}},
}
b := sb.InsertRecords(records, "items")
require.Error(t, b.Err())
}