2019-02-25 22:44:14 +00:00
|
|
|
package database_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/database"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const path = "temp"
|
|
|
|
|
|
|
|
func cleanup(db *database.LDB) {
|
|
|
|
db.Close()
|
2019-03-23 16:57:05 +00:00
|
|
|
os.RemoveAll(database.DbDir)
|
2019-02-25 22:44:14 +00:00
|
|
|
}
|
|
|
|
func TestDBCreate(t *testing.T) {
|
2019-03-23 16:57:05 +00:00
|
|
|
|
|
|
|
db, err := database.New(path)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2019-02-25 22:44:14 +00:00
|
|
|
assert.NotEqual(t, nil, db)
|
|
|
|
cleanup(db)
|
|
|
|
}
|
|
|
|
func TestPutGet(t *testing.T) {
|
2019-03-23 16:57:05 +00:00
|
|
|
|
|
|
|
db, err := database.New(path)
|
|
|
|
assert.Nil(t, err)
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
key := []byte("Hello")
|
|
|
|
value := []byte("World")
|
|
|
|
|
2019-03-23 16:57:05 +00:00
|
|
|
err = db.Put(key, value)
|
2019-02-25 22:44:14 +00:00
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
|
|
|
|
res, err := db.Get(key)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, value, res)
|
|
|
|
cleanup(db)
|
|
|
|
}
|
|
|
|
func TestPutDelete(t *testing.T) {
|
|
|
|
|
2019-03-23 16:57:05 +00:00
|
|
|
db, err := database.New(path)
|
|
|
|
assert.Nil(t, err)
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
key := []byte("Hello")
|
|
|
|
value := []byte("World")
|
|
|
|
|
2019-03-23 16:57:05 +00:00
|
|
|
err = db.Put(key, value)
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
err = db.Delete(key)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
|
|
|
|
res, err := db.Get(key)
|
|
|
|
|
2019-03-23 16:57:05 +00:00
|
|
|
assert.Equal(t, database.ErrNotFound, err)
|
2019-02-25 22:44:14 +00:00
|
|
|
assert.Equal(t, res, []byte{})
|
|
|
|
cleanup(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHas(t *testing.T) {
|
2019-03-23 16:57:05 +00:00
|
|
|
|
|
|
|
db, err := database.New(path)
|
|
|
|
assert.Nil(t, err)
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
res, err := db.Has([]byte("NotExist"))
|
|
|
|
assert.Equal(t, res, false)
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
|
|
|
|
key := []byte("Hello")
|
|
|
|
value := []byte("World")
|
|
|
|
|
|
|
|
err = db.Put(key, value)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
|
|
|
|
res, err = db.Has(key)
|
|
|
|
assert.Equal(t, res, true)
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
cleanup(db)
|
|
|
|
|
|
|
|
}
|
|
|
|
func TestDBClose(t *testing.T) {
|
2019-03-23 16:57:05 +00:00
|
|
|
|
|
|
|
db, err := database.New(path)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = db.Close()
|
2019-02-25 22:44:14 +00:00
|
|
|
assert.Equal(t, nil, err)
|
2019-03-23 16:57:05 +00:00
|
|
|
|
2019-02-25 22:44:14 +00:00
|
|
|
cleanup(db)
|
|
|
|
}
|