transactions_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package sqlstore
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. "github.com/grafana/grafana/pkg/models"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. type testQuery struct {
  10. result bool
  11. }
  12. var ProvokedError = errors.New("testing error.")
  13. func TestTransaction(t *testing.T) {
  14. ss := InitTestDB(t)
  15. Convey("InTransaction asdf asdf", t, func() {
  16. cmd := &models.AddApiKeyCommand{Key: "secret-key", Name: "key", OrgId: 1}
  17. err := AddApiKey(cmd)
  18. So(err, ShouldBeNil)
  19. deleteApiKeyCmd := &models.DeleteApiKeyCommand{Id: cmd.Result.Id, OrgId: 1}
  20. Convey("can update key", func() {
  21. err := ss.InTransaction(context.Background(), func(ctx context.Context) error {
  22. return DeleteApiKeyCtx(ctx, deleteApiKeyCmd)
  23. })
  24. So(err, ShouldBeNil)
  25. query := &models.GetApiKeyByIdQuery{ApiKeyId: cmd.Result.Id}
  26. err = GetApiKeyById(query)
  27. So(err, ShouldEqual, models.ErrInvalidApiKey)
  28. })
  29. Convey("won't update if one handler fails", func() {
  30. err := ss.InTransaction(context.Background(), func(ctx context.Context) error {
  31. err := DeleteApiKeyCtx(ctx, deleteApiKeyCmd)
  32. if err != nil {
  33. return err
  34. }
  35. return ProvokedError
  36. })
  37. So(err, ShouldEqual, ProvokedError)
  38. query := &models.GetApiKeyByIdQuery{ApiKeyId: cmd.Result.Id}
  39. err = GetApiKeyById(query)
  40. So(err, ShouldBeNil)
  41. So(query.Result.Id, ShouldEqual, cmd.Result.Id)
  42. })
  43. })
  44. }