github.com/esnet/gdg@v0.6.1-0.20240412190737-6b6eba9c14d8/internal/service/common_test.go (about) 1 package service 2 3 import ( 4 "github.com/esnet/gdg/internal/config" 5 "github.com/gosimple/slug" 6 "github.com/stretchr/testify/assert" 7 "os" 8 "strings" 9 "testing" 10 ) 11 12 func TestRelativePathLogin(t *testing.T) { 13 cwd, err := os.Getwd() 14 assert.Nil(t, err) 15 if strings.Contains(cwd, "service") { 16 os.Chdir("../..") 17 } 18 os.Setenv("GDG_CONTEXTS__TESTING__URL", "http://localhost:3000/grafana/") 19 config.InitGdgConfig("config/testing.yml", "'") 20 defer os.Unsetenv("GDG_CONTEXTS__TESTING__URL") 21 22 svc := NewApiService("dummy") 23 _, cfg := svc.(*DashNGoImpl).getNewClient() 24 assert.Equal(t, cfg.Host, "localhost:3000") 25 assert.Equal(t, cfg.BasePath, "/grafana/api") 26 27 } 28 29 // Validates the paths for the various entity types using the common 30 // code used to create folders and generate paths. 31 func TestSlug(t *testing.T) { 32 result := GetSlug("thisTestMoo") 33 assert.Equal(t, "thistestmoo", result) 34 //This 35 result = GetSlug("This Test Moo") 36 assert.Equal(t, "this-test-moo", result) 37 } 38 39 func TestUserPath(t *testing.T) { 40 err := os.Setenv("GDG_CONTEXT_NAME", "qa") 41 assert.Nil(t, err) 42 config.InitGdgConfig("testing.yml", "'") 43 path := BuildResourceFolder("", config.UserResource) 44 assert.Equal(t, "test/data/users/", path) 45 } 46 func TestBuildDashboardPath(t *testing.T) { 47 result := BuildResourceFolder("General", config.DashboardResource) 48 assert.Equal(t, "test/data/org_your-org/dashboards/General", result) 49 } 50 51 func TestBuildFolderSourcePath(t *testing.T) { 52 result := buildResourcePath(slug.Make("Some Folder"), config.FolderResource) 53 assert.Equal(t, "test/data/org_your-org/folders/some-folder.json", result) 54 55 } 56 57 func TestBuildDataSourcePath(t *testing.T) { 58 result := buildResourcePath(slug.Make("My DS"), config.ConnectionResource) 59 assert.Equal(t, "test/data/org_your-org/connections/my-ds.json", result) 60 }