DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
6.06k stars 408 forks source link

feature request: allow to set mocks on transactions #159

Closed dcu closed 5 years ago

dcu commented 5 years ago

my code calls internally to tx.Stmt() which returns a new statement but there's no way (or at least I can't find it) to set expectation on it.

l3pp4rd commented 5 years ago

Hi, will need to have a look, but guess this should be possible with standard statement expectation.

dcu commented 5 years ago

@l3pp4rd any update about this?

l3pp4rd commented 5 years ago

hi @dcu maybe you are willing to make a contribution? sadly I'm too busy with other projects

dcu commented 5 years ago

no, sorry I'm super super super busy these days. I'd be good at least to define an API for this use case

l3pp4rd commented 5 years ago

sqlmock allows to mock the statements and they can be upgraded to *sql.Tx just like in every sql driver. func (tx Tx) Stmt(stmt Stmt) *Stmt does not return any error, it always will swap the connection of the statement to tx connection and it is handled internally by the sql driver. So the only problem you may face in prod code, is that it may take some time to do it, for that reason you have a Context where you can set the deadline, but all of this is not managed by any sql driver implementation and has nothing to do with sqlmock.

l3pp4rd commented 5 years ago

though, for some go standard library issue, it was not working on go 1.8. the bug was fixed on 1.9 and later

dcu commented 5 years ago

the problem is that I don't have access to the inner stmt because that's created internally by a function

l3pp4rd commented 5 years ago

well, sqlmock cannot do anything more convenient for you, it replicates the sql driver interface and allows to mock it.