github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/models/scripts/upgrade_test.go (about)

     1  package scripts
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"github.com/DATA-DOG/go-sqlmock"
     7  	"github.com/stretchr/testify/assert"
     8  	"testing"
     9  )
    10  
    11  func TestUpgradeTo340_Run(t *testing.T) {
    12  	a := assert.New(t)
    13  	script := UpgradeTo340(0)
    14  
    15  	// skip
    16  	{
    17  		mock.ExpectQuery("SELECT(.+)settings").WillReturnRows(sqlmock.NewRows([]string{"name"}))
    18  		script.Run(context.Background())
    19  		a.NoError(mock.ExpectationsWereMet())
    20  	}
    21  
    22  	// node not found
    23  	{
    24  		mock.ExpectQuery("SELECT(.+)settings").WillReturnRows(sqlmock.NewRows([]string{"name"}).AddRow("1"))
    25  		mock.ExpectQuery("SELECT(.+)nodes").WillReturnRows(sqlmock.NewRows([]string{"id"}))
    26  		script.Run(context.Background())
    27  		a.NoError(mock.ExpectationsWereMet())
    28  	}
    29  
    30  	// success
    31  	{
    32  		mock.ExpectQuery("SELECT(.+)settings").WillReturnRows(sqlmock.NewRows([]string{"name", "value"}).
    33  			AddRow("aria2_rpcurl", "expected_aria2_rpcurl").
    34  			AddRow("aria2_interval", "expected_aria2_interval").
    35  			AddRow("aria2_temp_path", "expected_aria2_temp_path").
    36  			AddRow("aria2_token", "expected_aria2_token").
    37  			AddRow("aria2_options", "{}"))
    38  
    39  		mock.ExpectQuery("SELECT(.+)nodes").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
    40  		mock.ExpectBegin()
    41  		mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
    42  		mock.ExpectCommit()
    43  		mock.ExpectBegin()
    44  		mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
    45  		mock.ExpectCommit()
    46  		script.Run(context.Background())
    47  		a.NoError(mock.ExpectationsWereMet())
    48  	}
    49  
    50  	// failed
    51  	{
    52  		mock.ExpectQuery("SELECT(.+)settings").WillReturnRows(sqlmock.NewRows([]string{"name", "value"}).
    53  			AddRow("aria2_rpcurl", "expected_aria2_rpcurl").
    54  			AddRow("aria2_interval", "expected_aria2_interval").
    55  			AddRow("aria2_temp_path", "expected_aria2_temp_path").
    56  			AddRow("aria2_token", "expected_aria2_token").
    57  			AddRow("aria2_options", "{}"))
    58  
    59  		mock.ExpectQuery("SELECT(.+)nodes").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
    60  		mock.ExpectBegin()
    61  		mock.ExpectExec("UPDATE(.+)").WillReturnError(errors.New("error"))
    62  		mock.ExpectRollback()
    63  		script.Run(context.Background())
    64  		a.NoError(mock.ExpectationsWereMet())
    65  	}
    66  }