github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/localcachelayer/webhook_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 TestWebhookStore(t *testing.T) {
    18  	StoreTest(t, storetest.TestWebhookStore)
    19  }
    20  
    21  func TestWebhookStoreCache(t *testing.T) {
    22  	fakeWebhook := model.IncomingWebhook{Id: "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  		incomingWebhook, err := cachedStore.Webhook().GetIncoming("123", true)
    31  		require.NoError(t, err)
    32  		assert.Equal(t, incomingWebhook, &fakeWebhook)
    33  		mockStore.Webhook().(*mocks.WebhookStore).AssertNumberOfCalls(t, "GetIncoming", 1)
    34  
    35  		assert.Equal(t, incomingWebhook, &fakeWebhook)
    36  		cachedStore.Webhook().GetIncoming("123", true)
    37  		mockStore.Webhook().(*mocks.WebhookStore).AssertNumberOfCalls(t, "GetIncoming", 1)
    38  	})
    39  
    40  	t.Run("first call not cached, second force not cached", 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.Webhook().GetIncoming("123", true)
    47  		mockStore.Webhook().(*mocks.WebhookStore).AssertNumberOfCalls(t, "GetIncoming", 1)
    48  		cachedStore.Webhook().GetIncoming("123", false)
    49  		mockStore.Webhook().(*mocks.WebhookStore).AssertNumberOfCalls(t, "GetIncoming", 2)
    50  	})
    51  
    52  	t.Run("first call not cached, invalidate, and then not cached again", func(t *testing.T) {
    53  		mockStore := getMockStore()
    54  		mockCacheProvider := getMockCacheProvider()
    55  		cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
    56  		require.NoError(t, err)
    57  
    58  		cachedStore.Webhook().GetIncoming("123", true)
    59  		mockStore.Webhook().(*mocks.WebhookStore).AssertNumberOfCalls(t, "GetIncoming", 1)
    60  		cachedStore.Webhook().InvalidateWebhookCache("123")
    61  		cachedStore.Webhook().GetIncoming("123", true)
    62  		mockStore.Webhook().(*mocks.WebhookStore).AssertNumberOfCalls(t, "GetIncoming", 2)
    63  	})
    64  }