13

I want to create a database driven application using Golang. I am trying to do it TDD way. When I try to test methods that make Sql queries, What all are the packages available ?

  • I don't want to connect to the default database that I use for development. I can write code to take up another test database while running a test, but is there any go library that already does it.

  • Is there any library that does db tests without connecting to database at all ?

What is the standard way to do database test with golang ?

3

2 Answers 2

17

I had a similar question not long ago when refactoring some of my own tests, and there's a couple of ways you can do it:

a) Provide an exported type and an Open or Connect function that returns it - e.g.

type DB struct {
    db *sql.DB
}

// Using http://jmoiron.github.io/sqlx/ for this example, but
// it has the same interface as database/sql
func Open(opts *Options) (*DB, error) {
    db, err := sqlx.Connect(opts.Driver, fmt.Sprintf("host=%s user=%s dbname=%s sslmode=%s", opts.Host, opts.User, opts.Name, opts.SSL))
    if err != nil {
        return nil, err
    }

    return &DB{db}, nil
}

... and then each of your tests, write setup & teardown functions that return an instance of *DB that you define your database functions on (as methods - i.e. func (db *DB) GetUser(user *User) (bool, error)):

// Setup the test environment.
func setup() (*DB, error) {
    err := withTestDB()
    if err != nil {
        return nil, err
    }

    // testOptions is a global in this case, but you could easily
    // create one per-test
    db, err := Open(testOptions)
    if err != nil {
        return nil, err
    }

    // Loads our test schema
    db.MustLoad()
    return db, nil
}

// Create our test database.
func withTestDB() error {
    db, err := open()
    if err != nil {
        return err
    }
    defer db.Close()

    _, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s;", testOptions.Name))
    if err != nil {
        return err
    }

    return nil
}

Note that this is somewhat "integration" testing, but I strongly prefer to test against a "real" database since mocking the interface won't help you catch issues with your queries/query syntax.

b) The alternative, although less extensible on the application side, is to have a global db *sql.DB variable that you initialise in init() within your tests—since tests have no guaranteed order you'll need to use init()—and then run your tests from there. i.e.

var db *sql.DB

func init() {
    var err error
    // Note the = and *not* the assignment - we don't want to shadow our global
    db, err = sqlx.Connect(...)
    if err != nil {
        ...
    }

    err := db.loadTestSchema
    // etc.
}

func TestGetUser(t *testing.T) {
   user := User{}
   exists, err := db.GetUser(user)
   ...
}

You can find some practical examples in drone.io's GitHub repo, and I'd also recommend this article on structuring Go applications (especially the DB stuff).

5
  • This is what I did at the end - github.com/sumitasok/go-test-db . Please give me your thoughts ob this. I will add a Readme soon. Aug 11, 2014 at 7:42
  • 1
    BTW, the explanation you gave way very precise. Aug 11, 2014 at 7:43
  • 2
    That init func can be replaced with TestMain() I think.
    – alexsmn
    Apr 24, 2016 at 20:34
  • 1
    @alexsmn Good point. TestMain didn't exist when I wrote this originally, but is the preferred way to do test setup now.
    – elithrar
    Apr 24, 2016 at 21:19
  • I'm probably missing something simple here, but any input on how I can run migrations with option (a)? I have all the migrations defined in .sql files in a db directory within the project...
    – RocketGuy3
    Apr 26, 2017 at 15:12
1

I use a global variable to store the data source (or connection string) of current database and set to different value in test function. Since there is only one database I need to operate so I choose the easiest way.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.