[英]Call to Query with args [], was not expected in go-sqlmock for compound SQL queries
我能夠成功地從一張表模擬對 select 的查詢,如下所示:
sqlMock.ExpectQuery("^SELECT DISTINCT (.+) FROM myTable1, myTable2").
WillReturnRows(myResultRows)
但我無法模擬以下查詢來檢查我的 postgres 數據庫中是否存在表:
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)
我也試過 mocking 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
線索是它正在期待下一個查詢。 所以我們知道它根本沒有讀過這個。 我的同事指出了這一點:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.