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

Operations on go-sqlmock.Driver fail with: expected a connection to be available, but it is not #267

Open
fho opened this issue Aug 16, 2021 · 4 comments

Comments

@fho
Copy link

fho commented Aug 16, 2021

Hello,

I'm working on a package that wraps an sql.Driver and adds functionality.
To test it, I'm trying to use go-sqlmock.
I'm running into the issue that if I wrap the go-sqlmock Driver, all operations fail with the error:
expected a connection to be available, but it is not

I'm accessing the driver via:

db, _, _ := sqlmock.New()
db.Driver()

This can be reproduced e.g. via sqlmw:

package dddbmock_test

import (
	"database/sql"
	"testing"

	"github.com/DATA-DOG/go-sqlmock"
	"github.com/ngrok/sqlmw"
)

func TestMWWrap(t *testing.T) {
	db, _, err := sqlmock.New()
	if err != nil {
		t.Fatal("sqlmock new failed:", err)
	}

	sql.Register(
		"mwdriver",
		sqlmw.Driver(db.Driver(), &sqlmw.NullInterceptor{}),
	)

	db, err = sql.Open("mydriver", "")
	if err != nil {
		t.Fatal("opening driver failed:", err)
	}

	if err := db.Ping(); err != nil {
		t.Error("ping failed", err)
	}
}

Without using other external packages and mimicking the wrapping behavior instead it can be reproduced like:

package dddbmock_test

import (
	"database/sql"
	"testing"

	"github.com/DATA-DOG/go-sqlmock"
)

func TestRegister(t *testing.T) {
	db, _, err := sqlmock.New(sqlmock.MonitorPingsOption(true))
	if err != nil {
		t.Fatal("sqlmock new failed:", err)
	}

	sql.Register("mydriver", db.Driver())

	db, err = sql.Open("mydriver", "")
	if err != nil {
		t.Fatal("opening driver failed:", err)
	}

	if err := db.Ping(); err != nil {
		t.Error("ping failed", err)
	}

	if _, err := db.Exec(""); err != nil {
		t.Error("exec failed", err)
	}
}

The comment #83 (comment) seems to be about the same issue.
I guess it's not intended to be used like that, but it would be awesome to support operations on the go-sqlmock Driver.

@l3pp4rd
Copy link
Member

l3pp4rd commented Aug 18, 2021

Hi, maybe you are willing to contribute this patch? Haven’t used golang for more than a year, would be difficult to find a willing to

@fho
Copy link
Author

fho commented Aug 18, 2021

@l3pp4rd depends on the effort :-). I ended up writing my own simple mock instead, that works okayish for my current usecase.
Do you maybe have an idea what the cause it how it could be fixed?

@dolmen
Copy link
Contributor

dolmen commented Apr 22, 2022

The first code has a typo in the driver name: mwdriver vs mydriver.

@dolmen
Copy link
Contributor

dolmen commented Apr 22, 2022

Here is a hack (unsupported): use the internal DSN:

db, err = sql.Open("mydriver", "sqlmock_db_0")

Example on the Go Playground

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

3 participants