| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | package folderimpl | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-13 23:55:15 +08:00
										 |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/infra/db" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/dashboards" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/dashboards/database" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/featuremgmt" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/quota/quotatest" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/tag/tagimpl" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2024-02-09 22:35:39 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/tests/testsuite" | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-09 22:35:39 +08:00
										 |  |  | // run tests with cleanup
 | 
					
						
							|  |  |  | func TestMain(m *testing.M) { | 
					
						
							|  |  |  | 	testsuite.Run(m) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | func TestIntegrationDashboardFolderStore(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2024-04-04 21:04:47 +08:00
										 |  |  | 	var sqlStore db.DB | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 	var cfg *setting.Cfg | 
					
						
							| 
									
										
										
										
											2023-03-01 23:52:16 +08:00
										 |  |  | 	var dashboardStore dashboards.Store | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	setup := func() { | 
					
						
							| 
									
										
										
										
											2024-04-24 16:38:40 +08:00
										 |  |  | 		sqlStore, cfg = db.InitTestDBWithCfg(t) | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 		quotaService := quotatest.New(false, nil) | 
					
						
							|  |  |  | 		var err error | 
					
						
							| 
									
										
										
										
											2023-11-13 23:55:15 +08:00
										 |  |  | 		dashboardStore, err = database.ProvideDashboardStore(sqlStore, cfg, featuremgmt.WithFeatures(featuremgmt.FlagPanelTitleSearch), tagimpl.ProvideService(sqlStore), quotaService) | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	t.Run("Given dashboard and folder with the same title", func(t *testing.T) { | 
					
						
							|  |  |  | 		setup() | 
					
						
							|  |  |  | 		var orgId int64 = 1 | 
					
						
							|  |  |  | 		title := "Very Unique Name" | 
					
						
							| 
									
										
										
										
											2024-04-04 21:04:47 +08:00
										 |  |  | 		var sqlStore db.DB | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 		var folder1, folder2 *dashboards.Dashboard | 
					
						
							| 
									
										
										
										
											2024-04-24 16:38:40 +08:00
										 |  |  | 		sqlStore, cfg = db.InitTestDBWithCfg(t) | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 		folderStore := ProvideDashboardFolderStore(sqlStore) | 
					
						
							| 
									
										
										
										
											2024-01-26 23:36:35 +08:00
										 |  |  | 		folder2 = insertTestFolder(t, dashboardStore, "TEST", orgId, "", "prod") | 
					
						
							| 
									
										
										
										
											2023-10-24 15:04:45 +08:00
										 |  |  | 		_ = insertTestDashboard(t, dashboardStore, title, orgId, folder2.ID, folder2.UID, "prod") | 
					
						
							| 
									
										
										
										
											2024-01-26 23:36:35 +08:00
										 |  |  | 		folder1 = insertTestFolder(t, dashboardStore, title, orgId, "", "prod") | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		t.Run("GetFolderByTitle should find the folder", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2024-01-25 17:29:56 +08:00
										 |  |  | 			result, err := folderStore.GetFolderByTitle(context.Background(), orgId, title, nil) | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2024-01-12 23:43:39 +08:00
										 |  |  | 			require.Equal(t, folder1.UID, result.UID) | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2024-01-25 17:29:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		t.Run("GetFolderByTitle should find the folder by folderUID", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2024-01-26 23:36:35 +08:00
										 |  |  | 			folder3 := insertTestFolder(t, dashboardStore, title, orgId, folder2.UID, "prod") | 
					
						
							| 
									
										
										
										
											2024-01-25 17:29:56 +08:00
										 |  |  | 			result, err := folderStore.GetFolderByTitle(context.Background(), orgId, title, &folder2.UID) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 			require.Equal(t, folder3.UID, result.UID) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("GetFolderByUID", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2024-01-25 17:29:56 +08:00
										 |  |  | 		setup() | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 		var orgId int64 = 1 | 
					
						
							|  |  |  | 		sqlStore := db.InitTestDB(t) | 
					
						
							|  |  |  | 		folderStore := ProvideDashboardFolderStore(sqlStore) | 
					
						
							| 
									
										
										
										
											2024-01-26 23:36:35 +08:00
										 |  |  | 		folder := insertTestFolder(t, dashboardStore, "TEST", orgId, "", "prod") | 
					
						
							| 
									
										
										
										
											2023-10-24 15:04:45 +08:00
										 |  |  | 		dash := insertTestDashboard(t, dashboardStore, "Very Unique Name", orgId, folder.ID, folder.UID, "prod") | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		t.Run("should return folder by UID", func(t *testing.T) { | 
					
						
							|  |  |  | 			d, err := folderStore.GetFolderByUID(context.Background(), orgId, folder.UID) | 
					
						
							| 
									
										
										
										
											2024-01-12 23:43:39 +08:00
										 |  |  | 			require.Equal(t, folder.UID, d.UID) | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		t.Run("should not find dashboard", func(t *testing.T) { | 
					
						
							|  |  |  | 			d, err := folderStore.GetFolderByUID(context.Background(), orgId, dash.UID) | 
					
						
							|  |  |  | 			require.Nil(t, d) | 
					
						
							|  |  |  | 			require.ErrorIs(t, err, dashboards.ErrFolderNotFound) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		t.Run("should search in organization", func(t *testing.T) { | 
					
						
							|  |  |  | 			d, err := folderStore.GetFolderByUID(context.Background(), orgId+1, folder.UID) | 
					
						
							|  |  |  | 			require.Nil(t, d) | 
					
						
							|  |  |  | 			require.ErrorIs(t, err, dashboards.ErrFolderNotFound) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("GetFolderByID", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2024-01-25 17:29:56 +08:00
										 |  |  | 		setup() | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 		var orgId int64 = 1 | 
					
						
							|  |  |  | 		sqlStore := db.InitTestDB(t) | 
					
						
							|  |  |  | 		folderStore := ProvideDashboardFolderStore(sqlStore) | 
					
						
							| 
									
										
										
										
											2024-01-26 23:36:35 +08:00
										 |  |  | 		folder := insertTestFolder(t, dashboardStore, "TEST", orgId, "", "prod") | 
					
						
							| 
									
										
										
										
											2023-10-24 15:04:45 +08:00
										 |  |  | 		dash := insertTestDashboard(t, dashboardStore, "Very Unique Name", orgId, folder.ID, folder.UID, "prod") | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		t.Run("should return folder by ID", func(t *testing.T) { | 
					
						
							|  |  |  | 			d, err := folderStore.GetFolderByID(context.Background(), orgId, folder.ID) | 
					
						
							| 
									
										
										
										
											2024-01-12 23:43:39 +08:00
										 |  |  | 			require.Equal(t, folder.UID, d.UID) | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		t.Run("should not find dashboard", func(t *testing.T) { | 
					
						
							|  |  |  | 			d, err := folderStore.GetFolderByID(context.Background(), orgId, dash.ID) | 
					
						
							|  |  |  | 			require.Nil(t, d) | 
					
						
							|  |  |  | 			require.ErrorIs(t, err, dashboards.ErrFolderNotFound) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		t.Run("should search in organization", func(t *testing.T) { | 
					
						
							|  |  |  | 			d, err := folderStore.GetFolderByID(context.Background(), orgId+1, folder.ID) | 
					
						
							|  |  |  | 			require.Nil(t, d) | 
					
						
							|  |  |  | 			require.ErrorIs(t, err, dashboards.ErrFolderNotFound) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 23:36:35 +08:00
										 |  |  | func insertTestDashboard(t *testing.T, dashboardStore dashboards.Store, title string, orgId, folderID int64, folderUID string, tags ...any) *dashboards.Dashboard { | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 	t.Helper() | 
					
						
							|  |  |  | 	cmd := dashboards.SaveDashboardCommand{ | 
					
						
							| 
									
										
										
										
											2024-01-26 23:36:35 +08:00
										 |  |  | 		OrgID:     orgId, | 
					
						
							|  |  |  | 		FolderUID: folderUID, | 
					
						
							|  |  |  | 		FolderID:  folderID, // nolint:staticcheck
 | 
					
						
							|  |  |  | 		IsFolder:  false, | 
					
						
							| 
									
										
										
										
											2023-08-30 23:46:47 +08:00
										 |  |  | 		Dashboard: simplejson.NewFromAny(map[string]any{ | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 			"id":    nil, | 
					
						
							|  |  |  | 			"title": title, | 
					
						
							|  |  |  | 			"tags":  tags, | 
					
						
							|  |  |  | 		}), | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-25 17:29:56 +08:00
										 |  |  | 	if folderUID != "" { | 
					
						
							|  |  |  | 		cmd.FolderUID = folderUID | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 	dash, err := dashboardStore.SaveDashboard(context.Background(), cmd) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.NotNil(t, dash) | 
					
						
							|  |  |  | 	dash.Data.Set("id", dash.ID) | 
					
						
							|  |  |  | 	dash.Data.Set("uid", dash.UID) | 
					
						
							|  |  |  | 	return dash | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 23:36:35 +08:00
										 |  |  | func insertTestFolder(t *testing.T, dashboardStore dashboards.Store, title string, orgId int64, folderUID string, tags ...any) *dashboards.Dashboard { | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 	t.Helper() | 
					
						
							|  |  |  | 	cmd := dashboards.SaveDashboardCommand{ | 
					
						
							| 
									
										
										
										
											2024-01-26 23:36:35 +08:00
										 |  |  | 		OrgID: orgId, | 
					
						
							|  |  |  | 		// FolderID: folderId, // nolint:staticcheck
 | 
					
						
							|  |  |  | 		FolderUID: folderUID, | 
					
						
							|  |  |  | 		IsFolder:  true, | 
					
						
							| 
									
										
										
										
											2023-08-30 23:46:47 +08:00
										 |  |  | 		Dashboard: simplejson.NewFromAny(map[string]any{ | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 			"id":    nil, | 
					
						
							|  |  |  | 			"title": title, | 
					
						
							|  |  |  | 			"tags":  tags, | 
					
						
							|  |  |  | 		}), | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-25 17:29:56 +08:00
										 |  |  | 	if folderUID != "" { | 
					
						
							|  |  |  | 		cmd.FolderUID = folderUID | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-02-01 21:43:21 +08:00
										 |  |  | 	dash, err := dashboardStore.SaveDashboard(context.Background(), cmd) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.NotNil(t, dash) | 
					
						
							|  |  |  | 	dash.Data.Set("id", dash.ID) | 
					
						
							|  |  |  | 	dash.Data.Set("uid", dash.UID) | 
					
						
							|  |  |  | 	return dash | 
					
						
							|  |  |  | } |