使用 args [] 调用查询,在 go-sqlmock 中不期望用于复合 SQL 查询
Call to Query with args [], was not expected in go-sqlmock for compound SQL queries
我能够像这样从一个 table 成功模拟到 select 的查询:
sqlMock.ExpectQuery("^SELECT DISTINCT (.+) FROM myTable1, myTable2").
WillReturnRows(myResultRows)
但是我无法模拟以下查询来检查我的 postgres 数据库中是否存在 table:
SELECT EXISTS
( SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'myTable3' );
组合:
existsRows := sqlmock.NewRows([]string{"exists"}).
AddRow(true)
和
slMock.ExpectQuery("^SELECT EXISTS").
WillReturnRows(existsRows)
我也尝试模拟 SELECT 1
但我得到了完全相同的错误:
time="2019-09-27T15:49:41-07:00" level=panic msg="db query" error="call to Query 'SELECT EXISTS\n\t\t( SELECT 1\n\t\tFROM information_schema.tables\n\t\tWHERE table_schema = 'public'\n\t\t AND table_name = 'myTable3' );' with args [], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which......
我正在使用的软件包:
import (
"database/sql"
"db"
"os"
"testing"
// not explicitly called
_ "github.com/denisenkom/go-mssqldb"
_ "github.com/lib/pq"
"github.com/DATA-DOG/go-sqlmock"
"github.com/sirupsen/logrus"
)
如有任何想法或指示,我们将不胜感激。我在网上找不到相关的例子
我不确定,但我认为问题出在你查询的缩进中,尝试删除查询中的换行符或表格 SELECT EXISTS
( SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'myTable3' );
像这样SELECT EXISTS( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'myTable3' );
实际上,
sqlMock.ExpectQuery("SELECT EXISTS \( SELECT 1 FROM information_schema\.tables WHERE table_schema = 'public' AND table_name = 'myTable3' \);").
WillReturnRows(existsRows)
成功了。
我也用过regex101.com
线索是它正在等待下一个查询。所以我们知道它根本没有读过这个。我的同事指出了:)
我能够像这样从一个 table 成功模拟到 select 的查询:
sqlMock.ExpectQuery("^SELECT DISTINCT (.+) FROM myTable1, myTable2").
WillReturnRows(myResultRows)
但是我无法模拟以下查询来检查我的 postgres 数据库中是否存在 table:
SELECT EXISTS
( SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'myTable3' );
组合:
existsRows := sqlmock.NewRows([]string{"exists"}).
AddRow(true)
和
slMock.ExpectQuery("^SELECT EXISTS").
WillReturnRows(existsRows)
我也尝试模拟 SELECT 1
但我得到了完全相同的错误:
time="2019-09-27T15:49:41-07:00" level=panic msg="db query" error="call to Query 'SELECT EXISTS\n\t\t( SELECT 1\n\t\tFROM information_schema.tables\n\t\tWHERE table_schema = 'public'\n\t\t AND table_name = 'myTable3' );' with args [], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which......
我正在使用的软件包:
import (
"database/sql"
"db"
"os"
"testing"
// not explicitly called
_ "github.com/denisenkom/go-mssqldb"
_ "github.com/lib/pq"
"github.com/DATA-DOG/go-sqlmock"
"github.com/sirupsen/logrus"
)
如有任何想法或指示,我们将不胜感激。我在网上找不到相关的例子
我不确定,但我认为问题出在你查询的缩进中,尝试删除查询中的换行符或表格 SELECT EXISTS
( SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'myTable3' );
像这样SELECT EXISTS( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'myTable3' );
实际上,
sqlMock.ExpectQuery("SELECT EXISTS \( SELECT 1 FROM information_schema\.tables WHERE table_schema = 'public' AND table_name = 'myTable3' \);").
WillReturnRows(existsRows)
成功了。
我也用过regex101.com
线索是它正在等待下一个查询。所以我们知道它根本没有读过这个。我的同事指出了:)