annotation_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package sqlstore
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/services/annotations"
  7. )
  8. func TestSavingTags(t *testing.T) {
  9. Convey("Testing annotation saving/loading", t, func() {
  10. InitTestDB(t)
  11. repo := SqlAnnotationRepo{}
  12. Convey("Can save tags", func() {
  13. tagPairs := []*models.Tag{
  14. {Key: "outage"},
  15. {Key: "type", Value: "outage"},
  16. {Key: "server", Value: "server-1"},
  17. {Key: "error"},
  18. }
  19. tags, err := repo.ensureTagsExist(newSession(), tagPairs)
  20. So(err, ShouldBeNil)
  21. So(len(tags), ShouldEqual, 4)
  22. })
  23. })
  24. }
  25. func TestAnnotations(t *testing.T) {
  26. Convey("Testing annotation saving/loading", t, func() {
  27. InitTestDB(t)
  28. repo := SqlAnnotationRepo{}
  29. Convey("Can save annotation", func() {
  30. err := repo.Save(&annotations.Item{
  31. OrgId: 1,
  32. UserId: 1,
  33. DashboardId: 1,
  34. Text: "hello",
  35. Epoch: 10,
  36. Tags: []string{"outage", "error", "type:outage", "server:server-1"},
  37. })
  38. So(err, ShouldBeNil)
  39. Convey("Can query for annotation", func() {
  40. items, err := repo.Find(&annotations.ItemQuery{
  41. OrgId: 1,
  42. DashboardId: 1,
  43. From: 0,
  44. To: 15,
  45. })
  46. So(err, ShouldBeNil)
  47. So(items, ShouldHaveLength, 1)
  48. Convey("Can read tags", func() {
  49. So(items[0].Tags, ShouldResemble, []string{"outage", "error", "type:outage", "server:server-1"})
  50. })
  51. })
  52. Convey("Should not find any when item is outside time range", func() {
  53. items, err := repo.Find(&annotations.ItemQuery{
  54. OrgId: 1,
  55. DashboardId: 1,
  56. From: 12,
  57. To: 15,
  58. })
  59. So(err, ShouldBeNil)
  60. So(items, ShouldHaveLength, 0)
  61. })
  62. Convey("Should not find one when tag filter does not match", func() {
  63. items, err := repo.Find(&annotations.ItemQuery{
  64. OrgId: 1,
  65. DashboardId: 1,
  66. From: 1,
  67. To: 15,
  68. Tags: []string{"asd"},
  69. })
  70. So(err, ShouldBeNil)
  71. So(items, ShouldHaveLength, 0)
  72. })
  73. Convey("Should find one when all tag filters does match", func() {
  74. items, err := repo.Find(&annotations.ItemQuery{
  75. OrgId: 1,
  76. DashboardId: 1,
  77. From: 1,
  78. To: 15,
  79. Tags: []string{"outage", "error"},
  80. })
  81. So(err, ShouldBeNil)
  82. So(items, ShouldHaveLength, 1)
  83. })
  84. Convey("Should find one when all key value tag filters does match", func() {
  85. items, err := repo.Find(&annotations.ItemQuery{
  86. OrgId: 1,
  87. DashboardId: 1,
  88. From: 1,
  89. To: 15,
  90. Tags: []string{"type:outage", "server:server-1"},
  91. })
  92. So(err, ShouldBeNil)
  93. So(items, ShouldHaveLength, 1)
  94. })
  95. Convey("Can update annotation and remove all tags", func() {
  96. query := &annotations.ItemQuery{
  97. OrgId: 1,
  98. DashboardId: 1,
  99. From: 0,
  100. To: 15,
  101. }
  102. items, err := repo.Find(query)
  103. So(err, ShouldBeNil)
  104. annotationId := items[0].Id
  105. err = repo.Update(&annotations.Item{
  106. Id: annotationId,
  107. OrgId: 1,
  108. Text: "something new",
  109. Tags: []string{},
  110. })
  111. So(err, ShouldBeNil)
  112. items, err = repo.Find(query)
  113. So(err, ShouldBeNil)
  114. Convey("Can read tags", func() {
  115. So(items[0].Id, ShouldEqual, annotationId)
  116. So(len(items[0].Tags), ShouldEqual, 0)
  117. So(items[0].Text, ShouldEqual, "something new")
  118. })
  119. })
  120. Convey("Can update annotation with new tags", func() {
  121. query := &annotations.ItemQuery{
  122. OrgId: 1,
  123. DashboardId: 1,
  124. From: 0,
  125. To: 15,
  126. }
  127. items, err := repo.Find(query)
  128. So(err, ShouldBeNil)
  129. annotationId := items[0].Id
  130. err = repo.Update(&annotations.Item{
  131. Id: annotationId,
  132. OrgId: 1,
  133. Text: "something new",
  134. Tags: []string{"newtag1", "newtag2"},
  135. })
  136. So(err, ShouldBeNil)
  137. items, err = repo.Find(query)
  138. So(err, ShouldBeNil)
  139. Convey("Can read tags", func() {
  140. So(items[0].Id, ShouldEqual, annotationId)
  141. So(items[0].Tags, ShouldResemble, []string{"newtag1", "newtag2"})
  142. So(items[0].Text, ShouldEqual, "something new")
  143. })
  144. })
  145. Convey("Can delete annotation", func() {
  146. query := &annotations.ItemQuery{
  147. OrgId: 1,
  148. DashboardId: 1,
  149. From: 0,
  150. To: 15,
  151. }
  152. items, err := repo.Find(query)
  153. So(err, ShouldBeNil)
  154. annotationId := items[0].Id
  155. err = repo.Delete(&annotations.DeleteParams{Id: annotationId})
  156. items, err = repo.Find(query)
  157. So(err, ShouldBeNil)
  158. Convey("Should be deleted", func() {
  159. So(len(items), ShouldEqual, 0)
  160. })
  161. })
  162. })
  163. })
  164. }