Feature Request: Add param values struct Matcher for use with sqlc #218
Replies: 8 comments
-
Or a subset of the above example. A minimal variant would be:
|
Beta Was this translation helpful? Give feedback.
-
Hello. Why cannot you just implement QueryRewriter interface for Some examples are available here. |
Beta Was this translation helpful? Give feedback.
-
The current situation is that you have variadic args that are strongly typed but only fail in runtime, rather than at compile time. The variadic situation means that we get positional failures where code is working in production, but fail in the mock without good feedback as to what is wrong or how to correct it. The same thing happens for the We would like to pass structs for both |
Beta Was this translation helpful? Give feedback.
-
You can pass struct to |
Beta Was this translation helpful? Give feedback.
-
Check this test code: func TestQueryRewriter(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, updated_utc = @updated_utc WHERE id = @id`
mock.ExpectExec(update).WithArgs(pgx.NamedArgs{
"id": "mockUser.ID",
"email": "mockUser.Email",
"password": "mockUser.Password",
"updated_utc": AnyArg(),
}).WillReturnError(errPanic)
_, err = mock.Exec(context.Background(), update, pgx.NamedArgs{
"id": "mockUser.ID",
"email": "mockUser.Email",
"password": "mockUser.Password",
"updated_utc": time.Now().UTC(),
})
a.Error(err)
a.NoError(mock.ExpectationsWereMet())
} |
Beta Was this translation helpful? Give feedback.
-
That example works because So, what about rows then? There is an identical issue where we have |
Beta Was this translation helpful? Give feedback.
-
I thought you are absolutely free to modify
Seems I missed that part completely. Would you please bring some example code? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
-
An example: // generated code, used in production
type MyRow struct {
Name string
Count int64
}
// in test
.AddRow("", int64(0))
// becomes
.AddStructRows(MyRow{Name: "", Count: 0}, ...) Benefits:
|
Beta Was this translation helpful? Give feedback.
-
We use
pgx/v5
with sqlc andpgxmock
, and we would like to contribute some convenience functions (written by my co-worker @coady) for this combination (without adding any new dependencies in pgxmock).sqlc will generate structs like the Params object that we can use with pgxmock.
Background
Given a SQL query like:
sqlc will generate a structs like Params and others that we can use with pgxmock.
We can then add a
structmock.go
:ExpectQuery
ExpectQuery
allows us to modify our tests to go from:to:
ExpectExec
ExpectExec
allows us to modify our tests to go from:to this:
Expect
Expect
allows us to modify our tests to go from:to this:
Beta Was this translation helpful? Give feedback.
All reactions