Search: Fix a bug when searching folders under the general folder (#91042)

* fix: search returns all folders when folder uid is general
* test: add unit test for FolderUIDFilter when NestedFolders is enabled
This commit is contained in:
Faye Lin 2024-09-12 11:55:28 +08:00 committed by GitHub
parent b5dd8a5e89
commit 634d590cf1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 69 additions and 26 deletions

View File

@ -102,11 +102,20 @@ func (f FolderUIDFilter) Where() (string, []any) {
if includeGeneral {
if q == "" {
q = "dashboard.folder_id = ? "
if f.NestedFoldersEnabled {
q = "dashboard.folder_uid IS NULL "
} else {
q = "dashboard.folder_id = ? "
params = append(params, 0)
}
} else {
q = "(" + q + " OR dashboard.folder_id = ?)"
if f.NestedFoldersEnabled {
q = "(" + q + " OR dashboard.folder_uid IS NULL)"
} else {
q = "(" + q + " OR dashboard.folder_id = ?)"
params = append(params, 0)
}
}
params = append(params, 0)
}
return q, params

View File

@ -9,34 +9,67 @@ import (
func TestFolderUIDFilter(t *testing.T) {
testCases := []struct {
description string
uids []string
expectedSql string
expectedParams []any
description string
uids []string
expectedSql string
expectedParams []any
nestedFoldersEnabled bool
}{
{
description: "searching general folder",
uids: []string{"general"},
expectedSql: "dashboard.folder_id = ? ",
expectedParams: []any{0},
description: "searching general folder",
uids: []string{"general"},
expectedSql: "dashboard.folder_id = ? ",
expectedParams: []any{0},
nestedFoldersEnabled: false,
},
{
description: "searching a specific folder",
uids: []string{"abc-123"},
expectedSql: "dashboard.folder_id IN (SELECT id FROM dashboard WHERE org_id = ? AND uid = ?)",
expectedParams: []any{int64(1), "abc-123"},
description: "searching a specific folder",
uids: []string{"abc-123"},
expectedSql: "dashboard.folder_id IN (SELECT id FROM dashboard WHERE org_id = ? AND uid = ?)",
expectedParams: []any{int64(1), "abc-123"},
nestedFoldersEnabled: false,
},
{
description: "searching a specific folders",
uids: []string{"abc-123", "def-456"},
expectedSql: "dashboard.folder_id IN (SELECT id FROM dashboard WHERE org_id = ? AND uid IN (?,?))",
expectedParams: []any{int64(1), "abc-123", "def-456"},
description: "searching a specific folders",
uids: []string{"abc-123", "def-456"},
expectedSql: "dashboard.folder_id IN (SELECT id FROM dashboard WHERE org_id = ? AND uid IN (?,?))",
expectedParams: []any{int64(1), "abc-123", "def-456"},
nestedFoldersEnabled: false,
},
{
description: "searching a specific folders or general",
uids: []string{"general", "abc-123", "def-456"},
expectedSql: "(dashboard.folder_id IN (SELECT id FROM dashboard WHERE org_id = ? AND uid IN (?,?)) OR dashboard.folder_id = ?)",
expectedParams: []any{int64(1), "abc-123", "def-456", 0},
description: "searching a specific folders or general",
uids: []string{"general", "abc-123", "def-456"},
expectedSql: "(dashboard.folder_id IN (SELECT id FROM dashboard WHERE org_id = ? AND uid IN (?,?)) OR dashboard.folder_id = ?)",
expectedParams: []any{int64(1), "abc-123", "def-456", 0},
nestedFoldersEnabled: false,
},
{
description: "searching general folder with nestedFoldersEnabled",
uids: []string{"general"},
expectedSql: "dashboard.folder_uid IS NULL ",
expectedParams: []any{},
nestedFoldersEnabled: true,
},
{
description: "searching a specific folder with nestedFoldersEnabled",
uids: []string{"abc-123"},
expectedSql: "dashboard.org_id = ? AND dashboard.folder_uid = ?",
expectedParams: []any{int64(1), "abc-123"},
nestedFoldersEnabled: true,
},
{
description: "searching a specific folders with nestedFoldersEnabled",
uids: []string{"abc-123", "def-456"},
expectedSql: "dashboard.org_id = ? AND dashboard.folder_uid IN (?,?)",
expectedParams: []any{int64(1), "abc-123", "def-456"},
nestedFoldersEnabled: true,
},
{
description: "searching a specific folders or general with nestedFoldersEnabled",
uids: []string{"general", "abc-123", "def-456"},
expectedSql: "(dashboard.org_id = ? AND dashboard.folder_uid IN (?,?) OR dashboard.folder_uid IS NULL)",
expectedParams: []any{int64(1), "abc-123", "def-456"},
nestedFoldersEnabled: true,
},
}
@ -45,9 +78,10 @@ func TestFolderUIDFilter(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
f := searchstore.FolderUIDFilter{
Dialect: store.GetDialect(),
OrgID: 1,
UIDs: tc.uids,
Dialect: store.GetDialect(),
OrgID: 1,
UIDs: tc.uids,
NestedFoldersEnabled: tc.nestedFoldersEnabled,
}
sql, params := f.Where()