2022-02-04 09:14:36 -06:00
|
|
|
package queryhistory
|
|
|
|
|
|
|
|
import (
|
2022-02-23 10:03:04 -06:00
|
|
|
"context"
|
2022-02-04 09:14:36 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-10-19 08:02:15 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2022-02-04 09:14:36 -06:00
|
|
|
)
|
|
|
|
|
2022-05-24 04:04:03 -05:00
|
|
|
func TestIntegrationDeleteQueryFromQueryHistory(t *testing.T) {
|
2022-06-10 10:46:21 -05:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
2022-02-04 09:14:36 -06:00
|
|
|
testScenarioWithQueryInQueryHistory(t, "When users tries to delete query in query history that does not exist, it should fail",
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
|
|
|
resp := sc.service.deleteHandler(sc.reqContext)
|
2022-02-15 08:43:17 -06:00
|
|
|
require.Equal(t, 500, resp.Status())
|
2022-02-04 09:14:36 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
testScenarioWithQueryInQueryHistory(t, "When users tries to delete query in query history that exists, it should succeed",
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
|
|
|
|
resp := sc.service.deleteHandler(sc.reqContext)
|
|
|
|
require.Equal(t, 200, resp.Status())
|
|
|
|
})
|
2022-02-23 10:03:04 -06:00
|
|
|
|
|
|
|
testScenarioWithQueryInQueryHistory(t, "When users tries to delete query in query history that exists, it should also unstar it and succeed",
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
|
|
|
|
// Star added query
|
|
|
|
sc.service.starHandler(sc.reqContext)
|
|
|
|
// Then delete it
|
|
|
|
resp := sc.service.deleteHandler(sc.reqContext)
|
|
|
|
// Check if query is still in query_history_star table
|
2022-10-19 08:02:15 -05:00
|
|
|
err := sc.sqlStore.WithDbSession(context.Background(), func(dbSession *db.Session) error {
|
2022-08-11 06:28:55 -05:00
|
|
|
exists, err := dbSession.Table("query_history_star").Where("user_id = ? AND query_uid = ?", sc.reqContext.SignedInUser.UserID, sc.initialResult.Result.UID).Exist()
|
2022-02-23 10:03:04 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, false, exists)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 200, resp.Status())
|
|
|
|
})
|
2022-02-04 09:14:36 -06:00
|
|
|
}
|