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?

6
  • What do you mean you “run queries_test.go”? You don’t run files, can you execute go test? Commented Jul 11, 2023 at 10:37
  • Yes thats what I meant by saying run queries_test.go. I corrected it already Commented Jul 11, 2023 at 10:42
  • You don’t run individual files, use go test without any filename arguments. Commented Jul 11, 2023 at 10:44
  • See go test documentation and the documentation relating to the [packages] 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." Commented Jul 11, 2023 at 11:04
  • And the above goes for all top level go commands, not just test, don’t use filename arguments Commented Jul 11, 2023 at 11:13

1 Answer 1

0

As shown in the comments below, 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 helped me to figure out I had to run go test queries_test.go queries.go db.go and everything was working fine.

The main solution of course was to solve the dependency conflict between both files.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.