mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Initial refactoring work for plugins kvstore * Replace implementations for keystore and angularstore * Cleanup * add interface check * lint * fix storeKeyGetter not being called in namespacedstore set * Fix tests * Comments * Add tests * Fix invalid cap in ListKeys when store is empty * Update docstrings * Add setLastUpdatedOnDelete * Renamed DefaultStoreKeyGetterFunc, add TestDefaultStoreKeyGetter * Sort imports * PR review: removed last_updated key * PR review: Removed setLastUpdatedOnDelete * Re-added relevant tests * PR review: Removed SingleKeyStore * PR review: Removed custom marshaling support * Renamed marshaler.go to marshal.go * PR review: removed unused interfaces * PR review: Moved marshal into namespacedstore.go * PR review: removed storekeygetter * Removed unused file cachekvstore.go * Renamed NamespacedStore to CacheKvStore * removed todo
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package angularpatternsstore
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/kvstore"
|
|
)
|
|
|
|
func TestAngularPatternsStore(t *testing.T) {
|
|
mockPatterns := []map[string]any{
|
|
{"name": "PanelCtrl", "type": "contains", "pattern": "PanelCtrl"},
|
|
{"name": "ConfigCtrl", "type": "contains", "pattern": "ConfigCtrl"},
|
|
}
|
|
|
|
t.Run("get set", func(t *testing.T) {
|
|
svc := ProvideService(kvstore.NewFakeKVStore())
|
|
|
|
t.Run("get empty", func(t *testing.T) {
|
|
_, ok, err := svc.Get(context.Background())
|
|
require.NoError(t, err)
|
|
require.False(t, ok)
|
|
})
|
|
|
|
t.Run("set and get", func(t *testing.T) {
|
|
err := svc.Set(context.Background(), mockPatterns)
|
|
require.NoError(t, err)
|
|
|
|
expV, err := json.Marshal(mockPatterns)
|
|
require.NoError(t, err)
|
|
|
|
dbV, ok, err := svc.Get(context.Background())
|
|
require.NoError(t, err)
|
|
require.True(t, ok)
|
|
require.Equal(t, string(expV), dbV)
|
|
})
|
|
})
|
|
|
|
t.Run("latest update", func(t *testing.T) {
|
|
underlyingKv := kvstore.NewFakeKVStore()
|
|
svc := ProvideService(underlyingKv)
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
lastUpdated, err := svc.GetLastUpdated(context.Background())
|
|
require.NoError(t, err)
|
|
require.Zero(t, lastUpdated)
|
|
})
|
|
|
|
t.Run("not empty", func(t *testing.T) {
|
|
err := svc.Set(context.Background(), mockPatterns)
|
|
require.NoError(t, err)
|
|
|
|
lastUpdated, err := svc.GetLastUpdated(context.Background())
|
|
require.NoError(t, err)
|
|
require.WithinDuration(t, time.Now(), lastUpdated, time.Second*10)
|
|
})
|
|
|
|
t.Run("invalid timestamp stored", func(t *testing.T) {
|
|
err := underlyingKv.Set(context.Background(), 0, kvNamespace, "last_updated", "abcd")
|
|
require.NoError(t, err)
|
|
|
|
lastUpdated, err := svc.GetLastUpdated(context.Background())
|
|
require.NoError(t, err)
|
|
require.Zero(t, lastUpdated)
|
|
})
|
|
})
|
|
}
|