I'm using sqlmock
to unit test a set of Mysql database query handlers written in Go. This works great for standard SELECT
/ INSERT
queries. For our db healthcheck, I'm using GORM's DB.Migrator().HasTable()
to make sure a particular table exists in our db. The functionality seems to be working properly, but I'm having difficulty mocking the query that's happening under the hood of HasTable()
. Does anybody have any advice on how to use sqlmock
to do this?
HasTable()
is executing the following query: SELECT count(*) FROM information_schema.tables WHERE table_schema = '' AND table_name = '[table_name]' AND table_type = 'BASE TABLE'
My helper function for the test looks like this:
sqlDB, mock, err := sqlmock.New()
if err != nil {
log.Panicln(err)
}
// This is the block I'm not sure about...
mock.ExpectBegin()
mock.ExpectQuery(regexp.QuoteMeta("SELECT count(*) FROM information_schema.tables WHERE table_schema = '' AND table_name = '[table_name]' AND table_type = 'BASE TABLE'")).
WillReturnRows(sqlmock.NewRows([]string{"count(*)"}).
AddRow(1))
mock.ExpectCommit()
setupSqlMock(sqlDB)
}
I've tried every combination of mock.ExpectExec()
, mock.ExpectQuery()
, and all of their methods that I can think of. Any ideas?
Using sqlmock.QueryMatcherEqual
you avoid the *
parsing problem
sqlDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.