github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/localcachelayer/reaction_layer_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package localcachelayer 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/mattermost/mattermost-server/v5/model" 13 "github.com/mattermost/mattermost-server/v5/store/storetest" 14 "github.com/mattermost/mattermost-server/v5/store/storetest/mocks" 15 ) 16 17 func TestReactionStore(t *testing.T) { 18 StoreTestWithSqlStore(t, storetest.TestReactionStore) 19 } 20 21 func TestReactionStoreCache(t *testing.T) { 22 fakeReaction := model.Reaction{PostId: "123"} 23 24 t.Run("first call not cached, second cached and returning same data", func(t *testing.T) { 25 mockStore := getMockStore() 26 mockCacheProvider := getMockCacheProvider() 27 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 28 require.NoError(t, err) 29 30 reaction, err := cachedStore.Reaction().GetForPost("123", true) 31 require.NoError(t, err) 32 assert.Equal(t, reaction, []*model.Reaction{&fakeReaction}) 33 mockStore.Reaction().(*mocks.ReactionStore).AssertNumberOfCalls(t, "GetForPost", 1) 34 assert.Equal(t, reaction, []*model.Reaction{&fakeReaction}) 35 cachedStore.Reaction().GetForPost("123", true) 36 mockStore.Reaction().(*mocks.ReactionStore).AssertNumberOfCalls(t, "GetForPost", 1) 37 }) 38 39 t.Run("first call not cached, second force not cached", func(t *testing.T) { 40 mockStore := getMockStore() 41 mockCacheProvider := getMockCacheProvider() 42 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 43 require.NoError(t, err) 44 45 cachedStore.Reaction().GetForPost("123", true) 46 mockStore.Reaction().(*mocks.ReactionStore).AssertNumberOfCalls(t, "GetForPost", 1) 47 cachedStore.Reaction().GetForPost("123", false) 48 mockStore.Reaction().(*mocks.ReactionStore).AssertNumberOfCalls(t, "GetForPost", 2) 49 }) 50 51 t.Run("first call not cached, save, and then not cached again", func(t *testing.T) { 52 mockStore := getMockStore() 53 mockCacheProvider := getMockCacheProvider() 54 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 55 require.NoError(t, err) 56 57 cachedStore.Reaction().GetForPost("123", true) 58 mockStore.Reaction().(*mocks.ReactionStore).AssertNumberOfCalls(t, "GetForPost", 1) 59 cachedStore.Reaction().Save(&fakeReaction) 60 cachedStore.Reaction().GetForPost("123", true) 61 mockStore.Reaction().(*mocks.ReactionStore).AssertNumberOfCalls(t, "GetForPost", 2) 62 }) 63 64 t.Run("first call not cached, delete, and then not cached again", func(t *testing.T) { 65 mockStore := getMockStore() 66 mockCacheProvider := getMockCacheProvider() 67 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 68 require.NoError(t, err) 69 70 cachedStore.Reaction().GetForPost("123", true) 71 mockStore.Reaction().(*mocks.ReactionStore).AssertNumberOfCalls(t, "GetForPost", 1) 72 cachedStore.Reaction().Delete(&fakeReaction) 73 cachedStore.Reaction().GetForPost("123", true) 74 mockStore.Reaction().(*mocks.ReactionStore).AssertNumberOfCalls(t, "GetForPost", 2) 75 }) 76 }