From 9b3f5b0efd5537c8ec021b9d72851f71939c6733 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Fri, 21 Feb 2020 11:32:59 +0800 Subject: [PATCH] Test: WebDAV model --- models/task.go | 1 - models/task_test.go | 12 +++++++++++ models/user_authn_test.go | 16 ++++++++++++++ models/user_test.go | 15 ++++++++++++- models/webdav_test.go | 45 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 2 deletions(-) diff --git a/models/task.go b/models/task.go index 3751c50..01ec5e5 100644 --- a/models/task.go +++ b/models/task.go @@ -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) diff --git a/models/task_test.go b/models/task_test.go index 32619ed..167819a 100644 --- a/models/task_test.go +++ b/models/task_test.go @@ -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) +} diff --git a/models/user_authn_test.go b/models/user_authn_test.go index 9293cbc..08a8ce1 100644 --- a/models/user_authn_test.go +++ b/models/user_authn_test.go @@ -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()) + } +} diff --git a/models/user_test.go b/models/user_test.go index 981eab3..7ac4f17 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -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()) +} diff --git a/models/webdav_test.go b/models/webdav_test.go index 2b5dc6f..4570c5c 100644 --- a/models/webdav_test.go +++ b/models/webdav_test.go @@ -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()) +}