chore: remove sqlstore & mockstore dependencies from (most) packages (#57087)
* chore: add alias for InitTestDB and Session Adds an alias for the sqlstore InitTestDB and Session, and updates tests using these to reduce dependencies on the sqlstore.Store. * next pass of removing sqlstore imports * last little bit * remove mockstore where possible
This commit is contained in:
@@ -5,19 +5,20 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/services/annotations"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/services/annotations"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
func TestAnnotationCleanUp(t *testing.T) {
|
||||
fakeSQL := sqlstore.InitTestDB(t)
|
||||
fakeSQL := db.InitTestDB(t)
|
||||
|
||||
t.Cleanup(func() {
|
||||
err := fakeSQL.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
|
||||
err := fakeSQL.WithDbSession(context.Background(), func(session *db.Session) error {
|
||||
_, err := session.Exec("DELETE FROM annotation")
|
||||
return err
|
||||
})
|
||||
@@ -111,10 +112,10 @@ func TestAnnotationCleanUp(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestOldAnnotationsAreDeletedFirst(t *testing.T) {
|
||||
fakeSQL := sqlstore.InitTestDB(t)
|
||||
fakeSQL := db.InitTestDB(t)
|
||||
|
||||
t.Cleanup(func() {
|
||||
err := fakeSQL.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
|
||||
err := fakeSQL.WithDbSession(context.Background(), func(session *db.Session) error {
|
||||
_, err := session.Exec("DELETE FROM annotation")
|
||||
return err
|
||||
})
|
||||
@@ -132,7 +133,7 @@ func TestOldAnnotationsAreDeletedFirst(t *testing.T) {
|
||||
Created: time.Now().AddDate(-10, 0, -10).UnixNano() / int64(time.Millisecond),
|
||||
}
|
||||
|
||||
err := fakeSQL.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
err := fakeSQL.WithDbSession(context.Background(), func(sess *db.Session) error {
|
||||
_, err := sess.Insert(a)
|
||||
require.NoError(t, err, "cannot insert annotation")
|
||||
_, err = sess.Insert(a)
|
||||
@@ -163,10 +164,10 @@ func TestOldAnnotationsAreDeletedFirst(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func assertAnnotationCount(t *testing.T, fakeSQL *sqlstore.SQLStore, sql string, expectedCount int64) {
|
||||
func assertAnnotationCount(t *testing.T, fakeSQL db.DB, sql string, expectedCount int64) {
|
||||
t.Helper()
|
||||
|
||||
err := fakeSQL.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
err := fakeSQL.WithDbSession(context.Background(), func(sess *db.Session) error {
|
||||
count, err := sess.Where(sql).Count(&annotations.Item{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expectedCount, count)
|
||||
@@ -175,10 +176,10 @@ func assertAnnotationCount(t *testing.T, fakeSQL *sqlstore.SQLStore, sql string,
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func assertAnnotationTagCount(t *testing.T, fakeSQL *sqlstore.SQLStore, expectedCount int64) {
|
||||
func assertAnnotationTagCount(t *testing.T, fakeSQL db.DB, expectedCount int64) {
|
||||
t.Helper()
|
||||
|
||||
err := fakeSQL.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
err := fakeSQL.WithDbSession(context.Background(), func(sess *db.Session) error {
|
||||
count, err := sess.SQL("select count(*) from annotation_tag").Count()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expectedCount, count)
|
||||
@@ -187,7 +188,7 @@ func assertAnnotationTagCount(t *testing.T, fakeSQL *sqlstore.SQLStore, expected
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func createTestAnnotations(t *testing.T, store *sqlstore.SQLStore, expectedCount int, oldAnnotations int) {
|
||||
func createTestAnnotations(t *testing.T, store db.DB, expectedCount int, oldAnnotations int) {
|
||||
t.Helper()
|
||||
|
||||
cutoffDate := time.Now()
|
||||
@@ -221,7 +222,7 @@ func createTestAnnotations(t *testing.T, store *sqlstore.SQLStore, expectedCount
|
||||
a.Created = cutoffDate.AddDate(-10, 0, -10).UnixNano() / int64(time.Millisecond)
|
||||
}
|
||||
|
||||
err := store.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
err := store.WithDbSession(context.Background(), func(sess *db.Session) error {
|
||||
_, err := sess.Insert(a)
|
||||
require.NoError(t, err, "should be able to save annotation", err)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user