2022-05-03 14:49:58 +02:00
|
|
|
package queryhistory
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-01-30 09:21:27 +01:00
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2022-05-03 14:49:58 +02:00
|
|
|
)
|
|
|
|
|
|
2022-05-24 05:04:03 -04:00
|
|
|
func TestIntegrationDeleteStaleQueryFromQueryHistory(t *testing.T) {
|
2022-06-10 11:46:21 -04:00
|
|
|
if testing.Short() {
|
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
|
}
|
2022-05-03 14:49:58 +02:00
|
|
|
testScenarioWithQueryInQueryHistory(t, "Stale query history can be deleted",
|
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
2023-04-17 12:02:47 +02:00
|
|
|
olderThan := sc.service.now().Unix() + 60
|
2022-05-03 14:49:58 +02:00
|
|
|
rowsDeleted, err := sc.service.DeleteStaleQueriesInQueryHistory(context.Background(), olderThan)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, 1, rowsDeleted)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
testScenarioWithQueryInQueryHistory(t, "Stale single starred query history can not be deleted",
|
|
|
|
|
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.starHandler(sc.reqContext)
|
|
|
|
|
require.Equal(t, 200, resp.Status())
|
|
|
|
|
|
2023-04-17 12:02:47 +02:00
|
|
|
olderThan := sc.service.now().Unix() + 60
|
2022-05-03 14:49:58 +02:00
|
|
|
rowsDeleted, err := sc.service.DeleteStaleQueriesInQueryHistory(context.Background(), olderThan)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, 0, rowsDeleted)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
testScenarioWithQueryInQueryHistory(t, "Not stale query history is not deleted",
|
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
2023-04-17 12:02:47 +02:00
|
|
|
olderThan := sc.service.now().Unix() - 60
|
2022-05-03 14:49:58 +02:00
|
|
|
rowsDeleted, err := sc.service.DeleteStaleQueriesInQueryHistory(context.Background(), olderThan)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, 0, rowsDeleted)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// In this scenario we have 2 starred queries and 1 not starred query
|
|
|
|
|
testScenarioWithMultipleQueriesInQueryHistory(t, "Stale starred query history can not be deleted",
|
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
2023-04-17 12:02:47 +02:00
|
|
|
olderThan := sc.service.now().Unix() + 60
|
2022-05-03 14:49:58 +02:00
|
|
|
rowsDeleted, err := sc.service.DeleteStaleQueriesInQueryHistory(context.Background(), olderThan)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, 1, rowsDeleted)
|
|
|
|
|
})
|
|
|
|
|
}
|