简体   繁体   English

Mocking sql.max() 在 golang 中使用 go-sqlmock

[英]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.

相关问题 Go 单元测试 - 在使用 go-sqlmock 模拟 gorm 时无法将实际 sql 与预期的正则表达式匹配? - Go Unit test - could not match actual sql with expected regexp while mocking gorm using go-sqlmock? mocking gorm `updates` 使用 go-sqlmock 时出现`could not match actual sql` 错误? - `could not match actual sql` error while mocking gorm `updates` using go-sqlmock? 使用go-sqlmock在SQL查询中获取参数值 - Get argument value in SQL query with go-sqlmock 对于复合 SQL 查询,在 go-sqlmock 中不会使用 args [] 调用查询 - Call to Query with args [], was not expected in go-sqlmock for compound SQL queries 如何使用带有可变数量参数的 go-sqlmock WithArgs()? - How to use go-sqlmock WithArgs() with a variable number of arguments? 使用 go-sqlmock 创建 gorm 数据库(运行时错误) - Creating a gorm database with go-sqlmock (runtime error) 如何在 go-sqlmock 中添加多个结果集? - How can I add multiple result sets in go-sqlmock? 如何为 go-sqlmock 正确设置 Mock Row 和 Query - How to correctly set Mock Row and Query for go-sqlmock 使用 go-sqlmock 问题测试 gorm 将查询与 mock.ExpectQuery 和 regexp.QuoteMeta 进行比较 - testing gorm with go-sqlmock issue comparing queries with mock.ExpectQuery and regexp.QuoteMeta 当我的程序中有并发查询时如何使用 go-sqlmock? - How to use go-sqlmock when I have concurrent query in my program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM