I have a gRPC service using MySQL and need to clear records after every test case. I try to wrap every test case with a transaction. It works if there's no transaction in my rpc code but fails if there are. And there'll be errors like:
can't start transaction
...
sql: Transaction has already been committed or rolled back
Then I try to use truncate to clear the records but some of the test cases fails randomly.
My code is like(I use gorm):
func foo(db *gorm.DB) {
tx := db.Begin()
// query and insert
tx.Commit()
}
// Use transaction to do database cleanup
func TestFooVersion1() {
testDB := initDB()
tx = testDB.Begin() // setup
foo(testDB)
tx.Rollback() // teardown
}
// Use truncate to do database cleanup
func TestFooVersion2() {
testDB := initDB()
foo(testDB)
truncateTables(testDB) // teardown
}
func truncateTables(db *gorm.DB) {
// exec "TRUNCATE TABLE table;" for every table
}
What's a proper way to test the code using DB(MySQL)? (I don't like mock like go-sqlmock)