summaryrefslogtreecommitdiffstats
path: root/store/storetest/plugin_store.go
blob: 3d7d0ec0506dfb15f7b7a672476744e10642209b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package storetest

import (
	"testing"

	"github.com/mattermost/mattermost-server/model"
	"github.com/mattermost/mattermost-server/store"
	"github.com/stretchr/testify/assert"
)

func TestPluginStore(t *testing.T, ss store.Store) {
	t.Run("PluginSaveGet", func(t *testing.T) { testPluginSaveGet(t, ss) })
	t.Run("PluginDelete", func(t *testing.T) { testPluginDelete(t, ss) })
}

func testPluginSaveGet(t *testing.T, ss store.Store) {
	kv := &model.PluginKeyValue{
		PluginId: model.NewId(),
		Key:      model.NewId(),
		Value:    []byte(model.NewId()),
	}

	if result := <-ss.Plugin().SaveOrUpdate(kv); result.Err != nil {
		t.Fatal(result.Err)
	}

	defer func() {
		<-ss.Plugin().Delete(kv.PluginId, kv.Key)
	}()

	if result := <-ss.Plugin().Get(kv.PluginId, kv.Key); result.Err != nil {
		t.Fatal(result.Err)
	} else {
		received := result.Data.(*model.PluginKeyValue)
		assert.Equal(t, kv.PluginId, received.PluginId)
		assert.Equal(t, kv.Key, received.Key)
		assert.Equal(t, kv.Value, received.Value)
	}

	// Try inserting when already exists
	kv.Value = []byte(model.NewId())
	if result := <-ss.Plugin().SaveOrUpdate(kv); result.Err != nil {
		t.Fatal(result.Err)
	}

	if result := <-ss.Plugin().Get(kv.PluginId, kv.Key); result.Err != nil {
		t.Fatal(result.Err)
	} else {
		received := result.Data.(*model.PluginKeyValue)
		assert.Equal(t, kv.PluginId, received.PluginId)
		assert.Equal(t, kv.Key, received.Key)
		assert.Equal(t, kv.Value, received.Value)
	}
}

func testPluginDelete(t *testing.T, ss store.Store) {
	kv := store.Must(ss.Plugin().SaveOrUpdate(&model.PluginKeyValue{
		PluginId: model.NewId(),
		Key:      model.NewId(),
		Value:    []byte(model.NewId()),
	})).(*model.PluginKeyValue)

	if result := <-ss.Plugin().Delete(kv.PluginId, kv.Key); result.Err != nil {
		t.Fatal(result.Err)
	}
}