database/sql works when run it, but with test it complains about unknown driver

1,103 views
Skip to first unread message

farid sobhany

unread,
Oct 15, 2020, 2:09:58 AM10/15/20
to golang-nuts
I am using database/sql to connect to a Mssql server and it works when I run the application. But when I setup a test file and tried to call the function that makes the connection and store it in an instance, it complains about "Unknown driver \"mssql\" (forgotten import?)"
I am not importing the driver in the main file and it works fine. Why does it complain when running the test funciton?

Gregor Best

unread,
Oct 15, 2020, 3:43:36 AM10/15/20
to farid sobhany, golang-nuts
Maybe you're importing it somewhere else in your main package. In any
case, adding an anonymous import in one of your _test.go-files should do
the trick.
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to golang-nuts...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/e8a43561-551b-44e1-8605-283ce1f064b0n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/e8a43561-551b-44e1-8605-283ce1f064b0n%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
Gregor Best
be...@pferdewetten.de

farid sobhany

unread,
Oct 15, 2020, 9:30:55 AM10/15/20
to golang-nuts
I have searched the whole application for that import, and I couldn't find it. I did import it in the test file and still got the same error.

Joop Kiefte

unread,
Oct 15, 2020, 9:36:11 AM10/15/20
to farid....@gmail.com, golan...@googlegroups.com
Just a hunch, what does your connection string look like?
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/598c16c2-aca1-4cf2-94c7-1733c3cee30fn%40googlegroups.com.

farid sobhany

unread,
Oct 15, 2020, 12:20:44 PM10/15/20
to golang-nuts
The connection works when running the application.
Here is the connection string (changed sensitive info):

Thanks

Joop Kiefte

unread,
Oct 15, 2020, 12:23:10 PM10/15/20
to farid....@gmail.com, golan...@googlegroups.com
I mean the full command that has the connection string in it, most importantly the driver string. Sorry for not putting that as clearly as I could...

q8f5w

farid sobhany

unread,
Oct 15, 2020, 12:51:46 PM10/15/20
to golang-nuts
Understand.
Here is the piece of code that does the connection:
db, err := sql.Open("mssql", dbInfo)
    if err != nil {
        log.Errorf("Could not open database: %s\n %s\n", dbInfo, err.Error())
        return nil, err
    }
    err = db.Ping()
    if err != nil {
        log.Error("db Ping error:", err)
        return nil, err
    }

Thanks

Axel Wagner

unread,
Oct 15, 2020, 1:34:44 PM10/15/20
to farid sobhany, golang-nuts
FWIW, the import of the driver doesn't have to be in the main-package itself, it could also be in one of its transitive imports (which isn't imported transitively by the test). Is there an `mssql` module in your `go.sum`? If so, you could use `go mod why` to find out how it's imported.

farid sobhany

unread,
Oct 15, 2020, 4:28:40 PM10/15/20
to golang-nuts
Thanks for this explanation. I have looked into the go.sum and found an mssql driver in there which was used in another package two levels above where I was testing. The driver that was needed was mssql not mysql; which was the reason why my first import of the mysql driver in the test file didn't work. I have included the driver in my test file and it works.
But the question remains.. I was calling the same functions to open a connection in my test file.. Why couldn't that function use the dependencies same way it was using it when not testing?

Thanks again Axel

Joop Kiefte

unread,
Oct 15, 2020, 5:07:52 PM10/15/20
to farid....@gmail.com, golan...@googlegroups.com
You basically have to see the test file as an alternative package main, which can but (generally?) does not have to have access to the contents of the main package, as such you don't have access to the drivers by default...

q8sct

farid sobhany

unread,
Oct 16, 2020, 5:51:22 AM10/16/20
to Joop Kiefte, golan...@googlegroups.com
I was thinking, since I am calling the same function from package main, it will have access to its dependences. I guess that function was using the driver through another controller it was called in.
The go mod why really helped.

Reply all
Reply to author
Forward
0 new messages