Test: share/storage/user model
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -37,3 +38,49 @@ func TestUser_GetAvailablePackSize(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestStoragePack_Create(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
pack := &StoragePack{}
|
||||
|
||||
// 成功
|
||||
{
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
id, err := pack.Create()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Equal(uint(1), id)
|
||||
asserts.NoError(err)
|
||||
}
|
||||
|
||||
// 失败
|
||||
{
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
|
||||
mock.ExpectRollback()
|
||||
id, err := pack.Create()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Equal(uint(0), id)
|
||||
asserts.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetExpiredStoragePack(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
res := GetExpiredStoragePack()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Len(res, 0)
|
||||
}
|
||||
|
||||
func TestStoragePack_Delete(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
pack := &StoragePack{}
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
asserts.NoError(pack.Delete())
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user