1

My structure is the following:

  1. In my db.go, I initialize my database
package database
type Database struct {
    Dbpool *pgxpool.Pool
}
var Db Database
func InitDb() {
    dbpool, err := pgxpool.New(...)
    if err != nil {...} // Check for creating connection
    Db = Database{Dbpool: dbpool}
    greeting, err := Db.GetHelloWorld()
    if err != nil {...} // Check for entering db
}
  1. In my queries.go, I create my handlers like my GetHelloWorldhandler for testing purposes
package database

func (db *Database) GetHelloWorld() (string, error) {
    var greeting string
    err := db.Dbpool.QueryRow(context.Background(), "select 'Hello, world!'").Scan(&greeting)
    if err != nil {...} // Check for error in selecting Hello World
    return greeting, err
}
  1. So far everything works as expected. Now in my queries_test.go, I like to test my handlers via dockertest in a separate database.
package database

var TestDB Database
var dbpool *pgxpool.Pool
 
func TestMain(m *testing.M) {
    // set up docker container, database and its connection
    dbpool, err = pgxpool.New(...)
    // once everything is set up I want to initialize the database like in db.go
    TestDB = Database{Dbpool: dbpool}
}
func Test_GetHelloWorld(t *testing.T) {
    var greeting string
    greeting, err := TestDB.GetHelloWorld()
}

Everything works in my queries_test.go if I use the dbpool.QueryRow(context.Background(), "select 'Hello, world!'").Scan(&greeting) command in my test as it is written down in my handlers but this is not really the sense of testing handlers...

If I run go test ./queries_test.go like shown above, I have no compile error but inside my testing container I get the error undefined: Database. If I try importing TestDB from db.go like

import app/database 
var TestDB database.Database

I receive the import cycle not allowed in test, which I completely understand is not the way of doing it.
So far I assume a third and maybe forth file with an interface might do the job like suggested here but does it really have to be that complicated?
I feel like there should be a simpler way...
Maybe anyone can help me out?

Leon
  • 143
  • 2
  • 9
  • What do you mean you “run queries_test.go”? You don’t run files, can you execute `go test`? – JimB Jul 11 '23 at 10:37
  • Yes thats what I meant by saying `run queries_test.go`. I corrected it already – Leon Jul 11 '23 at 10:42
  • You don’t run individual files, use `go test` without any filename arguments. – JimB Jul 11 '23 at 10:44
  • See [`go test`](https://pkg.go.dev/cmd/go#hdr-Test_packages) documentation and the documentation relating to the [`[packages]`](https://pkg.go.dev/cmd/go#hdr-Package_lists_and_patterns) argument: *"As a special case, if the package list is a list of .go files from a single directory, the command is applied to a single synthesized package __made up of exactly those files__, ignoring any build constraints in those files __and ignoring any other files in the directory__."* – mkopriva Jul 11 '23 at 11:04
  • And the above goes for all top level `go` commands, not just `test`, don’t use filename arguments – JimB Jul 11 '23 at 11:13
  • Oh wow never thought this would be my actual problem! Thanks a lot for explaining! I could not run `go test .` since my `main_test.go` and my `queries_test.go` were in conflict to each other, which was the reason I "isolated" the file by executing only one file. [This post](https://stackoverflow.com/a/16936314/19932351) helped me to figure out I had to run `go test queries_test.go queries.go db.go` and everything was working fine. Now I start cleaning up my `main_test.go` to make everything work as expected and finally be able to run `go test .` – Leon Jul 11 '23 at 12:32

0 Answers0