test(source link): add unit test
This commit is contained in:
parent
1c25232b06
commit
7dda81368d
4 changed files with 150 additions and 16 deletions
57
middleware/file_test.go
Normal file
57
middleware/file_test.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidateSourceLink(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
rec := httptest.NewRecorder()
|
||||
testFunc := ValidateSourceLink()
|
||||
|
||||
// ID 不存在
|
||||
{
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
testFunc(c)
|
||||
a.True(c.IsAborted())
|
||||
}
|
||||
|
||||
// SourceLink 不存在
|
||||
{
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Set("object_id", 1)
|
||||
mock.ExpectQuery("SELECT(.+)source_links(.+)").WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
testFunc(c)
|
||||
a.True(c.IsAborted())
|
||||
a.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// 原文件不存在
|
||||
{
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Set("object_id", 1)
|
||||
mock.ExpectQuery("SELECT(.+)source_links(.+)").WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WithArgs(0).WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
testFunc(c)
|
||||
a.True(c.IsAborted())
|
||||
a.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// 成功
|
||||
{
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Set("object_id", 1)
|
||||
mock.ExpectQuery("SELECT(.+)source_links(.+)").WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"id", "file_id"}).AddRow(1, 2))
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WithArgs(2).WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(2))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)source_links").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
testFunc(c)
|
||||
a.False(c.IsAborted())
|
||||
a.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
}
|
|
@ -611,3 +611,44 @@ func TestGetFilesByKeywords(t *testing.T) {
|
|||
asserts.Len(res, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFile_CreateOrGetSourceLink(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
file := &File{}
|
||||
file.ID = 1
|
||||
|
||||
// 已存在,返回老的 SourceLink
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)source_links(.+)").WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(2))
|
||||
res, err := file.CreateOrGetSourceLink()
|
||||
a.NoError(err)
|
||||
a.EqualValues(2, res.ID)
|
||||
a.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// 不存在,插入失败
|
||||
{
|
||||
expectedErr := errors.New("error")
|
||||
mock.ExpectQuery("SELECT(.+)source_links(.+)").WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)source_links(.+)").WillReturnError(expectedErr)
|
||||
mock.ExpectRollback()
|
||||
res, err := file.CreateOrGetSourceLink()
|
||||
a.Nil(res)
|
||||
a.ErrorIs(err, expectedErr)
|
||||
a.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// 成功
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)source_links(.+)").WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)source_links(.+)").WillReturnResult(sqlmock.NewResult(2, 1))
|
||||
mock.ExpectCommit()
|
||||
res, err := file.CreateOrGetSourceLink()
|
||||
a.NoError(err)
|
||||
a.EqualValues(2, res.ID)
|
||||
a.EqualValues(file.ID, res.File.ID)
|
||||
a.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
}
|
||||
|
|
52
models/source_link_test.go
Normal file
52
models/source_link_test.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSourceLink_Link(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
s := &SourceLink{}
|
||||
s.ID = 1
|
||||
|
||||
// 失败
|
||||
{
|
||||
s.File.Name = string([]byte{0x7f})
|
||||
res, err := s.Link()
|
||||
a.Error(err)
|
||||
a.Empty(res)
|
||||
}
|
||||
|
||||
// 成功
|
||||
{
|
||||
s.File.Name = "filename"
|
||||
res, err := s.Link()
|
||||
a.NoError(err)
|
||||
a.Contains(res, s.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSourceLinkByID(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
mock.ExpectQuery("SELECT(.+)source_links(.+)").WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"id", "file_id"}).AddRow(1, 2))
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WithArgs(2).WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(2))
|
||||
|
||||
res, err := GetSourceLinkByID(1)
|
||||
a.NoError(err)
|
||||
a.NotNil(res)
|
||||
a.EqualValues(2, res.File.ID)
|
||||
a.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestSourceLink_Downloaded(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
s := &SourceLink{}
|
||||
s.ID = 1
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)source_links(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
s.Downloaded()
|
||||
a.NoError(mock.ExpectationsWereMet())
|
||||
}
|
|
@ -3,7 +3,6 @@ package onedrive
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/auth"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/mq"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||
"github.com/jinzhu/gorm"
|
||||
|
@ -161,21 +160,6 @@ func TestDriver_Source(t *testing.T) {
|
|||
asserts.NoError(err)
|
||||
asserts.Equal("123321", res)
|
||||
}
|
||||
|
||||
// 成功 永久直链
|
||||
{
|
||||
file := model.File{}
|
||||
file.ID = 1
|
||||
file.Name = "123.jpg"
|
||||
file.UpdatedAt = time.Now()
|
||||
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, file)
|
||||
handler.Client.Credential.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()
|
||||
auth.General = auth.HMACAuth{}
|
||||
handler.Client.Credential.AccessToken = "1"
|
||||
res, err := handler.Source(ctx, "123.jpg", url.URL{}, 0, true, 0)
|
||||
asserts.NoError(err)
|
||||
asserts.Contains(res, "/api/v3/file/source/1/123.jpg?sign")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriver_List(t *testing.T) {
|
||||
|
|
Loading…
Add table
Reference in a new issue