datasources_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package api
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/bus"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. const (
  10. TestOrgID = 1
  11. TestUserID = 1
  12. )
  13. func TestDataSourcesProxy(t *testing.T) {
  14. Convey("Given a user is logged in", t, func() {
  15. loggedInUserScenario("When calling GET on", "/api/datasources/", func(sc *scenarioContext) {
  16. // Stubs the database query
  17. bus.AddHandler("test", func(query *models.GetDataSourcesQuery) error {
  18. So(query.OrgId, ShouldEqual, TestOrgID)
  19. query.Result = []*models.DataSource{
  20. {Name: "mmm"},
  21. {Name: "ZZZ"},
  22. {Name: "BBB"},
  23. {Name: "aaa"},
  24. }
  25. return nil
  26. })
  27. // handler func being tested
  28. sc.handlerFunc = GetDataSources
  29. sc.fakeReq("GET", "/api/datasources").exec()
  30. respJSON := []map[string]interface{}{}
  31. err := json.NewDecoder(sc.resp.Body).Decode(&respJSON)
  32. So(err, ShouldBeNil)
  33. Convey("should return list of datasources for org sorted alphabetically and case insensitively", func() {
  34. So(respJSON[0]["name"], ShouldEqual, "aaa")
  35. So(respJSON[1]["name"], ShouldEqual, "BBB")
  36. So(respJSON[2]["name"], ShouldEqual, "mmm")
  37. So(respJSON[3]["name"], ShouldEqual, "ZZZ")
  38. })
  39. })
  40. })
  41. }