-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
100 lines (89 loc) · 2.66 KB
/
helpers_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
94
95
96
97
98
99
100
package pgtalk
import (
"bytes"
"context"
"fmt"
"io"
"testing"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgtype"
)
var (
polyTable = TableInfo{Name: "polies", Schema: "public", Alias: "p1"}
polyFTime = NewTimeAccess(MakeColumnInfo(polyTable, "ftime", NotPrimary, Nullable, 1),
func(dest any) any { return &dest.(*poly).FTime })
polyFFloat = NewFloat64Access(MakeColumnInfo(polyTable, "ffloat", NotPrimary, Nullable, 1),
func(dest any) any { return &dest.(*poly).FFloat })
polyFUUID = NewFieldAccess[pgtype.UUID](MakeColumnInfo(polyTable, "fuuid", NotPrimary, Nullable, 1),
func(dest any) any { return &dest.(*poly).FUUID })
polyFString = NewTextAccess(MakeColumnInfo(polyTable, "fstring", NotPrimary, Nullable, 1),
func(dest any) any { return &dest.(*poly).FString })
polyColumns = append([]ColumnAccessor{}, polyFTime, polyFFloat)
)
type poly struct {
FTime time.Time
FFloat float64
FBool bool
FString string
// pgtypes
FUUID pgtype.UUID
// for storing custom field expression result values
expressionResults map[string]any
}
func diff(left, right string) string {
//assume one line
b := new(bytes.Buffer)
io.WriteString(b, "\n")
io.WriteString(b, left)
io.WriteString(b, "\n")
leftRunes := []rune(left)
rightRunes := []rune(right)
size := len(leftRunes)
if l := len(rightRunes); l < size {
size = l
}
for c := 0; c < size; c++ {
l := leftRunes[c]
r := rightRunes[c]
if l == r {
b.WriteRune(l)
} else {
fmt.Fprintf(b, "^(%s)...", string(r))
break
}
}
return b.String()
}
func newMockConnection(t *testing.T) *fakeConnection {
return &fakeConnection{t: t}
}
type fakeConnection struct {
t *testing.T
}
type fakeRows struct {
t *testing.T
}
func (f fakeRows) Close() {}
func (f fakeRows) Err() error { return nil }
func (f fakeRows) Conn() *pgx.Conn { return nil }
func (f fakeRows) CommandTag() pgconn.CommandTag { return pgconn.CommandTag{} }
func (f fakeRows) FieldDescriptions() []pgconn.FieldDescription {
return []pgconn.FieldDescription{}
}
func (f fakeRows) Next() bool { return false }
func (f fakeRows) Scan(dest ...interface{}) error {
f.t.Helper()
f.t.Log("destinations:", dest)
return nil
}
func (f fakeRows) Values() ([]interface{}, error) { return []any{}, nil }
func (f fakeRows) RawValues() [][]byte { return [][]byte{} }
func (f *fakeConnection) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
f.t.Helper()
f.t.Log("sql:", oneliner(sql))
f.t.Log("parameters:", args)
return fakeRows{f.t}, nil
}
func (f *fakeConnection) ctx() context.Context { return context.Background() }