[英]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"
)我想在我的代码中模拟这个查询( "select max(a) from public.abc where id = %d"
)
The way I am mocking this line is我的方式是 mocking 这一行是
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模拟 max() 这样的 sql 函数的正确方法是什么
My mistake was that i wasnt wrapping up my mock query within (regexp.QuoteMeta()).我的错误是我没有在 (regexp.QuoteMeta()) 中结束我的模拟查询。 wrapping up the query within the quotemeta did resolve my issue Reference: https://pkg.go.dev/regexp#QuoteMeta在 quotemeta 中结束查询确实解决了我的问题参考: https://pkg.go.dev/regexp#QuoteMeta
Not deleting this post hoping it to help someone like me.不删除此帖子希望它能帮助像我这样的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.