1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-09 09:35:00 +09:00

fix tests

This commit is contained in:
Sergey Cherepanov 2021-03-16 10:24:35 +03:00
parent 02fc8b7a48
commit c85280e8a5
No known key found for this signature in database
GPG key ID: 085319C64294F576
17 changed files with 479 additions and 144 deletions

38
app/testapp/testapp.go Normal file
View file

@ -0,0 +1,38 @@
package testapp
import (
"github.com/anytypeio/go-anytype-middleware/app"
"github.com/anytypeio/go-anytype-middleware/core/event"
"github.com/anytypeio/go-anytype-middleware/pb"
)
func New() *TestApp {
return &TestApp{&app.App{}}
}
type TestApp struct {
*app.App
}
func (ta *TestApp) With(cmp app.Component) *TestApp {
ta.Register(cmp)
return ta
}
type EventSender struct {
F func(e *pb.Event)
}
func (e *EventSender) Init(a *app.App) (err error) {
return
}
func (e *EventSender) Name() (name string) {
return event.CName
}
func (e *EventSender) Send(event *pb.Event) {
if e.F != nil {
e.F(event)
}
}

View file

@ -34,7 +34,7 @@ var reindex = &cobra.Command{
return
}
migrated, err := core2.ReindexAll(mw.Anytype.(*core2.Anytype))
migrated, err := core2.ReindexAll(mw.GetAnytype().(*core2.Anytype))
if err != nil {
c.PrintErrf("failed to run reindex migration: %s\n", resp.Error.Description)
}

View file

@ -1,62 +0,0 @@
package meta
import (
"fmt"
"testing"
"time"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/state"
"github.com/anytypeio/go-anytype-middleware/core/block/source"
"github.com/anytypeio/go-anytype-middleware/util/testMock"
"github.com/anytypeio/go-anytype-middleware/util/testMock/mockSource"
"github.com/golang/mock/gomock"
)
func TestSubscriber_Subscribe(t *testing.T) {
fx := newFixture(t)
defer fx.tearDown()
var (
blockId = "1"
)
fx.source.EXPECT().ReadMeta(gomock.Any()).Return(state.NewDoc("", nil), nil)
s := fx.PubSub().NewSubscriber()
var mch = make(chan Meta, 1)
f := func(m Meta) {
mch <- m
}
s.Callback(f).Subscribe(blockId)
select {
case <-time.After(time.Second):
t.Errorf("timeout")
case <-mch:
}
}
func newFixture(t *testing.T) (fx *fixture) {
ctrl := gomock.NewController(t)
at := testMock.NewMockService(ctrl)
s := mockSource.NewMockSource(ctrl)
at.EXPECT().InitNewSmartblocksChan(gomock.Any()).Return(fmt.Errorf("test"))
fx = &fixture{
ctrl: ctrl,
anytype: at,
Service: NewService(at, nil),
source: s,
}
fx.PubSub().(*pubSub).newSource = func(id string) (source.Source, error) {
return s, nil
}
return fx
}
type fixture struct {
Service
ctrl *gomock.Controller
anytype *testMock.MockService
source *mockSource.MockSource
}
func (fx *fixture) tearDown() {
fx.ctrl.Finish()
}

View file

