github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/models/source_link_test.go (about) 1 package model 2 3 import ( 4 "github.com/DATA-DOG/go-sqlmock" 5 "github.com/stretchr/testify/assert" 6 "testing" 7 ) 8 9 func TestSourceLink_Link(t *testing.T) { 10 a := assert.New(t) 11 s := &SourceLink{} 12 s.ID = 1 13 14 // 失败 15 { 16 s.File.Name = string([]byte{0x7f}) 17 res, err := s.Link() 18 a.Error(err) 19 a.Empty(res) 20 } 21 22 // 成功 23 { 24 s.File.Name = "filename" 25 res, err := s.Link() 26 a.NoError(err) 27 a.Contains(res, s.Name) 28 } 29 } 30 31 func TestGetSourceLinkByID(t *testing.T) { 32 a := assert.New(t) 33 mock.ExpectQuery("SELECT(.+)source_links(.+)").WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"id", "file_id"}).AddRow(1, 2)) 34 mock.ExpectQuery("SELECT(.+)files(.+)").WithArgs(2).WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(2)) 35 36 res, err := GetSourceLinkByID(1) 37 a.NoError(err) 38 a.NotNil(res) 39 a.EqualValues(2, res.File.ID) 40 a.NoError(mock.ExpectationsWereMet()) 41 } 42 43 func TestSourceLink_Downloaded(t *testing.T) { 44 a := assert.New(t) 45 s := &SourceLink{} 46 s.ID = 1 47 mock.ExpectBegin() 48 mock.ExpectExec("UPDATE(.+)source_links(.+)").WillReturnResult(sqlmock.NewResult(1, 1)) 49 mock.ExpectCommit() 50 s.Downloaded() 51 a.NoError(mock.ExpectationsWereMet()) 52 }