Closed gandarez closed 1 year ago
The error happens when it's comparing the expected table name without sanitization. The table name at pgx
is sanitized using this func.
The possible fix is to also sanitize the expected table name like:
e.expectedTableName = `"` + strings.ReplaceAll(strings.ReplaceAll(expectedTableName, string([]byte{0}), ""), `"`, `""`) + `"`
Should I create a PR for that?
no one?
It would be great if you can provide a working solution. I'm out of capacity at the moment. Thanks for understanding
This works for me:
func TestCopyFromBug(t *testing.T) {
mock, _ := NewConn()
defer func() {
err := mock.ExpectationsWereMet()
if err != nil {
t.Errorf("expectation were not met: %s", err)
}
}()
mock.ExpectCopyFrom(pgx.Identifier{"foo"}.Sanitize(), []string{"bar"}).WillReturnResult(1)
var rows [][]any
rows = append(rows, []any{"baz"})
_, err := mock.CopyFrom(context.Background(), pgx.Identifier{"foo"}, []string{"bar"}, pgx.CopyFromRows(rows))
if err != nil {
t.Errorf("unexpected error: %s", err)
}
}
It just worked because you called Sanitize()
on pgx identifier. Should it be expected to be called and known by the developer when writing the test?
Make sense. I will change the ExpectCopyFrom()
definition to match the CopyFrom()
method.
Thanks for pointing this out.
Describe the bug Calling
ExpectCopyFrom
is not considering either table name and column names passed in.To Reproduce Steps to reproduce the behavior:
Error
Expected behavior It should assert with success.
Additional context pgxmock/v2 v2.5.0