github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/store/searchtest/channel_layer.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package searchtest
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/masterhung0112/hk_server/v5/model"
    10  	"github.com/masterhung0112/hk_server/v5/store"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  var searchChannelStoreTests = []searchTest{
    15  	{
    16  		Name: "Should be able to autocomplete a channel by name",
    17  		Fn:   testAutocompleteChannelByName,
    18  		Tags: []string{EngineAll},
    19  	},
    20  	{
    21  		Name: "Should be able to autocomplete a channel by display name",
    22  		Fn:   testAutocompleteChannelByDisplayName,
    23  		Tags: []string{EngineAll},
    24  	},
    25  	{
    26  		Name: "Should be able to autocomplete a channel by a part of its name when has parts splitted by - character",
    27  		Fn:   testAutocompleteChannelByNameSplittedWithDashChar,
    28  		Tags: []string{EngineAll},
    29  	},
    30  	{
    31  		Name: "Should be able to autocomplete a channel by a part of its name when has parts splitted by _ character",
    32  		Fn:   testAutocompleteChannelByNameSplittedWithUnderscoreChar,
    33  		Tags: []string{EngineMySql, EngineElasticSearch, EngineBleve},
    34  	},
    35  	{
    36  		Name: "Should be able to autocomplete a channel by a part of its display name when has parts splitted by whitespace character",
    37  		Fn:   testAutocompleteChannelByDisplayNameSplittedByWhitespaces,
    38  		Tags: []string{EngineMySql, EngineElasticSearch, EngineBleve},
    39  	},
    40  	{
    41  		Name: "Should be able to autocomplete retrieving all channels if the term is empty",
    42  		Fn:   testAutocompleteAllChannelsIfTermIsEmpty,
    43  		Tags: []string{EngineAll},
    44  	},
    45  	{
    46  		Name: "Should be able to autocomplete channels in a case insensitive manner",
    47  		Fn:   testSearchChannelsInCaseInsensitiveManner,
    48  		Tags: []string{EngineAll},
    49  	},
    50  	{
    51  		Name: "Should autocomplete only returning public channels",
    52  		Fn:   testSearchOnlyPublicChannels,
    53  		Tags: []string{EngineAll},
    54  	},
    55  	{
    56  		Name: "Should support to autocomplete having a hyphen as the last character",
    57  		Fn:   testSearchShouldSupportHavingHyphenAsLastCharacter,
    58  		Tags: []string{EngineAll},
    59  	},
    60  	{
    61  		Name: "Should support to autocomplete with archived channels",
    62  		Fn:   testSearchShouldSupportAutocompleteWithArchivedChannels,
    63  		Tags: []string{EngineAll},
    64  	},
    65  }
    66  
    67  func TestSearchChannelStore(t *testing.T, s store.Store, testEngine *SearchTestEngine) {
    68  	th := &SearchTestHelper{
    69  		Store: s,
    70  	}
    71  	err := th.SetupBasicFixtures()
    72  	require.NoError(t, err)
    73  	defer th.CleanFixtures()
    74  	runTestSearch(t, testEngine, searchChannelStoreTests, th)
    75  }
    76  
    77  func testAutocompleteChannelByName(t *testing.T, th *SearchTestHelper) {
    78  	alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "Channel Alternate", "Channel Alternate", model.CHANNEL_OPEN, false)
    79  	require.NoError(t, err)
    80  	defer th.deleteChannel(alternate)
    81  	res, err := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channel-a", false)
    82  	require.NoError(t, err)
    83  	th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id}, res)
    84  }
    85  
    86  func testAutocompleteChannelByDisplayName(t *testing.T, th *SearchTestHelper) {
    87  	alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "ChannelAlternate", "", model.CHANNEL_OPEN, false)
    88  	require.NoError(t, err)
    89  	defer th.deleteChannel(alternate)
    90  	res, err := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "ChannelA", false)
    91  	require.NoError(t, err)
    92  	th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id}, res)
    93  }
    94  
    95  func testAutocompleteChannelByNameSplittedWithDashChar(t *testing.T, th *SearchTestHelper) {
    96  	alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "ChannelAlternate", "", model.CHANNEL_OPEN, false)
    97  	require.NoError(t, err)
    98  	defer th.deleteChannel(alternate)
    99  	res, err := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channel-a", false)
   100  	require.NoError(t, err)
   101  	th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id}, res)
   102  }
   103  
   104  func testAutocompleteChannelByNameSplittedWithUnderscoreChar(t *testing.T, th *SearchTestHelper) {
   105  	alternate, err := th.createChannel(th.Team.Id, "channel_alternate", "ChannelAlternate", "", model.CHANNEL_OPEN, false)
   106  	require.NoError(t, err)
   107  	defer th.deleteChannel(alternate)
   108  	res, err := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channel_a", false)
   109  	require.NoError(t, err)
   110  	th.checkChannelIdsMatch(t, []string{alternate.Id}, res)
   111  }
   112  
   113  func testAutocompleteChannelByDisplayNameSplittedByWhitespaces(t *testing.T, th *SearchTestHelper) {
   114  	alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "Channel Alternate", "", model.CHANNEL_OPEN, false)
   115  	require.NoError(t, err)
   116  
   117  	defer th.deleteChannel(alternate)
   118  	res, err := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "Channel A", false)
   119  	require.NoError(t, err)
   120  	th.checkChannelIdsMatch(t, []string{alternate.Id}, res)
   121  }
   122  func testAutocompleteAllChannelsIfTermIsEmpty(t *testing.T, th *SearchTestHelper) {
   123  	alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "Channel Alternate", "", model.CHANNEL_OPEN, false)
   124  	require.NoError(t, err)
   125  	other, err := th.createChannel(th.Team.Id, "other-channel", "Other Channel", "", model.CHANNEL_OPEN, false)
   126  	require.NoError(t, err)
   127  	defer th.deleteChannel(alternate)
   128  	defer th.deleteChannel(other)
   129  	res, err := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "", false)
   130  	require.NoError(t, err)
   131  	th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id, other.Id}, res)
   132  }
   133  
   134  func testSearchChannelsInCaseInsensitiveManner(t *testing.T, th *SearchTestHelper) {
   135  	alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "ChannelAlternate", "", model.CHANNEL_OPEN, false)
   136  	require.NoError(t, err)
   137  	defer th.deleteChannel(alternate)
   138  	res, err := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channela", false)
   139  	require.NoError(t, err)
   140  	th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id}, res)
   141  	res, err = th.Store.Channel().AutocompleteInTeam(th.Team.Id, "ChAnNeL-a", false)
   142  	require.NoError(t, err)
   143  	th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id}, res)
   144  }
   145  
   146  func testSearchOnlyPublicChannels(t *testing.T, th *SearchTestHelper) {
   147  	alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "ChannelAlternate", "", model.CHANNEL_PRIVATE, false)
   148  	require.NoError(t, err)
   149  	defer th.deleteChannel(alternate)
   150  	res, err := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channel-a", false)
   151  	require.NoError(t, err)
   152  	th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id}, res)
   153  }
   154  
   155  func testSearchShouldSupportHavingHyphenAsLastCharacter(t *testing.T, th *SearchTestHelper) {
   156  	alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "ChannelAlternate", "", model.CHANNEL_OPEN, false)
   157  	require.NoError(t, err)
   158  	defer th.deleteChannel(alternate)
   159  	res, err := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channel-", false)
   160  	require.NoError(t, err)
   161  	th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id}, res)
   162  }
   163  
   164  func testSearchShouldSupportAutocompleteWithArchivedChannels(t *testing.T, th *SearchTestHelper) {
   165  	res, err := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channel-", true)
   166  	require.NoError(t, err)
   167  	th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, th.ChannelDeleted.Id}, res)
   168  }