You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudreve/models/scripts/reset_test.go

51 lines
1.3 KiB

package scripts
import (
"context"
"errors"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
"testing"
)
func TestResetAdminPassword_Run(t *testing.T) {
asserts := assert.New(t)
script := ResetAdminPassword(0)
// 初始用户不存在
{
mock.ExpectQuery("SELECT(.+)users(.+)").
WillReturnRows(sqlmock.NewRows([]string{"id", "email", "storage"}))
asserts.Panics(func() {
script.Run(context.Background())
})
asserts.NoError(mock.ExpectationsWereMet())
}
// 密码更新失败
{
mock.ExpectQuery("SELECT(.+)users(.+)").
WillReturnRows(sqlmock.NewRows([]string{"id", "email", "storage"}).AddRow(1, "a@a.com", 10))
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnError(errors.New("error"))
mock.ExpectRollback()
asserts.Panics(func() {
script.Run(context.Background())
})
asserts.NoError(mock.ExpectationsWereMet())
}
// 成功
{
mock.ExpectQuery("SELECT(.+)users(.+)").
WillReturnRows(sqlmock.NewRows([]string{"id", "email", "storage"}).AddRow(1, "a@a.com", 10))
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
asserts.NotPanics(func() {
script.Run(context.Background())
})
asserts.NoError(mock.ExpectationsWereMet())
}
}