DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
5.95k stars 404 forks source link

Issues with prepare statement #293

Closed rluders closed 2 years ago

rluders commented 2 years ago

Question

Hi folks, I need some help. I'm trying to test my repository using go-sqlmock, but I'm getting some errors. I don't think it is a bug, so... let me open it as a question, so we can figure out if it needs to be converted to a bug.

So, the code of the tests is here And the error that I'm getting seems to be at the Repository constructor

2022/07/16 18:23:31 an error 'error preparing statement list course: Prepare: could not match actual sql: "SELECT * FROM courses" with expected regexp "SELECT \* FROM courses WHERE uuid = \$1"' was not expected when creating the repository

The craziest part is that it fails in some random query preparation, it is not always the same, and of course, it means that sometimes it passes without any issues. What am I doing wrong? What am I missing?

rluders commented 2 years ago

Well, solved with:

mock.ExpectPrepare(fmt.Sprintf("^%s$", regexp.QuoteMeta(string(query))))