remove global variable in annotation (#46746)

* remove global varaible in annotation

* remove todo

* replace intransaction with withdbtransaction

* fix typo
This commit is contained in:
ying-jeanne
2022-03-22 19:20:57 +08:00
committed by GitHub
parent 3d43535bc3
commit adc0cbf176
8 changed files with 157 additions and 152 deletions
+22 -22
View File
@@ -4,10 +4,10 @@
package sqlstore
import (
"testing"
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"github.com/grafana/grafana/pkg/services/annotations"
)
@@ -15,8 +15,9 @@ import (
func TestAnnotations(t *testing.T) {
mockTimeNow()
defer resetTimeNow()
InitTestDB(t)
repo := SQLAnnotationRepo{}
repo.sql = InitTestDB(t)
t.Run("Testing annotation create, read, update and delete", func(t *testing.T) {
t.Cleanup(func() {
@@ -79,9 +80,8 @@ func TestAnnotations(t *testing.T) {
err = repo.Save(globalAnnotation2)
require.NoError(t, err)
assert.Greater(t, globalAnnotation2.Id, int64(0))
t.Run("Can query for annotation by dashboard id", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
items, err := repo.Find(context.Background(), &annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
From: 0,
@@ -99,7 +99,7 @@ func TestAnnotations(t *testing.T) {
})
t.Run("Can query for annotation by id", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
items, err := repo.Find(context.Background(), &annotations.ItemQuery{
OrgId: 1,
AnnotationId: annotation2.Id,
})
@@ -109,7 +109,7 @@ func TestAnnotations(t *testing.T) {
})
t.Run("Should not find any when item is outside time range", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
items, err := repo.Find(context.Background(), &annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
From: 12,
@@ -120,7 +120,7 @@ func TestAnnotations(t *testing.T) {
})
t.Run("Should not find one when tag filter does not match", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
items, err := repo.Find(context.Background(), &annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
From: 1,
@@ -132,7 +132,7 @@ func TestAnnotations(t *testing.T) {
})
t.Run("Should not find one when type filter does not match", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
items, err := repo.Find(context.Background(), &annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
From: 1,
@@ -144,7 +144,7 @@ func TestAnnotations(t *testing.T) {
})
t.Run("Should find one when all tag filters does match", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
items, err := repo.Find(context.Background(), &annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
From: 1,
@@ -156,7 +156,7 @@ func TestAnnotations(t *testing.T) {
})
t.Run("Should find two annotations using partial match", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
items, err := repo.Find(context.Background(), &annotations.ItemQuery{
OrgId: 1,
From: 1,
To: 25,
@@ -168,7 +168,7 @@ func TestAnnotations(t *testing.T) {
})
t.Run("Should find one when all key value tag filters does match", func(t *testing.T) {
items, err := repo.Find(&annotations.ItemQuery{
items, err := repo.Find(context.Background(), &annotations.ItemQuery{
OrgId: 1,
DashboardId: 1,
From: 1,
@@ -186,11 +186,11 @@ func TestAnnotations(t *testing.T) {
From: 0,
To: 15,
}
items, err := repo.Find(query)
items, err := repo.Find(context.Background(), query)
require.NoError(t, err)
annotationId := items[0].Id
err = repo.Update(&annotations.Item{
err = repo.Update(context.Background(), &annotations.Item{
Id: annotationId,
OrgId: 1,
Text: "something new",
@@ -198,7 +198,7 @@ func TestAnnotations(t *testing.T) {
})
require.NoError(t, err)
items, err = repo.Find(query)
items, err = repo.Find(context.Background(), query)
require.NoError(t, err)
assert.Equal(t, annotationId, items[0].Id)
@@ -213,11 +213,11 @@ func TestAnnotations(t *testing.T) {
From: 0,
To: 15,
}
items, err := repo.Find(query)
items, err := repo.Find(context.Background(), query)
require.NoError(t, err)
annotationId := items[0].Id
err = repo.Update(&annotations.Item{
err = repo.Update(context.Background(), &annotations.Item{
Id: annotationId,
OrgId: 1,
Text: "something new",
@@ -225,7 +225,7 @@ func TestAnnotations(t *testing.T) {
})
require.NoError(t, err)
items, err = repo.Find(query)
items, err = repo.Find(context.Background(), query)
require.NoError(t, err)
assert.Equal(t, annotationId, items[0].Id)
@@ -241,14 +241,14 @@ func TestAnnotations(t *testing.T) {
From: 0,
To: 15,
}
items, err := repo.Find(query)
items, err := repo.Find(context.Background(), query)
require.NoError(t, err)
annotationId := items[0].Id
err = repo.Delete(&annotations.DeleteParams{Id: annotationId, OrgId: 1})
require.NoError(t, err)
items, err = repo.Find(query)
items, err = repo.Find(context.Background(), query)
require.NoError(t, err)
assert.Empty(t, items)
})
@@ -270,7 +270,7 @@ func TestAnnotations(t *testing.T) {
OrgId: 1,
AnnotationId: annotation3.Id,
}
items, err := repo.Find(query)
items, err := repo.Find(context.Background(), query)
require.NoError(t, err)
dashboardId := items[0].DashboardId
@@ -278,7 +278,7 @@ func TestAnnotations(t *testing.T) {
err = repo.Delete(&annotations.DeleteParams{DashboardId: dashboardId, PanelId: panelId, OrgId: 1})
require.NoError(t, err)
items, err = repo.Find(query)
items, err = repo.Find(context.Background(), query)
require.NoError(t, err)
assert.Empty(t, items)
})