Skip to content

Commit 2dc58a5

Browse files
committed
test: backport to go1.18
1 parent 1023392 commit 2dc58a5

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed

sentrysql/fakedb_test.go

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,13 @@ import (
4444
"fmt"
4545
"io"
4646
"reflect"
47-
"slices"
4847
"strconv"
4948
"strings"
5049
"sync"
51-
"sync/atomic"
5250
"testing"
5351
"time"
54-
)
55-
56-
// fakeDriver is a fake database that implements Go's driver.Driver
57-
// (and driver.DriverContext for Sentry purposes) interface, just for testing.
52+
) // fakeDriver is a fake database that implements Go's driver.Driver
53+
// interface, just for testing.
5854
//
5955
// It speaks a query language that's semantically similar to but
6056
// syntactically different and simpler than SQL. The syntax is as
@@ -93,8 +89,6 @@ type fakeConnector struct {
9389
closed bool
9490
}
9591

96-
var _ driver.Connector = &fakeConnector{}
97-
9892
func (c *fakeConnector) Connect(context.Context) (driver.Conn, error) {
9993
conn, err := fdriver.Open(c.name)
10094
conn.(*fakeConn).waiter = c.waiter
@@ -126,8 +120,6 @@ func (cc *fakeDriverCtx) OpenConnector(name string) (driver.Connector, error) {
126120
type fakeDB struct {
127121
name string
128122

129-
useRawBytes atomic.Bool
130-
131123
mu sync.Mutex
132124
tables map[string]*table
133125
badConn bool
@@ -155,7 +147,12 @@ type table struct {
155147
}
156148

157149
func (t *table) columnIndex(name string) int {
158-
return slices.Index(t.colname, name)
150+
for n, nname := range t.colname {
151+
if name == nname {
152+
return n
153+
}
154+
}
155+
return -1
159156
}
160157

161158
type row struct {
@@ -243,6 +240,15 @@ type fakeStmt struct {
243240

244241
var fdriver driver.Driver = &fakeDriver{}
245242

243+
func contains(list []string, y string) bool {
244+
for _, x := range list {
245+
if x == y {
246+
return true
247+
}
248+
}
249+
return false
250+
}
251+
246252
type Dummy struct {
247253
driver.Driver
248254
}
@@ -352,8 +358,10 @@ func (db *fakeDB) columnType(table, column string) (typ string, ok bool) {
352358
if !ok {
353359
return
354360
}
355-
if i := slices.Index(t.colname, column); i != -1 {
356-
return t.coltype[i], true
361+
for n, cname := range t.colname {
362+
if cname == column {
363+
return t.coltype[n], true
364+
}
357365
}
358366
return "", false
359367
}
@@ -519,7 +527,8 @@ func errf(msg string, args ...any) error {
519527

520528
// parts are table|selectCol1,selectCol2|whereCol=?,whereCol2=?
521529
// (note that where columns must always contain ? marks,
522-
// just a limitation for fakedb)
530+
//
531+
// just a limitation for fakedb)
523532
func (c *fakeConn) prepareSelect(stmt *fakeStmt, parts []string) (*fakeStmt, error) {
524533
if len(parts) != 3 {
525534
stmt.Close()
@@ -651,7 +660,7 @@ func (c *fakeConn) PrepareContext(ctx context.Context, query string) (driver.Stm
651660
}
652661

653662
if c.stickyBad || (hookPrepareBadConn != nil && hookPrepareBadConn()) {
654-
return nil, fakeError{Message: "Prepare: Sticky Bad", Wrapped: driver.ErrBadConn}
663+
return nil, fakeError{Message: "Preapre: Sticky Bad", Wrapped: driver.ErrBadConn}
655664
}
656665

657666
c.touchMem()
@@ -705,8 +714,6 @@ func (c *fakeConn) PrepareContext(ctx context.Context, query string) (driver.Stm
705714
switch cmd {
706715
case "WIPE":
707716
// Nothing
708-
case "USE_RAWBYTES":
709-
c.db.useRawBytes.Store(true)
710717
case "SELECT":
711718
stmt, err = c.prepareSelect(stmt, parts)
712719
case "CREATE":
@@ -810,9 +817,6 @@ func (s *fakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (d
810817
case "WIPE":
811818
db.wipe()
812819
return driver.ResultNoRows, nil
813-
case "USE_RAWBYTES":
814-
s.c.db.useRawBytes.Store(true)
815-
return driver.ResultNoRows, nil
816820
case "CREATE":
817821
if err := db.createTable(s.table, s.colName, s.colType); err != nil {
818822
return nil, err
@@ -828,15 +832,6 @@ func (s *fakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (d
828832
return nil, fmt.Errorf("fakedb: unimplemented statement Exec command type of %q", s.cmd)
829833
}
830834

831-
func valueFromPlaceholderName(args []driver.NamedValue, name string) driver.Value {
832-
for i := range args {
833-
if args[i].Name == name {
834-
return args[i].Value
835-
}
836-
}
837-
return nil
838-
}
839-
840835
// When doInsert is true, add the row to the table.
841836
// When doInsert is false do prep-work and error checking, but don't
842837
// actually add the row to the table.
@@ -871,8 +866,11 @@ func (s *fakeStmt) execInsert(args []driver.NamedValue, doInsert bool) (driver.R
871866
val = args[argPos].Value
872867
} else {
873868
// Assign value from argument placeholder name.
874-
if v := valueFromPlaceholderName(args, strvalue[1:]); v != nil {
875-
val = v
869+
for _, a := range args {
870+
if a.Name == strvalue[1:] {
871+
val = a.Value
872+
break
873+
}
876874
}
877875
}
878876
argPos++
@@ -948,7 +946,6 @@ func (s *fakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (
948946
txStatus = "transaction"
949947
}
950948
cursor := &rowsCursor{
951-
db: s.c.db,
952949
parentMem: s.c,
953950
posRow: -1,
954951
rows: [][]*row{
@@ -1008,8 +1005,12 @@ func (s *fakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (
10081005
if wcol.Placeholder == "?" {
10091006
argValue = args[wcol.Ordinal-1].Value
10101007
} else {
1011-
if v := valueFromPlaceholderName(args, wcol.Placeholder[1:]); v != nil {
1012-
argValue = v
1008+
// Assign arg value from placeholder name.
1009+
for _, a := range args {
1010+
if a.Name == wcol.Placeholder[1:] {
1011+
argValue = a.Value
1012+
break
1013+
}
10131014
}
10141015
}
10151016
if fmt.Sprintf("%v", tcol) != fmt.Sprintf("%v", argValue) {
@@ -1041,7 +1042,6 @@ func (s *fakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (
10411042
}
10421043

10431044
cursor := &rowsCursor{
1044-
db: s.c.db,
10451045
parentMem: s.c,
10461046
posRow: -1,
10471047
rows: setMRows,
@@ -1084,7 +1084,6 @@ func (tx *fakeTx) Rollback() error {
10841084
}
10851085

10861086
type rowsCursor struct {
1087-
db *fakeDB
10881087
parentMem memToucher
10891088
cols [][]string
10901089
colType [][]string
@@ -1107,9 +1106,6 @@ type rowsCursor struct {
11071106
// This is separate from the fakeConn.line to allow for drivers that
11081107
// can start multiple queries on the same transaction at the same time.
11091108
line int64
1110-
1111-
// closeErr is returned when rowsCursor.Close
1112-
closeErr error
11131109
}
11141110

11151111
func (rc *rowsCursor) touchMem() {
@@ -1121,7 +1117,7 @@ func (rc *rowsCursor) Close() error {
11211117
rc.touchMem()
11221118
rc.parentMem.touchMem()
11231119
rc.closed = true
1124-
return rc.closeErr
1120+
return nil
11251121
}
11261122

11271123
func (rc *rowsCursor) Columns() []string {
@@ -1159,7 +1155,7 @@ func (rc *rowsCursor) Next(dest []driver.Value) error {
11591155
// messing up conversions or doing them differently.
11601156
dest[i] = v
11611157

1162-
if bs, ok := v.([]byte); ok && !rc.db.useRawBytes.Load() {
1158+
if bs, ok := v.([]byte); ok {
11631159
if rc.bytesClone == nil {
11641160
rc.bytesClone = make(map[*byte][]byte)
11651161
}

0 commit comments

Comments
 (0)