transactions_test.go 1.4 KB

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