Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExpectQuery in prepared statement fails if the argument is "2" ( or, putatively greater than 1) #51

Closed
luistm opened this issue Nov 2, 2016 · 3 comments

Comments

@luistm
Copy link

luistm commented Nov 2, 2016

Hi ,

The following test will fail:

package main

import (
	"database/sql"
	"fmt"
)

func testFunction(db *sql.DB) {
	stmt, err := db.Prepare("SELECT id FROM test WHERE id=$1")

	if err != nil {
		fmt.Println(err)
	}

	stmt.QueryRow("2")

	stmt.Close()

}

func main() {}

package main

import (
	"testing"

	"github.com/stretchr/testify/assert"
	sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
)

func TestPrepare(t *testing.T) {
	db, mock, mErr := sqlmock.New()
	assert.NoError(t, mErr)
	defer db.Close()

	mock.ExpectPrepare("^SELECT id FROM test WHERE id=\\$1$")
	mock.ExpectQuery("2")

	testFunction(db)

	if err := mock.ExpectationsWereMet(); err != nil {
		t.Errorf("there were unfulfilled expections: %s", err)
	}

}

If i replace the code with

mock.ExpectQuery("1")

and

stmt.QueryRow("1")

everything runs as expected.

I'm i doing something wrong?

Thanks

@l3pp4rd
Copy link
Member

l3pp4rd commented Nov 2, 2016

Yes, you are not doing it right in this case. The previous issue was valid, this isn't.

There are two ways, one to expect the query on that statement (which is more straightforward)

package main

import (
    "testing"

    "github.com/stretchr/testify/assert"
    sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
)

func TestPrepare(t *testing.T) {
    db, mock, mErr := sqlmock.New()
    assert.NoError(t, mErr)
    defer db.Close()

    mock.ExpectPrepare("^SELECT id FROM test WHERE id=\\$1$").ExpectQuery().WithArgs("2")

    testFunction(db)

    if err := mock.ExpectationsWereMet(); err != nil {
        t.Errorf("there were unfulfilled expections: %s", err)
    }
}

or make a next expectation for query, by repeating the query string

package main

import (
    "testing"

    "github.com/stretchr/testify/assert"
    sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
)

func TestPrepare(t *testing.T) {
    db, mock, mErr := sqlmock.New()
    assert.NoError(t, mErr)
    defer db.Close()

        mock.ExpectPrepare("^SELECT id FROM test WHERE id=\\$1$")
    mock.ExpectQuery("^SELECT id FROM test WHERE id=\\$1$").WithArgs("2")

    testFunction(db)

    if err := mock.ExpectationsWereMet(); err != nil {
        t.Errorf("there were unfulfilled expections: %s", err)
    }
}

@l3pp4rd
Copy link
Member

l3pp4rd commented Nov 2, 2016

see tests for more details or have a look in godoc

@luistm
Copy link
Author

luistm commented Nov 3, 2016

Works like a charm! 👍
Thanks for enlightening me.

@luistm luistm closed this as completed Nov 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants