blockstore_test.go 1.26 KB
Newer Older
1 2 3 4 5 6
package blockstore

import (
	"bytes"
	"testing"

7
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
8
	ds_sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
9
	blocks "github.com/jbenet/go-ipfs/blocks"
10 11 12 13 14 15
	u "github.com/jbenet/go-ipfs/util"
)

// TODO(brian): TestGetReturnsNil

func TestGetWhenKeyNotPresent(t *testing.T) {
16
	bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
17 18 19 20 21 22 23 24 25 26
	_, err := bs.Get(u.Key("not present"))

	if err != nil {
		t.Log("As expected, block is not present")
		return
	}
	t.Fail()
}

func TestPutThenGetBlock(t *testing.T) {
27
	bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
Jeromy's avatar
Jeromy committed
28
	block := blocks.NewBlock([]byte("some data"))
29

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
	err := bs.Put(block)
31 32 33 34 35 36 37 38 39 40 41 42 43 44
	if err != nil {
		t.Fatal(err)
	}

	blockFromBlockstore, err := bs.Get(block.Key())
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(block.Data, blockFromBlockstore.Data) {
		t.Fail()
	}
}

func TestValueTypeMismatch(t *testing.T) {
Jeromy's avatar
Jeromy committed
45
	block := blocks.NewBlock([]byte("some data"))
46 47

	datastore := ds.NewMapDatastore()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48
	datastore.Put(block.Key().DsKey(), "data that isn't a block!")
49

50
	blockstore := NewBlockstore(ds_sync.MutexWrap(datastore))
51 52 53 54 55 56

	_, err := blockstore.Get(block.Key())
	if err != ValueTypeMismatch {
		t.Fatal(err)
	}
}