Oussama Ben Ghorbel
Oussama Ben Ghorbel

Reputation: 2129

What's the best way to delete all database records used for a Test suite?

I have a test suite that pollute my database using a seed read from a YAML file.

I'm wondering is there a way to clean my database (delete all records used for the test suite) after running my tests.

// Open db and returns pointer and closer func
func prepareMySQLDB(t *testing.T) (db *sql.DB, closer func() error) {
    db, err := sql.Open("mysql", "user:pass@/database")
    if err != nil {
        t.Fatalf("open mysql connection: %s", err)
    }
    return db, db.Close
}
// Pollute my database
func polluteDb(db *sql.DB, t *testing.T) {
    seed, err := os.Open("seed.yml")
    if err != nil {
        t.Fatalf("failed to open seed file: %s", err)
    }
    defer seed.Close()
    p := polluter.New(polluter.MySQLEngine(db))
    if err := p.Pollute(seed); err != nil {
        t.Fatalf("failed to pollute: %s", err)
    }
}

func TestAllUsers(t *testing.T) {
    t.Parallel()

    db, closeDb := prepareMySQLDB(t)
    defer closeDb()

    polluteDb(db, t)

    users, err := AllUsersD(db)
    if err != nil {
        t.Fatal("AllUsers() failed")
    }

    got := users[0].Email
    if got != "[email protected]" {
        t.Errorf("AllUsers().Email = %s; want [email protected]", got)
    }

    got1 := len(users)
    if got1 != 1 {
        t.Errorf("len(AllUsers()) = %d; want 1", got1)
    }
}


// Test I'm interested in
func TestAddUser(t *testing.T) {
    t.Parallel()

    db, closeDb := prepareMySQLDB(t)
    defer closeDb()

    polluteDb(db, t)

    user, err := AddUser(...)
    if err != nil {
        t.Fatal("AddUser() failed")
    }

    //how can I clean my database after this? 
}

Should I retrieve the last ID inserted in TestAddUser() and just delete that line manually or there's any other way to save my database state and retrieve it after?

As I said I'm new to Go so any other comments on my code or what so ever are strongly appreciated.

Upvotes: 0

Views: 1327

Answers (1)

Jonathan Hall
Jonathan Hall

Reputation: 79744

The best way is usually to use a transaction, then ROLLBACK, so they are never committed in the first place.

The github.com/DATA-DOG/go-txdb package can help a lot with that.


Final code:

import (
    "database/sql"
    "os"
    "testing"

    txdb "github.com/DATA-DOG/go-txdb"

    "github.com/romanyx/polluter"
)

//mostly sql tests
func init() {
    txdb.Register("txdb", "mysql", "root:root@/betell_rest")
}

func TestAddUser(t *testing.T) {
    db, err := sql.Open("txdb", "root:root@/betell_rest")
    if err != nil {
        t.Fatal(err)
    }
    defer db.Close()

    users, _ := AllUsers(db)
    userscount := len(users)
    err = AddUser(db, "[email protected]", "pass")
    if err != nil {
        t.Fatal("AddUser() failed")
    }

    users, _ = AllUsers(db)
    if (userscount + 1) != len(users) {
        t.Fatal("AddUser() failed to write in database")
    }
}

Note: Also you can pass db into your polluter so you don't affect your database at all.

Upvotes: 1

Related Questions