A go program connects to a database using a connection string. Currently that connection string is stored in an environment variable.
My challenge is to write a an automated test that exercises the program against a test database.
I thought I would set the connection string environment variable in the init() function of the test, but the main program's init() function is called before the test init(), so this does not work.
The workaround seems to be to default the connection string to the test database. If no environment variable is defined, use test; if it is defined (production), then use that instead.
Is there an alternative? Is there a better approach?
My current solution is to make the connection an uninitialised global. The function checks to see if the connection is nil - if so, it initialises it. The test has a setup() function which sets the connection to the integration test database - an alternative would be for the test to just set the environment variable.