mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-07 21:37:04 +09:00
GO-4316: Add tests
This commit is contained in:
parent
5ccd6dd09d
commit
1e1563f9ae
4 changed files with 207 additions and 5 deletions
184
core/block/editor/smartblock/dependencies_test.go
Normal file
184
core/block/editor/smartblock/dependencies_test.go
Normal file
|
@ -0,0 +1,184 @@
|
|||
package smartblock
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogo/protobuf/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/anyproto/anytype-heart/core/block/editor/state"
|
||||
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
|
||||
"github.com/anyproto/anytype-heart/pkg/lib/localstore/objectstore"
|
||||
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
|
||||
bb "github.com/anyproto/anytype-heart/tests/blockbuilder"
|
||||
"github.com/anyproto/anytype-heart/util/pbtypes"
|
||||
)
|
||||
|
||||
func TestDependenciesSubscription(t *testing.T) {
|
||||
t.Run("with existing dependencies", func(t *testing.T) {
|
||||
mainObjId := "id"
|
||||
fx := newFixture(mainObjId, t)
|
||||
|
||||
space1obj1 := "obj1"
|
||||
space1obj2 := "obj2"
|
||||
space2obj1 := "obj3"
|
||||
|
||||
fx.objectStore.AddObjects(t, testSpaceId, []objectstore.TestObject{
|
||||
{
|
||||
bundle.RelationKeyId: pbtypes.String(space1obj1),
|
||||
bundle.RelationKeySpaceId: pbtypes.String(testSpaceId),
|
||||
bundle.RelationKeyName: pbtypes.String("Object 1"),
|
||||
},
|
||||
{
|
||||
bundle.RelationKeyId: pbtypes.String(space1obj2),
|
||||
bundle.RelationKeySpaceId: pbtypes.String(testSpaceId),
|
||||
bundle.RelationKeyName: pbtypes.String("Object 2"),
|
||||
},
|
||||
})
|
||||
fx.objectStore.AddObjects(t, "space2", []objectstore.TestObject{
|
||||
{
|
||||
bundle.RelationKeyId: pbtypes.String(space2obj1),
|
||||
bundle.RelationKeySpaceId: pbtypes.String("space2"),
|
||||
bundle.RelationKeyName: pbtypes.String("Object 3"),
|
||||
},
|
||||
})
|
||||
|
||||
fx.spaceIdResolver.EXPECT().ResolveSpaceID(space1obj1).Return(testSpaceId, nil)
|
||||
fx.spaceIdResolver.EXPECT().ResolveSpaceID(space1obj2).Return(testSpaceId, nil)
|
||||
fx.spaceIdResolver.EXPECT().ResolveSpaceID(space2obj1).Return("space2", nil)
|
||||
|
||||
root := bb.Root(
|
||||
bb.ID(mainObjId),
|
||||
bb.Children(
|
||||
bb.Link(space1obj1),
|
||||
bb.Link(space1obj2),
|
||||
bb.Link(space2obj1),
|
||||
),
|
||||
)
|
||||
|
||||
fx.Doc = state.NewDoc(mainObjId, root.BuildMap()).NewState()
|
||||
objDetails := &types.Struct{
|
||||
Fields: map[string]*types.Value{
|
||||
bundle.RelationKeyId.String(): pbtypes.String(mainObjId),
|
||||
bundle.RelationKeySpaceId.String(): pbtypes.String(testSpaceId),
|
||||
bundle.RelationKeyName.String(): pbtypes.String("Main object"),
|
||||
},
|
||||
}
|
||||
|
||||
fx.Doc.(*state.State).SetDetails(objDetails)
|
||||
|
||||
details, err := fx.fetchMeta()
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, details)
|
||||
|
||||
wantDetails := []*model.ObjectViewDetailsSet{
|
||||
{
|
||||
Id: mainObjId,
|
||||
Details: &types.Struct{
|
||||
Fields: map[string]*types.Value{
|
||||
bundle.RelationKeyId.String(): pbtypes.String(mainObjId),
|
||||
bundle.RelationKeySpaceId.String(): pbtypes.String(testSpaceId),
|
||||
bundle.RelationKeyName.String(): pbtypes.String("Main object"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Id: space1obj1,
|
||||
Details: &types.Struct{
|
||||
Fields: map[string]*types.Value{
|
||||
bundle.RelationKeyId.String(): pbtypes.String(space1obj1),
|
||||
bundle.RelationKeySpaceId.String(): pbtypes.String(testSpaceId),
|
||||
bundle.RelationKeyName.String(): pbtypes.String("Object 1"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Id: space1obj2,
|
||||
Details: &types.Struct{
|
||||
Fields: map[string]*types.Value{
|
||||
bundle.RelationKeyId.String(): pbtypes.String(space1obj2),
|
||||
bundle.RelationKeySpaceId.String(): pbtypes.String(testSpaceId),
|
||||
bundle.RelationKeyName.String(): pbtypes.String("Object 2"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Id: space2obj1,
|
||||
Details: &types.Struct{
|
||||
Fields: map[string]*types.Value{
|
||||
bundle.RelationKeyId.String(): pbtypes.String(space2obj1),
|
||||
bundle.RelationKeySpaceId.String(): pbtypes.String("space2"),
|
||||
bundle.RelationKeyName.String(): pbtypes.String("Object 3"),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
assert.ElementsMatch(t, wantDetails, details)
|
||||
|
||||
fx.closeRecordsSub()
|
||||
})
|
||||
|
||||
t.Run("with added dependencies", func(t *testing.T) {
|
||||
mainObjId := "id"
|
||||
fx := newFixture(mainObjId, t)
|
||||
|
||||
root := bb.Root(
|
||||
bb.ID(mainObjId),
|
||||
bb.Children(),
|
||||
)
|
||||
fx.Doc = state.NewDoc(mainObjId, root.BuildMap()).NewState()
|
||||
|
||||
details, err := fx.fetchMeta()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, details, 1) // Only its own details
|
||||
|
||||
// Simulate changes in state
|
||||
|
||||
space1obj1 := "obj1"
|
||||
space1obj2 := "obj2"
|
||||
space2obj1 := "obj3"
|
||||
|
||||
fx.objectStore.AddObjects(t, testSpaceId, []objectstore.TestObject{
|
||||
{
|
||||
bundle.RelationKeyId: pbtypes.String(space1obj1),
|
||||
bundle.RelationKeySpaceId: pbtypes.String(testSpaceId),
|
||||
bundle.RelationKeyName: pbtypes.String("Object 1"),
|
||||
},
|
||||
{
|
||||
bundle.RelationKeyId: pbtypes.String(space1obj2),
|
||||
bundle.RelationKeySpaceId: pbtypes.String(testSpaceId),
|
||||
bundle.RelationKeyName: pbtypes.String("Object 2"),
|
||||
},
|
||||
})
|
||||
fx.objectStore.AddObjects(t, "space2", []objectstore.TestObject{
|
||||
{
|
||||
bundle.RelationKeyId: pbtypes.String(space2obj1),
|
||||
bundle.RelationKeySpaceId: pbtypes.String("space2"),
|
||||
bundle.RelationKeyName: pbtypes.String("Object 3"),
|
||||
},
|
||||
})
|
||||
|
||||
fx.spaceIdResolver.EXPECT().ResolveSpaceID(space1obj1).Return(testSpaceId, nil)
|
||||
fx.spaceIdResolver.EXPECT().ResolveSpaceID(space1obj2).Return(testSpaceId, nil)
|
||||
fx.spaceIdResolver.EXPECT().ResolveSpaceID(space2obj1).Return("space2", nil)
|
||||
|
||||
root = bb.Root(
|
||||
bb.ID(mainObjId),
|
||||
bb.Children(
|
||||
bb.Link(space1obj1),
|
||||
bb.Link(space1obj2),
|
||||
bb.Link(space2obj1),
|
||||
),
|
||||
)
|
||||
fx.Doc = state.NewDoc(mainObjId, root.BuildMap()).NewState()
|
||||
|
||||
fx.CheckSubscriptions()
|
||||
|
||||
assert.Contains(t, fx.smartBlock.lastDepDetails, space1obj1)
|
||||
assert.Contains(t, fx.smartBlock.lastDepDetails, space1obj2)
|
||||
assert.Contains(t, fx.smartBlock.lastDepDetails, space2obj1)
|
||||
})
|
||||
|
||||
}
|
|
@ -119,6 +119,7 @@ func New(
|
|||
eventSender: eventSender,
|
||||
objectStore: objectStore,
|
||||
spaceIdResolver: spaceIdResolver,
|
||||
lastDepDetails: map[string]*pb.EventObjectDetailsSet{},
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
@ -327,7 +328,6 @@ func (sb *smartBlock) Init(ctx *InitContext) (err error) {
|
|||
}
|
||||
sb.undo = undo.NewHistory(0)
|
||||
sb.restrictions = sb.restrictionService.GetRestrictions(sb)
|
||||
sb.lastDepDetails = map[string]*pb.EventObjectDetailsSet{}
|
||||
if ctx.State != nil {
|
||||
// need to store file keys in case we have some new files in the state
|
||||
sb.storeFileKeys(ctx.State)
|
||||
|
|
|
@ -139,13 +139,14 @@ func TestSmartBlock_getDetailsFromStore(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
fx.store.AddObjects(t, []spaceindex.TestObject{
|
||||
{
|
||||
err := fx.store.UpdateObjectDetails(context.Background(), id, &types.Struct{
|
||||
Fields: map[string]*types.Value{
|
||||
"id": pbtypes.String(id),
|
||||
"number": pbtypes.Float64(2.18281828459045),
|
||||
"🔥": pbtypes.StringList([]string{"Jeanne d'Arc", "Giordano Bruno", "Capocchio"}),
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// when
|
||||
detailsFromStore, err := fx.getDetailsFromStore()
|
||||
|
@ -457,18 +458,22 @@ func TestInjectDerivedDetails(t *testing.T) {
|
|||
}
|
||||
|
||||
type fixture struct {
|
||||
store *spaceindex.StoreFixture
|
||||
objectStore *objectstore.StoreFixture
|
||||
store spaceindex.Store
|
||||
restrictionService *mock_restriction.MockService
|
||||
indexer *MockIndexer
|
||||
eventSender *mock_event.MockSender
|
||||
source *sourceStub
|
||||
spaceIdResolver *mock_idresolver.MockResolver
|
||||
|
||||
*smartBlock
|
||||
}
|
||||
|
||||
const testSpaceId = "space1"
|
||||
|
||||
func newFixture(id string, t *testing.T) *fixture {
|
||||
objectStore := objectstore.NewStoreFixture(t)
|
||||
spaceIndex := spaceindex.NewStoreFixture(t)
|
||||
spaceIndex := objectStore.SpaceIndex(testSpaceId)
|
||||
|
||||
spaceIdResolver := mock_idresolver.NewMockResolver(t)
|
||||
|
||||
|
@ -486,6 +491,7 @@ func newFixture(id string, t *testing.T) *fixture {
|
|||
sbType: smartblock.SmartBlockTypePage,
|
||||
}
|
||||
sb.source = source
|
||||
|
||||
return &fixture{
|
||||
source: source,
|
||||
smartBlock: sb,
|
||||
|
@ -493,6 +499,8 @@ func newFixture(id string, t *testing.T) *fixture {
|
|||
restrictionService: restrictionService,
|
||||
indexer: indexer,
|
||||
eventSender: sender,
|
||||
spaceIdResolver: spaceIdResolver,
|
||||
objectStore: objectStore,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/globalsign/mgo/bson"
|
||||
"github.com/gogo/protobuf/types"
|
||||
|
||||
"github.com/anyproto/anytype-heart/core/block/simple"
|
||||
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
|
||||
"github.com/anyproto/anytype-heart/util/pbtypes"
|
||||
)
|
||||
|
@ -53,6 +54,15 @@ func (b *Block) Build() []*model.Block {
|
|||
}, descendants...)
|
||||
}
|
||||
|
||||
func (b *Block) BuildMap() map[string]simple.Block {
|
||||
blocks := b.Build()
|
||||
res := make(map[string]simple.Block, len(blocks))
|
||||
for _, bl := range blocks {
|
||||
res[bl.Id] = simple.New(bl)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func mkBlock(b *model.Block, opts ...Option) *Block {
|
||||
o := options{
|
||||
// Init children for easier equality check in tests
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue