grafana/pkg/services/queryhistory/queryhistory_delete_stale_test.go
Kat Yang 50c2b4682a
Chore: Rename integration tests (#49438)
* Chore: Rename integration tests

* Remove one Integration

Co-authored-by: Ida Furjesova <ida.furjesova@grafana.com>
2022-05-24 11:04:03 +02:00

53 lines
1.8 KiB
Go

//go:build integration
// +build integration
package queryhistory
import (
"context"
"testing"
"time"
"github.com/grafana/grafana/pkg/web"
"github.com/stretchr/testify/require"
)
func TestIntegrationDeleteStaleQueryFromQueryHistory(t *testing.T) {
testScenarioWithQueryInQueryHistory(t, "Stale query history can be deleted",
func(t *testing.T, sc scenarioContext) {
olderThan := time.Now().Unix() + 60
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())
olderThan := time.Now().Unix() + 60
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) {
olderThan := time.Now().Unix() - 60
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) {
olderThan := time.Now().Unix() + 60
rowsDeleted, err := sc.service.DeleteStaleQueriesInQueryHistory(context.Background(), olderThan)
require.NoError(t, err)
require.Equal(t, 1, rowsDeleted)
})
}