github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/localcachelayer/scheme_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 TestSchemeStore(t *testing.T) { 18 StoreTest(t, storetest.TestSchemeStore) 19 } 20 21 func TestSchemeStoreCache(t *testing.T) { 22 fakeScheme := model.Scheme{Id: "123", Name: "scheme-name"} 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 scheme, err := cachedStore.Scheme().Get("123") 31 require.NoError(t, err) 32 assert.Equal(t, scheme, &fakeScheme) 33 mockStore.Scheme().(*mocks.SchemeStore).AssertNumberOfCalls(t, "Get", 1) 34 require.NoError(t, err) 35 assert.Equal(t, scheme, &fakeScheme) 36 cachedStore.Scheme().Get("123") 37 mockStore.Scheme().(*mocks.SchemeStore).AssertNumberOfCalls(t, "Get", 1) 38 }) 39 40 t.Run("first call not cached, save, and then not cached again", func(t *testing.T) { 41 mockStore := getMockStore() 42 mockCacheProvider := getMockCacheProvider() 43 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 44 require.NoError(t, err) 45 46 cachedStore.Scheme().Get("123") 47 mockStore.Scheme().(*mocks.SchemeStore).AssertNumberOfCalls(t, "Get", 1) 48 cachedStore.Scheme().Save(&fakeScheme) 49 cachedStore.Scheme().Get("123") 50 mockStore.Scheme().(*mocks.SchemeStore).AssertNumberOfCalls(t, "Get", 2) 51 }) 52 53 t.Run("first call not cached, delete, and then not cached again", func(t *testing.T) { 54 mockStore := getMockStore() 55 mockCacheProvider := getMockCacheProvider() 56 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 57 require.NoError(t, err) 58 59 cachedStore.Scheme().Get("123") 60 mockStore.Scheme().(*mocks.SchemeStore).AssertNumberOfCalls(t, "Get", 1) 61 cachedStore.Scheme().Delete("123") 62 cachedStore.Scheme().Get("123") 63 mockStore.Scheme().(*mocks.SchemeStore).AssertNumberOfCalls(t, "Get", 2) 64 }) 65 66 t.Run("first call not cached, permanent delete all, and then not cached again", func(t *testing.T) { 67 mockStore := getMockStore() 68 mockCacheProvider := getMockCacheProvider() 69 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 70 require.NoError(t, err) 71 72 cachedStore.Scheme().Get("123") 73 mockStore.Scheme().(*mocks.SchemeStore).AssertNumberOfCalls(t, "Get", 1) 74 cachedStore.Scheme().PermanentDeleteAll() 75 cachedStore.Scheme().Get("123") 76 mockStore.Scheme().(*mocks.SchemeStore).AssertNumberOfCalls(t, "Get", 2) 77 }) 78 }