jschaf / pggen

Generate type-safe Go for any Postgres query. If Postgres can run the query, pggen can generate code for it.
MIT License
278 stars 26 forks source link

Unexported genericBatch prevents GoMock mockgen on Querier interface #84

Closed kirk-anchor closed 6 months ago

kirk-anchor commented 1 year ago

I am trying to use GoMock to generate a mock for the pggen Querier interface. https://github.com/golang/mock

The problem is the struct generated by mockgen cannot have methods with the unexported genericBatch interface as params on the mocked Batch and Scan methods. I tried copying the genericBatch into the mock package and while that makes the methods on the struct work, the struct does not satisfy the Querier interface anymore because the Querier interface expects the methods to have its package's unexported genericBatch interface.

Shouldn't we be able to mock the Querier interface?

jschaf commented 1 year ago

Does using source mode help? https://github.com/golang/mock/issues/297

kirk-anchor commented 1 year ago

I am using source mode. It will generate a MockgenericBatch struct that is a mock of the interface but the MockQuerier struct still has Batch and Scan methods with batch genericBatch as an arg, which does not exist. If I manually modify the mock methods to use an arg batch MockgenericBatch then the mock struct won't satisfy the Querier interface because it expects to have Batch and Scan methods with an arg for the unexported batch genericBatch interface.

kirk-anchor commented 1 year ago

It would work fine for generating mocks within the same package but I would like the generated mocks to be in a separate package. A couple options would be to make genericBatch exported, i.e. GenericBatch. Or an option to skip generating batch methods. I am using batch queries but only 3 or 4 per batch so disabling batching and using non-batched queries in a transaction would be fine. I can create a pull request if you approve of one of these options.

kirk-anchor commented 6 months ago

Fixed after batch queries were removed in https://github.com/jschaf/pggen/pull/93