From 4f45c74c26e5d1b1011eb87771dd79adccc93b33 Mon Sep 17 00:00:00 2001 From: kirillston Date: Thu, 9 Jan 2025 13:51:18 +0300 Subject: [PATCH] GO-4588 Test on changeResolvedLayout --- .mockery.yaml | 5 + .../editor/smartblock/detailsinject_test.go | 107 ++++++++++++++++++ .../editor/smartblock/smartblock_test.go | 6 +- 3 files changed, 117 insertions(+), 1 deletion(-) diff --git a/.mockery.yaml b/.mockery.yaml index b401ba835..e2165456c 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -37,6 +37,11 @@ packages: dir: "{{.InterfaceDir}}" outpkg: "{{.PackageName}}" inpackage: true + Space: + config: + dir: "{{.InterfaceDir}}" + outpkg: "{{.PackageName}}" + inpackage: true github.com/anyproto/anytype-heart/core/block/editor/lastused: interfaces: ObjectUsageUpdater: diff --git a/core/block/editor/smartblock/detailsinject_test.go b/core/block/editor/smartblock/detailsinject_test.go index e9d37d930..7589a1428 100644 --- a/core/block/editor/smartblock/detailsinject_test.go +++ b/core/block/editor/smartblock/detailsinject_test.go @@ -6,12 +6,15 @@ import ( "testing" "time" + "github.com/anyproto/any-sync/app/ocache" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "github.com/anyproto/anytype-heart/core/block/editor/state" "github.com/anyproto/anytype-heart/core/block/simple" "github.com/anyproto/anytype-heart/core/domain" + "github.com/anyproto/anytype-heart/pb" "github.com/anyproto/anytype-heart/pkg/lib/bundle" "github.com/anyproto/anytype-heart/pkg/lib/core/smartblock" "github.com/anyproto/anytype-heart/pkg/lib/localstore/objectstore" @@ -376,3 +379,107 @@ func TestInjectResolvedLayout(t *testing.T) { assert.Equal(t, int64(model.ObjectType_basic), st.LocalDetails().GetInt64(bundle.RelationKeyResolvedLayout)) }) } + +func TestChangeResolvedLayoutForObjects(t *testing.T) { + typeId := "typeId" + t.Run("change resolvedLayout, do not delete layout", func(t *testing.T) { + // given + fx := newFixture(typeId, t) + fx.source.sbType = smartblock.SmartBlockTypeObjectType + + fx.objectStore.AddObjects(t, testSpaceId, []objectstore.TestObject{ + { + bundle.RelationKeyId: domain.String("obj1"), + bundle.RelationKeyType: domain.String(typeId), + bundle.RelationKeyResolvedLayout: domain.Int64(int64(model.ObjectType_basic)), + }, + { + bundle.RelationKeyId: domain.String("obj2"), + bundle.RelationKeyType: domain.String(typeId), + bundle.RelationKeyResolvedLayout: domain.Int64(int64(model.ObjectType_todo)), + }, + { + bundle.RelationKeyId: domain.String("obj3"), + bundle.RelationKeyType: domain.String(typeId), + bundle.RelationKeyResolvedLayout: domain.Int64(int64(model.ObjectType_profile)), + }, + }) + + fx.space.EXPECT().DoLockedIfNotExists(mock.Anything, mock.Anything).RunAndReturn(func(id string, f func() error) error { + if id == "obj1" { + return f() + } + return ocache.ErrExists + }) + + fx.space.EXPECT().Do(mock.Anything, mock.Anything).RunAndReturn(func(id string, f func(SmartBlock) error) error { + assert.Equal(t, "obj3", id) + return nil + }) + + // when + err := fx.changeResolvedLayoutForObjects(makeLayoutChanges(int64(model.ObjectType_todo)), false) + + // then + assert.NoError(t, err) + }) + + t.Run("change resolvedLayout, do not delete layout", func(t *testing.T) { + // given + fx := newFixture(typeId, t) + fx.source.sbType = smartblock.SmartBlockTypeObjectType + + fx.objectStore.AddObjects(t, testSpaceId, []objectstore.TestObject{ + { + bundle.RelationKeyId: domain.String("obj1"), + bundle.RelationKeyType: domain.String(typeId), + bundle.RelationKeyLayout: domain.Int64(int64(model.ObjectType_basic)), + }, + { + bundle.RelationKeyId: domain.String("obj2"), + bundle.RelationKeyType: domain.String(typeId), + bundle.RelationKeyResolvedLayout: domain.Int64(int64(model.ObjectType_todo)), + bundle.RelationKeyLayout: domain.Int64(int64(model.ObjectType_todo)), + }, + { + bundle.RelationKeyId: domain.String("obj3"), + bundle.RelationKeyType: domain.String(typeId), + bundle.RelationKeyLayout: domain.Int64(int64(model.ObjectType_profile)), + }, + { + bundle.RelationKeyId: domain.String("obj4"), + bundle.RelationKeyType: domain.String(typeId), + bundle.RelationKeyResolvedLayout: domain.Int64(int64(model.ObjectType_note)), + }, + }) + + fx.space.EXPECT().DoLockedIfNotExists(mock.Anything, mock.Anything).RunAndReturn(func(id string, f func() error) error { + assert.Equal(t, "obj4", id) + return f() + }) + + counter := 0 + fx.space.EXPECT().Do(mock.Anything, mock.Anything).RunAndReturn(func(id string, f func(SmartBlock) error) error { + counter++ + return nil + }) + + // when + err := fx.changeResolvedLayoutForObjects(makeLayoutChanges(int64(model.ObjectType_todo)), true) + + // then + assert.NoError(t, err) + assert.Equal(t, 3, counter) + }) +} + +func makeLayoutChanges(layout int64) []simple.EventMessage { + return []simple.EventMessage{{ + Msg: &pb.EventMessage{Value: &pb.EventMessageValueOfObjectDetailsAmend{ObjectDetailsAmend: &pb.EventObjectDetailsAmend{ + Details: []*pb.EventObjectDetailsAmendKeyValue{{ + Key: bundle.RelationKeyRecommendedLayout.String(), + Value: domain.Int64(layout).ToProto(), + }, + }}}}, + }} +} diff --git a/core/block/editor/smartblock/smartblock_test.go b/core/block/editor/smartblock/smartblock_test.go index 52db38a0c..a7893aa46 100644 --- a/core/block/editor/smartblock/smartblock_test.go +++ b/core/block/editor/smartblock/smartblock_test.go @@ -187,6 +187,7 @@ type fixture struct { eventSender *mock_event.MockSender source *sourceStub spaceIdResolver *mock_idresolver.MockResolver + space *MockSpace *smartBlock } @@ -199,6 +200,8 @@ func newFixture(id string, t *testing.T) *fixture { spaceIdResolver := mock_idresolver.NewMockResolver(t) + space := NewMockSpace(t) + indexer := NewMockIndexer(t) restrictionService := mock_restriction.NewMockService(t) @@ -206,7 +209,7 @@ func newFixture(id string, t *testing.T) *fixture { sender := mock_event.NewMockSender(t) - sb := New(nil, "", nil, restrictionService, spaceIndex, objectStore, indexer, sender, spaceIdResolver).(*smartBlock) + sb := New(space, "", nil, restrictionService, spaceIndex, objectStore, indexer, sender, spaceIdResolver).(*smartBlock) source := &sourceStub{ id: id, spaceId: "space1", @@ -223,6 +226,7 @@ func newFixture(id string, t *testing.T) *fixture { eventSender: sender, spaceIdResolver: spaceIdResolver, objectStore: objectStore, + space: space, } }