Skip to content

Instantly share code, notes, and snippets.

@jarifibrahim
Last active January 27, 2019 08:39
Show Gist options
  • Save jarifibrahim/3650b2dc27a292008efd43c3b346c63a to your computer and use it in GitHub Desktop.
Save jarifibrahim/3650b2dc27a292008efd43c3b346c63a to your computer and use it in GitHub Desktop.
Gist showing a not-so-cool way of deleting test fixtures
package main
import (
"database/sql"
"testing"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type Product struct {
gorm.Model
Code string
Price uint
}
// Error checking intentionally skipped at some places
func TestCleanup(t *testing.T) {
db, _ := gorm.Open("sqlite3", "test.db")
defer db.Close()
// Create
db.Create(&Product{Code: "foo", Price: 100})
db.Create(&Product{Code: "bar", Price: 2000})
db.Create(&Product{Code: "fooBar", Price: 400})
// Perform some test here
// ...
// ...
var product Product
db.Delete(&product, "code = ?", "foo")
db.Delete(&product, "code = ?", "foo")
db.Delete(&product, "code = ?", "foo")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment