mirror of https://github.com/grafana/grafana
Query history: Create API to star and unstar query in query history (#45077)
* Query history: Add starring and unstarring API * Return dto with starred info when commenting * Add documentation for starring and unstarring of query * Return dto when starring/unstarring * Update documentation * Update deleting with unstarring * Check queryUID length in queryhistory * Fix linting issues * Update docs/sources/http_api/query_history.md Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com> * Update docs/sources/http_api/query_history.md Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com> Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>pull/45322/head
parent
3cfbbbdbf2
commit
a3a852be81
@ -0,0 +1,31 @@ |
||||
package queryhistory |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/grafana/grafana/pkg/web" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
func TestStarQueryInQueryHistory(t *testing.T) { |
||||
testScenarioWithQueryInQueryHistory(t, "When users tries to star query in query history that does not exists, it should fail", |
||||
func(t *testing.T, sc scenarioContext) { |
||||
resp := sc.service.starHandler(sc.reqContext) |
||||
require.Equal(t, 500, resp.Status()) |
||||
}) |
||||
|
||||
testScenarioWithQueryInQueryHistory(t, "When users tries to star 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.starHandler(sc.reqContext) |
||||
require.Equal(t, 200, resp.Status()) |
||||
}) |
||||
|
||||
testScenarioWithQueryInQueryHistory(t, "When users tries to star query that is already starred, it should fail", |
||||
func(t *testing.T, sc scenarioContext) { |
||||
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID}) |
||||
sc.service.starHandler(sc.reqContext) |
||||
resp := sc.service.starHandler(sc.reqContext) |
||||
require.Equal(t, 500, resp.Status()) |
||||
}) |
||||
} |
@ -0,0 +1,33 @@ |
||||
package queryhistory |
||||
|
||||
import ( |
||||
"fmt" |
||||
"testing" |
||||
|
||||
"github.com/grafana/grafana/pkg/web" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
func TestUnstarQueryInQueryHistory(t *testing.T) { |
||||
testScenarioWithQueryInQueryHistory(t, "When users tries to unstar query in query history that does not exists, it should fail", |
||||
func(t *testing.T, sc scenarioContext) { |
||||
resp := sc.service.starHandler(sc.reqContext) |
||||
require.Equal(t, 500, resp.Status()) |
||||
}) |
||||
|
||||
testScenarioWithQueryInQueryHistory(t, "When users tries to unstar starred query in query history, 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}) |
||||
sc.service.starHandler(sc.reqContext) |
||||
resp := sc.service.unstarHandler(sc.reqContext) |
||||
fmt.Println(resp) |
||||
require.Equal(t, 200, resp.Status()) |
||||
}) |
||||
|
||||
testScenarioWithQueryInQueryHistory(t, "When users tries to unstar query in query history that is not starred, it should fail", |
||||
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.unstarHandler(sc.reqContext) |
||||
require.Equal(t, 500, resp.Status()) |
||||
}) |
||||
} |
@ -0,0 +1,23 @@ |
||||
package migrations |
||||
|
||||
import ( |
||||
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator" |
||||
) |
||||
|
||||
func addQueryHistoryStarMigrations(mg *Migrator) { |
||||
queryHistoryStarV1 := Table{ |
||||
Name: "query_history_star", |
||||
Columns: []*Column{ |
||||
{Name: "id", Type: DB_BigInt, Nullable: false, IsPrimaryKey: true, IsAutoIncrement: true}, |
||||
{Name: "query_uid", Type: DB_NVarchar, Length: 40, Nullable: false}, |
||||
{Name: "user_id", Type: DB_Int, Nullable: false}, |
||||
}, |
||||
Indices: []*Index{ |
||||
{Cols: []string{"user_id", "query_uid"}, Type: UniqueIndex}, |
||||
}, |
||||
} |
||||
|
||||
mg.AddMigration("create query_history_star table v1", NewAddTableMigration(queryHistoryStarV1)) |
||||
|
||||
mg.AddMigration("add index query_history.user_id-query_uid", NewAddIndexMigration(queryHistoryStarV1, queryHistoryStarV1.Indices[0])) |
||||
} |
Loading…
Reference in new issue