github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/localcachelayer/file_info_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 TestFileInfoStore(t *testing.T) { 18 StoreTest(t, storetest.TestFileInfoStore) 19 } 20 21 func TestFileInfoStoreCache(t *testing.T) { 22 fakeFileInfo := model.FileInfo{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 fileInfos, err := cachedStore.FileInfo().GetForPost("123", true, true, true) 31 require.NoError(t, err) 32 assert.Equal(t, fileInfos, []*model.FileInfo{&fakeFileInfo}) 33 mockStore.FileInfo().(*mocks.FileInfoStore).AssertNumberOfCalls(t, "GetForPost", 1) 34 assert.Equal(t, fileInfos, []*model.FileInfo{&fakeFileInfo}) 35 cachedStore.FileInfo().GetForPost("123", true, true, true) 36 mockStore.FileInfo().(*mocks.FileInfoStore).AssertNumberOfCalls(t, "GetForPost", 1) 37 }) 38 39 t.Run("first call not cached, second force no 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.FileInfo().GetForPost("123", true, true, true) 46 mockStore.FileInfo().(*mocks.FileInfoStore).AssertNumberOfCalls(t, "GetForPost", 1) 47 cachedStore.FileInfo().GetForPost("123", true, true, false) 48 mockStore.FileInfo().(*mocks.FileInfoStore).AssertNumberOfCalls(t, "GetForPost", 2) 49 }) 50 51 t.Run("first call not cached, invalidate, 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.FileInfo().GetForPost("123", true, true, true) 58 mockStore.FileInfo().(*mocks.FileInfoStore).AssertNumberOfCalls(t, "GetForPost", 1) 59 cachedStore.FileInfo().InvalidateFileInfosForPostCache("123", true) 60 cachedStore.FileInfo().GetForPost("123", true, true, true) 61 mockStore.FileInfo().(*mocks.FileInfoStore).AssertNumberOfCalls(t, "GetForPost", 2) 62 }) 63 }