neo-go/pkg/database/leveldb_test.go
decentralisedkev 30e5aa8f48
Add Chain saving functionality to Database (#206)
* [database]

- Add Prefix method to interface
- Convert leveldb error to `database error`
- Be explicit with prefixedKey in `Table` as slices can be pointers

* [protocol]
- Add stringer method to protocol

* [Chaindb]

- Added saveBlock() which will allow us to save a block into the
database. The block is broken up into transactions and Headers. The
headers are saved as is. The transactions are saved as is, then the
utxos in the transactions are collected to make the utxo db.

- Verification for blocks and transactions will reside in the same
package. Note that the save methods are all unexported, while the Get
methods are exported. Making it so that any can call a get method, but
only code in this package may save to the database. The other code which
will reside in this package will be code verification logic.

* [chaindb]

- Added saveHeader function which saveHeaders  uses

- Update the latest header, each time we save a header instead of after a batch. This is so that we can call saveHeader without saveHeaders. This functionality can be rolled back if the performance of updating the header after a batch is significant

- small refactor in test code
2019-03-23 16:57:05 +00:00

91 lines
1.5 KiB
Go

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()
os.RemoveAll(database.DbDir)
}
func TestDBCreate(t *testing.T) {
db, err := database.New(path)
assert.Nil(t, err)
assert.NotEqual(t, nil, db)
cleanup(db)
}
func TestPutGet(t *testing.T) {
db, err := database.New(path)
assert.Nil(t, err)
key := []byte("Hello")
value := []byte("World")
err = db.Put(key, value)
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) {
db, err := database.New(path)
assert.Nil(t, err)
key := []byte("Hello")
value := []byte("World")
err = db.Put(key, value)
err = db.Delete(key)
assert.Equal(t, nil, err)
res, err := db.Get(key)
assert.Equal(t, database.ErrNotFound, err)
assert.Equal(t, res, []byte{})
cleanup(db)
}
func TestHas(t *testing.T) {
db, err := database.New(path)
assert.Nil(t, err)
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) {
db, err := database.New(path)
assert.Nil(t, err)
err = db.Close()
assert.Equal(t, nil, err)
cleanup(db)
}