github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/aria2/aria2_test.go (about) 1 package aria2 2 3 import ( 4 "database/sql" 5 "github.com/cloudreve/Cloudreve/v3/pkg/mocks" 6 "github.com/cloudreve/Cloudreve/v3/pkg/mq" 7 "github.com/stretchr/testify/assert" 8 testMock "github.com/stretchr/testify/mock" 9 "testing" 10 11 "github.com/DATA-DOG/go-sqlmock" 12 model "github.com/cloudreve/Cloudreve/v3/models" 13 "github.com/jinzhu/gorm" 14 ) 15 16 var mock sqlmock.Sqlmock 17 18 // TestMain 初始化数据库Mock 19 func TestMain(m *testing.M) { 20 var db *sql.DB 21 var err error 22 db, mock, err = sqlmock.New() 23 if err != nil { 24 panic("An error was not expected when opening a stub database connection") 25 } 26 model.DB, _ = gorm.Open("mysql", db) 27 defer db.Close() 28 m.Run() 29 } 30 31 func TestInit(t *testing.T) { 32 a := assert.New(t) 33 mockPool := &mocks.NodePoolMock{} 34 mockPool.On("GetNodeByID", testMock.Anything).Return(nil) 35 mockQueue := mq.NewMQ() 36 37 mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) 38 Init(false, mockPool, mockQueue) 39 a.NoError(mock.ExpectationsWereMet()) 40 mockPool.AssertExpectations(t) 41 } 42 43 func TestTestRPCConnection(t *testing.T) { 44 a := assert.New(t) 45 46 // url not legal 47 { 48 res, err := TestRPCConnection(string([]byte{0x7f}), "", 10) 49 a.Error(err) 50 a.Empty(res.Version) 51 } 52 53 // rpc failed 54 { 55 res, err := TestRPCConnection("ws://0.0.0.0", "", 0) 56 a.Error(err) 57 a.Empty(res.Version) 58 } 59 } 60 61 func TestGetLoadBalancer(t *testing.T) { 62 a := assert.New(t) 63 a.NotPanics(func() { 64 GetLoadBalancer() 65 }) 66 }