【问题标题】:Call to Query with args [], was not expected in go-sqlmock for compound SQL queries使用 args [] 调用查询,在 go-sqlmock 中不期望用于复合 SQL 查询
【发布时间】:2020-01-28 05:33:02
【问题描述】:

我能够成功地模拟查询以从一个表中进行选择,如下所示:

sqlMock.ExpectQuery("^SELECT DISTINCT (.+) FROM myTable1, myTable2").
        WillReturnRows(myResultRows)

但我无法模拟以下用于检查我的 postgres db 中是否存在表的查询:

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"
)

感谢任何想法或指针。我在网上找不到相关的例子

【问题讨论】:

  • 我取得了一些进展,crawlerMock.ExpectQuery("SELECT EXISTS \\( SELECT 1 (.*) 'myTable3' \\);"). WillReturnRows(existsRows) 给了我“参数不匹配:预期为 1,但得到了 0 个参数”。请让我知道如何进行。我没有在我的实际代码中提供任何参数。它有效。更多示例:chromium.googlesource.com/external/github.com/DATA-DOG/…
  • 我又尝试了一件事情:crawlerMock.ExpectQuery("SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'myTable3' );"). WillReturnRows(existsRows) 给出了错误could not match actual sql: \"SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'myTable3' );\" with expected regexp \"SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'myTable3' );\"

标签: postgresql go testing go-sqlmock


【解决方案1】:

我不确定,但认为问题在于您的查询缩进尝试删除查询中的换行符或制表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' );

【讨论】:

    【解决方案2】:

    其实

    
        sqlMock.ExpectQuery("SELECT EXISTS \\( SELECT 1 FROM information_schema\\.tables WHERE table_schema = 'public' AND table_name = 'myTable3' \\);").
            WillReturnRows(existsRows)
    

    成功了。

    这里有更多示例: https://chromium.googlesource.com/external/github.com/DATA-DOG/go-sqlmock/+/e36ad8d068217ee8e4df50408476b153e115e3e6/README.md

    我也用过 regex101.com

    线索是它直接期待下一个查询。所以我们知道它根本没有读过这个。我的同事指出了这一点:)

    【讨论】:

      猜你喜欢
      • 2021-01-04
      • 2020-12-12
      • 1970-01-01
      • 2021-01-04
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      相关资源
      最近更新 更多