github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/store/storetest/plugin_store.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  	"github.com/mattermost/mattermost-server/store"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestPluginStore(t *testing.T, ss store.Store) {
    15  	t.Run("PluginSaveGet", func(t *testing.T) { testPluginSaveGet(t, ss) })
    16  	t.Run("PluginDelete", func(t *testing.T) { testPluginDelete(t, ss) })
    17  }
    18  
    19  func testPluginSaveGet(t *testing.T, ss store.Store) {
    20  	kv := &model.PluginKeyValue{
    21  		PluginId: model.NewId(),
    22  		Key:      model.NewId(),
    23  		Value:    []byte(model.NewId()),
    24  	}
    25  
    26  	if result := <-ss.Plugin().SaveOrUpdate(kv); result.Err != nil {
    27  		t.Fatal(result.Err)
    28  	}
    29  
    30  	defer func() {
    31  		<-ss.Plugin().Delete(kv.PluginId, kv.Key)
    32  	}()
    33  
    34  	if result := <-ss.Plugin().Get(kv.PluginId, kv.Key); result.Err != nil {
    35  		t.Fatal(result.Err)
    36  	} else {
    37  		received := result.Data.(*model.PluginKeyValue)
    38  		assert.Equal(t, kv.PluginId, received.PluginId)
    39  		assert.Equal(t, kv.Key, received.Key)
    40  		assert.Equal(t, kv.Value, received.Value)
    41  	}
    42  
    43  	// Try inserting when already exists
    44  	kv.Value = []byte(model.NewId())
    45  	if result := <-ss.Plugin().SaveOrUpdate(kv); result.Err != nil {
    46  		t.Fatal(result.Err)
    47  	}
    48  
    49  	if result := <-ss.Plugin().Get(kv.PluginId, kv.Key); result.Err != nil {
    50  		t.Fatal(result.Err)
    51  	} else {
    52  		received := result.Data.(*model.PluginKeyValue)
    53  		assert.Equal(t, kv.PluginId, received.PluginId)
    54  		assert.Equal(t, kv.Key, received.Key)
    55  		assert.Equal(t, kv.Value, received.Value)
    56  	}
    57  }
    58  
    59  func testPluginDelete(t *testing.T, ss store.Store) {
    60  	kv := store.Must(ss.Plugin().SaveOrUpdate(&model.PluginKeyValue{
    61  		PluginId: model.NewId(),
    62  		Key:      model.NewId(),
    63  		Value:    []byte(model.NewId()),
    64  	})).(*model.PluginKeyValue)
    65  
    66  	if result := <-ss.Plugin().Delete(kv.PluginId, kv.Key); result.Err != nil {
    67  		t.Fatal(result.Err)
    68  	}
    69  }