@ -10,14 +10,14 @@ import (
)
func TestService_NewQueue(t *testing.T) {
s := NewService(func(e *pb.Event) {})
s := NewTest(nil)
q := s.NewQueue(pb.ModelProcess{}, 0)
assert.NotEmpty(t, q.Id())
assert.NotEmpty(t, q.Info())
}
func TestQueue_Start(t *testing.T) {
s := NewService(func(e *pb.Event) {})
s := NewTest(nil)
q := s.NewQueue(pb.ModelProcess{}, 5)
assert.NoError(t, q.Start())
assert.Error(t, q.Start()) // error for second start
@ -26,7 +26,7 @@ func TestQueue_Start(t *testing.T) {
func TestQueue_Add(t *testing.T) {
var a, b int32
s := NewService(func(e *pb.Event) {})
s := NewTest(nil)
q := s.NewQueue(pb.ModelProcess{}, 5)
incrA := func() {
atomic.AddInt32(&a, 1)
@ -54,7 +54,7 @@ func TestQueue_Add(t *testing.T) {
func TestQueue_Wait(t *testing.T) {
var a, b int32
var aCh = make(chan struct{})
s := NewService(func(e *pb.Event) {})
s := NewTest(nil)
q := s.NewQueue(pb.ModelProcess{}, 5)
incrA := func() {
atomic.AddInt32(&a, 1)
@ -89,7 +89,7 @@ func TestQueue_Cancel(t *testing.T) {
var aStarts = make(chan struct{})
var aLock = make(chan struct{})
var bLock chan struct{}
s := NewService(func(e *pb.Event) {})
s := NewTest(nil)
q := s.NewQueue(pb.ModelProcess{}, 1)
assert.NoError(t, q.Start())
fl := func() {
@ -117,7 +117,7 @@ func TestQueue_Cancel(t *testing.T) {
}
func TestQueue_Finalize(t *testing.T) {
s := NewService(func(e *pb.Event) {})
s := NewTest(nil)
q := s.NewQueue(pb.ModelProcess{}, 1)
assert.Error(t, q.Finalize())
assert.NoError(t, q.Start())

View file

@ -4,6 +4,8 @@ import (
"testing"
"time"
"github.com/anytypeio/go-anytype-middleware/app"
"github.com/anytypeio/go-anytype-middleware/app/testapp"
"github.com/anytypeio/go-anytype-middleware/pb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -11,7 +13,7 @@ import (
func TestService_Add(t *testing.T) {
var events = make(chan *pb.Event, 20)
s := NewService(func(e *pb.Event) {
s := NewTest(func(e *pb.Event) {
events <- e
})
@ -34,7 +36,7 @@ func TestService_Add(t *testing.T) {
func TestService_Cancel(t *testing.T) {
var events = make(chan *pb.Event, 20)
s := NewService(func(e *pb.Event) {
s := NewTest(func(e *pb.Event) {
events <- e
})
@ -84,3 +86,13 @@ func (t testProcess) Info() pb.ModelProcess {
func (t *testProcess) Done() chan struct{} {
return t.done
}
func NewTest(se func(e *pb.Event)) Service {
s := New()
a := new(app.App)
a.Register(&testapp.EventSender{
F: se,
}).Register(s)
a.Start()
return s
}

View file

@ -5,7 +5,7 @@ import (
)
func (mw *Middleware) ConfigGet(*pb.RpcConfigGetRequest) *pb.RpcConfigGetResponse {
at := mw.getAnytype()
at := mw.GetAnytype()
if at == nil {
return &pb.RpcConfigGetResponse{Error: &pb.RpcConfigGetResponseError{pb.RpcConfigGetResponseError_NODE_NOT_STARTED, "account not started"}}
}

View file

@ -82,7 +82,7 @@ func (mw *Middleware) stop() error {
return nil
}
func (mw *Middleware) getAnytype() core.Service {
func (mw *Middleware) GetAnytype() core.Service {
mw.m.RLock()
defer mw.m.RUnlock()
if mw.app != nil {

View file

@ -3,6 +3,8 @@ package history
import (
"testing"
"github.com/anytypeio/go-anytype-middleware/app"
"github.com/anytypeio/go-anytype-middleware/app/testapp"
"github.com/anytypeio/go-anytype-middleware/change"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/state"
"github.com/anytypeio/go-anytype-middleware/pb"
@ -17,7 +19,7 @@ import (
func TestHistory_Versions(t *testing.T) {
t.Run("no version", func(t *testing.T) {
fx := newFixture(t)
defer fx.tearDown()
defer fx.tearDown(t)
fx.newTestSB("pageId").AddChanges("a",
newSnapshot("s1", "", nil),
newChange("c2", "s1", "s1"),
@ -29,7 +31,7 @@ func TestHistory_Versions(t *testing.T) {
})
t.Run("chunks", func(t *testing.T) {
fx := newFixture(t)
defer fx.tearDown()
defer fx.tearDown(t)
fx.newTestSB("pageId").AddChanges("a",
newSnapshot("s1", "", nil),
newChange("c2", "s1", "s1"),
@ -47,41 +49,53 @@ func TestHistory_Versions(t *testing.T) {
func newFixture(t *testing.T) *fixture {
ctrl := gomock.NewController(t)
a := testMock.NewMockService(ctrl)
m := mockMeta.NewMockService(ctrl)
h := New()
ta := testapp.New().
With(&bs{}).
With(h)
a := testMock.RegisterMockAnytype(ctrl, ta)
a.EXPECT().PredefinedBlocks().Return(threads.DerivedSmartblockIds{
Profile: "profileId",
}).AnyTimes()
a.EXPECT().ObjectStore().Return(nil).AnyTimes()
a.EXPECT().ProfileID().AnyTimes()
a.EXPECT().LocalProfile().AnyTimes()
mockMeta.RegisterMockMeta(ctrl, ta)
require.NoError(t, ta.Start())
return &fixture{
History: NewHistory(a, new(bs), m),
anytype: a,
meta: m,
History: h,
ta: ta,
ctrl: ctrl,
}
}
type fixture struct {
History
anytype *testMock.MockService
meta *mockMeta.MockService
ctrl *gomock.Controller
ta *testapp.TestApp
ctrl *gomock.Controller
}
func (fx *fixture) newTestSB(id string) *change.TestSmartblock {
sb := change.NewTestSmartBlock()
fx.anytype.EXPECT().GetBlock(id).Return(sb, nil).AnyTimes()
testMock.GetMockAnytype(fx.ta).EXPECT().GetBlock(id).Return(sb, nil).AnyTimes()
return sb
}
func (fx *fixture) tearDown() {
func (fx *fixture) tearDown(t *testing.T) {
require.NoError(t, fx.ta.Close())
fx.ctrl.Finish()
}
type bs struct{}
func (b *bs) Init(_ *app.App) (err error) {
return
}
func (b *bs) Name() (name string) {
return "blockService"
}
func (b *bs) ResetToState(pageId string, s *state.State) (err error) {
return
}

View file

@ -53,6 +53,7 @@ type SearchInfo struct {
type GetSearchInfo interface {
GetSearchInfo(id string) (info SearchInfo, err error)
app.Component
}
type indexer struct {

View file

@ -4,7 +4,7 @@ import (
"testing"
"time"
"github.com/anytypeio/go-anytype-middleware/app"
"github.com/anytypeio/go-anytype-middleware/app/testapp"
"github.com/anytypeio/go-anytype-middleware/core/indexer"
"github.com/anytypeio/go-anytype-middleware/pb"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/core"
@ -81,15 +81,17 @@ func TestNewIndexer(t *testing.T) {
}
func newFixture(t *testing.T) *fixture {
var err error
a := new(app.App)
ta := testapp.New()
fx := &fixture{
a: a,
ctrl: gomock.NewController(t),
ta: ta,
}
fx.anytype = testMock.RegisterMockAnytype(fx.ctrl, ta)
fx.getSerach = mockIndexer.NewMockGetSearchInfo(fx.ctrl)
fx.anytype = testMock.NewMockService(fx.ctrl)
fx.getSerach.EXPECT().Name().AnyTimes().Return("blockService")
fx.getSerach.EXPECT().Init(gomock.Any())
fx.objectStore = testMock.NewMockObjectStore(fx.ctrl)
fx.objectStore.EXPECT().FTSearch().Return(nil).AnyTimes()
fx.objectStore.EXPECT().CreateObject(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
@ -99,13 +101,9 @@ func newFixture(t *testing.T) *fixture {
fx.ch = make(chan core.SmartblockRecordWithThreadID)
fx.anytype.EXPECT().SubscribeForNewRecords(gomock.Any()).Return(fx.ch, nil)
fx.Indexer = indexer.New()
a.Register(fx.getSerach).
Register(fx.anytype).
Register(fx.objectStore).
Register(fx.Indexer)
ta.With(fx.Indexer).With(fx.getSerach)
err = a.Start()
require.NoError(t, err)
require.NoError(t, ta.Start())
return fx
}
@ -116,10 +114,10 @@ type fixture struct {
objectStore *testMock.MockObjectStore
getSerach *mockIndexer.MockGetSearchInfo
ch chan core.SmartblockRecordWithThreadID
a *app.App
ta *testapp.TestApp
}
func (fx *fixture) tearDown() {
fx.a.Close()
fx.ta.Close()
fx.ctrl.Finish()
}

View file

@ -27,7 +27,7 @@ func (mw *Middleware) ObjectTypeRelationList(req *pb.RpcObjectTypeRelationListRe
}
return m
}
at := mw.getAnytype()
at := mw.GetAnytype()
if at == nil {
return response(pb.RpcObjectTypeRelationListResponseError_BAD_INPUT, nil, fmt.Errorf("account must be started"))
}
@ -53,7 +53,7 @@ func (mw *Middleware) ObjectTypeRelationAdd(req *pb.RpcObjectTypeRelationAddRequ
return m
}
at := mw.getAnytype()
at := mw.GetAnytype()
if at == nil {
return response(pb.RpcObjectTypeRelationAddResponseError_BAD_INPUT, nil, fmt.Errorf("account must be started"))
}
@ -97,7 +97,7 @@ func (mw *Middleware) ObjectTypeRelationUpdate(req *pb.RpcObjectTypeRelationUpda
return m
}
at := mw.getAnytype()
at := mw.GetAnytype()
if at == nil {
return response(pb.RpcObjectTypeRelationUpdateResponseError_BAD_INPUT, fmt.Errorf("account must be started"))
}
@ -139,7 +139,7 @@ func (mw *Middleware) ObjectTypeRelationRemove(req *pb.RpcObjectTypeRelationRemo
return m
}
at := mw.getAnytype()
at := mw.GetAnytype()
if at == nil {
return response(pb.RpcObjectTypeRelationRemoveResponseError_BAD_INPUT, fmt.Errorf("account must be started"))
}
@ -249,7 +249,7 @@ func (mw *Middleware) ObjectTypeList(_ *pb.RpcObjectTypeListRequest) *pb.RpcObje
return response(pb.RpcObjectTypeListResponseError_UNKNOWN_ERROR, nil, err)
}
at := mw.getAnytype()
at := mw.GetAnytype()
if at == nil {
return response(pb.RpcObjectTypeListResponseError_BAD_INPUT, nil, fmt.Errorf("account must be started"))
}

View file

@ -82,7 +82,7 @@ func addRelation(t *testing.T, contextId string, mw *Middleware) (key string, na
func TestRelationAdd(t *testing.T) {
rootPath, mw := start(t)
respOpenNewPage := mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.Anytype.PredefinedBlocks().SetPages})
respOpenNewPage := mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.GetAnytype().PredefinedBlocks().SetPages})
require.Equal(t, 0, int(respOpenNewPage.Error.Code), respOpenNewPage.Error.Description)
require.Len(t, respOpenNewPage.Event.Messages, 2)
block := getBlockById("dataview", getEventBlockShow(respOpenNewPage.Event.Messages).Blocks)
@ -91,7 +91,7 @@ func TestRelationAdd(t *testing.T) {
t.Run("add_incorrect", func(t *testing.T) {
respDataviewRelationAdd := mw.BlockDataviewRelationAdd(&pb.RpcBlockDataviewRelationAddRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
Relation: &pbrelation.Relation{
Key: "name",
@ -101,7 +101,7 @@ func TestRelationAdd(t *testing.T) {
},
})
require.Equal(t, pb.RpcBlockDataviewRelationAddResponseError_BAD_INPUT, respDataviewRelationAdd.Error.Code, respDataviewRelationAdd.Error.Description)
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.Anytype.PredefinedBlocks().SetPages})
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.GetAnytype().PredefinedBlocks().SetPages})
require.Equal(t, 0, int(respOpenNewPage.Error.Code), respOpenNewPage.Error.Description)
require.Len(t, respOpenNewPage.Event.Messages, 2)
block = getBlockById("dataview", getEventBlockShow(respOpenNewPage.Event.Messages).Blocks)
@ -111,7 +111,7 @@ func TestRelationAdd(t *testing.T) {
t.Run("add_correct", func(t *testing.T) {
respDataviewRelationAdd := mw.BlockDataviewRelationAdd(&pb.RpcBlockDataviewRelationAddRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
Relation: &pbrelation.Relation{
Key: "",
@ -122,15 +122,15 @@ func TestRelationAdd(t *testing.T) {
})
require.Equal(t, 0, int(respDataviewRelationAdd.Error.Code), respDataviewRelationAdd.Error.Description)
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.Anytype.PredefinedBlocks().SetPages})
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.GetAnytype().PredefinedBlocks().SetPages})
require.Equal(t, 0, int(respOpenNewPage.Error.Code), respOpenNewPage.Error.Description)
require.Len(t, respOpenNewPage.Event.Messages, 2)
block = getBlockById("dataview", getEventBlockShow(respOpenNewPage.Event.Messages).Blocks)
require.Len(t, block.GetDataview().Relations, len(bundle.MustGetType(bundle.TypeKeyPage).Relations)+1)
respAccountCreate := mw.AccountSelect(&pb.RpcAccountSelectRequest{Id: mw.Anytype.Account(), RootPath: rootPath})
respAccountCreate := mw.AccountSelect(&pb.RpcAccountSelectRequest{Id: mw.GetAnytype().Account(), RootPath: rootPath})
require.Equal(t, 0, int(respAccountCreate.Error.Code))
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.Anytype.PredefinedBlocks().SetPages})
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.GetAnytype().PredefinedBlocks().SetPages})
require.Equal(t, 0, int(respOpenNewPage.Error.Code), respOpenNewPage.Error.Description)
require.Len(t, respOpenNewPage.Event.Messages, 2)
block = getBlockById("dataview", getEventBlockShow(respOpenNewPage.Event.Messages).Blocks)
@ -246,22 +246,22 @@ func TestRelationAdd(t *testing.T) {
t.Run("update_not_existing", func(t *testing.T) {
respUpdate := mw.BlockDataviewRelationUpdate(&pb.RpcBlockDataviewRelationUpdateRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
RelationKey: "not_existing_key",
Relation: &pbrelation.Relation{Key: "ffff"},
})
require.Equal(t, pb.RpcBlockDataviewRelationUpdateResponseError_BAD_INPUT, respUpdate.Error.Code, respUpdate.Error.Description)
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.Anytype.PredefinedBlocks().SetPages})
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.GetAnytype().PredefinedBlocks().SetPages})
require.Equal(t, 0, int(respOpenNewPage.Error.Code), respOpenNewPage.Error.Description)
require.Len(t, respOpenNewPage.Event.Messages, 2)
block = getBlockById("dataview", getEventBlockShow(respOpenNewPage.Event.Messages).Blocks)
})
t.Run("update_cant_change_format", func(t *testing.T) {
relKey, relName := addRelation(t, mw.Anytype.PredefinedBlocks().SetPages, mw)
relKey, relName := addRelation(t, mw.GetAnytype().PredefinedBlocks().SetPages, mw)
respUpdate := mw.BlockDataviewRelationUpdate(&pb.RpcBlockDataviewRelationUpdateRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
RelationKey: relKey,
Relation: &pbrelation.Relation{
@ -272,7 +272,7 @@ func TestRelationAdd(t *testing.T) {
},
})
require.Equal(t, pb.RpcBlockDataviewRelationUpdateResponseError_BAD_INPUT, respUpdate.Error.Code, respUpdate.Error.Description)
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.Anytype.PredefinedBlocks().SetPages})
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.GetAnytype().PredefinedBlocks().SetPages})
require.Equal(t, 0, int(respOpenNewPage.Error.Code), respOpenNewPage.Error.Description)
require.Len(t, respOpenNewPage.Event.Messages, 2)
block = getBlockById("dataview", getEventBlockShow(respOpenNewPage.Event.Messages).Blocks)
@ -281,10 +281,10 @@ func TestRelationAdd(t *testing.T) {
})
t.Run("update_correct", func(t *testing.T) {
relKey, _ := addRelation(t, mw.Anytype.PredefinedBlocks().SetPages, mw)
relKey, _ := addRelation(t, mw.GetAnytype().PredefinedBlocks().SetPages, mw)
respUpdate := mw.BlockDataviewRelationUpdate(&pb.RpcBlockDataviewRelationUpdateRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
RelationKey: relKey,
Relation: &pbrelation.Relation{
@ -295,7 +295,7 @@ func TestRelationAdd(t *testing.T) {
},
})
require.Equal(t, pb.RpcBlockDataviewRelationUpdateResponseError_NULL, respUpdate.Error.Code, respUpdate.Error.Description)
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.Anytype.PredefinedBlocks().SetPages})
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.GetAnytype().PredefinedBlocks().SetPages})
require.Equal(t, 0, int(respOpenNewPage.Error.Code), respOpenNewPage.Error.Description)
require.Len(t, respOpenNewPage.Event.Messages, 2)
block = getBlockById("dataview", getEventBlockShow(respOpenNewPage.Event.Messages).Blocks)
@ -305,31 +305,31 @@ func TestRelationAdd(t *testing.T) {
t.Run("delete_incorrect", func(t *testing.T) {
respDataviewRelationAdd := mw.BlockDataviewRelationDelete(&pb.RpcBlockDataviewRelationDeleteRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
RelationKey: "not_existing_key",
})
require.Equal(t, pb.RpcBlockDataviewRelationDeleteResponseError_BAD_INPUT, respDataviewRelationAdd.Error.Code, respDataviewRelationAdd.Error.Description)
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.Anytype.PredefinedBlocks().SetPages})
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.GetAnytype().PredefinedBlocks().SetPages})
require.Equal(t, 0, int(respOpenNewPage.Error.Code), respOpenNewPage.Error.Description)
require.Len(t, respOpenNewPage.Event.Messages, 2)
block = getBlockById("dataview", getEventBlockShow(respOpenNewPage.Event.Messages).Blocks)
})
t.Run("delete_correct", func(t *testing.T) {
relKey, _ := addRelation(t, mw.Anytype.PredefinedBlocks().SetPages, mw)
relKey, _ := addRelation(t, mw.GetAnytype().PredefinedBlocks().SetPages, mw)
respDataviewRelationDelete := mw.BlockDataviewRelationDelete(&pb.RpcBlockDataviewRelationDeleteRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
RelationKey: relKey,
})
//mw.blocksService.Close()
respAccountCreate := mw.AccountSelect(&pb.RpcAccountSelectRequest{Id: mw.Anytype.Account(), RootPath: rootPath})
respAccountCreate := mw.AccountSelect(&pb.RpcAccountSelectRequest{Id: mw.GetAnytype().Account(), RootPath: rootPath})
require.Equal(t, 0, int(respAccountCreate.Error.Code))
require.Equal(t, 0, int(respDataviewRelationDelete.Error.Code), respDataviewRelationDelete.Error.Description)
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.Anytype.PredefinedBlocks().SetPages})
respOpenNewPage = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.GetAnytype().PredefinedBlocks().SetPages})
require.Equal(t, 0, int(respOpenNewPage.Error.Code), respOpenNewPage.Error.Description)
require.Len(t, respOpenNewPage.Event.Messages, 2)
block = getBlockById("dataview", getEventBlockShow(respOpenNewPage.Event.Messages).Blocks)
@ -339,7 +339,7 @@ func TestRelationAdd(t *testing.T) {
t.Run("relation_add_select_option", func(t *testing.T) {
respRelCreate := mw.BlockDataviewRelationAdd(&pb.RpcBlockDataviewRelationAddRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
Relation: &pbrelation.Relation{
Format: pbrelation.RelationFormat_status,
@ -370,7 +370,7 @@ func TestRelationAdd(t *testing.T) {
respRecordCreate := mw.BlockDataviewRecordCreate(
&pb.RpcBlockDataviewRecordCreateRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
})
@ -378,7 +378,7 @@ func TestRelationAdd(t *testing.T) {
newPageId := respRecordCreate.Record.Fields["id"].GetStringValue()
respRelOptCreate := mw.BlockDataviewRecordRelationOptionAdd(&pb.RpcBlockDataviewRecordRelationOptionAddRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
Option: &pbrelation.RelationOption{
Text: "opt1",
@ -392,7 +392,7 @@ func TestRelationAdd(t *testing.T) {
respRecordUpdate := mw.BlockDataviewRecordUpdate(
&pb.RpcBlockDataviewRecordUpdateRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
RecordId: newPageId,
Record: &types2.Struct{
@ -412,7 +412,7 @@ func TestRelationAdd(t *testing.T) {
require.Equal(t, foundRel.Key, relOnPage.Key)
respOptAdd := mw.BlockDataviewRecordRelationOptionAdd(&pb.RpcBlockDataviewRecordRelationOptionAddRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
RelationKey: foundRel.Key,
RecordId: newPageId,
@ -427,7 +427,7 @@ func TestRelationAdd(t *testing.T) {
respRecordUpdate2 := mw.BlockDataviewRecordUpdate(
&pb.RpcBlockDataviewRecordUpdateRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
RecordId: newPageId,
Record: &types2.Struct{
@ -608,27 +608,27 @@ func TestRelationAdd(t *testing.T) {
},
}
respDvRelAdd := mw.BlockDataviewRelationAdd(&pb.RpcBlockDataviewRelationAddRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
Relation: rel1,
})
require.Equal(t, 0, int(respDvRelAdd.Error.Code), respDvRelAdd.Error.Description)
respDvRelAdd = mw.BlockDataviewRelationAdd(&pb.RpcBlockDataviewRelationAddRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
Relation: rel2,
})
require.Equal(t, 0, int(respDvRelAdd.Error.Code), respDvRelAdd.Error.Description)
respDvRelAdd = mw.BlockDataviewRelationAdd(&pb.RpcBlockDataviewRelationAddRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
Relation: rel3,
})
require.Equal(t, 0, int(respDvRelAdd.Error.Code), respDvRelAdd.Error.Description)
respOpenNewPage := mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.Anytype.PredefinedBlocks().SetPages})
respOpenNewPage := mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.GetAnytype().PredefinedBlocks().SetPages})
require.Equal(t, 0, int(respOpenNewPage.Error.Code), respOpenNewPage.Error.Description)
block := getBlockById("dataview", getEventBlockShow(respOpenNewPage.Event.Messages).Blocks)
for _, test := range tests {
@ -673,7 +673,7 @@ func TestRelationAdd(t *testing.T) {
})
respRelCreate := mw.BlockDataviewRelationAdd(&pb.RpcBlockDataviewRelationAddRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
Relation: &pbrelation.Relation{
Format: pbrelation.RelationFormat_status,
@ -702,7 +702,7 @@ func TestRelationAdd(t *testing.T) {
respRecordCreate := mw.BlockDataviewRecordCreate(
&pb.RpcBlockDataviewRecordCreateRequest{
ContextId: mw.Anytype.PredefinedBlocks().SetPages,
ContextId: mw.GetAnytype().PredefinedBlocks().SetPages,
BlockId: "dataview",
})
@ -774,7 +774,7 @@ func TestCustomType(t *testing.T) {
show := getEventBlockShow(respOpenCustomTypeObject.Event.Messages)
require.NotNil(t, show)
profile := getDetailsForContext(show.Details, mw.Anytype.PredefinedBlocks().Profile)
profile := getDetailsForContext(show.Details, mw.GetAnytype().PredefinedBlocks().Profile)
require.NotNil(t, profile)
// should have custom obj type + profile, because it has the relation `creator`
require.Len(t, show.ObjectTypes, 3)
@ -792,7 +792,7 @@ func TestCustomType(t *testing.T) {
var customObjectDetails = getDetailsForContext(show.Details, customObjectId)
require.NotNil(t, customObjectDetails)
require.Equal(t, mw.Anytype.PredefinedBlocks().Profile, pbtypes.GetString(customObjectDetails, bundle.RelationKeyCreator.String()))
require.Equal(t, mw.GetAnytype().PredefinedBlocks().Profile, pbtypes.GetString(customObjectDetails, bundle.RelationKeyCreator.String()))
rel := getRelationByKey(show.Relations, newRelation.Key)
require.NotNil(t, rel)
require.Equal(t, newRelation, rel)
@ -841,13 +841,13 @@ func TestBundledType(t *testing.T) {
pageDetails := getDetailsForContext(show.Details, respCreatePage.PageId)
require.NotNil(t, pageDetails)
require.Equal(t, mw.Anytype.PredefinedBlocks().Profile, pbtypes.GetString(pageDetails, bundle.RelationKeyCreator.String()))
profile := getDetailsForContext(show.Details, mw.Anytype.PredefinedBlocks().Profile)
require.Equal(t, mw.GetAnytype().PredefinedBlocks().Profile, pbtypes.GetString(pageDetails, bundle.RelationKeyCreator.String()))
profile := getDetailsForContext(show.Details, mw.GetAnytype().PredefinedBlocks().Profile)
require.NotNil(t, profile, fmt.Sprintf("%s got no details for profile", show.RootId))
time.Sleep(time.Second)
respOpenPagesSet := mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.Anytype.PredefinedBlocks().SetPages})
respOpenPagesSet := mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.GetAnytype().PredefinedBlocks().SetPages})
require.Equal(t, 0, int(respOpenPagesSet.Error.Code), respOpenPagesSet.Error.Description)
show = getEventBlockShow(respOpenPagesSet.Event.Messages)
@ -863,7 +863,7 @@ func TestBundledType(t *testing.T) {
require.Equal(t, 0, int(respCreatePage.Error.Code), respCreatePage.Error.Description)
time.Sleep(time.Second)
respOpenPagesSet = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.Anytype.PredefinedBlocks().SetPages})
respOpenPagesSet = mw.BlockOpen(&pb.RpcBlockOpenRequest{BlockId: mw.GetAnytype().PredefinedBlocks().SetPages})
require.Equal(t, 0, int(respOpenPagesSet.Error.Code), respOpenPagesSet.Error.Description)
show = getEventBlockShow(respOpenPagesSet.Event.Messages)

289
go.sum

File diff suppressed because it is too large Load diff

View file

@ -12,7 +12,7 @@ import (
func TestCache_Fetch(t *testing.T) {
ts := newTestServer("text/html", strings.NewReader(tetsHtml))
lp := NewWithCache()
lp.Init(nil)
info, err := lp.Fetch(ctx, ts.URL)
require.NoError(t, err)
assert.Equal(t, model.LinkPreview{

View file

@ -20,6 +20,7 @@ func TestLinkPreview_Fetch(t *testing.T) {
ts := newTestServer("text/html", strings.NewReader(tetsHtml))
defer ts.Close()
lp := New()
lp.Init(nil)
info, err := lp.Fetch(ctx, ts.URL)
require.NoError(t, err)
@ -37,6 +38,7 @@ func TestLinkPreview_Fetch(t *testing.T) {
ts := newTestServer("text/html", strings.NewReader(tetsHtmlWithoutDescription))
defer ts.Close()
lp := New()
lp.Init(nil)
info, err := lp.Fetch(ctx, ts.URL)
require.NoError(t, err)
@ -56,6 +58,7 @@ func TestLinkPreview_Fetch(t *testing.T) {
defer ts.Close()
url := ts.URL + "/filename.jpg"
lp := New()
lp.Init(nil)
info, err := lp.Fetch(ctx, url)
require.NoError(t, err)
assert.Equal(t, model.LinkPreview{
@ -73,6 +76,7 @@ func TestLinkPreview_Fetch(t *testing.T) {
defer ts.Close()
url := ts.URL + "/filename.jpg"
lp := New()
lp.Init(nil)
info, err := lp.Fetch(ctx, url)
require.NoError(t, err)
assert.Equal(t, model.LinkPreview{

View file

@ -1,3 +1,24 @@
//go:generate mockgen -package testMock -destination anytype_mock.go github.com/anytypeio/go-anytype-middleware/core/anytype Service,SmartBlock,SmartBlockSnapshot,File,Image,ObjectStore
//go:generate mockgen -package testMock -destination anytype_mock.go github.com/anytypeio/go-anytype-middleware/pkg/lib/core Service,SmartBlock,SmartBlockSnapshot,File,Image
//go:generate mockgen -package testMock -destination objectstore_mock.go github.com/anytypeio/go-anytype-middleware/pkg/lib/localstore ObjectStore
//go:generate mockgen -package testMock -destination history_mock.go github.com/anytypeio/go-anytype-middleware/core/block/undo History
package testMock
import (
"github.com/anytypeio/go-anytype-middleware/app/testapp"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/core"
"github.com/golang/mock/gomock"
)
func RegisterMockAnytype(ctrl *gomock.Controller, ta *testapp.TestApp) *MockService {
ms := NewMockService(ctrl)
ms.EXPECT().Name().AnyTimes().Return(core.CName)
ms.EXPECT().Init(gomock.Any()).AnyTimes()
ms.EXPECT().Run().AnyTimes()
ms.EXPECT().Close().AnyTimes()
ta.Register(ms)
return ms
}
func GetMockAnytype(ta *testapp.TestApp) *MockService {
return ta.MustComponent(core.CName).(*MockService)
}

View file

@ -1,2 +1,22 @@
//go:generate mockgen -package mockMeta -destination meta_mock.go github.com/anytypeio/go-anytype-middleware/core/block/meta Service,PubSub,Subscriber
package mockMeta
import (
"github.com/anytypeio/go-anytype-middleware/app/testapp"
"github.com/anytypeio/go-anytype-middleware/core/block/meta"
"github.com/golang/mock/gomock"
)
func RegisterMockMeta(ctrl *gomock.Controller, ta *testapp.TestApp) *MockService {
ms := NewMockService(ctrl)
ms.EXPECT().Name().AnyTimes().Return(meta.CName)
ms.EXPECT().Init(gomock.Any()).AnyTimes()
ms.EXPECT().Run().AnyTimes()
ms.EXPECT().Close().AnyTimes()
ta.Register(ms)
return ms
}
func GetMockMeta(ta *testapp.TestApp) *MockService {
return ta.MustComponent(meta.CName).(*MockService)
}