Your Question
Hi everyone,
I use testify and gorm in my project. I've created a testsuite which can be seen below. I've debugged the code many times and I am pretty sure that SetupTest and TearDownTest works as expected. But RollbackTo function does not work as expected. The inserts are not reverted after the tests.
func (s *ToDoTestSuite) SetupSuite() {
s.config = config.Init()
s.config.Database.Name = fmt.Sprintf("test_%s", s.config.Database.Name)
database.SetupTestDatabase(s.config.Database)
}
func (s *ToDoTestSuite) TearDownSuite() {
db, _ := s.db.DBConn.DB()
err := db.Close()
if err != nil {
panic(err)
}
database.DropTestDatabase(s.config.Database)
}
func (s *ToDoTestSuite) SetupTest() {
s.db = database.New(&s.config.Database)
s.tx = s.db.DBConn.Begin()
s.tx.SavePoint("sp")
}
func (s *ToDoTestSuite) TearDownTest() {
s.tx.RollbackTo("sp") // Did NOT WORK
s.tx.Commit()
db, _ := s.db.DBConn.DB()
_ = db.Close()
}
func TestTestSuite(t *testing.T) {
suite.Run(t, new(ToDoTestSuite))
}
The document you expected this should be explained
https://gorm.io/docs/transactions.html#SavePoint-RollbackTo
Expected answer
By the way, my repository can be checked here for those want to get more information.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: ismailbayram
Resolved by using s.tx instead of s.db