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.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-15 13:03:24 +00:00
|
|
|
|
2019-01-11 10:08:51 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDatabase(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
t.Run("BeginTx return err when db is nil", func(t *testing.T) {
|
2019-01-15 13:03:24 +00:00
|
|
|
db := &ConsoleDB{
|
2019-01-11 10:08:51 +00:00
|
|
|
db: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction, err := db.BeginTx(ctx)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, transaction)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("BeginTx and Commit success", func(t *testing.T) {
|
2019-01-15 13:03:24 +00:00
|
|
|
db, err := NewConsoleDB("sqlite3", "file::memory:?mode=memory&cache=shared")
|
2019-01-11 10:08:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction, err := db.BeginTx(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, transaction)
|
|
|
|
|
|
|
|
err = transaction.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = db.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("BeginTx and Rollback success", func(t *testing.T) {
|
2019-01-15 13:03:24 +00:00
|
|
|
db, err := NewConsoleDB("sqlite3", "file::memory:?mode=memory&cache=shared")
|
2019-01-11 10:08:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction, err := db.BeginTx(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, transaction)
|
|
|
|
|
|
|
|
err = transaction.Rollback()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = db.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Commit fails", func(t *testing.T) {
|
|
|
|
transaction := &DBTx{
|
2019-01-15 13:03:24 +00:00
|
|
|
ConsoleDB: &ConsoleDB{
|
2019-01-11 10:08:51 +00:00
|
|
|
tx: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := transaction.Commit()
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Rollback fails", func(t *testing.T) {
|
|
|
|
transaction := &DBTx{
|
2019-01-15 13:03:24 +00:00
|
|
|
ConsoleDB: &ConsoleDB{
|
2019-01-11 10:08:51 +00:00
|
|
|
tx: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := transaction.Rollback()
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|