2014-08-22 08:32:42 -05:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-03-11 17:13:06 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2018-03-27 13:24:11 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-01-01 06:29:40 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2014-08-22 08:32:42 -05:00
|
|
|
)
|
|
|
|
|
2021-01-01 06:29:40 -06:00
|
|
|
func TestGetDashboardUrl(t *testing.T) {
|
|
|
|
origAppURL := setting.AppUrl
|
|
|
|
t.Cleanup(func() { setting.AppUrl = origAppURL })
|
2018-03-27 13:24:11 -05:00
|
|
|
|
2021-01-01 06:29:40 -06:00
|
|
|
setting.AppUrl = ""
|
|
|
|
url := GetDashboardUrl("uid", "my-dashboard")
|
|
|
|
assert.Equal(t, "/d/uid/my-dashboard", url)
|
|
|
|
}
|
2014-08-22 08:32:42 -05:00
|
|
|
|
2021-01-01 06:29:40 -06:00
|
|
|
func TestGetFullDashboardUrl(t *testing.T) {
|
|
|
|
origAppURL := setting.AppUrl
|
|
|
|
t.Cleanup(func() { setting.AppUrl = origAppURL })
|
2020-04-21 04:31:10 -05:00
|
|
|
|
2021-01-01 06:29:40 -06:00
|
|
|
setting.AppUrl = "http://grafana.local/"
|
|
|
|
url := GetFullDashboardUrl("uid", "my-dashboard")
|
|
|
|
assert.Equal(t, "http://grafana.local/d/uid/my-dashboard", url)
|
|
|
|
}
|
2020-04-21 04:31:10 -05:00
|
|
|
|
2021-01-01 06:29:40 -06:00
|
|
|
func TestDashboard_UpdateSlug(t *testing.T) {
|
|
|
|
dashboard := NewDashboard("Grafana Play Home")
|
|
|
|
assert.Equal(t, "grafana-play-home", dashboard.Slug)
|
2017-12-27 09:32:39 -06:00
|
|
|
|
2021-01-01 06:29:40 -06:00
|
|
|
dashboard.UpdateSlug()
|
|
|
|
assert.Equal(t, "grafana-play-home", dashboard.Slug)
|
|
|
|
}
|
2015-06-12 02:04:10 -05:00
|
|
|
|
2021-01-01 06:29:40 -06:00
|
|
|
func TestNewDashboardFromJson(t *testing.T) {
|
|
|
|
json := simplejson.New()
|
|
|
|
json.Set("title", "test dash")
|
|
|
|
json.Set("tags", "")
|
2015-06-12 02:04:10 -05:00
|
|
|
|
2021-01-01 06:29:40 -06:00
|
|
|
dash := NewDashboardFromJson(json)
|
|
|
|
assert.Equal(t, "test dash", dash.Title)
|
|
|
|
require.Empty(t, dash.GetTags())
|
|
|
|
}
|
2015-06-12 02:04:10 -05:00
|
|
|
|
2021-01-01 06:29:40 -06:00
|
|
|
func TestSaveDashboardCommand_GetDashboardModel(t *testing.T) {
|
|
|
|
t.Run("should set IsFolder", func(t *testing.T) {
|
2017-06-02 04:49:27 -05:00
|
|
|
json := simplejson.New()
|
|
|
|
json.Set("title", "test dash")
|
|
|
|
|
|
|
|
cmd := &SaveDashboardCommand{Dashboard: json, IsFolder: true}
|
|
|
|
dash := cmd.GetDashboardModel()
|
|
|
|
|
2021-01-01 06:29:40 -06:00
|
|
|
assert.Equal(t, "test dash", dash.Title)
|
|
|
|
assert.True(t, dash.IsFolder)
|
2017-06-02 04:49:27 -05:00
|
|
|
})
|
|
|
|
|
2021-01-01 06:29:40 -06:00
|
|
|
t.Run("should set FolderId", func(t *testing.T) {
|
2017-06-02 04:49:27 -05:00
|
|
|
json := simplejson.New()
|
|
|
|
json.Set("title", "test dash")
|
|
|
|
|
2017-06-23 15:00:26 -05:00
|
|
|
cmd := &SaveDashboardCommand{Dashboard: json, FolderId: 1}
|
2017-06-02 04:49:27 -05:00
|
|
|
dash := cmd.GetDashboardModel()
|
|
|
|
|
2021-01-01 06:29:40 -06:00
|
|
|
assert.Equal(t, int64(1), dash.FolderId)
|
2017-06-02 04:49:27 -05:00
|
|
|
})
|
2014-08-22 08:32:42 -05:00
|
|
|
}
|
2021-01-01 06:29:40 -06:00
|
|
|
|
|
|
|
func TestSlugifyTitle(t *testing.T) {
|
|
|
|
testCases := map[string]string{
|
|
|
|
"Grafana Play Home": "grafana-play-home",
|
|
|
|
"snöräv-över-ån": "snorav-over-an",
|
|
|
|
"漢字": "han-zi", // Hanzi for hanzi
|
|
|
|
"🇦🇶": "8J-HpvCfh7Y", // flag of Antarctica-emoji, using fallback
|
|
|
|
"𒆠": "8JKGoA", // cuneiform Ki, using fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
for input, expected := range testCases {
|
|
|
|
t.Run(input, func(t *testing.T) {
|
|
|
|
slug := SlugifyTitle(input)
|
|
|
|
assert.Equal(t, expected, slug)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|