annotation_test.go 4.7 KB

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