Test: WebDAV model

pull/247/head
HFO4 5 years ago
parent 5363b90167
commit 9b3f5b0efd

@ -48,7 +48,6 @@ func GetTasksByStatus(status ...int) []Task {
}
// GetTasksByID 根据ID检索任务
// TODO 测试
func GetTasksByID(id interface{}) (*Task, error) {
task := &Task{}
result := DB.Where("id = ?", id).First(task)

@ -79,3 +79,15 @@ func TestGetTasksByID(t *testing.T) {
asserts.NoError(err)
asserts.EqualValues(1, res.ID)
}
func TestListTasks(t *testing.T) {
asserts := assert.New(t)
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(5))
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(5))
res, total := ListTasks(1, 1, 10, "")
asserts.NoError(mock.ExpectationsWereMet())
asserts.EqualValues(5, total)
asserts.Len(res, 1)
}

@ -82,3 +82,19 @@ func TestUser_WebAuthnName(t *testing.T) {
asserts.Equal("abslant@foxmail.com", name)
}
}
func TestUser_RemoveAuthn(t *testing.T) {
asserts := assert.New(t)
user := User{
Model: gorm.Model{ID: 1},
Authn: `[{"ID":"123","PublicKey":"+4sg1vYcjg/+=","AttestationType":"packed","Authenticator":{"AAGUID":"+lg==","SignCount":0,"CloneWarning":false}}]`,
}
{
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
user.RemoveAuthn("123")
asserts.NoError(mock.ExpectationsWereMet())
}
}

@ -343,10 +343,11 @@ func TestUser_IncreaseStorageWithoutCheck(t *testing.T) {
Model: gorm.Model{ID: 1},
}
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WithArgs(10, 1).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec("UPDATE(.+)").WithArgs(10, sqlmock.AnyArg(), 1).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
user.IncreaseStorageWithoutCheck(10)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Equal(uint64(10), user.Storage)
}
}
@ -449,3 +450,15 @@ func TestUser_SetStatus(t *testing.T) {
asserts.NoError(mock.ExpectationsWereMet())
asserts.Equal(Baned, user.Status)
}
func TestUser_UpdateOptions(t *testing.T) {
asserts := assert.New(t)
user := User{}
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
asserts.NoError(user.UpdateOptions())
asserts.NoError(mock.ExpectationsWereMet())
}

@ -1,11 +1,39 @@
package model
import (
"errors"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
"testing"
)
func TestWebdav_Create(t *testing.T) {
asserts := assert.New(t)
// 成功
{
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
task := Webdav{}
id, err := task.Create()
asserts.NoError(mock.ExpectationsWereMet())
asserts.NoError(err)
asserts.EqualValues(1, id)
}
// 失败
{
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
mock.ExpectRollback()
task := Webdav{}
id, err := task.Create()
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.EqualValues(0, id)
}
}
func TestGetWebdavByPassword(t *testing.T) {
asserts := assert.New(t)
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}))
@ -13,3 +41,20 @@ func TestGetWebdavByPassword(t *testing.T) {
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
}
func TestListWebDAVAccounts(t *testing.T) {
asserts := assert.New(t)
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}))
res := ListWebDAVAccounts(1)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Len(res, 0)
}
func TestDeleteWebDAVAccountByID(t *testing.T) {
asserts := assert.New(t)
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
asserts.NoError(DeleteTagByID(1, 1))
asserts.NoError(mock.ExpectationsWereMet())
}

Loading…
Cancel
Save