filebrowser/backend/files/search_test.go

225 lines
5.3 KiB
Go
Raw Normal View History

package files
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func BenchmarkSearchAllIndexes(b *testing.B) {
InitializeIndex(5, false)
si := GetIndex(rootPath)
2024-10-07 22:44:53 +00:00
si.createMockData(50, 3) // 50 dirs, 3 files per dir
// Generate 100 random search terms
searchTerms := generateRandomSearchTerms(100)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// Execute the SearchAllIndexes function
for _, term := range searchTerms {
si.Search(term, "/", "test")
}
}
}
func TestParseSearch(t *testing.T) {
2024-10-07 22:44:53 +00:00
tests := []struct {
input string
want *SearchOptions
}{
{
input: "my test search",
want: &SearchOptions{
Conditions: map[string]bool{"exact": false},
Terms: []string{"my test search"},
},
},
2024-10-07 22:44:53 +00:00
{
input: "case:exact my|test|search",
want: &SearchOptions{
Conditions: map[string]bool{"exact": true},
Terms: []string{"my", "test", "search"},
},
},
2024-10-07 22:44:53 +00:00
{
input: "type:largerThan=100 type:smallerThan=1000 test",
want: &SearchOptions{
Conditions: map[string]bool{"exact": false, "larger": true, "smaller": true},
Terms: []string{"test"},
LargerThan: 100,
SmallerThan: 1000,
},
},
2024-10-07 22:44:53 +00:00
{
input: "type:audio thisfile",
want: &SearchOptions{
Conditions: map[string]bool{"exact": false, "audio": true},
Terms: []string{"thisfile"},
},
},
}
2024-10-07 22:44:53 +00:00
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
value := ParseSearch(tt.input)
if !reflect.DeepEqual(value, tt.want) {
t.Fatalf("\n got: %+v\n want: %+v", value, tt.want)
}
})
}
}
func TestSearchWhileIndexing(t *testing.T) {
InitializeIndex(5, false)
si := GetIndex(rootPath)
2024-10-07 22:44:53 +00:00
searchTerms := generateRandomSearchTerms(10)
for i := 0; i < 5; i++ {
2024-10-07 22:44:53 +00:00
go si.createMockData(100, 100) // Creating mock data concurrently
for _, term := range searchTerms {
2024-10-07 22:44:53 +00:00
go si.Search(term, "/", "test") // Search concurrently
}
}
}
func TestSearchIndexes(t *testing.T) {
index := Index{
2024-10-07 22:44:53 +00:00
Directories: map[string]FileInfo{
"test": {Items: []*FileInfo{{Name: "audio1.wav"}}},
"test/path": {Items: []*FileInfo{{Name: "file.txt"}}},
"new/test": {Items: []*FileInfo{
{Name: "audio.wav"},
{Name: "video.mp4"},
{Name: "video.MP4"},
}},
"new/test/path": {Items: []*FileInfo{{Name: "archive.zip"}}},
"/firstDir": {Items: []*FileInfo{
{Name: "archive.zip", Size: 100},
{Name: "thisIsDir", IsDir: true, Size: 2 * 1024 * 1024},
}},
"/firstDir/thisIsDir": {
Items: []*FileInfo{
{Name: "hi.txt"},
},
Size: 2 * 1024 * 1024,
},
},
}
2024-10-07 22:44:53 +00:00
tests := []struct {
search string
scope string
expectedResult []string
expectedTypes map[string]map[string]bool
}{
{
search: "audio",
scope: "/new/",
expectedResult: []string{"test/audio.wav"},
expectedTypes: map[string]map[string]bool{
2024-10-07 22:44:53 +00:00
"test/audio.wav": {"audio": true, "dir": false},
},
},
{
search: "test",
scope: "/",
expectedResult: []string{"test/", "new/test/"},
expectedTypes: map[string]map[string]bool{
2024-10-07 22:44:53 +00:00
"test/": {"dir": true},
"new/test/": {"dir": true},
},
},
{
search: "archive",
scope: "/",
2024-10-07 22:44:53 +00:00
expectedResult: []string{"firstDir/archive.zip", "new/test/path/archive.zip"},
expectedTypes: map[string]map[string]bool{
"new/test/path/archive.zip": {"archive": true, "dir": false},
"firstDir/archive.zip": {"archive": true, "dir": false},
},
},
{
search: "arch",
scope: "/firstDir",
expectedResult: []string{"archive.zip"},
expectedTypes: map[string]map[string]bool{
"archive.zip": {"archive": true, "dir": false},
},
},
{
search: "isdir",
scope: "/",
expectedResult: []string{"firstDir/thisIsDir/"},
expectedTypes: map[string]map[string]bool{
2024-10-07 22:44:53 +00:00
"firstDir/thisIsDir/": {"dir": true},
},
},
{
search: "dir type:largerThan=1",
scope: "/",
expectedResult: []string{"firstDir/thisIsDir/"},
expectedTypes: map[string]map[string]bool{
"firstDir/thisIsDir/": {"dir": true},
},
},
{
search: "video",
scope: "/",
expectedResult: []string{
"new/test/video.mp4",
"new/test/video.MP4",
},
expectedTypes: map[string]map[string]bool{
2024-10-07 22:44:53 +00:00
"new/test/video.MP4": {"video": true, "dir": false},
"new/test/video.mp4": {"video": true, "dir": false},
},
},
}
2024-10-07 22:44:53 +00:00
for _, tt := range tests {
t.Run(tt.search, func(t *testing.T) {
actualResult, actualTypes := index.Search(tt.search, tt.scope, "")
assert.Equal(t, tt.expectedResult, actualResult)
2024-10-07 22:44:53 +00:00
assert.Equal(t, tt.expectedTypes, actualTypes)
})
}
}
func Test_scopedPathNameFilter(t *testing.T) {
tests := []struct {
name string
args struct {
pathName string
scope string
isDir bool // Assuming isDir should be included in args
}
want string
}{
{
name: "scope test",
args: struct {
pathName string
scope string
isDir bool
}{
pathName: "/",
scope: "/",
isDir: false,
},
want: "", // Update this with the expected result
},
}
2024-10-07 22:44:53 +00:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := scopedPathNameFilter(tt.args.pathName, tt.args.scope, tt.args.isDir); got != tt.want {
t.Errorf("scopedPathNameFilter() = %v, want %v", got, tt.want)
}
})
}
}