Commit f0b169be by Emil Hessman Committed by GitHub

Chore: Convert sqlstore annotation test from GoConvey to testify (#28715)

* Chore: Convert sqlstore annotation test from GoConvey to testify

* Use require.NoError

* Use assert.Empty

* Use assert.NoError in test cleanup
parent 24a98213
......@@ -5,7 +5,8 @@ package sqlstore
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/services/annotations"
)
......@@ -14,16 +15,14 @@ func TestAnnotations(t *testing.T) {
mockTimeNow()
defer resetTimeNow()
InitTestDB(t)
Convey("Testing annotation saving/loading", t, func() {
repo := SqlAnnotationRepo{}
Convey("Can save annotation", func() {
Reset(func() {
t.Run("Testing annotation create, read, update and delete", func(t *testing.T) {
t.Cleanup(func() {
_, err := x.Exec("DELETE FROM annotation WHERE 1=1")
So(err, ShouldBeNil)
assert.NoError(t, err)
_, err = x.Exec("DELETE FROM annotation_tag WHERE 1=1")
So(err, ShouldBeNil)
assert.NoError(t, err)
})
annotation := &annotations.Item{
......@@ -36,10 +35,9 @@ func TestAnnotations(t *testing.T) {
Tags: []string{"outage", "error", "type:outage", "server:server-1"},
}
err := repo.Save(annotation)
So(err, ShouldBeNil)
So(annotation.Id, ShouldBeGreaterThan, 0)
So(annotation.Epoch, ShouldEqual, annotation.EpochEnd)
require.NoError(t, err)
assert.Greater(t, annotation.Id, int64(0))
assert.Equal(t, annotation.Epoch, annotation.EpochEnd)
annotation2 := &annotations.Item{
OrgId: 1,
......@@ -52,10 +50,10 @@ func TestAnnotations(t *testing.T) {
Tags: []string{"outage", "error", "type:outage", "server:server-1"},
}
err = repo.Save(annotation2)
So(err, ShouldBeNil)
So(annotation2.Id, ShouldBeGreaterThan, 0)
So(annotation2.Epoch, ShouldEqual, 20)
So(annotation2.EpochEnd, ShouldEqual, 21)
require.NoError(t, err)
assert.Greater(t, annotation2.Id, int64(0))
assert.Equal(t, int64(20), annotation2.Epoch)
assert.Equal(t, int64(21), annotation2.EpochEnd)
globalAnnotation1 := &annotations.Item{
OrgId: 1,
......@@ -66,8 +64,8 @@ func TestAnnotations(t *testing.T) {
Tags: []string{"deploy"},
}
err = repo.Save(globalAnnotation1)
So(err, ShouldBeNil)
So(globalAnnotation1.Id, ShouldBeGreaterThan, 0)
require.NoError(t, err)
assert.Greater(t, globalAnnotation1.Id, int64(0))
globalAnnotation2 := &annotations.Item{
OrgId: 1,
......@@ -78,10 +76,10 @@ func TestAnnotations(t *testing.T) {
Tags: []string{"rollback"},
}
err = repo.Save(globalAnnotation2)
So(err, ShouldBeNil)
So(globalAnnotation2.Id, ShouldBeGreaterThan, 0)
require.NoError(t, err)
assert.Greater(t, globalAnnotation2.Id, int64(0))
Convey("Can query for annotation by dashboard id", func() {
t.Run("Can query for annotation by dashboard id", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
......@@ -89,44 +87,38 @@ func TestAnnotations(t *testing.T) {
To: 15,
})
So(err, ShouldBeNil)
So(items, ShouldHaveLength, 1)
require.NoError(t, err)
assert.Len(t, items, 1)
Convey("Can read tags", func() {
So(items[0].Tags, ShouldResemble, []string{"outage", "error", "type:outage", "server:server-1"})
})
assert.Equal(t, []string{"outage", "error", "type:outage", "server:server-1"}, items[0].Tags)
Convey("Has created and updated values", func() {
So(items[0].Created, ShouldBeGreaterThan, 0)
So(items[0].Updated, ShouldBeGreaterThan, 0)
So(items[0].Updated, ShouldEqual, items[0].Created)
})
assert.GreaterOrEqual(t, items[0].Created, int64(0))
assert.GreaterOrEqual(t, items[0].Updated, int64(0))
assert.Equal(t, items[0].Updated, items[0].Created)
})
Convey("Can query for annotation by id", func() {
t.Run("Can query for annotation by id", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
OrgId: 1,
AnnotationId: annotation2.Id,
})
So(err, ShouldBeNil)
So(items, ShouldHaveLength, 1)
So(items[0].Id, ShouldEqual, annotation2.Id)
require.NoError(t, err)
assert.Len(t, items, 1)
assert.Equal(t, annotation2.Id, items[0].Id)
})
Convey("Should not find any when item is outside time range", func() {
t.Run("Should not find any when item is outside time range", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
From: 12,
To: 15,
})
So(err, ShouldBeNil)
So(items, ShouldHaveLength, 0)
require.NoError(t, err)
assert.Empty(t, items)
})
Convey("Should not find one when tag filter does not match", func() {
t.Run("Should not find one when tag filter does not match", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
......@@ -134,12 +126,11 @@ func TestAnnotations(t *testing.T) {
To: 15,
Tags: []string{"asd"},
})
So(err, ShouldBeNil)
So(items, ShouldHaveLength, 0)
require.NoError(t, err)
assert.Empty(t, items)
})
Convey("Should not find one when type filter does not match", func() {
t.Run("Should not find one when type filter does not match", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
......@@ -147,12 +138,11 @@ func TestAnnotations(t *testing.T) {
To: 15,
Type: "alert",
})
So(err, ShouldBeNil)
So(items, ShouldHaveLength, 0)
require.NoError(t, err)
assert.Empty(t, items)
})
Convey("Should find one when all tag filters does match", func() {
t.Run("Should find one when all tag filters does match", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
......@@ -160,12 +150,11 @@ func TestAnnotations(t *testing.T) {
To: 15, // this will exclude the second test annotation
Tags: []string{"outage", "error"},
})
So(err, ShouldBeNil)
So(items, ShouldHaveLength, 1)
require.NoError(t, err)
assert.Len(t, items, 1)
})
Convey("Should find two annotations using partial match", func() {
t.Run("Should find two annotations using partial match", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
OrgId: 1,
From: 1,
......@@ -173,12 +162,11 @@ func TestAnnotations(t *testing.T) {
MatchAny: true,
Tags: []string{"rollback", "deploy"},
})
So(err, ShouldBeNil)
So(items, ShouldHaveLength, 2)
require.NoError(t, err)
assert.Len(t, items, 2)
})
Convey("Should find one when all key value tag filters does match", func() {
t.Run("Should find one when all key value tag filters does match", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
......@@ -186,12 +174,11 @@ func TestAnnotations(t *testing.T) {
To: 15,
Tags: []string{"type:outage", "server:server-1"},
})
So(err, ShouldBeNil)
So(items, ShouldHaveLength, 1)
require.NoError(t, err)
assert.Len(t, items, 1)
})
Convey("Can update annotation and remove all tags", func() {
t.Run("Can update annotation and remove all tags", func(t *testing.T) {
query := &annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
......@@ -199,32 +186,26 @@ func TestAnnotations(t *testing.T) {
To: 15,
}
items, err := repo.Find(query)
So(err, ShouldBeNil)
require.NoError(t, err)
annotationId := items[0].Id
err = repo.Update(&annotations.Item{
Id: annotationId,
OrgId: 1,
Text: "something new",
Tags: []string{},
})
So(err, ShouldBeNil)
require.NoError(t, err)
items, err = repo.Find(query)
require.NoError(t, err)
So(err, ShouldBeNil)
Convey("Can read tags", func() {
So(items[0].Id, ShouldEqual, annotationId)
So(len(items[0].Tags), ShouldEqual, 0)
So(items[0].Text, ShouldEqual, "something new")
})
assert.Equal(t, annotationId, items[0].Id)
assert.Empty(t, items[0].Tags)
assert.Equal(t, "something new", items[0].Text)
})
Convey("Can update annotation with new tags", func() {
t.Run("Can update annotation with new tags", func(t *testing.T) {
query := &annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
......@@ -232,36 +213,27 @@ func TestAnnotations(t *testing.T) {
To: 15,
}
items, err := repo.Find(query)
So(err, ShouldBeNil)
require.NoError(t, err)
annotationId := items[0].Id
err = repo.Update(&annotations.Item{
Id: annotationId,
OrgId: 1,
Text: "something new",
Tags: []string{"newtag1", "newtag2"},
})
So(err, ShouldBeNil)
require.NoError(t, err)
items, err = repo.Find(query)
require.NoError(t, err)
So(err, ShouldBeNil)
Convey("Can read tags", func() {
So(items[0].Id, ShouldEqual, annotationId)
So(items[0].Tags, ShouldResemble, []string{"newtag1", "newtag2"})
So(items[0].Text, ShouldEqual, "something new")
assert.Equal(t, annotationId, items[0].Id)
assert.Equal(t, []string{"newtag1", "newtag2"}, items[0].Tags)
assert.Equal(t, "something new", items[0].Text)
assert.Greater(t, items[0].Updated, items[0].Created)
})
Convey("Updated time has increased", func() {
So(items[0].Updated, ShouldBeGreaterThan, items[0].Created)
})
})
Convey("Can delete annotation", func() {
t.Run("Can delete annotation", func(t *testing.T) {
query := &annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
......@@ -269,20 +241,15 @@ func TestAnnotations(t *testing.T) {
To: 15,
}
items, err := repo.Find(query)
So(err, ShouldBeNil)
require.NoError(t, err)
annotationId := items[0].Id
err = repo.Delete(&annotations.DeleteParams{Id: annotationId, OrgId: 1})
So(err, ShouldBeNil)
require.NoError(t, err)
items, err = repo.Find(query)
So(err, ShouldBeNil)
Convey("Should be deleted", func() {
So(len(items), ShouldEqual, 0)
})
})
require.NoError(t, err)
assert.Empty(t, items)
})
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment