Skip to content

Commit

Permalink
simply panic if truncate error
Browse files Browse the repository at this point in the history
  • Loading branch information
khaiql committed Nov 2, 2017
1 parent ea8f278 commit c01fff1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
8 changes: 3 additions & 5 deletions dbcleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type DbCleaner interface {
Acquire(tables ...string)

// Clean calls Truncate the tables
Clean(tables ...string) error
Clean(tables ...string)

// Close calls corresponding method on dbEngine to release connection to db
Close() error
Expand Down Expand Up @@ -56,7 +56,7 @@ func (c *cleanerImpl) Acquire(tables ...string) {
}
}

func (c *cleanerImpl) Clean(tables ...string) error {
func (c *cleanerImpl) Clean(tables ...string) {
for _, table := range tables {
if c.locks[table] != nil {
c.locks[table].RUnlock()
Expand All @@ -65,11 +65,9 @@ func (c *cleanerImpl) Clean(tables ...string) error {
}

if err := c.dbEngine.Truncate(table); err != nil {
return err
panic(err)
}
}

return nil
}

func (c *cleanerImpl) Close() error {
Expand Down
17 changes: 8 additions & 9 deletions dbcleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ func TestClean(t *testing.T) {
t.Run("TestLockAndThenUnlock", func(t *testing.T) {
tbName := "lock_table"
cleaner.Acquire(tbName)
err := cleaner.Clean(tbName)
if err != nil {
t.Error(err.Error())
}
cleaner.Clean(tbName)

mockEngine.AssertCalled(t, "Truncate", tbName)
})
Expand All @@ -40,14 +37,16 @@ func TestClean(t *testing.T) {
})

t.Run("TestTruncateError", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("The code did not panic")
}
}()

errorTruncateMock := &engine.MockEngine{}
errorTruncateMock.On("Truncate", mock.AnythingOfType("string")).Return(errors.New("Truncate error"))

cleaner.SetEngine(errorTruncateMock)
err := cleaner.Clean("error_table")

if err.Error() != "Truncate error" {
t.Error("Error mismatch")
}
cleaner.Clean("error_table")
})
}

0 comments on commit c01fff1

Please sign in to comment.