-
-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
panic when calling rows.Values() #195
Comments
Would you please provide reproducible test case showing the issue, that I can run on my environment? |
package object_test
import (
"github.com/jackc/pgx/v5"
"github.com/pashagolub/pgxmock/v3"
"testing"
)
func _map2Rows(data []map[string]string) pgx.Rows {
var keys = make([]string, 0, len(data[0]))
for key := range data[0] {
keys = append(keys, key)
}
mock, _ := pgxmock.NewPool()
rows := mock.NewRows(keys)
values := make([]interface{}, len(keys))
for _, row := range data {
for kl := 0; kl < len(keys); kl++ {
key := keys[kl]
values[kl] = row[key]
}
rows.AddRow(values...)
clear(values)
}
return rows.Kind()
}
type testObject struct {
ID int
}
func (o *testObject) ScanRow(rows pgx.Rows) error {
//rows.Next()
_, rowErr := rows.Values()
if rowErr != nil {
return rowErr
}
return nil
}
func TestObjects_ScanRow(t *testing.T) {
rows := _map2Rows([]map[string]string{
{
"id": "1",
},
{
"id": "2",
},
})
o := testObject{}
err := o.ScanRow(rows)
if err != nil {
t.Fatal(err)
}
} |
I don't see here any testing. What are you trying to achieve? And by the way,
|
when the database returns only one record and there is a Next() call, the error rows is closed is returned |
Would you please provide me with the simplest code snippet showing this issue? Thanks in advance! |
the error will occur if I use rows.Next() in the ScanRow method. package main
import (
"context"
"fmt"
"github.com/jackc/pgx/v5"
"log"
"os"
)
func main() {
config, _ := pgx.ParseConfig("dsn")
config.DefaultQueryExecMode = pgx.QueryExecModeSimpleProtocol
conn, err := pgx.ConnectConfig(context.TODO(), config)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err)
os.Exit(1)
}
pf := Profile{}
if testErr := conn.QueryRow(context.Background(), "select 1 as id, 'ivan' as name").Scan(&pf); testErr != nil {
log.Fatal(testErr)
}
}
type Profile struct {
}
func (p *Profile) ScanRow(rows pgx.Rows) error {
//rows.Next()
values, err := rows.Values()
if err != nil {
return err
}
_ = values
return rows.Err()
} |
Would you please check #196? This should work:
|
there's still panic in the tests |
In which exactly tests? Would you please add code showing the issue |
|
That code must use Why? Because you are playing with low level structs and interfaces. Thus you are responsible for correct usage. Now can you, please, provide a code showing a real life problem, meaning when we are really testing database code? |
func TestObjects_ScanRow(t *testing.T) {
rows := _map2Rows([]map[string]string{
{
"id": "1",
},
{
"id": "2",
},
})
o := testObject{}
rows.Next()
err := o.ScanRow(rows)
if err != nil {
t.Fatal(err)
}
} |
honestly it's so unimportant, i'll just redo the test. thanks for wasting your time on this mess |
pgxmock/rows.go
Line 69 in f9f4e9f
panic: runtime error: index out of range [-1]
i noticed that the error doesn't occur when I call rows.Next(), but in this case study I don't need to call this method
The text was updated successfully, but these errors were encountered: