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.


On Thu, Oct 15, 2020, 2:07 PM Joop Kiefte <iko...@gmail.com> wrote:

> 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...
>
>   *Joop Kiefte* - Chat @ Spike
> <https://spikenow.com/r/a/?ref=spike-organic-signature&_ts=q8sct> [image:
> q8sct]
>
> On October 15, 2020 at 20:28 GMT, farid sobhany <farid.sobh...@gmail.com>
> wrote:
>
>
> 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
>
> On Thursday, October 15, 2020 at 10:34:44 AM UTC-7
> axel.wa...@googlemail.com wrote:
>
>> 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.
>>
>> On Thu, Oct 15, 2020 at 6:51 PM farid sobhany <farid....@gmail.com>
>> wrote:
>>
>>> Understand.
>>> Here is the piece of code that does the connection:
>>>  // dbInfo = sqlserver://gh:password!655@localhost
>>> :7867?database=test_database
>>> 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
>>>
>>> On Thursday, October 15, 2020 at 9:23:10 AM UTC-7 iko...@gmail.com
>>> wrote:
>>>
>>>> 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...
>>>>
>>>>   *Joop Kiefte* - Chat @ Spike
>>>> <https://spikenow.com/r/a/?ref=spike-organic-signature&_ts=q8f5w>
>>>> [image: q8f5w]
>>>>
>>>> On October 15, 2020 at 16:19 GMT, farid sobhany <farid....@gmail.com>
>>>> wrote:
>>>>
>>>>
>>>> The connection works when running the application.
>>>> Here is the connection string (changed sensitive info):
>>>> sqlserver://gh:password!655@localhost:7867?database=test_database
>>>>
>>>> Thanks
>>>>
>>>> On Thursday, October 15, 2020 at 6:36:11 AM UTC-7 iko...@gmail.com
>>>> wrote:
>>>>
>>>>> Just a hunch, what does your connection string look like?
>>>>>
>>>>>   *Joop Kiefte* - Chat @ Spike
>>>>> <https://spikenow.com/r/a/?ref=spike-organic-signature&_ts=q87fr> [image:
>>>>> q87fr]
>>>>>
>>>>> On October 15, 2020 at 13:30 GMT, farid sobhany <farid....@gmail.com>
>>>>> wrote:
>>>>>
>>>>>
>>>>> 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.
>>>>> On Thursday, October 15, 2020 at 12:43:36 AM UTC-7
>>>>> be...@pferdewetten.de wrote:
>>>>>
>>>>>> 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.
>>>>>>
>>>>>> On 15.10.20 03:53, farid sobhany wrote:
>>>>>> > 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?
>>>>>> >
>>>>>> > --
>>>>>> > 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
>>>>>>
>>>>> --
>>>>> 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.
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/golang-nuts/598c16c2-aca1-4cf2-94c7-1733c3cee30fn%40googlegroups.com
>>>>> <https://groups.google.com/d/msgid/golang-nuts/598c16c2-aca1-4cf2-94c7-1733c3cee30fn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>>> --
>>>> 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.
>>>>
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/golang-nuts/a208b607-8d0f-45f4-9308-44a5c91657dfn%40googlegroups.com
>>>> <https://groups.google.com/d/msgid/golang-nuts/a208b607-8d0f-45f4-9308-44a5c91657dfn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>>> --
>>> 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.
>>>
>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/golang-nuts/791cdfd1-2066-42d8-9b3d-72f4a1ee00ffn%40googlegroups.com
>>> <https://groups.google.com/d/msgid/golang-nuts/791cdfd1-2066-42d8-9b3d-72f4a1ee00ffn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/584583b8-e041-484c-8862-903ca5061b08n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/584583b8-e041-484c-8862-903ca5061b08n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEXtMZUSUboj94Pv-EXRpJet0eTWOJ8zqFot%3DXU7y_jZaYrA3A%40mail.gmail.com.

Reply via email to