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

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/mattermost/mattermost-server/model"
    11  	"github.com/mattermost/mattermost-server/store"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestEmojiStore(t *testing.T, ss store.Store) {
    17  	t.Run("EmojiSaveDelete", func(t *testing.T) { testEmojiSaveDelete(t, ss) })
    18  	t.Run("EmojiGet", func(t *testing.T) { testEmojiGet(t, ss) })
    19  	t.Run("EmojiGetByName", func(t *testing.T) { testEmojiGetByName(t, ss) })
    20  	t.Run("EmojiGetList", func(t *testing.T) { testEmojiGetList(t, ss) })
    21  	t.Run("EmojiSearch", func(t *testing.T) { testEmojiSearch(t, ss) })
    22  }
    23  
    24  func testEmojiSaveDelete(t *testing.T, ss store.Store) {
    25  	emoji1 := &model.Emoji{
    26  		CreatorId: model.NewId(),
    27  		Name:      model.NewId(),
    28  	}
    29  
    30  	if result := <-ss.Emoji().Save(emoji1); result.Err != nil {
    31  		t.Fatal(result.Err)
    32  	}
    33  
    34  	if len(emoji1.Id) != 26 {
    35  		t.Fatal("should've set id for emoji")
    36  	}
    37  
    38  	emoji2 := model.Emoji{
    39  		CreatorId: model.NewId(),
    40  		Name:      emoji1.Name,
    41  	}
    42  	if result := <-ss.Emoji().Save(&emoji2); result.Err == nil {
    43  		t.Fatal("shouldn't be able to save emoji with duplicate name")
    44  	}
    45  
    46  	if result := <-ss.Emoji().Delete(emoji1.Id, time.Now().Unix()); result.Err != nil {
    47  		t.Fatal(result.Err)
    48  	}
    49  
    50  	if result := <-ss.Emoji().Save(&emoji2); result.Err != nil {
    51  		t.Fatal("should be able to save emoji with duplicate name now that original has been deleted", result.Err)
    52  	}
    53  
    54  	if result := <-ss.Emoji().Delete(emoji2.Id, time.Now().Unix()+1); result.Err != nil {
    55  		t.Fatal(result.Err)
    56  	}
    57  }
    58  
    59  func testEmojiGet(t *testing.T, ss store.Store) {
    60  	emojis := []model.Emoji{
    61  		{
    62  			CreatorId: model.NewId(),
    63  			Name:      model.NewId(),
    64  		},
    65  		{
    66  			CreatorId: model.NewId(),
    67  			Name:      model.NewId(),
    68  		},
    69  		{
    70  			CreatorId: model.NewId(),
    71  			Name:      model.NewId(),
    72  		},
    73  	}
    74  
    75  	for i, emoji := range emojis {
    76  		emojis[i] = *store.Must(ss.Emoji().Save(&emoji)).(*model.Emoji)
    77  	}
    78  	defer func() {
    79  		for _, emoji := range emojis {
    80  			store.Must(ss.Emoji().Delete(emoji.Id, time.Now().Unix()))
    81  		}
    82  	}()
    83  
    84  	for _, emoji := range emojis {
    85  		if result := <-ss.Emoji().Get(emoji.Id, false); result.Err != nil {
    86  			t.Fatalf("failed to get emoji with id %v: %v", emoji.Id, result.Err)
    87  		}
    88  	}
    89  
    90  	for _, emoji := range emojis {
    91  		if result := <-ss.Emoji().Get(emoji.Id, true); result.Err != nil {
    92  			t.Fatalf("failed to get emoji with id %v: %v", emoji.Id, result.Err)
    93  		}
    94  	}
    95  
    96  	for _, emoji := range emojis {
    97  		if result := <-ss.Emoji().Get(emoji.Id, true); result.Err != nil {
    98  			t.Fatalf("failed to get emoji with id %v: %v", emoji.Id, result.Err)
    99  		}
   100  	}
   101  }
   102  
   103  func testEmojiGetByName(t *testing.T, ss store.Store) {
   104  	emojis := []model.Emoji{
   105  		{
   106  			CreatorId: model.NewId(),
   107  			Name:      model.NewId(),
   108  		},
   109  		{
   110  			CreatorId: model.NewId(),
   111  			Name:      model.NewId(),
   112  		},
   113  		{
   114  			CreatorId: model.NewId(),
   115  			Name:      model.NewId(),
   116  		},
   117  	}
   118  
   119  	for i, emoji := range emojis {
   120  		emojis[i] = *store.Must(ss.Emoji().Save(&emoji)).(*model.Emoji)
   121  	}
   122  	defer func() {
   123  		for _, emoji := range emojis {
   124  			store.Must(ss.Emoji().Delete(emoji.Id, time.Now().Unix()))
   125  		}
   126  	}()
   127  
   128  	for _, emoji := range emojis {
   129  		if result := <-ss.Emoji().GetByName(emoji.Name); result.Err != nil {
   130  			t.Fatalf("failed to get emoji with name %v: %v", emoji.Name, result.Err)
   131  		}
   132  	}
   133  }
   134  
   135  func testEmojiGetList(t *testing.T, ss store.Store) {
   136  	emojis := []model.Emoji{
   137  		{
   138  			CreatorId: model.NewId(),
   139  			Name:      "00000000000000000000000000a" + model.NewId(),
   140  		},
   141  		{
   142  			CreatorId: model.NewId(),
   143  			Name:      "00000000000000000000000000b" + model.NewId(),
   144  		},
   145  		{
   146  			CreatorId: model.NewId(),
   147  			Name:      "00000000000000000000000000c" + model.NewId(),
   148  		},
   149  	}
   150  
   151  	for i, emoji := range emojis {
   152  		emojis[i] = *store.Must(ss.Emoji().Save(&emoji)).(*model.Emoji)
   153  	}
   154  	defer func() {
   155  		for _, emoji := range emojis {
   156  			store.Must(ss.Emoji().Delete(emoji.Id, time.Now().Unix()))
   157  		}
   158  	}()
   159  
   160  	if result := <-ss.Emoji().GetList(0, 100, ""); result.Err != nil {
   161  		t.Fatal(result.Err)
   162  	} else {
   163  		for _, emoji := range emojis {
   164  			found := false
   165  
   166  			for _, savedEmoji := range result.Data.([]*model.Emoji) {
   167  				if emoji.Id == savedEmoji.Id {
   168  					found = true
   169  					break
   170  				}
   171  			}
   172  
   173  			if !found {
   174  				t.Fatalf("failed to get emoji with id %v", emoji.Id)
   175  			}
   176  		}
   177  	}
   178  
   179  	result := <-ss.Emoji().GetList(0, 3, model.EMOJI_SORT_BY_NAME)
   180  	assert.Nil(t, result.Err)
   181  	remojis := result.Data.([]*model.Emoji)
   182  	assert.Equal(t, 3, len(remojis))
   183  	assert.Equal(t, emojis[0].Name, remojis[0].Name)
   184  	assert.Equal(t, emojis[1].Name, remojis[1].Name)
   185  	assert.Equal(t, emojis[2].Name, remojis[2].Name)
   186  
   187  	result = <-ss.Emoji().GetList(1, 2, model.EMOJI_SORT_BY_NAME)
   188  	assert.Nil(t, result.Err)
   189  	remojis = result.Data.([]*model.Emoji)
   190  	assert.Equal(t, 2, len(remojis))
   191  	assert.Equal(t, emojis[1].Name, remojis[0].Name)
   192  	assert.Equal(t, emojis[2].Name, remojis[1].Name)
   193  
   194  }
   195  
   196  func testEmojiSearch(t *testing.T, ss store.Store) {
   197  	emojis := []model.Emoji{
   198  		{
   199  			CreatorId: model.NewId(),
   200  			Name:      "blargh_" + model.NewId(),
   201  		},
   202  		{
   203  			CreatorId: model.NewId(),
   204  			Name:      model.NewId() + "_blargh",
   205  		},
   206  		{
   207  			CreatorId: model.NewId(),
   208  			Name:      model.NewId() + "_blargh_" + model.NewId(),
   209  		},
   210  		{
   211  			CreatorId: model.NewId(),
   212  			Name:      model.NewId(),
   213  		},
   214  	}
   215  
   216  	for i, emoji := range emojis {
   217  		emojis[i] = *store.Must(ss.Emoji().Save(&emoji)).(*model.Emoji)
   218  	}
   219  	defer func() {
   220  		for _, emoji := range emojis {
   221  			store.Must(ss.Emoji().Delete(emoji.Id, time.Now().Unix()))
   222  		}
   223  	}()
   224  
   225  	shouldFind := []bool{true, false, false, false}
   226  
   227  	if result := <-ss.Emoji().Search("blargh", true, 100); result.Err != nil {
   228  		t.Fatal(result.Err)
   229  	} else {
   230  		for i, emoji := range emojis {
   231  			found := false
   232  
   233  			for _, savedEmoji := range result.Data.([]*model.Emoji) {
   234  				if emoji.Id == savedEmoji.Id {
   235  					found = true
   236  					break
   237  				}
   238  			}
   239  
   240  			assert.Equal(t, shouldFind[i], found, emoji.Name)
   241  		}
   242  	}
   243  
   244  	shouldFind = []bool{true, true, true, false}
   245  	if result := <-ss.Emoji().Search("blargh", false, 100); result.Err != nil {
   246  		t.Fatal(result.Err)
   247  	} else {
   248  		for i, emoji := range emojis {
   249  			found := false
   250  
   251  			for _, savedEmoji := range result.Data.([]*model.Emoji) {
   252  				if emoji.Id == savedEmoji.Id {
   253  					found = true
   254  					break
   255  				}
   256  			}
   257  
   258  			assert.Equal(t, shouldFind[i], found, emoji.Name)
   259  		}
   260  	}
   261  }