2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2019-01-11 10:08:51 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-31 13:01:13 +00:00
|
|
|
package satellitedb_test
|
2019-01-11 10:08:51 +00:00
|
|
|
|
|
|
|
import (
|
2019-12-19 10:07:56 +00:00
|
|
|
"context"
|
2019-01-11 10:08:51 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zeebo/errs"
|
2019-01-15 13:03:24 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
2019-01-31 13:01:13 +00:00
|
|
|
"storj.io/storj/satellite"
|
2019-12-19 10:07:56 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-01-31 13:01:13 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
2019-01-11 10:08:51 +00:00
|
|
|
)
|
|
|
|
|
2019-01-31 13:01:13 +00:00
|
|
|
func TestConsoleTx(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2019-01-11 10:08:51 +00:00
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
dbConsole := db.Console()
|
2019-01-11 10:08:51 +00:00
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
t.Run("WithTx with success", func(t *testing.T) {
|
|
|
|
name := "Sleve McDichael"
|
|
|
|
var projInfo *console.Project
|
2019-01-11 10:08:51 +00:00
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
err := dbConsole.WithTx(ctx, func(ctx context.Context, tx console.DBTx) (err error) {
|
|
|
|
projectDB := tx.Projects()
|
|
|
|
projInfo, err = projectDB.Insert(ctx, &console.Project{Name: name})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotZero(t, projInfo.ID)
|
|
|
|
require.Equal(t, name, projInfo.Name)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-01-11 10:08:51 +00:00
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
projectDB := dbConsole.Projects()
|
|
|
|
gotProjectInfo, err := projectDB.Get(ctx, projInfo.ID)
|
|
|
|
require.NoError(t, err)
|
2019-01-11 10:08:51 +00:00
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
require.Equal(t, projInfo.ID, gotProjectInfo.ID)
|
|
|
|
require.Equal(t, projInfo.Name, gotProjectInfo.Name)
|
|
|
|
require.Equal(t, projInfo.CreatedAt, gotProjectInfo.CreatedAt)
|
2019-01-31 13:01:13 +00:00
|
|
|
})
|
2019-01-11 10:08:51 +00:00
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
t.Run("WithTx with failure", func(t *testing.T) {
|
|
|
|
name := "Bobson Dugnutt"
|
|
|
|
var projInfo *console.Project
|
2019-01-11 10:08:51 +00:00
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
err := dbConsole.WithTx(ctx, func(ctx context.Context, tx console.DBTx) (err error) {
|
|
|
|
projectDB := tx.Projects()
|
|
|
|
projInfo, err = projectDB.Insert(ctx, &console.Project{Name: name})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotZero(t, projInfo.ID)
|
2019-01-11 10:08:51 +00:00
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
// verify retrievability inside the transaction
|
|
|
|
gotProjInfo, err := projectDB.Get(ctx, projInfo.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, projInfo.ID, gotProjInfo.ID)
|
|
|
|
require.Equal(t, projInfo.Name, gotProjInfo.Name)
|
|
|
|
require.Equal(t, projInfo.CreatedAt, gotProjInfo.CreatedAt)
|
2019-01-11 10:08:51 +00:00
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
// but return an error anyway to cause rollback
|
|
|
|
return errs.New("some errors just want to see the world burn")
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "see the world burn")
|
|
|
|
|
|
|
|
// insertion should have been rolled back
|
|
|
|
projectDB := dbConsole.Projects()
|
|
|
|
gotProjInfo, err := projectDB.Get(ctx, projInfo.ID)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, gotProjInfo)
|
2019-01-31 13:01:13 +00:00
|
|
|
})
|
2019-01-11 10:08:51 +00:00
|
|
|
})
|
|
|
|
}
|