mock blockservice interfaces for tests
This commit is contained in:
parent
21786bcf52
commit
09277325ef
144
mock_blockservice.go
Normal file
144
mock_blockservice.go
Normal file
@ -0,0 +1,144 @@
|
||||
// VulcanizeDB
|
||||
// Copyright © 2020 Vulcanize
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package ipfsethdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/ipfs/go-block-format"
|
||||
"github.com/ipfs/go-blockservice"
|
||||
"github.com/ipfs/go-cid"
|
||||
"github.com/ipfs/go-ipfs-blockstore"
|
||||
"github.com/ipfs/go-ipfs-exchange-interface"
|
||||
)
|
||||
|
||||
var (
|
||||
blockNotFoundErr = errors.New("block not found")
|
||||
)
|
||||
|
||||
type MockBlockservice struct {
|
||||
blockStore *MockBlockstore
|
||||
err error
|
||||
}
|
||||
|
||||
func NewMockBlockservice() blockservice.BlockService {
|
||||
return &MockBlockservice{
|
||||
blockStore: &MockBlockstore{
|
||||
blocks: make(map[string]blocks.Block),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (mbs *MockBlockservice) Blockstore() blockstore.Blockstore {
|
||||
return mbs.blockStore
|
||||
}
|
||||
|
||||
func (mbs *MockBlockservice) Exchange() exchange.Interface {
|
||||
panic("Exchange: implement me")
|
||||
}
|
||||
|
||||
func (mbs *MockBlockservice) AddBlock(b blocks.Block) error {
|
||||
return mbs.blockStore.Put(b)
|
||||
}
|
||||
|
||||
func (mbs *MockBlockservice) AddBlocks(bs []blocks.Block) error {
|
||||
return mbs.blockStore.PutMany(bs)
|
||||
}
|
||||
|
||||
func (mbs *MockBlockservice) DeleteBlock(c cid.Cid) error {
|
||||
return mbs.blockStore.DeleteBlock(c)
|
||||
}
|
||||
|
||||
func (mbs *MockBlockservice) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
|
||||
return mbs.blockStore.Get(c)
|
||||
}
|
||||
|
||||
func (mbs *MockBlockservice) GetBlocks(ctx context.Context, cs []cid.Cid) <-chan blocks.Block {
|
||||
blockChan := make(chan blocks.Block)
|
||||
go func() {
|
||||
for _, c := range cs {
|
||||
if b, err := mbs.blockStore.Get(c); err == nil {
|
||||
blockChan <- b
|
||||
}
|
||||
}
|
||||
}()
|
||||
return blockChan
|
||||
}
|
||||
|
||||
func (mbs *MockBlockservice) Close() error {
|
||||
return mbs.err
|
||||
}
|
||||
|
||||
func (mbs *MockBlockservice) SetError(err error) {
|
||||
mbs.err = err
|
||||
}
|
||||
|
||||
type MockBlockstore struct {
|
||||
blocks map[string]blocks.Block
|
||||
err error
|
||||
}
|
||||
|
||||
func (mbs *MockBlockstore) DeleteBlock(c cid.Cid) error {
|
||||
delete(mbs.blocks, c.String())
|
||||
return mbs.err
|
||||
}
|
||||
|
||||
func (mbs *MockBlockstore) Has(c cid.Cid) (bool, error) {
|
||||
_, ok := mbs.blocks[c.String()]
|
||||
return ok, mbs.err
|
||||
}
|
||||
|
||||
func (mbs *MockBlockstore) Get(c cid.Cid) (blocks.Block, error) {
|
||||
obj, ok := mbs.blocks[c.String()]
|
||||
if !ok {
|
||||
return nil, blockNotFoundErr
|
||||
}
|
||||
return obj, mbs.err
|
||||
}
|
||||
|
||||
func (mbs *MockBlockstore) GetSize(c cid.Cid) (int, error) {
|
||||
obj, ok := mbs.blocks[c.String()]
|
||||
if !ok {
|
||||
return 0, blockNotFoundErr
|
||||
}
|
||||
return len(obj.RawData()), mbs.err
|
||||
}
|
||||
|
||||
func (mbs *MockBlockstore) Put(b blocks.Block) error {
|
||||
mbs.blocks[b.Cid().String()] = b
|
||||
return mbs.err
|
||||
}
|
||||
|
||||
func (mbs *MockBlockstore) PutMany(bs []blocks.Block) error {
|
||||
for _, b := range bs {
|
||||
mbs.blocks[b.Cid().String()] = b
|
||||
}
|
||||
return mbs.err
|
||||
}
|
||||
|
||||
func (mbs *MockBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
||||
panic("AllKeysChan: implement me")
|
||||
}
|
||||
|
||||
func (mbs *MockBlockstore) HashOnRead(enabled bool) {
|
||||
panic("HasOnRead: implement me")
|
||||
}
|
||||
|
||||
func (mbs *MockBlockstore) SetError(err error) {
|
||||
mbs.err = err
|
||||
}
|
Loading…
Reference in New Issue
Block a user