storj/satellite/satellitedb/consoledb_test.go
paul cannon 0c5e381434 satellite/console: use transaction helpers in consoledb
Transactions in our code that might need to work against CockroachDB
need to be retried in the event of a retryable error. The transaction
helper functions in dbutil do that automatically. I am changing this
code to use those helpers instead.

I also fleshed out consoledb_test.go to do actual inserts and gets to
make sure things were working correctly.

Change-Id: I089bf4c776d15dc8578080e26760bd6dff4beec9
2020-01-07 17:59:10 +00:00

80 lines
2.3 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb_test
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/zeebo/errs"
"storj.io/common/testcontext"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
)
func TestConsoleTx(t *testing.T) {
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
dbConsole := db.Console()
t.Run("WithTx with success", func(t *testing.T) {
name := "Sleve McDichael"
var projInfo *console.Project
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)
projectDB := dbConsole.Projects()
gotProjectInfo, err := projectDB.Get(ctx, projInfo.ID)
require.NoError(t, err)
require.Equal(t, projInfo.ID, gotProjectInfo.ID)
require.Equal(t, projInfo.Name, gotProjectInfo.Name)
require.Equal(t, projInfo.CreatedAt, gotProjectInfo.CreatedAt)
})
t.Run("WithTx with failure", func(t *testing.T) {
name := "Bobson Dugnutt"
var projInfo *console.Project
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)
// 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)
// 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)
})
})
}