[#137] metabase: Implement and useful test funcs for work with DB

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-02 10:55:35 +03:00 committed by Alex Vanin
parent fc2038e929
commit 7704811654
5 changed files with 31 additions and 84 deletions

View file

@ -76,17 +76,9 @@ func TestDB(t *testing.T) {
obj.SetAttributes(a) obj.SetAttributes(a)
path := "test.db" db := newDB(t)
bdb, err := bbolt.Open(path, 0600, nil) defer releaseDB(db)
require.NoError(t, err)
defer func() {
bdb.Close()
os.Remove(path)
}()
db := NewDB(bdb)
o := obj.Object() o := obj.Object()
@ -117,17 +109,9 @@ func TestDB(t *testing.T) {
} }
func TestDB_Delete(t *testing.T) { func TestDB_Delete(t *testing.T) {
path := "test.db" db := newDB(t)
bdb, err := bbolt.Open(path, 0600, nil) defer releaseDB(db)
require.NoError(t, err)
defer func() {
bdb.Close()
os.Remove(path)
}()
db := NewDB(bdb)
obj := object.NewRaw() obj := object.NewRaw()
obj.SetContainerID(testCID()) obj.SetContainerID(testCID())
@ -139,7 +123,7 @@ func TestDB_Delete(t *testing.T) {
addr := o.Address() addr := o.Address()
_, err = db.Get(addr) _, err := db.Get(addr)
require.NoError(t, err) require.NoError(t, err)
fs := objectSDK.SearchFilters{} fs := objectSDK.SearchFilters{}
@ -156,17 +140,9 @@ func TestDB_Delete(t *testing.T) {
} }
func TestDB_SelectProperties(t *testing.T) { func TestDB_SelectProperties(t *testing.T) {
path := "test.db" db := newDB(t)
bdb, err := bbolt.Open(path, 0600, nil) defer releaseDB(db)
require.NoError(t, err)
defer func() {
bdb.Close()
os.Remove(path)
}()
db := NewDB(bdb)
parent := object.NewRaw() parent := object.NewRaw()
parent.SetContainerID(testCID()) parent.SetContainerID(testCID())
@ -243,12 +219,23 @@ func TestDB_Path(t *testing.T) {
bdb, err := bbolt.Open(path, 0600, nil) bdb, err := bbolt.Open(path, 0600, nil)
require.NoError(t, err) require.NoError(t, err)
defer func() {
bdb.Close()
os.Remove(path)
}()
db := NewDB(bdb) db := NewDB(bdb)
defer releaseDB(db)
require.Equal(t, path, db.Path()) require.Equal(t, path, db.Path())
} }
func newDB(t testing.TB) *DB {
path := t.Name()
bdb, err := bbolt.Open(path, 0600, nil)
require.NoError(t, err)
return NewDB(bdb)
}
func releaseDB(db *DB) {
db.Close()
os.Remove(db.Path())
}

View file

@ -1,26 +1,16 @@
package meta package meta
import ( import (
"os"
"testing" "testing"
"github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.etcd.io/bbolt"
) )
func BenchmarkDB_Delete(b *testing.B) { func BenchmarkDB_Delete(b *testing.B) {
path := "delete_test.db" db := newDB(b)
bdb, err := bbolt.Open(path, 0600, nil) defer releaseDB(db)
require.NoError(b, err)
defer func() {
bdb.Close()
os.Remove(path)
}()
db := NewDB(bdb)
var existingAddr *object.Address var existingAddr *object.Address

View file

@ -1,26 +1,16 @@
package meta package meta
import ( import (
"os"
"testing" "testing"
"github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.etcd.io/bbolt"
) )
func BenchmarkDB_Get(b *testing.B) { func BenchmarkDB_Get(b *testing.B) {
path := "get_test.db" db := newDB(b)
bdb, err := bbolt.Open(path, 0600, nil) defer releaseDB(db)
require.NoError(b, err)
defer func() {
bdb.Close()
os.Remove(path)
}()
db := NewDB(bdb)
var existingAddr *object.Address var existingAddr *object.Address

View file

@ -4,7 +4,6 @@ import (
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"os"
"testing" "testing"
"github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/pkg"
@ -13,7 +12,6 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/core/object"
"github.com/nspcc-dev/neofs-node/pkg/util/test" "github.com/nspcc-dev/neofs-node/pkg/util/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.etcd.io/bbolt"
) )
type testPrm struct { type testPrm struct {
@ -87,17 +85,9 @@ func generateObject(t require.TestingT, prm testPrm) *object.Object {
} }
func BenchmarkDB_Put(b *testing.B) { func BenchmarkDB_Put(b *testing.B) {
path := "put_test.db" db := newDB(b)
bdb, err := bbolt.Open(path, 0600, nil) defer releaseDB(db)
require.NoError(b, err)
defer func() {
bdb.Close()
os.Remove(path)
}()
db := NewDB(bdb)
for _, prm := range []testPrm{ for _, prm := range []testPrm{
{ {

View file

@ -2,12 +2,10 @@ package meta
import ( import (
"crypto/rand" "crypto/rand"
"os"
"testing" "testing"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.etcd.io/bbolt"
) )
func addNFilters(fs *objectSDK.SearchFilters, n int) { func addNFilters(fs *objectSDK.SearchFilters, n int) {
@ -23,17 +21,9 @@ func addNFilters(fs *objectSDK.SearchFilters, n int) {
} }
func BenchmarkDB_Select(b *testing.B) { func BenchmarkDB_Select(b *testing.B) {
path := "select_test.db" db := newDB(b)
bdb, err := bbolt.Open(path, 0600, nil) defer releaseDB(db)
require.NoError(b, err)
defer func() {
bdb.Close()
os.Remove(path)
}()
db := NewDB(bdb)
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
obj := generateObject(b, testPrm{ obj := generateObject(b, testPrm{