annotation_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. annotation := &annotations.Item{
  31. OrgId: 1,
  32. UserId: 1,
  33. DashboardId: 1,
  34. Text: "hello",
  35. Type: "alert",
  36. Epoch: 10,
  37. Tags: []string{"outage", "error", "type:outage", "server:server-1"},
  38. }
  39. err := repo.Save(annotation)
  40. So(err, ShouldBeNil)
  41. So(annotation.Id, ShouldBeGreaterThan, 0)
  42. Convey("Can query for annotation", func() {
  43. items, err := repo.Find(&annotations.ItemQuery{
  44. OrgId: 1,
  45. DashboardId: 1,
  46. From: 0,
  47. To: 15,
  48. })
  49. So(err, ShouldBeNil)
  50. So(items, ShouldHaveLength, 1)
  51. Convey("Can read tags", func() {
  52. So(items[0].Tags, ShouldResemble, []string{"outage", "error", "type:outage", "server:server-1"})
  53. })
  54. })
  55. Convey("Should not find any when item is outside time range", func() {
  56. items, err := repo.Find(&annotations.ItemQuery{
  57. OrgId: 1,
  58. DashboardId: 1,
  59. From: 12,
  60. To: 15,
  61. })
  62. So(err, ShouldBeNil)
  63. So(items, ShouldHaveLength, 0)
  64. })
  65. Convey("Should not find one when tag filter does not match", func() {
  66. items, err := repo.Find(&annotations.ItemQuery{
  67. OrgId: 1,
  68. DashboardId: 1,
  69. From: 1,
  70. To: 15,
  71. Tags: []string{"asd"},
  72. })
  73. So(err, ShouldBeNil)
  74. So(items, ShouldHaveLength, 0)
  75. })
  76. Convey("Should not find one when type filter does not match", func() {
  77. items, err := repo.Find(&annotations.ItemQuery{
  78. OrgId: 1,
  79. DashboardId: 1,
  80. From: 1,
  81. To: 15,
  82. Type: "alert",
  83. })
  84. So(err, ShouldBeNil)
  85. So(items, ShouldHaveLength, 0)
  86. })
  87. Convey("Should find one when all tag filters does match", func() {
  88. items, err := repo.Find(&annotations.ItemQuery{
  89. OrgId: 1,
  90. DashboardId: 1,
  91. From: 1,
  92. To: 15,
  93. Tags: []string{"outage", "error"},
  94. })
  95. So(err, ShouldBeNil)
  96. So(items, ShouldHaveLength, 1)
  97. })
  98. Convey("Should find one when all key value tag filters does match", func() {
  99. items, err := repo.Find(&annotations.ItemQuery{
  100. OrgId: 1,
  101. DashboardId: 1,
  102. From: 1,
  103. To: 15,
  104. Tags: []string{"type:outage", "server:server-1"},
  105. })
  106. So(err, ShouldBeNil)
  107. So(items, ShouldHaveLength, 1)
  108. })
  109. Convey("Can update annotation and remove all tags", func() {
  110. query := &annotations.ItemQuery{
  111. OrgId: 1,
  112. DashboardId: 1,
  113. From: 0,
  114. To: 15,
  115. }
  116. items, err := repo.Find(query)
  117. So(err, ShouldBeNil)
  118. annotationId := items[0].Id
  119. err = repo.Update(&annotations.Item{
  120. Id: annotationId,
  121. OrgId: 1,
  122. Text: "something new",
  123. Tags: []string{},
  124. })
  125. So(err, ShouldBeNil)
  126. items, err = repo.Find(query)
  127. So(err, ShouldBeNil)
  128. Convey("Can read tags", func() {
  129. So(items[0].Id, ShouldEqual, annotationId)
  130. So(len(items[0].Tags), ShouldEqual, 0)
  131. So(items[0].Text, ShouldEqual, "something new")
  132. })
  133. })
  134. Convey("Can update annotation with new tags", func() {
  135. query := &annotations.ItemQuery{
  136. OrgId: 1,
  137. DashboardId: 1,
  138. From: 0,
  139. To: 15,
  140. }
  141. items, err := repo.Find(query)
  142. So(err, ShouldBeNil)
  143. annotationId := items[0].Id
  144. err = repo.Update(&annotations.Item{
  145. Id: annotationId,
  146. OrgId: 1,
  147. Text: "something new",
  148. Tags: []string{"newtag1", "newtag2"},
  149. })
  150. So(err, ShouldBeNil)
  151. items, err = repo.Find(query)
  152. So(err, ShouldBeNil)
  153. Convey("Can read tags", func() {
  154. So(items[0].Id, ShouldEqual, annotationId)
  155. So(items[0].Tags, ShouldResemble, []string{"newtag1", "newtag2"})
  156. So(items[0].Text, ShouldEqual, "something new")
  157. })
  158. })
  159. Convey("Can delete annotation", func() {
  160. query := &annotations.ItemQuery{
  161. OrgId: 1,
  162. DashboardId: 1,
  163. From: 0,
  164. To: 15,
  165. }
  166. items, err := repo.Find(query)
  167. So(err, ShouldBeNil)
  168. annotationId := items[0].Id
  169. err = repo.Delete(&annotations.DeleteParams{Id: annotationId})
  170. items, err = repo.Find(query)
  171. So(err, ShouldBeNil)
  172. Convey("Should be deleted", func() {
  173. So(len(items), ShouldEqual, 0)
  174. })
  175. })
  176. })
  177. })
  178. }