annotation_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. InitTestDB(t)
  10. Convey("Testing annotation saving/loading", t, func() {
  11. repo := SqlAnnotationRepo{}
  12. Convey("Can save tags", func() {
  13. Reset(func() {
  14. _, err := x.Exec("DELETE FROM annotation_tag WHERE 1=1")
  15. So(err, ShouldBeNil)
  16. })
  17. tagPairs := []*models.Tag{
  18. {Key: "outage"},
  19. {Key: "type", Value: "outage"},
  20. {Key: "server", Value: "server-1"},
  21. {Key: "error"},
  22. }
  23. tags, err := repo.ensureTagsExist(newSession(), tagPairs)
  24. So(err, ShouldBeNil)
  25. So(len(tags), ShouldEqual, 4)
  26. })
  27. })
  28. }
  29. func TestAnnotations(t *testing.T) {
  30. InitTestDB(t)
  31. Convey("Testing annotation saving/loading", t, func() {
  32. repo := SqlAnnotationRepo{}
  33. Convey("Can save annotation", func() {
  34. Reset(func() {
  35. _, err := x.Exec("DELETE FROM annotation WHERE 1=1")
  36. So(err, ShouldBeNil)
  37. _, err = x.Exec("DELETE FROM annotation_tag WHERE 1=1")
  38. So(err, ShouldBeNil)
  39. })
  40. annotation := &annotations.Item{
  41. OrgId: 1,
  42. UserId: 1,
  43. DashboardId: 1,
  44. Text: "hello",
  45. Type: "alert",
  46. Epoch: 10,
  47. Tags: []string{"outage", "error", "type:outage", "server:server-1"},
  48. }
  49. err := repo.Save(annotation)
  50. So(err, ShouldBeNil)
  51. So(annotation.Id, ShouldBeGreaterThan, 0)
  52. annotation2 := &annotations.Item{
  53. OrgId: 1,
  54. UserId: 1,
  55. DashboardId: 2,
  56. Text: "hello",
  57. Type: "alert",
  58. Epoch: 20,
  59. Tags: []string{"outage", "error", "type:outage", "server:server-1"},
  60. RegionId: 1,
  61. }
  62. err = repo.Save(annotation2)
  63. So(err, ShouldBeNil)
  64. So(annotation2.Id, ShouldBeGreaterThan, 0)
  65. Convey("Can query for annotation", func() {
  66. items, err := repo.Find(&annotations.ItemQuery{
  67. OrgId: 1,
  68. DashboardId: 1,
  69. From: 0,
  70. To: 15,
  71. })
  72. So(err, ShouldBeNil)
  73. So(items, ShouldHaveLength, 1)
  74. Convey("Can read tags", func() {
  75. So(items[0].Tags, ShouldResemble, []string{"outage", "error", "type:outage", "server:server-1"})
  76. })
  77. Convey("Has created and updated values", func() {
  78. So(items[0].Created, ShouldBeGreaterThan, 0)
  79. So(items[0].Updated, ShouldBeGreaterThan, 0)
  80. So(items[0].Updated, ShouldEqual, items[0].Created)
  81. })
  82. })
  83. Convey("Can query for annotation by id", func() {
  84. items, err := repo.Find(&annotations.ItemQuery{
  85. OrgId: 1,
  86. AnnotationId: annotation2.Id,
  87. })
  88. So(err, ShouldBeNil)
  89. So(items, ShouldHaveLength, 1)
  90. So(items[0].Id, ShouldEqual, annotation2.Id)
  91. })
  92. Convey("Can query for annotation by region id", func() {
  93. items, err := repo.Find(&annotations.ItemQuery{
  94. OrgId: 1,
  95. RegionId: annotation2.RegionId,
  96. })
  97. So(err, ShouldBeNil)
  98. So(items, ShouldHaveLength, 1)
  99. So(items[0].Id, ShouldEqual, annotation2.Id)
  100. })
  101. Convey("Should not find any when item is outside time range", func() {
  102. items, err := repo.Find(&annotations.ItemQuery{
  103. OrgId: 1,
  104. DashboardId: 1,
  105. From: 12,
  106. To: 15,
  107. })
  108. So(err, ShouldBeNil)
  109. So(items, ShouldHaveLength, 0)
  110. })
  111. Convey("Should not find one when tag filter does not match", func() {
  112. items, err := repo.Find(&annotations.ItemQuery{
  113. OrgId: 1,
  114. DashboardId: 1,
  115. From: 1,
  116. To: 15,
  117. Tags: []string{"asd"},
  118. })
  119. So(err, ShouldBeNil)
  120. So(items, ShouldHaveLength, 0)
  121. })
  122. Convey("Should not find one when type filter does not match", func() {
  123. items, err := repo.Find(&annotations.ItemQuery{
  124. OrgId: 1,
  125. DashboardId: 1,
  126. From: 1,
  127. To: 15,
  128. Type: "alert",
  129. })
  130. So(err, ShouldBeNil)
  131. So(items, ShouldHaveLength, 0)
  132. })
  133. Convey("Should find one when all tag filters does match", func() {
  134. items, err := repo.Find(&annotations.ItemQuery{
  135. OrgId: 1,
  136. DashboardId: 1,
  137. From: 1,
  138. To: 15,
  139. Tags: []string{"outage", "error"},
  140. })
  141. So(err, ShouldBeNil)
  142. So(items, ShouldHaveLength, 1)
  143. })
  144. Convey("Should find one when all key value tag filters does match", func() {
  145. items, err := repo.Find(&annotations.ItemQuery{
  146. OrgId: 1,
  147. DashboardId: 1,
  148. From: 1,
  149. To: 15,
  150. Tags: []string{"type:outage", "server:server-1"},
  151. })
  152. So(err, ShouldBeNil)
  153. So(items, ShouldHaveLength, 1)
  154. })
  155. Convey("Can update annotation and remove all tags", func() {
  156. query := &annotations.ItemQuery{
  157. OrgId: 1,
  158. DashboardId: 1,
  159. From: 0,
  160. To: 15,
  161. }
  162. items, err := repo.Find(query)
  163. So(err, ShouldBeNil)
  164. annotationId := items[0].Id
  165. err = repo.Update(&annotations.Item{
  166. Id: annotationId,
  167. OrgId: 1,
  168. Text: "something new",
  169. Tags: []string{},
  170. })
  171. So(err, ShouldBeNil)
  172. items, err = repo.Find(query)
  173. So(err, ShouldBeNil)
  174. Convey("Can read tags", func() {
  175. So(items[0].Id, ShouldEqual, annotationId)
  176. So(len(items[0].Tags), ShouldEqual, 0)
  177. So(items[0].Text, ShouldEqual, "something new")
  178. })
  179. })
  180. Convey("Can update annotation with new tags", func() {
  181. query := &annotations.ItemQuery{
  182. OrgId: 1,
  183. DashboardId: 1,
  184. From: 0,
  185. To: 15,
  186. }
  187. items, err := repo.Find(query)
  188. So(err, ShouldBeNil)
  189. annotationId := items[0].Id
  190. err = repo.Update(&annotations.Item{
  191. Id: annotationId,
  192. OrgId: 1,
  193. Text: "something new",
  194. Tags: []string{"newtag1", "newtag2"},
  195. })
  196. So(err, ShouldBeNil)
  197. items, err = repo.Find(query)
  198. So(err, ShouldBeNil)
  199. Convey("Can read tags", func() {
  200. So(items[0].Id, ShouldEqual, annotationId)
  201. So(items[0].Tags, ShouldResemble, []string{"newtag1", "newtag2"})
  202. So(items[0].Text, ShouldEqual, "something new")
  203. })
  204. Convey("Updated time has increased", func() {
  205. So(items[0].Updated, ShouldBeGreaterThan, items[0].Created)
  206. })
  207. })
  208. Convey("Can delete annotation", func() {
  209. query := &annotations.ItemQuery{
  210. OrgId: 1,
  211. DashboardId: 1,
  212. From: 0,
  213. To: 15,
  214. }
  215. items, err := repo.Find(query)
  216. So(err, ShouldBeNil)
  217. annotationId := items[0].Id
  218. err = repo.Delete(&annotations.DeleteParams{Id: annotationId})
  219. So(err, ShouldBeNil)
  220. items, err = repo.Find(query)
  221. So(err, ShouldBeNil)
  222. Convey("Should be deleted", func() {
  223. So(len(items), ShouldEqual, 0)
  224. })
  225. })
  226. })
  227. })
  228. }