annotation_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. annotation2 := &annotations.Item{
  43. OrgId: 1,
  44. UserId: 1,
  45. DashboardId: 2,
  46. Text: "hello",
  47. Type: "alert",
  48. Epoch: 20,
  49. Tags: []string{"outage", "error", "type:outage", "server:server-1"},
  50. RegionId: 1,
  51. }
  52. err = repo.Save(annotation2)
  53. So(err, ShouldBeNil)
  54. So(annotation2.Id, ShouldBeGreaterThan, 0)
  55. Convey("Can query for annotation", func() {
  56. items, err := repo.Find(&annotations.ItemQuery{
  57. OrgId: 1,
  58. DashboardId: 1,
  59. From: 0,
  60. To: 15,
  61. })
  62. So(err, ShouldBeNil)
  63. So(items, ShouldHaveLength, 1)
  64. Convey("Can read tags", func() {
  65. So(items[0].Tags, ShouldResemble, []string{"outage", "error", "type:outage", "server:server-1"})
  66. })
  67. Convey("Has created and updated values", func() {
  68. So(items[0].Created, ShouldBeGreaterThan, 0)
  69. So(items[0].Updated, ShouldBeGreaterThan, 0)
  70. So(items[0].Updated, ShouldEqual, items[0].Created)
  71. })
  72. })
  73. Convey("Can query for annotation by id", func() {
  74. items, err := repo.Find(&annotations.ItemQuery{
  75. OrgId: 1,
  76. AnnotationId: annotation2.Id,
  77. })
  78. So(err, ShouldBeNil)
  79. So(items, ShouldHaveLength, 1)
  80. So(items[0].Id, ShouldEqual, annotation2.Id)
  81. })
  82. Convey("Can query for annotation by region id", func() {
  83. items, err := repo.Find(&annotations.ItemQuery{
  84. OrgId: 1,
  85. RegionId: annotation2.RegionId,
  86. })
  87. So(err, ShouldBeNil)
  88. So(items, ShouldHaveLength, 1)
  89. So(items[0].Id, ShouldEqual, annotation2.Id)
  90. })
  91. Convey("Should not find any when item is outside time range", func() {
  92. items, err := repo.Find(&annotations.ItemQuery{
  93. OrgId: 1,
  94. DashboardId: 1,
  95. From: 12,
  96. To: 15,
  97. })
  98. So(err, ShouldBeNil)
  99. So(items, ShouldHaveLength, 0)
  100. })
  101. Convey("Should not find one when tag filter does not match", func() {
  102. items, err := repo.Find(&annotations.ItemQuery{
  103. OrgId: 1,
  104. DashboardId: 1,
  105. From: 1,
  106. To: 15,
  107. Tags: []string{"asd"},
  108. })
  109. So(err, ShouldBeNil)
  110. So(items, ShouldHaveLength, 0)
  111. })
  112. Convey("Should not find one when type filter does not match", func() {
  113. items, err := repo.Find(&annotations.ItemQuery{
  114. OrgId: 1,
  115. DashboardId: 1,
  116. From: 1,
  117. To: 15,
  118. Type: "alert",
  119. })
  120. So(err, ShouldBeNil)
  121. So(items, ShouldHaveLength, 0)
  122. })
  123. Convey("Should find one when all tag filters does match", func() {
  124. items, err := repo.Find(&annotations.ItemQuery{
  125. OrgId: 1,
  126. DashboardId: 1,
  127. From: 1,
  128. To: 15,
  129. Tags: []string{"outage", "error"},
  130. })
  131. So(err, ShouldBeNil)
  132. So(items, ShouldHaveLength, 1)
  133. })
  134. Convey("Should find one when all key value tag filters does match", func() {
  135. items, err := repo.Find(&annotations.ItemQuery{
  136. OrgId: 1,
  137. DashboardId: 1,
  138. From: 1,
  139. To: 15,
  140. Tags: []string{"type:outage", "server:server-1"},
  141. })
  142. So(err, ShouldBeNil)
  143. So(items, ShouldHaveLength, 1)
  144. })
  145. Convey("Can update annotation and remove all tags", 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.Update(&annotations.Item{
  156. Id: annotationId,
  157. OrgId: 1,
  158. Text: "something new",
  159. Tags: []string{},
  160. })
  161. So(err, ShouldBeNil)
  162. items, err = repo.Find(query)
  163. So(err, ShouldBeNil)
  164. Convey("Can read tags", func() {
  165. So(items[0].Id, ShouldEqual, annotationId)
  166. So(len(items[0].Tags), ShouldEqual, 0)
  167. So(items[0].Text, ShouldEqual, "something new")
  168. })
  169. })
  170. Convey("Can update annotation with new tags", func() {
  171. query := &annotations.ItemQuery{
  172. OrgId: 1,
  173. DashboardId: 1,
  174. From: 0,
  175. To: 15,
  176. }
  177. items, err := repo.Find(query)
  178. So(err, ShouldBeNil)
  179. annotationId := items[0].Id
  180. err = repo.Update(&annotations.Item{
  181. Id: annotationId,
  182. OrgId: 1,
  183. Text: "something new",
  184. Tags: []string{"newtag1", "newtag2"},
  185. })
  186. So(err, ShouldBeNil)
  187. items, err = repo.Find(query)
  188. So(err, ShouldBeNil)
  189. Convey("Can read tags", func() {
  190. So(items[0].Id, ShouldEqual, annotationId)
  191. So(items[0].Tags, ShouldResemble, []string{"newtag1", "newtag2"})
  192. So(items[0].Text, ShouldEqual, "something new")
  193. })
  194. Convey("Updated time has increased", func() {
  195. So(items[0].Updated, ShouldBeGreaterThan, items[0].Created)
  196. })
  197. })
  198. Convey("Can delete annotation", func() {
  199. query := &annotations.ItemQuery{
  200. OrgId: 1,
  201. DashboardId: 1,
  202. From: 0,
  203. To: 15,
  204. }
  205. items, err := repo.Find(query)
  206. So(err, ShouldBeNil)
  207. annotationId := items[0].Id
  208. err = repo.Delete(&annotations.DeleteParams{Id: annotationId})
  209. So(err, ShouldBeNil)
  210. items, err = repo.Find(query)
  211. So(err, ShouldBeNil)
  212. Convey("Should be deleted", func() {
  213. So(len(items), ShouldEqual, 0)
  214. })
  215. })
  216. })
  217. })
  218. }