pashagolub / pgxmock

pgx mock driver for golang to test database interactions
Other
393 stars 49 forks source link

Get value of arg to use in subsequent query #99

Closed manatarms closed 2 years ago

manatarms commented 2 years ago

I have code that generates UUIDs that are passed into a second query. How would it be possible to get the value of the arg passed into ExpectQuery and re-use it in subsequent Expect.

Here's a quick example

// AnyArg is a generated UUID
mockDB.ExpectExec("insert into table").
    WithArgs(AnyArg).
    WillReturnResult(pgxmock.NewResult("INSERT", 1))

mockDB.ExpectQuery("^select (.+) from table (.+)").
        // I'm trying to reuse the ID generated and passed into the 
        // previous query
    WithArgs(ARG_FROM_PREVIOUS).
    WillReturnRows(mockDB.NewRows(columns).
        AddRow(ARG_FROM_PREVIOUS))

Thanks for your work on this package!

pashagolub commented 2 years ago

Hello,

sorry, I don't understand the question. You already have your AnyArg. Use it

manatarms commented 2 years ago

I was hoping to assert that the logic between the queries did the right thing.

I wanted my argument assertion to be a little stricter but I did end up creating an AnyUUID type based on your Anytime example.

Thanks for responding!

pashagolub commented 2 years ago

If would be cool if you can share the code so I can include it in tests and/or the manual

manatarms commented 2 years ago

Absolutely, here it is

import (
    "github.com/google/uuid"
)

type AnyUUID struct{}

// Match satisfies sqlmock.Argument interface
func (a AnyUUID) Match(v interface{}) bool {
    _, err := uuid.Parse(v.(string))
    if err != nil {
        return false
    }
    return true
}