简体   繁体   中英

Mocking sql.max() in golang using go-sqlmock

I am looking to mock up this query in my code ( "select max(a) from public.abc where id = %d" )

The way I am mocking this line is

maxOfA := 1
maxOfARows := sqlmock.NewRows([]string{"a"}).AddRow(maxOfA)
suite.mock.ExpectQuery("select max(a) from public.abc co where id = \\$1").WithArgs(1).WillReturnRows(maxOfARows)

And I am seeing this error

Error:
    Query: could not match actual sql: "select max(a) from public.abc where id = 1" with
        expected regexp "select max(a) from public.abc where id = 1"

What is the right way to mock the max() such sql functions

My mistake was that i wasnt wrapping up my mock query within (regexp.QuoteMeta()). wrapping up the query within the quotemeta did resolve my issue Reference: https://pkg.go.dev/regexp#QuoteMeta

Not deleting this post hoping it to help someone like me.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM