-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Issues with new NamedArgs mocks #167
Comments
If it helps, the sql seems to get rewritten by argsMatches here. This seems like where the issue is coming from, but I'm not sure what the best way to fix it would be. |
What is the definition of |
type AnyTime struct{}
// Match satisfies pgxmock.Argument interface
func (a AnyTime) Match(v interface{}) bool {
_, ok := v.(time.Time)
return ok
} |
OK, I found the issue. In short: pgxmock always expects func TestFail(t *testing.T) {
t.Parallel()
mock, err := NewConn(QueryMatcherOption(QueryMatcherEqual))
a := assert.New(t)
a.NoError(err)
update := `
UPDATE "user"
SET email = @email,
password = @password,
pin = @pin,
name = @name,
software_admin = @software_admin,
refresh_token = @refresh_token,
updated_utc = @updated_utc
WHERE id = @id`
mock.ExpectExec(update).WithRewrittenSQL(`
UPDATE "user"
SET email = $1,
password = $2,
pin = $3,
name = $4,
software_admin = $5,
refresh_token = $6,
updated_utc = $7
WHERE id = $8`).WithArgs(pgx.NamedArgs{
"id": "mockUser.ID",
"email": "mockUser.Email",
"password": "mockUser.Password",
"pin": "mockUser.Pin",
"name": "mockUser.Name",
"software_admin": "mockUser.SoftwareAdmin",
"refresh_token": "mockUser.RefreshToken",
"updated_utc": AnyArg(),
}).WillReturnError(errPanic)
_, err = mock.Exec(context.Background(), update, pgx.NamedArgs{
"id": "mockUser.ID",
"email": "mockUser.Email",
"password": "mockUser.Password",
"pin": "mockUser.Pin",
"name": "mockUser.Name",
"software_admin": "mockUser.SoftwareAdmin",
"refresh_token": "mockUser.RefreshToken",
"updated_utc": time.Now().UTC(),
})
a.Error(err)
a.NoError(mock.ExpectationsWereMet())
} As you can see I specified both queries to expect: initial and rewritten. Let me try to fix it. I think it's not that complicated |
I see, yeah, it almost feels backwards. If you manage to change it around, I'll be very happy! |
Would you please try a fix in |
Yes, that works perfectly! I tested it with both my Execs and my Queries. |
[-] make sure `WithRewrittenSQL()` call is optional, fixes #167
Describe the bug
I'm trying to test an update command (via Exec) using pgx.NamedArgs, but ExpectExec does not seem to be doing the rewrite.
To Reproduce
I've removed a lot of the abstractions my application uses, but this is the jist of it. In this case, I get the following errors:
and
It's possible I'm just missing something, but as far as I can tell, this should work.
The text was updated successfully, but these errors were encountered: