filebrowser/backend/files/sync_test.go

261 lines
6.1 KiB
Go
Raw Normal View History

2024-09-16 21:01:16 +00:00
package files
import (
"testing"
2024-10-07 22:44:53 +00:00
"github.com/stretchr/testify/assert"
)
2024-09-16 21:01:16 +00:00
var testIndex Index
2024-10-07 22:44:53 +00:00
// Test for GetFileMetadata// Test for GetFileMetadata
func TestGetFileMetadataSize(t *testing.T) {
t.Parallel()
tests := []struct {
name string
adjustedPath string
expectedName string
expectedSize int64
}{
{
name: "testpath exists",
adjustedPath: "/testpath",
expectedName: "testfile.txt",
expectedSize: 100,
},
{
name: "testpath exists",
adjustedPath: "/testpath",
expectedName: "directory",
expectedSize: 100,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2024-11-21 00:15:30 +00:00
fileInfo, _ := testIndex.GetReducedMetadata(tt.adjustedPath, true)
2024-10-07 22:44:53 +00:00
// Iterate over fileInfo.Items to look for expectedName
2024-11-21 00:15:30 +00:00
for _, item := range fileInfo.Items {
2024-10-07 22:44:53 +00:00
// Assert the existence and the name
if item.Name == tt.expectedName {
assert.Equal(t, tt.expectedSize, item.Size)
break
}
}
})
}
}
// Test for GetFileMetadata// Test for GetFileMetadata
func TestGetFileMetadata(t *testing.T) {
t.Parallel()
tests := []struct {
name string
adjustedPath string
expectedName string
expectedExists bool
2024-11-21 00:15:30 +00:00
isDir bool
2024-10-07 22:44:53 +00:00
}{
{
name: "testpath exists",
2024-11-21 00:15:30 +00:00
adjustedPath: "/testpath/testfile.txt",
2024-10-07 22:44:53 +00:00
expectedName: "testfile.txt",
expectedExists: true,
},
{
name: "testpath not exists",
2024-11-21 00:15:30 +00:00
adjustedPath: "/testpath/nonexistent.txt",
2024-10-07 22:44:53 +00:00
expectedName: "nonexistent.txt",
expectedExists: false,
},
{
name: "File exists in /anotherpath",
2024-11-21 00:15:30 +00:00
adjustedPath: "/anotherpath/afile.txt",
2024-10-07 22:44:53 +00:00
expectedName: "afile.txt",
expectedExists: true,
},
{
name: "File does not exist in /anotherpath",
2024-11-21 00:15:30 +00:00
adjustedPath: "/anotherpath/nonexistentfile.txt",
2024-10-07 22:44:53 +00:00
expectedName: "nonexistentfile.txt",
expectedExists: false,
},
{
name: "Directory does not exist",
adjustedPath: "/nonexistentpath",
expectedName: "",
expectedExists: false,
2024-11-21 00:15:30 +00:00
isDir: true,
2024-10-07 22:44:53 +00:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2024-11-21 00:15:30 +00:00
fileInfo, _ := testIndex.GetReducedMetadata(tt.adjustedPath, tt.isDir)
if fileInfo == nil {
found := false
assert.Equal(t, tt.expectedExists, found)
return
}
2024-10-07 22:44:53 +00:00
found := false
2024-11-21 00:15:30 +00:00
if tt.isDir {
// Iterate over fileInfo.Items to look for expectedName
for _, item := range fileInfo.Items {
// Assert the existence and the name
if item.Name == tt.expectedName {
found = true
break
}
}
} else {
if fileInfo.Name == tt.expectedName {
2024-10-07 22:44:53 +00:00
found = true
}
}
2024-11-21 00:15:30 +00:00
2024-10-07 22:44:53 +00:00
assert.Equal(t, tt.expectedExists, found)
})
}
}
2024-09-16 21:01:16 +00:00
// Test for UpdateFileMetadata
func TestUpdateFileMetadata(t *testing.T) {
2024-11-21 00:15:30 +00:00
info := &FileInfo{
Path: "/testpath",
Name: "testpath",
Type: "directory",
Files: []ReducedItem{
{Name: "testfile.txt"},
{Name: "anotherfile.txt"},
2024-09-16 21:01:16 +00:00
},
}
2024-11-21 00:15:30 +00:00
index := &Index{
Directories: map[string]*FileInfo{
"/testpath": info,
},
}
2024-09-16 21:01:16 +00:00
2024-11-21 00:15:30 +00:00
success := index.UpdateMetadata(info)
2024-09-16 21:01:16 +00:00
if !success {
t.Fatalf("expected UpdateFileMetadata to succeed")
}
2024-11-21 00:15:30 +00:00
fileInfo, exists := index.GetReducedMetadata("/testpath/testfile.txt", false)
if !exists || fileInfo.Name != "testfile.txt" {
t.Fatalf("expected testfile.txt to be updated in the directory metadata:%v %v", exists, info.Name)
2024-09-16 21:01:16 +00:00
}
}
// Test for GetDirMetadata
func TestGetDirMetadata(t *testing.T) {
t.Parallel()
2024-11-21 00:15:30 +00:00
_, exists := testIndex.GetReducedMetadata("/testpath", true)
2024-09-16 21:01:16 +00:00
if !exists {
t.Fatalf("expected GetDirMetadata to return initialized metadata map")
}
2024-11-21 00:15:30 +00:00
_, exists = testIndex.GetReducedMetadata("/nonexistent", true)
2024-09-16 21:01:16 +00:00
if exists {
t.Fatalf("expected GetDirMetadata to return false for nonexistent directory")
}
}
// Test for SetDirectoryInfo
func TestSetDirectoryInfo(t *testing.T) {
index := &Index{
2024-11-21 00:15:30 +00:00
Directories: map[string]*FileInfo{
2024-09-16 21:01:16 +00:00
"/testpath": {
2024-11-21 00:15:30 +00:00
Path: "/testpath",
Name: "testpath",
Type: "directory",
Items: []ReducedItem{
2024-10-07 22:44:53 +00:00
{Name: "testfile.txt"},
{Name: "anotherfile.txt"},
2024-09-16 21:01:16 +00:00
},
},
},
}
2024-11-21 00:15:30 +00:00
dir := &FileInfo{
Path: "/newPath",
Name: "newPath",
Type: "directory",
Items: []ReducedItem{
2024-10-07 22:44:53 +00:00
{Name: "testfile.txt"},
},
}
2024-11-21 00:15:30 +00:00
index.UpdateMetadata(dir)
2024-09-16 21:01:16 +00:00
storedDir, exists := index.Directories["/newPath"]
2024-10-07 22:44:53 +00:00
if !exists || storedDir.Items[0].Name != "testfile.txt" {
2024-09-16 21:01:16 +00:00
t.Fatalf("expected SetDirectoryInfo to store directory info correctly")
}
}
// Test for RemoveDirectory
func TestRemoveDirectory(t *testing.T) {
index := &Index{
2024-11-21 00:15:30 +00:00
Directories: map[string]*FileInfo{
2024-09-16 21:01:16 +00:00
"/testpath": {},
},
}
index.RemoveDirectory("/testpath")
_, exists := index.Directories["/testpath"]
if exists {
t.Fatalf("expected directory to be removed")
}
}
// Test for UpdateCount
func TestUpdateCount(t *testing.T) {
index := &Index{}
index.UpdateCount("files")
if index.NumFiles != 1 {
t.Fatalf("expected NumFiles to be 1 after UpdateCount('files')")
}
if index.NumFiles != 1 {
t.Fatalf("expected NumFiles to be 1 after UpdateCount('files')")
}
index.UpdateCount("dirs")
if index.NumDirs != 1 {
t.Fatalf("expected NumDirs to be 1 after UpdateCount('dirs')")
}
index.UpdateCount("unknown")
// Just ensure it does not panic or update any counters
if index.NumFiles != 1 || index.NumDirs != 1 {
t.Fatalf("expected counts to remain unchanged for unknown type")
}
index.resetCount()
if index.NumFiles != 0 || index.NumDirs != 0 || !index.inProgress {
t.Fatalf("expected resetCount to reset counts and set inProgress to true")
}
}
func init() {
testIndex = Index{
2024-10-07 22:44:53 +00:00
Root: "/",
2024-09-16 21:01:16 +00:00
NumFiles: 10,
NumDirs: 5,
inProgress: false,
2024-11-21 00:15:30 +00:00
Directories: map[string]*FileInfo{
2024-09-16 21:01:16 +00:00
"/testpath": {
2024-11-21 00:15:30 +00:00
Path: "/testpath",
Name: "testpath",
Type: "directory",
Files: []ReducedItem{
2024-10-07 22:44:53 +00:00
{Name: "testfile.txt", Size: 100},
{Name: "anotherfile.txt", Size: 100},
2024-09-16 21:01:16 +00:00
},
},
"/anotherpath": {
2024-11-21 00:15:30 +00:00
Path: "/anotherpath",
Name: "anotherpath",
Type: "directory",
Files: []ReducedItem{
2024-10-07 22:44:53 +00:00
{Name: "afile.txt", Size: 100},
2024-09-16 21:01:16 +00:00
},
2024-11-21 00:15:30 +00:00
Dirs: map[string]*FileInfo{
"directory": {Name: "directory", Type: "directory", Size: 100},
},
2024-09-16 21:01:16 +00:00
},
},
}
}