mirror of
https://github.com/grafana/grafana.git
synced 2024-11-26 19:00:54 -06:00
4937f0daab
* sqlstore: Run tests as integration tests * Truncate database instead of re-creating it on each test * Fix test description See https://github.com/grafana/grafana/pull/12129 * Fix lint issues * Fix postgres dialect after review suggestion * Rename and document functions after review suggestion * Add periods * Fix auto-increment value for mysql dialect Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
// +build integration
|
|
|
|
package sqlstore
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
)
|
|
|
|
var ErrProvokedError = errors.New("testing error")
|
|
|
|
func TestTransaction(t *testing.T) {
|
|
ss := InitTestDB(t)
|
|
|
|
Convey("InTransaction", t, func() {
|
|
cmd := &models.AddApiKeyCommand{Key: "secret-key", Name: "key", OrgId: 1}
|
|
|
|
err := AddApiKey(cmd)
|
|
So(err, ShouldBeNil)
|
|
|
|
deleteApiKeyCmd := &models.DeleteApiKeyCommand{Id: cmd.Result.Id, OrgId: 1}
|
|
|
|
Convey("can update key", func() {
|
|
err := ss.InTransaction(context.Background(), func(ctx context.Context) error {
|
|
return DeleteApiKeyCtx(ctx, deleteApiKeyCmd)
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
query := &models.GetApiKeyByIdQuery{ApiKeyId: cmd.Result.Id}
|
|
err = GetApiKeyById(query)
|
|
So(err, ShouldEqual, models.ErrInvalidApiKey)
|
|
})
|
|
|
|
Convey("won't update if one handler fails", func() {
|
|
err := ss.InTransaction(context.Background(), func(ctx context.Context) error {
|
|
err := DeleteApiKeyCtx(ctx, deleteApiKeyCmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return ErrProvokedError
|
|
})
|
|
|
|
So(err, ShouldEqual, ErrProvokedError)
|
|
|
|
query := &models.GetApiKeyByIdQuery{ApiKeyId: cmd.Result.Id}
|
|
err = GetApiKeyById(query)
|
|
So(err, ShouldBeNil)
|
|
So(query.Result.Id, ShouldEqual, cmd.Result.Id)
|
|
})
|
|
})
|
|
}
|