forked from cloudreve/Cloudreve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webdav_test.go
60 lines (54 loc) · 1.48 KB
/
webdav_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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"}))
_, err := GetWebdavByPassword("e", 1)
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()
DeleteWebDAVAccountByID(1, 1)
asserts.NoError(mock.ExpectationsWereMet())
}