Fix staticcheck issues

pull/1708/head
Jack Christensen 2023-07-28 18:04:26 -05:00
parent 8fb309c631
commit 1a9b2a53a5
8 changed files with 34 additions and 37 deletions

View File

@ -507,7 +507,7 @@ func (c *Conn) execSimpleProtocol(ctx context.Context, sql string, arguments []a
mrr := c.pgConn.Exec(ctx, sql)
for mrr.NextResult() {
commandTag, err = mrr.ResultReader().Close()
commandTag, _ = mrr.ResultReader().Close()
}
err = mrr.Close()
return commandTag, err

View File

@ -315,7 +315,7 @@ func TestExecContextWithoutCancelation(t *testing.T) {
defer cancel()
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
ctx, cancelFunc := context.WithCancel(context.Background())
ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc()
commandTag, err := conn.Exec(ctx, "create temporary table foo(id integer primary key);")
@ -336,7 +336,7 @@ func TestExecContextFailureWithoutCancelation(t *testing.T) {
defer cancel()
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
ctx, cancelFunc := context.WithCancel(context.Background())
ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc()
_, err := conn.Exec(ctx, "selct;")
@ -361,7 +361,7 @@ func TestExecContextFailureWithoutCancelationWithArguments(t *testing.T) {
defer cancel()
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
ctx, cancelFunc := context.WithCancel(context.Background())
ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc()
_, err := conn.Exec(ctx, "selct $1;", 1)
@ -565,6 +565,7 @@ func TestListenNotify(t *testing.T) {
defer cancel()
notification, err = listener.WaitForNotification(ctx)
assert.True(t, pgconn.Timeout(err))
assert.Nil(t, notification)
// listener can listen again after a timeout
mustExec(t, notifier, "notify chat")

View File

@ -656,13 +656,6 @@ func TestQueryRowCoreIntegerEncoding(t *testing.T) {
defer closeConn(t, conn)
type allTypes struct {
ui uint
ui8 uint8
ui16 uint16
ui32 uint32
ui64 uint64
i int
i8 int8
i16 int16
i32 int32
i64 int64
@ -983,7 +976,6 @@ func TestQueryRowErrors(t *testing.T) {
type allTypes struct {
i16 int16
i int
s string
}

View File

@ -306,7 +306,7 @@ func (rows *baseRows) Values() ([]any, error) {
copy(newBuf, buf)
values = append(values, newBuf)
default:
rows.fatal(errors.New("Unknown format code"))
rows.fatal(errors.New("unknown format code"))
}
}

View File

@ -270,6 +270,7 @@ func TestCollectOneRowPrefersPostgreSQLErrorOverErrNoRows(t *testing.T) {
var pgErr *pgconn.PgError
require.ErrorAs(t, err, &pgErr)
require.Equal(t, "23505", pgErr.Code)
require.Equal(t, "", name)
})
}

View File

@ -24,6 +24,8 @@ type testTracer struct {
traceConnectEnd func(ctx context.Context, data pgx.TraceConnectEndData)
}
type ctxKey string
func (tt *testTracer) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryStartData) context.Context {
if tt.traceQueryStart != nil {
return tt.traceQueryStart(ctx, conn, data)
@ -117,13 +119,13 @@ func TestTraceExec(t *testing.T) {
require.Equal(t, `select $1::text`, data.SQL)
require.Len(t, data.Args, 1)
require.Equal(t, `testing`, data.Args[0])
return context.WithValue(ctx, "fromTraceQueryStart", "foo")
return context.WithValue(ctx, ctxKey(ctxKey("fromTraceQueryStart")), "foo")
}
traceQueryEndCalled := false
tracer.traceQueryEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) {
traceQueryEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceQueryStart"))
require.Equal(t, "foo", ctx.Value(ctxKey(ctxKey("fromTraceQueryStart"))))
require.Equal(t, `SELECT 1`, data.CommandTag.String())
require.NoError(t, data.Err)
}
@ -157,13 +159,13 @@ func TestTraceQuery(t *testing.T) {
require.Equal(t, `select $1::text`, data.SQL)
require.Len(t, data.Args, 1)
require.Equal(t, `testing`, data.Args[0])
return context.WithValue(ctx, "fromTraceQueryStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceQueryStart"), "foo")
}
traceQueryEndCalled := false
tracer.traceQueryEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) {
traceQueryEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceQueryStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceQueryStart")))
require.Equal(t, `SELECT 1`, data.CommandTag.String())
require.NoError(t, data.Err)
}
@ -198,20 +200,20 @@ func TestTraceBatchNormal(t *testing.T) {
traceBatchStartCalled = true
require.NotNil(t, data.Batch)
require.Equal(t, 2, data.Batch.Len())
return context.WithValue(ctx, "fromTraceBatchStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceBatchStart"), "foo")
}
traceBatchQueryCalledCount := 0
tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) {
traceBatchQueryCalledCount++
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.NoError(t, data.Err)
}
traceBatchEndCalled := false
tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) {
traceBatchEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.NoError(t, data.Err)
}
@ -261,20 +263,20 @@ func TestTraceBatchClose(t *testing.T) {
traceBatchStartCalled = true
require.NotNil(t, data.Batch)
require.Equal(t, 2, data.Batch.Len())
return context.WithValue(ctx, "fromTraceBatchStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceBatchStart"), "foo")
}
traceBatchQueryCalledCount := 0
tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) {
traceBatchQueryCalledCount++
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.NoError(t, data.Err)
}
traceBatchEndCalled := false
tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) {
traceBatchEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.NoError(t, data.Err)
}
@ -312,13 +314,13 @@ func TestTraceBatchErrorWhileReadingResults(t *testing.T) {
traceBatchStartCalled = true
require.NotNil(t, data.Batch)
require.Equal(t, 3, data.Batch.Len())
return context.WithValue(ctx, "fromTraceBatchStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceBatchStart"), "foo")
}
traceBatchQueryCalledCount := 0
tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) {
traceBatchQueryCalledCount++
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
if traceBatchQueryCalledCount == 2 {
require.Error(t, data.Err)
} else {
@ -329,7 +331,7 @@ func TestTraceBatchErrorWhileReadingResults(t *testing.T) {
traceBatchEndCalled := false
tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) {
traceBatchEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.Error(t, data.Err)
}
@ -381,13 +383,13 @@ func TestTraceBatchErrorWhileReadingResultsWhileClosing(t *testing.T) {
traceBatchStartCalled = true
require.NotNil(t, data.Batch)
require.Equal(t, 3, data.Batch.Len())
return context.WithValue(ctx, "fromTraceBatchStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceBatchStart"), "foo")
}
traceBatchQueryCalledCount := 0
tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) {
traceBatchQueryCalledCount++
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
if traceBatchQueryCalledCount == 2 {
require.Error(t, data.Err)
} else {
@ -398,7 +400,7 @@ func TestTraceBatchErrorWhileReadingResultsWhileClosing(t *testing.T) {
traceBatchEndCalled := false
tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) {
traceBatchEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.Error(t, data.Err)
}
@ -440,13 +442,13 @@ func TestTraceCopyFrom(t *testing.T) {
traceCopyFromStartCalled = true
require.Equal(t, pgx.Identifier{"foo"}, data.TableName)
require.Equal(t, []string{"a"}, data.ColumnNames)
return context.WithValue(ctx, "fromTraceCopyFromStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceCopyFromStart"), "foo")
}
traceCopyFromEndCalled := false
tracer.traceCopyFromEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceCopyFromEndData) {
traceCopyFromEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceCopyFromStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceCopyFromStart")))
require.Equal(t, `COPY 2`, data.CommandTag.String())
require.NoError(t, data.Err)
}
@ -488,7 +490,7 @@ func TestTracePrepare(t *testing.T) {
tracePrepareStartCalled = true
require.Equal(t, `ps`, data.Name)
require.Equal(t, `select $1::text`, data.SQL)
return context.WithValue(ctx, "fromTracePrepareStart", "foo")
return context.WithValue(ctx, ctxKey("fromTracePrepareStart"), "foo")
}
tracePrepareEndCalled := false
@ -530,7 +532,7 @@ func TestTraceConnect(t *testing.T) {
tracer.traceConnectStart = func(ctx context.Context, data pgx.TraceConnectStartData) context.Context {
traceConnectStartCalled = true
require.NotNil(t, data.ConnConfig)
return context.WithValue(ctx, "fromTraceConnectStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceConnectStart"), "foo")
}
traceConnectEndCalled := false

1
tx.go
View File

@ -152,7 +152,6 @@ type Tx interface {
// called on the dbTx.
type dbTx struct {
conn *Conn
err error
savepointNum int64
closed bool
}

View File

@ -555,6 +555,7 @@ func TestTxBeginFuncNestedTransactionCommit(t *testing.T) {
require.NoError(t, err)
return nil
})
require.NoError(t, err)
return nil
})
@ -601,6 +602,7 @@ func TestTxBeginFuncNestedTransactionRollback(t *testing.T) {
return nil
})
require.NoError(t, err)
var n int64
err = db.QueryRow(context.Background(), "select count(*) from foo").Scan(&n)