github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/filesystem/path_test.go (about)

     1  package filesystem
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/DATA-DOG/go-sqlmock"
     7  	model "github.com/cloudreve/Cloudreve/v3/models"
     8  	"github.com/jinzhu/gorm"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestFileSystem_IsFileExist(t *testing.T) {
    13  	asserts := assert.New(t)
    14  	fs := &FileSystem{User: &model.User{
    15  		Model: gorm.Model{
    16  			ID: 1,
    17  		},
    18  	}}
    19  
    20  	// 存在
    21  	{
    22  		path := "/1.txt"
    23  		// 根目录
    24  		mock.ExpectQuery("SELECT(.+)").
    25  			WithArgs(1).
    26  			WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
    27  		mock.ExpectQuery("SELECT(.+)").WithArgs(1, "1.txt").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(1, "1.txt"))
    28  		exist, file := fs.IsFileExist(path)
    29  		asserts.NoError(mock.ExpectationsWereMet())
    30  		asserts.True(exist)
    31  		asserts.Equal(uint(1), file.ID)
    32  	}
    33  
    34  	// 文件不存在
    35  	{
    36  		path := "/1.txt"
    37  		// 根目录
    38  		mock.ExpectQuery("SELECT(.+)").
    39  			WithArgs(1).
    40  			WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
    41  		mock.ExpectQuery("SELECT(.+)").WithArgs(1, "1.txt").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
    42  		exist, _ := fs.IsFileExist(path)
    43  		asserts.NoError(mock.ExpectationsWereMet())
    44  		asserts.False(exist)
    45  	}
    46  
    47  	// 父目录不存在
    48  	{
    49  		path := "/1.txt"
    50  		// 根目录
    51  		mock.ExpectQuery("SELECT(.+)").
    52  			WithArgs(1).
    53  			WillReturnRows(sqlmock.NewRows([]string{"id"}))
    54  		exist, _ := fs.IsFileExist(path)
    55  		asserts.NoError(mock.ExpectationsWereMet())
    56  		asserts.False(exist)
    57  	}
    58  }
    59  
    60  func TestFileSystem_IsPathExist(t *testing.T) {
    61  	asserts := assert.New(t)
    62  	fs := &FileSystem{User: &model.User{
    63  		Model: gorm.Model{
    64  			ID: 1,
    65  		},
    66  	}}
    67  
    68  	// 查询根目录
    69  	{
    70  		path := "/"
    71  		// 根目录
    72  		mock.ExpectQuery("SELECT(.+)").
    73  			WithArgs(1).
    74  			WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
    75  		exist, folder := fs.IsPathExist(path)
    76  		asserts.NoError(mock.ExpectationsWereMet())
    77  		asserts.True(exist)
    78  		asserts.Equal(uint(1), folder.ID)
    79  	}
    80  
    81  	// 深层路径
    82  	{
    83  		path := "/1/2/3"
    84  		// 根目录
    85  		mock.ExpectQuery("SELECT(.+)").
    86  			WithArgs(1).
    87  			WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
    88  		// 1
    89  		mock.ExpectQuery("SELECT(.+)").
    90  			WithArgs(1, 1, "1").
    91  			WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
    92  		// 2
    93  		mock.ExpectQuery("SELECT(.+)").
    94  			WithArgs(2, 1, "2").
    95  			WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(3, 1))
    96  		// 3
    97  		mock.ExpectQuery("SELECT(.+)").
    98  			WithArgs(3, 1, "3").
    99  			WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(4, 1))
   100  		exist, folder := fs.IsPathExist(path)
   101  		asserts.NoError(mock.ExpectationsWereMet())
   102  		asserts.True(exist)
   103  		asserts.Equal(uint(4), folder.ID)
   104  	}
   105  
   106  	// 深层路径 重设根目录为/1
   107  	{
   108  		path := "/2/3"
   109  		fs.Root = &model.Folder{Name: "1", Model: gorm.Model{ID: 2}, OwnerID: 1}
   110  		// 2
   111  		mock.ExpectQuery("SELECT(.+)").
   112  			WithArgs(2, 1, "2").
   113  			WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(3, 1))
   114  		// 3
   115  		mock.ExpectQuery("SELECT(.+)").
   116  			WithArgs(3, 1, "3").
   117  			WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(4, 1))
   118  		exist, folder := fs.IsPathExist(path)
   119  		asserts.NoError(mock.ExpectationsWereMet())
   120  		asserts.True(exist)
   121  		asserts.Equal(uint(4), folder.ID)
   122  		fs.Root = nil
   123  	}
   124  
   125  	// 深层 不存在
   126  	{
   127  		path := "/1/2/3"
   128  		// 根目录
   129  		mock.ExpectQuery("SELECT(.+)").
   130  			WithArgs(1).
   131  			WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
   132  		// 1
   133  		mock.ExpectQuery("SELECT(.+)").
   134  			WithArgs(1, 1, "1").
   135  			WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
   136  		// 2
   137  		mock.ExpectQuery("SELECT(.+)").
   138  			WithArgs(2, 1, "2").
   139  			WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(3, 1))
   140  		// 3
   141  		mock.ExpectQuery("SELECT(.+)").
   142  			WithArgs(3, 1, "3").
   143  			WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
   144  		exist, folder := fs.IsPathExist(path)
   145  		asserts.NoError(mock.ExpectationsWereMet())
   146  		asserts.False(exist)
   147  		asserts.Nil(folder)
   148  	}
   149  
   150  }
   151  
   152  func TestFileSystem_IsChildFileExist(t *testing.T) {
   153  	asserts := assert.New(t)
   154  	fs := &FileSystem{User: &model.User{
   155  		Model: gorm.Model{
   156  			ID: 1,
   157  		},
   158  	}}
   159  	folder := model.Folder{
   160  		Model:    gorm.Model{ID: 1},
   161  		Name:     "123",
   162  		Position: "/",
   163  	}
   164  
   165  	mock.ExpectQuery("SELECT(.+)").
   166  		WithArgs(1, "321").
   167  		WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(2, "321"))
   168  	exist, childFile := fs.IsChildFileExist(&folder, "321")
   169  	asserts.NoError(mock.ExpectationsWereMet())
   170  	asserts.True(exist)
   171  	asserts.Equal("/123", childFile.Position)
   172  }