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:
Kristin Laemmert
2022-10-19 09:02:15 -04:00
committed by GitHub
parent 5285d34cc0
commit 05709ce411
273 changed files with 1595 additions and 1491 deletions

View File

@@ -9,10 +9,10 @@ import (
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/permissions"
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
"github.com/grafana/grafana/pkg/services/user"
@@ -31,24 +31,24 @@ func TestBuilder_EqualResults_Basic(t *testing.T) {
OrgRole: org.RoleEditor,
}
db := setupTestEnvironment(t)
dashIds := createDashboards(t, db, 0, 1, user.OrgID)
store := setupTestEnvironment(t)
dashIds := createDashboards(t, store, 0, 1, user.OrgID)
require.Len(t, dashIds, 1)
// create one dashboard in another organization that shouldn't
// be listed in the results.
createDashboards(t, db, 1, 2, 2)
createDashboards(t, store, 1, 2, 2)
builder := &searchstore.Builder{
Filters: []interface{}{
searchstore.OrgFilter{OrgId: user.OrgID},
searchstore.TitleSorter{},
},
Dialect: db.Dialect,
Dialect: store.GetDialect(),
}
res := []dashboards.DashboardSearchProjection{}
err := db.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err := store.WithDbSession(context.Background(), func(sess *db.Session) error {
sql, params := builder.ToSQL(limit, page)
return sess.SQL(sql, params...).Find(&res)
})
@@ -73,21 +73,21 @@ func TestBuilder_Pagination(t *testing.T) {
OrgRole: org.RoleViewer,
}
db := setupTestEnvironment(t)
createDashboards(t, db, 0, 25, user.OrgID)
store := setupTestEnvironment(t)
createDashboards(t, store, 0, 25, user.OrgID)
builder := &searchstore.Builder{
Filters: []interface{}{
searchstore.OrgFilter{OrgId: user.OrgID},
searchstore.TitleSorter{},
},
Dialect: db.Dialect,
Dialect: store.GetDialect(),
}
resPg1 := []dashboards.DashboardSearchProjection{}
resPg2 := []dashboards.DashboardSearchProjection{}
resPg3 := []dashboards.DashboardSearchProjection{}
err := db.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err := store.WithDbSession(context.Background(), func(sess *db.Session) error {
sql, params := builder.ToSQL(15, 1)
err := sess.SQL(sql, params...).Find(&resPg1)
if err != nil {
@@ -119,8 +119,8 @@ func TestBuilder_Permissions(t *testing.T) {
OrgRole: org.RoleViewer,
}
db := setupTestEnvironment(t)
createDashboards(t, db, 0, 1, user.OrgID)
store := setupTestEnvironment(t)
createDashboards(t, store, 0, 1, user.OrgID)
level := models.PERMISSION_EDIT
@@ -129,18 +129,18 @@ func TestBuilder_Permissions(t *testing.T) {
searchstore.OrgFilter{OrgId: user.OrgID},
searchstore.TitleSorter{},
permissions.DashboardPermissionFilter{
Dialect: db.Dialect,
Dialect: store.GetDialect(),
OrgRole: user.OrgRole,
OrgId: user.OrgID,
UserId: user.UserID,
PermissionLevel: level,
},
},
Dialect: db.Dialect,
Dialect: store.GetDialect(),
}
res := []dashboards.DashboardSearchProjection{}
err := db.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err := store.WithDbSession(context.Background(), func(sess *db.Session) error {
sql, params := builder.ToSQL(limit, page)
return sess.SQL(sql, params...).Find(&res)
})
@@ -149,13 +149,13 @@ func TestBuilder_Permissions(t *testing.T) {
assert.Len(t, res, 0)
}
func setupTestEnvironment(t *testing.T) *sqlstore.SQLStore {
func setupTestEnvironment(t *testing.T) db.DB {
t.Helper()
store := sqlstore.InitTestDB(t)
store := db.InitTestDB(t)
return store
}
func createDashboards(t *testing.T, db *sqlstore.SQLStore, startID, endID int, orgID int64) []int64 {
func createDashboards(t *testing.T, store db.DB, startID, endID int, orgID int64) []int64 {
t.Helper()
require.GreaterOrEqual(t, endID, startID)
@@ -174,7 +174,7 @@ func createDashboards(t *testing.T, db *sqlstore.SQLStore, startID, endID int, o
require.NoError(t, err)
var dash *models.Dashboard
err = db.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err = store.WithDbSession(context.Background(), func(sess *db.Session) error {
dash = models.NewDashboardFromJson(dashboard)
dash.OrgId = orgID
dash.Uid = util.GenerateShortUID()
@@ -186,7 +186,7 @@ func createDashboards(t *testing.T, db *sqlstore.SQLStore, startID, endID int, o
tags := dash.GetTags()
if len(tags) > 0 {
for _, tag := range tags {
if _, err := sess.Insert(&sqlstore.DashboardTag{DashboardId: dash.Id, Term: tag}); err != nil {
if _, err := sess.Insert(&DashboardTag{DashboardId: dash.Id, Term: tag}); err != nil {
return err
}
}
@@ -213,3 +213,9 @@ func lexiCounter(n int) string {
return value
}
type DashboardTag struct {
Id int64
DashboardId int64
Term string
}