2021-09-01 22:38:56 +08:00
//go:build integration
2020-10-16 15:46:14 +08:00
// +build integration
2015-01-06 00:04:29 +08:00
package sqlstore
import (
2018-06-16 03:23:57 +08:00
"context"
2021-07-22 17:27:13 +08:00
"encoding/json"
"errors"
2018-01-30 22:24:14 +08:00
"fmt"
2015-01-06 00:04:29 +08:00
"testing"
2018-02-22 18:54:28 +08:00
"time"
2015-01-06 00:04:29 +08:00
2016-03-12 17:13:49 +08:00
"github.com/grafana/grafana/pkg/components/simplejson"
2020-02-29 20:35:15 +08:00
"github.com/grafana/grafana/pkg/models"
2015-06-05 17:08:19 +08:00
"github.com/grafana/grafana/pkg/services/search"
2020-05-06 17:42:52 +08:00
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
2017-06-17 08:33:53 +08:00
"github.com/grafana/grafana/pkg/setting"
2018-02-01 00:27:28 +08:00
"github.com/grafana/grafana/pkg/util"
2020-05-06 17:42:52 +08:00
2018-02-01 00:27:28 +08:00
. "github.com/smartystreets/goconvey/convey"
2020-05-06 17:42:52 +08:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2015-01-06 00:04:29 +08:00
)
func TestDashboardDataAccess ( t * testing . T ) {
Convey ( "Testing DB" , t , func ( ) {
2021-03-17 23:06:10 +08:00
sqlStore := InitTestDB ( t )
2015-01-06 00:04:29 +08:00
Convey ( "Given saved dashboard" , func ( ) {
2021-03-17 23:06:10 +08:00
savedFolder := insertTestDashboard ( t , sqlStore , "1 test dash folder" , 1 , 0 , true , "prod" , "webapp" )
savedDash := insertTestDashboard ( t , sqlStore , "test dash 23" , 1 , savedFolder . Id , false , "prod" , "webapp" )
insertTestDashboard ( t , sqlStore , "test dash 45" , 1 , savedFolder . Id , false , "prod" )
2021-04-21 16:22:46 +08:00
savedDash2 := insertTestDashboard ( t , sqlStore , "test dash 67" , 1 , 0 , false , "prod" )
2021-07-22 17:27:13 +08:00
insertTestRule ( t , sqlStore , savedFolder . OrgId , savedFolder . Uid )
2015-01-06 00:04:29 +08:00
Convey ( "Should return dashboard model" , func ( ) {
So ( savedDash . Title , ShouldEqual , "test dash 23" )
So ( savedDash . Slug , ShouldEqual , "test-dash-23" )
So ( savedDash . Id , ShouldNotEqual , 0 )
2017-03-27 20:36:28 +08:00
So ( savedDash . IsFolder , ShouldBeFalse )
2017-06-24 04:00:26 +08:00
So ( savedDash . FolderId , ShouldBeGreaterThan , 0 )
2018-01-30 04:23:07 +08:00
So ( len ( savedDash . Uid ) , ShouldBeGreaterThan , 0 )
2017-03-27 20:36:28 +08:00
So ( savedFolder . Title , ShouldEqual , "1 test dash folder" )
So ( savedFolder . Slug , ShouldEqual , "1-test-dash-folder" )
So ( savedFolder . Id , ShouldNotEqual , 0 )
So ( savedFolder . IsFolder , ShouldBeTrue )
2017-06-24 04:00:26 +08:00
So ( savedFolder . FolderId , ShouldEqual , 0 )
2018-01-30 04:23:07 +08:00
So ( len ( savedFolder . Uid ) , ShouldBeGreaterThan , 0 )
2015-01-06 00:04:29 +08:00
} )
2018-01-30 04:23:07 +08:00
Convey ( "Should be able to get dashboard by id" , func ( ) {
2020-02-29 20:35:15 +08:00
query := models . GetDashboardQuery {
2018-01-30 04:23:07 +08:00
Id : savedDash . Id ,
OrgId : 1 ,
}
err := GetDashboard ( & query )
So ( err , ShouldBeNil )
So ( query . Result . Title , ShouldEqual , "test dash 23" )
So ( query . Result . Slug , ShouldEqual , "test-dash-23" )
So ( query . Result . Id , ShouldEqual , savedDash . Id )
So ( query . Result . Uid , ShouldEqual , savedDash . Uid )
So ( query . Result . IsFolder , ShouldBeFalse )
} )
Convey ( "Should be able to get dashboard by slug" , func ( ) {
2020-02-29 20:35:15 +08:00
query := models . GetDashboardQuery {
2015-02-24 03:07:49 +08:00
Slug : "test-dash-23" ,
OrgId : 1 ,
2015-01-06 00:04:29 +08:00
}
err := GetDashboard ( & query )
So ( err , ShouldBeNil )
So ( query . Result . Title , ShouldEqual , "test dash 23" )
So ( query . Result . Slug , ShouldEqual , "test-dash-23" )
2018-01-30 04:23:07 +08:00
So ( query . Result . Id , ShouldEqual , savedDash . Id )
So ( query . Result . Uid , ShouldEqual , savedDash . Uid )
So ( query . Result . IsFolder , ShouldBeFalse )
} )
Convey ( "Should be able to get dashboard by uid" , func ( ) {
2020-02-29 20:35:15 +08:00
query := models . GetDashboardQuery {
2018-01-30 04:23:07 +08:00
Uid : savedDash . Uid ,
OrgId : 1 ,
}
err := GetDashboard ( & query )
So ( err , ShouldBeNil )
So ( query . Result . Title , ShouldEqual , "test dash 23" )
So ( query . Result . Slug , ShouldEqual , "test-dash-23" )
So ( query . Result . Id , ShouldEqual , savedDash . Id )
So ( query . Result . Uid , ShouldEqual , savedDash . Uid )
2017-03-27 20:36:28 +08:00
So ( query . Result . IsFolder , ShouldBeFalse )
2015-01-06 00:04:29 +08:00
} )
2020-02-17 22:32:20 +08:00
Convey ( "Shouldn't be able to get a dashboard with just an OrgID" , func ( ) {
2020-02-29 20:35:15 +08:00
query := models . GetDashboardQuery {
2020-02-17 22:32:20 +08:00
OrgId : 1 ,
}
err := GetDashboard ( & query )
2020-02-29 20:35:15 +08:00
So ( err , ShouldEqual , models . ErrDashboardIdentifierNotSet )
2020-02-17 22:32:20 +08:00
} )
2016-04-25 14:46:15 +08:00
Convey ( "Should be able to delete dashboard" , func ( ) {
2021-03-17 23:06:10 +08:00
dash := insertTestDashboard ( t , sqlStore , "delete me" , 1 , 0 , false , "delete this" )
2016-04-25 14:46:15 +08:00
2020-02-29 20:35:15 +08:00
err := DeleteDashboard ( & models . DeleteDashboardCommand {
2017-06-18 06:24:38 +08:00
Id : dash . Id ,
2016-04-25 14:46:15 +08:00
OrgId : 1 ,
} )
So ( err , ShouldBeNil )
} )
2018-02-01 00:27:28 +08:00
Convey ( "Should retry generation of uid once if it fails." , func ( ) {
timesCalled := 0
generateNewUid = func ( ) string {
timesCalled += 1
if timesCalled <= 2 {
return savedDash . Uid
}
2019-01-29 05:37:44 +08:00
return util . GenerateShortUID ( )
2018-02-01 00:27:28 +08:00
}
2020-02-29 20:35:15 +08:00
cmd := models . SaveDashboardCommand {
2018-02-01 00:27:28 +08:00
OrgId : 1 ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "new dash 12334" ,
"tags" : [ ] interface { } { } ,
} ) ,
}
2021-03-17 23:06:10 +08:00
_ , err := sqlStore . SaveDashboard ( cmd )
2018-02-01 00:27:28 +08:00
So ( err , ShouldBeNil )
2019-01-29 05:37:44 +08:00
generateNewUid = util . GenerateShortUID
2018-02-01 00:27:28 +08:00
} )
2018-02-22 18:54:28 +08:00
Convey ( "Should be able to create dashboard" , func ( ) {
2020-02-29 20:35:15 +08:00
cmd := models . SaveDashboardCommand {
2018-02-22 18:54:28 +08:00
OrgId : 1 ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "folderId" ,
"tags" : [ ] interface { } { } ,
} ) ,
UserId : 100 ,
}
2021-03-17 23:06:10 +08:00
dashboard , err := sqlStore . SaveDashboard ( cmd )
2018-02-22 18:54:28 +08:00
So ( err , ShouldBeNil )
2021-03-17 23:06:10 +08:00
So ( dashboard . CreatedBy , ShouldEqual , 100 )
So ( dashboard . Created . IsZero ( ) , ShouldBeFalse )
So ( dashboard . UpdatedBy , ShouldEqual , 100 )
So ( dashboard . Updated . IsZero ( ) , ShouldBeFalse )
2018-02-22 18:54:28 +08:00
} )
2018-02-08 19:48:38 +08:00
Convey ( "Should be able to update dashboard by id and remove folderId" , func ( ) {
2020-02-29 20:35:15 +08:00
cmd := models . SaveDashboardCommand {
2017-05-31 22:53:12 +08:00
OrgId : 1 ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2018-02-08 19:48:38 +08:00
"id" : savedDash . Id ,
2017-06-24 04:00:26 +08:00
"title" : "folderId" ,
2017-05-31 22:53:12 +08:00
"tags" : [ ] interface { } { } ,
} ) ,
Overwrite : true ,
2017-06-24 04:00:26 +08:00
FolderId : 2 ,
2018-02-22 18:54:28 +08:00
UserId : 100 ,
2017-05-31 22:53:12 +08:00
}
2021-03-17 23:06:10 +08:00
dash , err := sqlStore . SaveDashboard ( cmd )
2017-05-31 22:53:12 +08:00
So ( err , ShouldBeNil )
2021-03-17 23:06:10 +08:00
So ( dash . FolderId , ShouldEqual , 2 )
2017-05-31 22:53:12 +08:00
2020-02-29 20:35:15 +08:00
cmd = models . SaveDashboardCommand {
2017-05-31 22:53:12 +08:00
OrgId : 1 ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2018-02-08 19:48:38 +08:00
"id" : savedDash . Id ,
2017-06-24 04:00:26 +08:00
"title" : "folderId" ,
2017-05-31 22:53:12 +08:00
"tags" : [ ] interface { } { } ,
} ) ,
2017-06-24 04:00:26 +08:00
FolderId : 0 ,
2017-05-31 22:53:12 +08:00
Overwrite : true ,
2018-02-22 18:54:28 +08:00
UserId : 100 ,
2017-05-31 22:53:12 +08:00
}
2021-03-17 23:06:10 +08:00
_ , err = sqlStore . SaveDashboard ( cmd )
2017-05-31 22:53:12 +08:00
So ( err , ShouldBeNil )
2020-02-29 20:35:15 +08:00
query := models . GetDashboardQuery {
2018-02-08 19:48:38 +08:00
Id : savedDash . Id ,
2017-05-31 22:53:12 +08:00
OrgId : 1 ,
}
err = GetDashboard ( & query )
So ( err , ShouldBeNil )
2017-06-24 04:00:26 +08:00
So ( query . Result . FolderId , ShouldEqual , 0 )
2018-02-22 18:54:28 +08:00
So ( query . Result . CreatedBy , ShouldEqual , savedDash . CreatedBy )
2018-07-25 01:05:09 +08:00
So ( query . Result . Created , ShouldHappenWithin , 3 * time . Second , savedDash . Created )
2018-02-22 18:54:28 +08:00
So ( query . Result . UpdatedBy , ShouldEqual , 100 )
So ( query . Result . Updated . IsZero ( ) , ShouldBeFalse )
2017-05-31 22:53:12 +08:00
} )
2020-11-06 16:02:31 +08:00
Convey ( "Should be able to delete empty folder" , func ( ) {
2021-03-17 23:06:10 +08:00
emptyFolder := insertTestDashboard ( t , sqlStore , "2 test dash folder" , 1 , 0 , true , "prod" , "webapp" )
2020-11-06 16:02:31 +08:00
deleteCmd := & models . DeleteDashboardCommand { Id : emptyFolder . Id }
err := DeleteDashboard ( deleteCmd )
So ( err , ShouldBeNil )
} )
2021-07-22 17:27:13 +08:00
Convey ( "Should be not able to delete a dashboard if force delete rules is disabled" , func ( ) {
deleteCmd := & models . DeleteDashboardCommand { Id : savedFolder . Id , ForceDeleteFolderRules : false }
err := DeleteDashboard ( deleteCmd )
So ( errors . Is ( err , models . ErrFolderContainsAlertRules ) , ShouldBeTrue )
} )
Convey ( "Should be able to delete a dashboard folder and its children if force delete rules is enabled" , func ( ) {
deleteCmd := & models . DeleteDashboardCommand { Id : savedFolder . Id , ForceDeleteFolderRules : true }
2017-06-17 03:14:42 +08:00
err := DeleteDashboard ( deleteCmd )
So ( err , ShouldBeNil )
query := search . FindPersistedDashboardsQuery {
2017-06-17 08:33:53 +08:00
OrgId : 1 ,
2017-11-21 18:53:56 +08:00
FolderIds : [ ] int64 { savedFolder . Id } ,
2020-02-29 20:35:15 +08:00
SignedInUser : & models . SignedInUser { } ,
2017-06-17 03:14:42 +08:00
}
err = SearchDashboards ( & query )
So ( err , ShouldBeNil )
So ( len ( query . Result ) , ShouldEqual , 0 )
2021-07-22 17:27:13 +08:00
sqlStore . WithDbSession ( context . Background ( ) , func ( sess * DBSession ) error {
var existingRuleID int64
exists , err := sess . Table ( "alert_rule" ) . Where ( "namespace_uid = (SELECT uid FROM dashboard WHERE id = ?)" , savedFolder . Id ) . Cols ( "id" ) . Get ( & existingRuleID )
require . NoError ( t , err )
So ( exists , ShouldBeFalse )
var existingRuleVersionID int64
exists , err = sess . Table ( "alert_rule_version" ) . Where ( "rule_namespace_uid = (SELECT uid FROM dashboard WHERE id = ?)" , savedFolder . Id ) . Cols ( "id" ) . Get ( & existingRuleVersionID )
require . NoError ( t , err )
So ( exists , ShouldBeFalse )
return nil
} )
2017-06-17 03:14:42 +08:00
} )
2018-02-19 18:12:56 +08:00
Convey ( "Should return error if no dashboard is found for update when dashboard id is greater than zero" , func ( ) {
2020-02-29 20:35:15 +08:00
cmd := models . SaveDashboardCommand {
2018-02-19 18:12:56 +08:00
OrgId : 1 ,
Overwrite : true ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : float64 ( 123412321 ) ,
"title" : "Expect error" ,
"tags" : [ ] interface { } { } ,
} ) ,
}
2021-03-17 23:06:10 +08:00
_ , err := sqlStore . SaveDashboard ( cmd )
2020-02-29 20:35:15 +08:00
So ( err , ShouldEqual , models . ErrDashboardNotFound )
2018-02-19 18:12:56 +08:00
} )
Convey ( "Should not return error if no dashboard is found for update when dashboard id is zero" , func ( ) {
2020-02-29 20:35:15 +08:00
cmd := models . SaveDashboardCommand {
2018-02-19 18:12:56 +08:00
OrgId : 1 ,
Overwrite : true ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : 0 ,
"title" : "New dash" ,
"tags" : [ ] interface { } { } ,
} ) ,
}
2021-03-17 23:06:10 +08:00
_ , err := sqlStore . SaveDashboard ( cmd )
2018-02-19 18:12:56 +08:00
So ( err , ShouldBeNil )
} )
2015-01-07 19:37:24 +08:00
Convey ( "Should be able to get dashboard tags" , func ( ) {
2020-02-29 20:35:15 +08:00
query := models . GetDashboardTagsQuery { OrgId : 1 }
2015-01-06 00:04:29 +08:00
2015-01-07 19:37:24 +08:00
err := GetDashboardTags ( & query )
So ( err , ShouldBeNil )
2015-01-06 00:04:29 +08:00
2015-01-20 21:15:48 +08:00
So ( len ( query . Result ) , ShouldEqual , 2 )
2015-01-07 19:37:24 +08:00
} )
2015-02-04 18:35:59 +08:00
2018-02-08 19:48:38 +08:00
Convey ( "Should be able to search for dashboard folder" , func ( ) {
query := search . FindPersistedDashboardsQuery {
Title : "1 test dash folder" ,
OrgId : 1 ,
2020-02-29 20:35:15 +08:00
SignedInUser : & models . SignedInUser { OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
2018-02-08 19:48:38 +08:00
}
err := SearchDashboards ( & query )
So ( err , ShouldBeNil )
So ( len ( query . Result ) , ShouldEqual , 1 )
hit := query . Result [ 0 ]
So ( hit . Type , ShouldEqual , search . DashHitFolder )
2021-02-11 15:49:16 +08:00
So ( hit . URL , ShouldEqual , fmt . Sprintf ( "/dashboards/f/%s/%s" , savedFolder . Uid , savedFolder . Slug ) )
2018-02-08 19:48:38 +08:00
So ( hit . FolderTitle , ShouldEqual , "" )
} )
2019-04-17 19:07:50 +08:00
Convey ( "Should be able to limit search" , func ( ) {
query := search . FindPersistedDashboardsQuery {
OrgId : 1 ,
Limit : 1 ,
2020-02-29 20:35:15 +08:00
SignedInUser : & models . SignedInUser { OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
2019-04-17 19:07:50 +08:00
}
err := SearchDashboards ( & query )
So ( err , ShouldBeNil )
So ( len ( query . Result ) , ShouldEqual , 1 )
So ( query . Result [ 0 ] . Title , ShouldEqual , "1 test dash folder" )
} )
Convey ( "Should be able to search beyond limit using paging" , func ( ) {
query := search . FindPersistedDashboardsQuery {
OrgId : 1 ,
Limit : 1 ,
Page : 2 ,
2020-02-29 20:35:15 +08:00
SignedInUser : & models . SignedInUser { OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
2019-04-17 19:07:50 +08:00
}
err := SearchDashboards ( & query )
So ( err , ShouldBeNil )
So ( len ( query . Result ) , ShouldEqual , 1 )
So ( query . Result [ 0 ] . Title , ShouldEqual , "test dash 23" )
} )
2019-04-18 01:07:13 +08:00
Convey ( "Should be able to filter by tag and type" , func ( ) {
query := search . FindPersistedDashboardsQuery {
OrgId : 1 ,
Type : "dash-db" ,
Tags : [ ] string { "prod" } ,
2020-02-29 20:35:15 +08:00
SignedInUser : & models . SignedInUser { OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
2019-04-18 01:07:13 +08:00
}
err := SearchDashboards ( & query )
So ( err , ShouldBeNil )
So ( len ( query . Result ) , ShouldEqual , 3 )
So ( query . Result [ 0 ] . Title , ShouldEqual , "test dash 23" )
} )
2018-02-08 19:48:38 +08:00
Convey ( "Should be able to search for a dashboard folder's children" , func ( ) {
query := search . FindPersistedDashboardsQuery {
OrgId : 1 ,
FolderIds : [ ] int64 { savedFolder . Id } ,
2020-02-29 20:35:15 +08:00
SignedInUser : & models . SignedInUser { OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
2018-02-08 19:48:38 +08:00
}
err := SearchDashboards ( & query )
So ( err , ShouldBeNil )
So ( len ( query . Result ) , ShouldEqual , 2 )
hit := query . Result [ 0 ]
2021-02-11 15:49:16 +08:00
So ( hit . ID , ShouldEqual , savedDash . Id )
So ( hit . URL , ShouldEqual , fmt . Sprintf ( "/d/%s/%s" , savedDash . Uid , savedDash . Slug ) )
So ( hit . FolderID , ShouldEqual , savedFolder . Id )
So ( hit . FolderUID , ShouldEqual , savedFolder . Uid )
2018-02-08 19:48:38 +08:00
So ( hit . FolderTitle , ShouldEqual , savedFolder . Title )
2021-02-11 15:49:16 +08:00
So ( hit . FolderURL , ShouldEqual , fmt . Sprintf ( "/dashboards/f/%s/%s" , savedFolder . Uid , savedFolder . Slug ) )
2018-02-08 19:48:38 +08:00
} )
Convey ( "Should be able to search for dashboard by dashboard ids" , func ( ) {
Convey ( "should be able to find two dashboards by id" , func ( ) {
query := search . FindPersistedDashboardsQuery {
2021-04-21 16:22:46 +08:00
DashboardIds : [ ] int64 { savedDash . Id , savedDash2 . Id } ,
2020-02-29 20:35:15 +08:00
SignedInUser : & models . SignedInUser { OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
2018-02-08 19:48:38 +08:00
}
err := SearchDashboards ( & query )
So ( err , ShouldBeNil )
So ( len ( query . Result ) , ShouldEqual , 2 )
hit := query . Result [ 0 ]
So ( len ( hit . Tags ) , ShouldEqual , 2 )
hit2 := query . Result [ 1 ]
So ( len ( hit2 . Tags ) , ShouldEqual , 1 )
} )
} )
2015-02-04 18:35:59 +08:00
Convey ( "Given two dashboards, one is starred dashboard by user 10, other starred by user 1" , func ( ) {
2021-03-17 23:06:10 +08:00
starredDash := insertTestDashboard ( t , sqlStore , "starred dash" , 1 , 0 , false )
2020-02-29 20:35:15 +08:00
err := StarDashboard ( & models . StarDashboardCommand {
2015-02-04 18:35:59 +08:00
DashboardId : starredDash . Id ,
UserId : 10 ,
} )
2019-10-22 20:08:18 +08:00
So ( err , ShouldBeNil )
2015-02-04 18:35:59 +08:00
2020-02-29 20:35:15 +08:00
err = StarDashboard ( & models . StarDashboardCommand {
2015-02-04 18:35:59 +08:00
DashboardId : savedDash . Id ,
UserId : 1 ,
} )
2019-10-22 20:08:18 +08:00
So ( err , ShouldBeNil )
2015-02-04 18:35:59 +08:00
Convey ( "Should be able to search for starred dashboards" , func ( ) {
2018-02-13 23:49:00 +08:00
query := search . FindPersistedDashboardsQuery {
2020-02-29 20:35:15 +08:00
SignedInUser : & models . SignedInUser { UserId : 10 , OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
2018-02-13 23:49:00 +08:00
IsStarred : true ,
}
2015-02-04 18:35:59 +08:00
err := SearchDashboards ( & query )
So ( err , ShouldBeNil )
So ( len ( query . Result ) , ShouldEqual , 1 )
So ( query . Result [ 0 ] . Title , ShouldEqual , "starred dash" )
} )
} )
2015-01-07 19:37:24 +08:00
} )
2017-06-17 08:33:53 +08:00
2017-11-28 00:08:39 +08:00
Convey ( "Given a plugin with imported dashboards" , func ( ) {
pluginId := "test-app"
2021-03-17 23:06:10 +08:00
appFolder := insertTestDashboardForPlugin ( t , sqlStore , "app-test" , 1 , 0 , true , pluginId )
insertTestDashboardForPlugin ( t , sqlStore , "app-dash1" , 1 , appFolder . Id , false , pluginId )
insertTestDashboardForPlugin ( t , sqlStore , "app-dash2" , 1 , appFolder . Id , false , pluginId )
2017-11-28 00:08:39 +08:00
Convey ( "Should return imported dashboard" , func ( ) {
2020-02-29 20:35:15 +08:00
query := models . GetDashboardsByPluginIdQuery {
2017-11-28 00:08:39 +08:00
PluginId : pluginId ,
OrgId : 1 ,
}
err := GetDashboardsByPluginId ( & query )
So ( err , ShouldBeNil )
So ( len ( query . Result ) , ShouldEqual , 2 )
} )
} )
2015-01-07 19:37:24 +08:00
} )
2015-01-06 00:04:29 +08:00
}
2017-06-17 08:33:53 +08:00
2020-05-06 17:42:52 +08:00
func TestDashboard_SortingOptions ( t * testing . T ) {
// insertTestDashboard uses GoConvey's assertions. Workaround.
Convey ( "test with multiple sorting options" , t , func ( ) {
2021-03-17 23:06:10 +08:00
sqlStore := InitTestDB ( t )
dashB := insertTestDashboard ( t , sqlStore , "Beta" , 1 , 0 , false )
dashA := insertTestDashboard ( t , sqlStore , "Alfa" , 1 , 0 , false )
2020-05-06 17:42:52 +08:00
assert . NotZero ( t , dashA . Id )
assert . Less ( t , dashB . Id , dashA . Id )
q := & search . FindPersistedDashboardsQuery {
SignedInUser : & models . SignedInUser { OrgId : 1 , UserId : 1 , OrgRole : models . ROLE_ADMIN } ,
// adding two sorting options (silly no-op example, but it'll complicate the query)
Filters : [ ] interface { } {
searchstore . TitleSorter { } ,
searchstore . TitleSorter { Descending : true } ,
} ,
}
dashboards , err := findDashboards ( q )
require . NoError ( t , err )
require . Len ( t , dashboards , 2 )
2021-02-11 15:49:16 +08:00
assert . Equal ( t , dashA . Id , dashboards [ 0 ] . ID )
assert . Equal ( t , dashB . Id , dashboards [ 1 ] . ID )
2020-05-06 17:42:52 +08:00
} )
}
2021-03-17 23:06:10 +08:00
func insertTestDashboard ( t * testing . T , sqlStore * SQLStore , title string , orgId int64 ,
folderId int64 , isFolder bool , tags ... interface { } ) * models . Dashboard {
2020-11-12 04:33:32 +08:00
t . Helper ( )
2020-02-29 20:35:15 +08:00
cmd := models . SaveDashboardCommand {
2017-06-24 05:22:09 +08:00
OrgId : orgId ,
FolderId : folderId ,
IsFolder : isFolder ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : title ,
"tags" : tags ,
} ) ,
}
2021-03-17 23:06:10 +08:00
dash , err := sqlStore . SaveDashboard ( cmd )
2020-11-12 04:33:32 +08:00
require . NoError ( t , err )
2021-03-17 23:06:10 +08:00
require . NotNil ( t , dash )
2017-06-24 05:22:09 +08:00
2021-03-17 23:06:10 +08:00
dash . Data . Set ( "id" , dash . Id )
dash . Data . Set ( "uid" , dash . Uid )
2018-02-19 18:12:56 +08:00
2021-03-17 23:06:10 +08:00
return dash
2017-06-24 05:22:09 +08:00
}
2021-07-22 17:27:13 +08:00
func insertTestRule ( t * testing . T , sqlStore * SQLStore , foderOrgID int64 , folderUID string ) {
sqlStore . WithDbSession ( context . Background ( ) , func ( sess * DBSession ) error {
type alertQuery struct {
RefID string
DatasourceUID string
Model json . RawMessage
}
type alertRule struct {
ID int64 ` xorm:"pk autoincr 'id'" `
OrgID int64 ` xorm:"org_id" `
Title string
Updated time . Time
UID string ` xorm:"uid" `
NamespaceUID string ` xorm:"namespace_uid" `
RuleGroup string
Condition string
Data [ ] alertQuery
}
rule := alertRule {
OrgID : foderOrgID ,
NamespaceUID : folderUID ,
UID : "rule" ,
RuleGroup : "rulegroup" ,
Updated : time . Now ( ) ,
Condition : "A" ,
Data : [ ] alertQuery {
{
RefID : "A" ,
DatasourceUID : "-100" ,
Model : json . RawMessage ( ` {
"type" : "math" ,
"expression" : "2 + 3 > 1"
} ` ) ,
} ,
} ,
}
_ , err := sess . Insert ( & rule )
require . NoError ( t , err )
type alertRuleVersion struct {
ID int64 ` xorm:"pk autoincr 'id'" `
RuleOrgID int64 ` xorm:"rule_org_id" `
RuleUID string ` xorm:"rule_uid" `
RuleNamespaceUID string ` xorm:"rule_namespace_uid" `
RuleGroup string
ParentVersion int64
RestoredFrom int64
Version int64
Created time . Time
Title string
Condition string
Data [ ] alertQuery
IntervalSeconds int64
}
ruleVersion := alertRuleVersion {
RuleOrgID : rule . OrgID ,
RuleUID : rule . UID ,
RuleNamespaceUID : rule . NamespaceUID ,
RuleGroup : rule . RuleGroup ,
Created : rule . Updated ,
Condition : rule . Condition ,
Data : rule . Data ,
ParentVersion : 0 ,
RestoredFrom : 0 ,
Version : 1 ,
IntervalSeconds : 60 ,
}
_ , err = sess . Insert ( & ruleVersion )
require . NoError ( t , err )
return err
} )
}
2021-03-17 23:06:10 +08:00
func insertTestDashboardForPlugin ( t * testing . T , sqlStore * SQLStore , title string , orgId int64 ,
folderId int64 , isFolder bool , pluginId string ) * models . Dashboard {
t . Helper ( )
2020-02-29 20:35:15 +08:00
cmd := models . SaveDashboardCommand {
2017-11-28 00:08:39 +08:00
OrgId : orgId ,
FolderId : folderId ,
IsFolder : isFolder ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : title ,
} ) ,
PluginId : pluginId ,
}
2021-03-17 23:06:10 +08:00
dash , err := sqlStore . SaveDashboard ( cmd )
2017-11-28 00:08:39 +08:00
So ( err , ShouldBeNil )
2021-03-17 23:06:10 +08:00
return dash
2017-11-28 00:08:39 +08:00
}
2021-03-17 23:06:10 +08:00
func createUser ( t * testing . T , sqlStore * SQLStore , name string , role string , isAdmin bool ) models . User {
2020-11-12 04:33:32 +08:00
t . Helper ( )
2017-06-17 08:33:53 +08:00
setting . AutoAssignOrg = true
2018-07-14 03:14:40 +08:00
setting . AutoAssignOrgId = 1
2017-06-17 08:33:53 +08:00
setting . AutoAssignOrgRole = role
2020-02-29 20:35:15 +08:00
currentUserCmd := models . CreateUserCommand { Login : name , Email : name + "@test.com" , Name : "a " + name , IsAdmin : isAdmin }
2021-03-17 23:06:10 +08:00
currentUser , err := sqlStore . CreateUser ( context . Background ( ) , currentUserCmd )
2020-11-12 04:33:32 +08:00
require . NoError ( t , err )
2017-06-17 08:33:53 +08:00
2021-03-17 23:06:10 +08:00
q1 := models . GetUserOrgListQuery { UserId : currentUser . Id }
2019-10-22 20:08:18 +08:00
err = GetUserOrgList ( & q1 )
2020-11-12 04:33:32 +08:00
require . NoError ( t , err )
require . Equal ( t , models . RoleType ( role ) , q1 . Result [ 0 ] . Role )
2017-06-17 08:33:53 +08:00
2021-03-17 23:06:10 +08:00
return * currentUser
2017-06-24 05:22:09 +08:00
}