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

98 Views Asked by At

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

1

There are 1 best solutions below

0
On BEST ANSWER

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.