vendredi 27 septembre 2019

Call to Query with args [], was not expected in go-sqlmock for compound SQL queries

I was able to successfully mock the query to select from one table like so:

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

But I was not able to mock the following query that checks for the existence of the table in my postgres db:

SELECT EXISTS
        ( SELECT 1
        FROM information_schema.tables
        WHERE table_schema = 'public'
           AND table_name = 'myTable3' );

The combination of:

    existsRows := sqlmock.NewRows([]string{"exists"}).
        AddRow(true)

AND

    slMock.ExpectQuery("^SELECT EXISTS").
        WillReturnRows(existsRows)

I tried mocking SELECT 1 as well but I get the exact same error:

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......

Packages I am using:

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

Any ideas or pointers are appreciated. I couldn't find relevant examples on the internet

Aucun commentaire:

Enregistrer un commentaire