Implementation of expectation tests (#14191)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
f79b7567b1
Коммит
9853056b7a
@@ -8,204 +8,160 @@ import (
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/store"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var searchChannelStoreTests = []searchTest{
|
||||
{"Database Channel AutocompleteInTeamForSearch tests", testSearchDatabaseChannelAutocompleteInTeamForSearch, []string{ENGINE_MYSQL, ENGINE_POSTGRES}},
|
||||
{"Elasticsearch search channels", testSearchESSearchChannels, []string{ENGINE_ELASTICSEARCH}},
|
||||
{
|
||||
Name: "Should be able to autocomplete a channel by name",
|
||||
Fn: testAutocompleteChannelByName,
|
||||
Tags: []string{ENGINE_ALL},
|
||||
},
|
||||
{
|
||||
Name: "Should be able to autocomplete a channel by display name",
|
||||
Fn: testAutocompleteChannelByDisplayName,
|
||||
Tags: []string{ENGINE_ALL},
|
||||
},
|
||||
{
|
||||
Name: "Should be able to autocomplete a channel by a part of its name when has parts splitted by - character",
|
||||
Fn: testAutocompleteChannelByNameSplittedWithDashChar,
|
||||
Tags: []string{ENGINE_ALL},
|
||||
},
|
||||
{
|
||||
Name: "Should be able to autocomplete a channel by a part of its name when has parts splitted by _ character",
|
||||
Fn: testAutocompleteChannelByNameSplittedWithUnderscoreChar,
|
||||
Tags: []string{ENGINE_MYSQL, ENGINE_ELASTICSEARCH},
|
||||
},
|
||||
{
|
||||
Name: "Should be able to autocomplete a channel by a part of its display name when has parts splitted by whitespace character",
|
||||
Fn: testAutocompleteChannelByDisplayNameSplittedByWhitespaces,
|
||||
Tags: []string{ENGINE_MYSQL, ENGINE_ELASTICSEARCH},
|
||||
},
|
||||
{
|
||||
Name: "Should be able to autocomplete retrieving all channels if the term is empty",
|
||||
Fn: testAutocompleteAllChannelsIfTermIsEmpty,
|
||||
Tags: []string{ENGINE_ALL},
|
||||
},
|
||||
{
|
||||
Name: "Should be able to autocomplete channels in a case insensitive manner",
|
||||
Fn: testSearchChannelsInCaseInsensitiveManner,
|
||||
Tags: []string{ENGINE_ALL},
|
||||
},
|
||||
{
|
||||
Name: "Should autocomplete only returning public channels",
|
||||
Fn: testSearchOnlyPublicChannels,
|
||||
Tags: []string{ENGINE_ALL},
|
||||
},
|
||||
{
|
||||
Name: "Should support to autocomplete having a hyphen as the last character",
|
||||
Fn: testSearchShouldSupportHavingHyphenAsLastCharacter,
|
||||
Tags: []string{ENGINE_ALL},
|
||||
},
|
||||
{
|
||||
Name: "Should support to autocomplete with archived channels",
|
||||
Fn: testSearchShouldSupportAutocompleteWithArchivedChannels,
|
||||
Tags: []string{ENGINE_ALL},
|
||||
},
|
||||
}
|
||||
|
||||
func TestSearchChannelStore(t *testing.T, s store.Store, testEngine *SearchTestEngine) {
|
||||
runTestSearch(t, s, testEngine, searchChannelStoreTests)
|
||||
th := &SearchTestHelper{
|
||||
Store: s,
|
||||
}
|
||||
err := th.SetupBasicFixtures()
|
||||
require.Nil(t, err)
|
||||
defer th.CleanFixtures()
|
||||
runTestSearch(t, testEngine, searchChannelStoreTests, th)
|
||||
}
|
||||
|
||||
func testSearchDatabaseChannelAutocompleteInTeamForSearch(t *testing.T, s store.Store) {
|
||||
u1 := &model.User{}
|
||||
u1.Email = makeEmail()
|
||||
u1.Username = "user1" + model.NewId()
|
||||
u1.Nickname = model.NewId()
|
||||
_, err := s.User().Save(u1)
|
||||
func testAutocompleteChannelByName(t *testing.T, th *SearchTestHelper) {
|
||||
alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "Channel Alternate", "", model.CHANNEL_OPEN, false)
|
||||
require.Nil(t, err)
|
||||
|
||||
u2 := &model.User{}
|
||||
u2.Email = makeEmail()
|
||||
u2.Username = "user2" + model.NewId()
|
||||
u2.Nickname = model.NewId()
|
||||
_, err = s.User().Save(u2)
|
||||
require.Nil(t, err)
|
||||
|
||||
u3 := &model.User{}
|
||||
u3.Email = makeEmail()
|
||||
u3.Username = "user3" + model.NewId()
|
||||
u3.Nickname = model.NewId()
|
||||
_, err = s.User().Save(u3)
|
||||
require.Nil(t, err)
|
||||
|
||||
u4 := &model.User{}
|
||||
u4.Email = makeEmail()
|
||||
u4.Username = "user4" + model.NewId()
|
||||
u4.Nickname = model.NewId()
|
||||
_, err = s.User().Save(u4)
|
||||
require.Nil(t, err)
|
||||
|
||||
o1 := model.Channel{}
|
||||
o1.TeamId = model.NewId()
|
||||
o1.DisplayName = "ChannelA"
|
||||
o1.Name = "zz" + model.NewId() + "b"
|
||||
o1.Type = model.CHANNEL_OPEN
|
||||
_, err = s.Channel().Save(&o1, -1)
|
||||
require.Nil(t, err)
|
||||
|
||||
m1 := model.ChannelMember{}
|
||||
m1.ChannelId = o1.Id
|
||||
m1.UserId = u1.Id
|
||||
m1.NotifyProps = model.GetDefaultChannelNotifyProps()
|
||||
_, err = s.Channel().SaveMember(&m1)
|
||||
require.Nil(t, err)
|
||||
|
||||
o2 := model.Channel{}
|
||||
o2.TeamId = model.NewId()
|
||||
o2.DisplayName = "Channel2"
|
||||
o2.Name = "zz" + model.NewId() + "b"
|
||||
o2.Type = model.CHANNEL_OPEN
|
||||
_, err = s.Channel().Save(&o2, -1)
|
||||
require.Nil(t, err)
|
||||
|
||||
m2 := model.ChannelMember{}
|
||||
m2.ChannelId = o2.Id
|
||||
m2.UserId = m1.UserId
|
||||
m2.NotifyProps = model.GetDefaultChannelNotifyProps()
|
||||
_, err = s.Channel().SaveMember(&m2)
|
||||
require.Nil(t, err)
|
||||
|
||||
o3 := model.Channel{}
|
||||
o3.TeamId = o1.TeamId
|
||||
o3.DisplayName = "ChannelA"
|
||||
o3.Name = "zz" + model.NewId() + "b"
|
||||
o3.Type = model.CHANNEL_OPEN
|
||||
_, err = s.Channel().Save(&o3, -1)
|
||||
require.Nil(t, err)
|
||||
|
||||
m3 := model.ChannelMember{}
|
||||
m3.ChannelId = o3.Id
|
||||
m3.UserId = m1.UserId
|
||||
m3.NotifyProps = model.GetDefaultChannelNotifyProps()
|
||||
_, err = s.Channel().SaveMember(&m3)
|
||||
require.Nil(t, err)
|
||||
|
||||
err = s.Channel().SetDeleteAt(o3.Id, 100, 100)
|
||||
require.Nil(t, err, "channel should have been deleted")
|
||||
|
||||
o4 := model.Channel{}
|
||||
o4.TeamId = o1.TeamId
|
||||
o4.DisplayName = "ChannelA"
|
||||
o4.Name = "zz" + model.NewId() + "b"
|
||||
o4.Type = model.CHANNEL_PRIVATE
|
||||
_, err = s.Channel().Save(&o4, -1)
|
||||
require.Nil(t, err)
|
||||
|
||||
m4 := model.ChannelMember{}
|
||||
m4.ChannelId = o4.Id
|
||||
m4.UserId = m1.UserId
|
||||
m4.NotifyProps = model.GetDefaultChannelNotifyProps()
|
||||
_, err = s.Channel().SaveMember(&m4)
|
||||
require.Nil(t, err)
|
||||
|
||||
o5 := model.Channel{}
|
||||
o5.TeamId = o1.TeamId
|
||||
o5.DisplayName = "ChannelC"
|
||||
o5.Name = "zz" + model.NewId() + "b"
|
||||
o5.Type = model.CHANNEL_PRIVATE
|
||||
_, err = s.Channel().Save(&o5, -1)
|
||||
require.Nil(t, err)
|
||||
|
||||
_, err = s.Channel().CreateDirectChannel(u1, u2)
|
||||
require.Nil(t, err)
|
||||
_, err = s.Channel().CreateDirectChannel(u2, u3)
|
||||
require.Nil(t, err)
|
||||
|
||||
tt := []struct {
|
||||
name string
|
||||
term string
|
||||
includeDeleted bool
|
||||
expectedMatches int
|
||||
}{
|
||||
{"Empty search (list all)", "", false, 4},
|
||||
{"Narrow search", "ChannelA", false, 2},
|
||||
{"Wide search", "Cha", false, 3},
|
||||
{"Direct messages", "user", false, 1},
|
||||
{"Wide search with archived channels", "Cha", true, 4},
|
||||
{"Narrow with archived channels", "ChannelA", true, 3},
|
||||
{"Direct messages with archived channels", "user", true, 1},
|
||||
{"Search without results", "blarg", true, 0},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
channels, err := s.Channel().AutocompleteInTeamForSearch(o1.TeamId, m1.UserId, "ChannelA", false)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, *channels, 2)
|
||||
})
|
||||
}
|
||||
defer th.deleteChannel(alternate)
|
||||
res, apperr := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channel-a", false)
|
||||
require.Nil(t, apperr)
|
||||
th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id}, res)
|
||||
}
|
||||
|
||||
func testSearchESSearchChannels(t *testing.T, s store.Store) {
|
||||
team, err := s.Team().Save(&model.Team{Name: "team", DisplayName: "team", Type: model.TEAM_OPEN})
|
||||
func testAutocompleteChannelByDisplayName(t *testing.T, th *SearchTestHelper) {
|
||||
alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "ChannelAlternate", "", model.CHANNEL_OPEN, false)
|
||||
require.Nil(t, err)
|
||||
channel1, err := s.Channel().Save(&model.Channel{TeamId: team.Id, Name: "channel", DisplayName: "Test One", Type: model.CHANNEL_OPEN}, -1)
|
||||
require.Nil(t, err)
|
||||
channel2, err := s.Channel().Save(&model.Channel{TeamId: team.Id, Name: "channel-second", DisplayName: "Test Two", Type: model.CHANNEL_OPEN}, -1)
|
||||
require.Nil(t, err)
|
||||
channel3, err := s.Channel().Save(&model.Channel{TeamId: team.Id, Name: "channel_third", DisplayName: "Test Three", Type: model.CHANNEL_OPEN}, -1)
|
||||
require.Nil(t, err)
|
||||
|
||||
testCases := []struct {
|
||||
Name string
|
||||
Term string
|
||||
Expected []string
|
||||
}{
|
||||
{
|
||||
Name: "autocomplete search for all channels by name",
|
||||
Term: "cha",
|
||||
Expected: []string{channel1.Id, channel2.Id, channel3.Id},
|
||||
},
|
||||
{
|
||||
Name: "autocomplete search for one channel by display name",
|
||||
Term: "one",
|
||||
Expected: []string{channel1.Id},
|
||||
},
|
||||
{
|
||||
Name: "autocomplete search for one channel split by -",
|
||||
Term: "seco",
|
||||
Expected: []string{channel2.Id},
|
||||
},
|
||||
{
|
||||
Name: "autocomplete search for one channel split by _",
|
||||
Term: "thir",
|
||||
Expected: []string{channel3.Id},
|
||||
},
|
||||
{
|
||||
Name: "autocomplete search that won't match anything",
|
||||
Term: "nothing",
|
||||
Expected: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
res, err := s.Channel().AutocompleteInTeam(team.Id, tc.Term, false)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, tc.Expected, len(*res))
|
||||
|
||||
resIds := make([]string, len(*res))
|
||||
for i, channel := range *res {
|
||||
resIds[i] = channel.Id
|
||||
}
|
||||
require.ElementsMatch(t, tc.Expected, resIds)
|
||||
})
|
||||
}
|
||||
defer th.deleteChannel(alternate)
|
||||
res, apperr := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "ChannelA", false)
|
||||
require.Nil(t, apperr)
|
||||
th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id}, res)
|
||||
}
|
||||
|
||||
func testAutocompleteChannelByNameSplittedWithDashChar(t *testing.T, th *SearchTestHelper) {
|
||||
alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "ChannelAlternate", "", model.CHANNEL_OPEN, false)
|
||||
require.Nil(t, err)
|
||||
defer th.deleteChannel(alternate)
|
||||
res, apperr := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channel-a", false)
|
||||
require.Nil(t, apperr)
|
||||
th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id}, res)
|
||||
}
|
||||
|
||||
func testAutocompleteChannelByNameSplittedWithUnderscoreChar(t *testing.T, th *SearchTestHelper) {
|
||||
alternate, err := th.createChannel(th.Team.Id, "channel_alternate", "ChannelAlternate", "", model.CHANNEL_OPEN, false)
|
||||
require.Nil(t, err)
|
||||
defer th.deleteChannel(alternate)
|
||||
res, apperr := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channel_a", false)
|
||||
require.Nil(t, apperr)
|
||||
th.checkChannelIdsMatch(t, []string{alternate.Id}, res)
|
||||
}
|
||||
|
||||
func testAutocompleteChannelByDisplayNameSplittedByWhitespaces(t *testing.T, th *SearchTestHelper) {
|
||||
alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "Channel Alternate", "", model.CHANNEL_OPEN, false)
|
||||
require.Nil(t, err)
|
||||
defer th.deleteChannel(alternate)
|
||||
res, apperr := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "Channel A", false)
|
||||
require.Nil(t, apperr)
|
||||
th.checkChannelIdsMatch(t, []string{alternate.Id}, res)
|
||||
}
|
||||
func testAutocompleteAllChannelsIfTermIsEmpty(t *testing.T, th *SearchTestHelper) {
|
||||
alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "Channel Alternate", "", model.CHANNEL_OPEN, false)
|
||||
require.Nil(t, err)
|
||||
other, err := th.createChannel(th.Team.Id, "other-channel", "Other Channel", "", model.CHANNEL_OPEN, false)
|
||||
require.Nil(t, err)
|
||||
defer th.deleteChannel(alternate)
|
||||
defer th.deleteChannel(other)
|
||||
res, apperr := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "", false)
|
||||
require.Nil(t, apperr)
|
||||
th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id, other.Id}, res)
|
||||
}
|
||||
|
||||
func testSearchChannelsInCaseInsensitiveManner(t *testing.T, th *SearchTestHelper) {
|
||||
alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "ChannelAlternate", "", model.CHANNEL_OPEN, false)
|
||||
require.Nil(t, err)
|
||||
defer th.deleteChannel(alternate)
|
||||
res, apperr := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channela", false)
|
||||
require.Nil(t, apperr)
|
||||
th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id}, res)
|
||||
res, apperr = th.Store.Channel().AutocompleteInTeam(th.Team.Id, "ChAnNeL-a", false)
|
||||
require.Nil(t, apperr)
|
||||
th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id}, res)
|
||||
}
|
||||
|
||||
func testSearchOnlyPublicChannels(t *testing.T, th *SearchTestHelper) {
|
||||
alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "ChannelAlternate", "", model.CHANNEL_PRIVATE, false)
|
||||
require.Nil(t, err)
|
||||
defer th.deleteChannel(alternate)
|
||||
res, apperr := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channel-a", false)
|
||||
require.Nil(t, apperr)
|
||||
th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id}, res)
|
||||
}
|
||||
|
||||
func testSearchShouldSupportHavingHyphenAsLastCharacter(t *testing.T, th *SearchTestHelper) {
|
||||
alternate, err := th.createChannel(th.Team.Id, "channel-alternate", "ChannelAlternate", "", model.CHANNEL_OPEN, false)
|
||||
require.Nil(t, err)
|
||||
defer th.deleteChannel(alternate)
|
||||
res, apperr := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channel-", false)
|
||||
require.Nil(t, apperr)
|
||||
th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, alternate.Id}, res)
|
||||
}
|
||||
|
||||
func testSearchShouldSupportAutocompleteWithArchivedChannels(t *testing.T, th *SearchTestHelper) {
|
||||
res, apperr := th.Store.Channel().AutocompleteInTeam(th.Team.Id, "channel-", true)
|
||||
require.Nil(t, apperr)
|
||||
th.checkChannelIdsMatch(t, []string{th.ChannelBasic.Id, th.ChannelDeleted.Id}, res)
|
||||
}
|
||||
|
||||
462
store/searchtest/helper.go
Обычный файл
462
store/searchtest/helper.go
Обычный файл
@@ -0,0 +1,462 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package searchtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/store"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type SearchTestHelper struct {
|
||||
Store store.Store
|
||||
Team *model.Team
|
||||
AnotherTeam *model.Team
|
||||
User *model.User
|
||||
User2 *model.User
|
||||
UserAnotherTeam *model.User
|
||||
ChannelBasic *model.Channel
|
||||
ChannelPrivate *model.Channel
|
||||
ChannelAnotherTeam *model.Channel
|
||||
ChannelDeleted *model.Channel
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) SetupBasicFixtures() error {
|
||||
// Create teams
|
||||
team, err := th.createTeam("searchtest-team", "Searchtest team", model.TEAM_OPEN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
anotherTeam, err := th.createTeam("another-searchtest-team", "Another Searchtest team", model.TEAM_OPEN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create users
|
||||
user, err := th.createUser("basicusername1", "basicnickname1", "basicfirstname1", "basiclastname1")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
user2, err := th.createUser("basicusername2", "basicnickname2", "basicfirstname2", "basiclastname2")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
useranother, err := th.createUser("basicusername3", "basicnickname3", "basicfirstname3", "basiclastname3")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create channels
|
||||
channelBasic, err := th.createChannel(team.Id, "channel-a", "ChannelA", "", model.CHANNEL_OPEN, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
channelPrivate, err := th.createChannel(team.Id, "channel-private", "ChannelPrivate", "", model.CHANNEL_PRIVATE, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
channelDeleted, err := th.createChannel(team.Id, "channel-deleted", "ChannelA (deleted)", "", model.CHANNEL_OPEN, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
channelAnotherTeam, err := th.createChannel(anotherTeam.Id, "channel-a", "ChannelA", "", model.CHANNEL_OPEN, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = th.addUserToTeams(user, []string{team.Id, anotherTeam.Id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = th.addUserToTeams(user2, []string{team.Id, anotherTeam.Id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = th.addUserToTeams(useranother, []string{anotherTeam.Id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = th.addUserToChannels(user, []string{channelBasic.Id, channelPrivate.Id, channelDeleted.Id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = th.addUserToChannels(user2, []string{channelPrivate.Id, channelDeleted.Id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = th.addUserToChannels(useranother, []string{channelAnotherTeam.Id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
th.Team = team
|
||||
th.AnotherTeam = anotherTeam
|
||||
th.User = user
|
||||
th.User2 = user2
|
||||
th.UserAnotherTeam = useranother
|
||||
th.ChannelBasic = channelBasic
|
||||
th.ChannelPrivate = channelPrivate
|
||||
th.ChannelAnotherTeam = channelAnotherTeam
|
||||
th.ChannelDeleted = channelDeleted
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) CleanFixtures() error {
|
||||
err := th.deleteChannels([]*model.Channel{
|
||||
th.ChannelBasic, th.ChannelPrivate, th.ChannelAnotherTeam, th.ChannelDeleted,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = th.deleteTeam(th.Team)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = th.deleteTeam(th.AnotherTeam)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = th.deleteUser(th.User)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = th.deleteUser(th.User2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = th.deleteUser(th.UserAnotherTeam)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) createTeam(name, displayName, teamType string) (*model.Team, error) {
|
||||
team, appError := th.Store.Team().Save(&model.Team{
|
||||
Name: name,
|
||||
DisplayName: displayName,
|
||||
Type: teamType,
|
||||
})
|
||||
if appError != nil {
|
||||
return nil, errors.New(appError.Error())
|
||||
}
|
||||
|
||||
return team, nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) deleteTeam(team *model.Team) error {
|
||||
appError := th.Store.Team().RemoveAllMembersByTeam(team.Id)
|
||||
if appError != nil {
|
||||
return errors.New(appError.Error())
|
||||
}
|
||||
appError = th.Store.Team().PermanentDelete(team.Id)
|
||||
if appError != nil {
|
||||
return errors.New(appError.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) makeEmail() string {
|
||||
return "success_" + model.NewId() + "@simulator.amazon.com"
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) createUser(username, nickname, firstName, lastName string) (*model.User, error) {
|
||||
user, appError := th.Store.User().Save(&model.User{
|
||||
Username: username,
|
||||
Password: username,
|
||||
Nickname: nickname,
|
||||
FirstName: firstName,
|
||||
LastName: lastName,
|
||||
Email: th.makeEmail(),
|
||||
})
|
||||
if appError != nil {
|
||||
return nil, errors.New(appError.Error())
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) deleteUser(user *model.User) error {
|
||||
appError := th.Store.User().PermanentDelete(user.Id)
|
||||
if appError != nil {
|
||||
return errors.New(appError.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) createBot(username, displayName, ownerID string) (*model.Bot, error) {
|
||||
botModel := &model.Bot{
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
OwnerId: ownerID,
|
||||
}
|
||||
|
||||
user, apperr := th.Store.User().Save(model.UserFromBot(botModel))
|
||||
if apperr != nil {
|
||||
return nil, errors.New(apperr.Error())
|
||||
}
|
||||
|
||||
botModel.UserId = user.Id
|
||||
bot, apperr := th.Store.Bot().Save(botModel)
|
||||
if apperr != nil {
|
||||
th.Store.User().PermanentDelete(bot.UserId)
|
||||
return nil, errors.New(apperr.Error())
|
||||
}
|
||||
|
||||
return bot, nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) deleteBot(botID string) error {
|
||||
err := th.Store.Bot().PermanentDelete(botID)
|
||||
if err != nil {
|
||||
return errors.New(err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) createChannel(teamID, name, displayName, purpose, channelType string, deleted bool) (*model.Channel, error) {
|
||||
channel, appError := th.Store.Channel().Save(&model.Channel{
|
||||
TeamId: teamID,
|
||||
DisplayName: displayName,
|
||||
Name: name,
|
||||
Type: channelType,
|
||||
Purpose: purpose,
|
||||
}, 999)
|
||||
if appError != nil {
|
||||
return nil, errors.New(appError.Error())
|
||||
}
|
||||
|
||||
if deleted {
|
||||
appError := th.Store.Channel().Delete(channel.Id, model.GetMillis())
|
||||
if appError != nil {
|
||||
return nil, errors.New(appError.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) createDirectChannel(teamID, name, displayName string, users []*model.User) (*model.Channel, error) {
|
||||
channel := &model.Channel{
|
||||
TeamId: teamID,
|
||||
Name: name,
|
||||
DisplayName: displayName,
|
||||
Type: model.CHANNEL_DIRECT,
|
||||
}
|
||||
|
||||
m1 := &model.ChannelMember{}
|
||||
m1.ChannelId = channel.Id
|
||||
m1.UserId = users[0].Id
|
||||
m1.NotifyProps = model.GetDefaultChannelNotifyProps()
|
||||
|
||||
m2 := &model.ChannelMember{}
|
||||
m2.ChannelId = channel.Id
|
||||
m2.UserId = users[0].Id
|
||||
m2.NotifyProps = model.GetDefaultChannelNotifyProps()
|
||||
|
||||
channel, err := th.Store.Channel().SaveDirectChannel(channel, m1, m2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) createGroupChannel(teamID, displayName string, users []*model.User) (*model.Channel, error) {
|
||||
userIDS := make([]string, len(users))
|
||||
for _, user := range users {
|
||||
userIDS = append(userIDS, user.Id)
|
||||
}
|
||||
|
||||
group := &model.Channel{
|
||||
TeamId: teamID,
|
||||
Name: model.GetGroupNameFromUserIds(userIDS),
|
||||
DisplayName: displayName,
|
||||
Type: model.CHANNEL_GROUP,
|
||||
}
|
||||
|
||||
channel, apperr := th.Store.Channel().Save(group, 10000)
|
||||
if apperr != nil {
|
||||
return nil, errors.New(apperr.Error())
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
_, err := th.addUserToChannels(user, []string{channel.Id})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return channel, nil
|
||||
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) deleteChannel(channel *model.Channel) error {
|
||||
appError := th.Store.Channel().PermanentDeleteMembersByChannel(channel.Id)
|
||||
if appError != nil {
|
||||
return errors.New(appError.Error())
|
||||
}
|
||||
|
||||
appError = th.Store.Channel().PermanentDelete(channel.Id)
|
||||
if appError != nil {
|
||||
return errors.New(appError.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) deleteChannels(channels []*model.Channel) error {
|
||||
for _, channel := range channels {
|
||||
err := th.deleteChannel(channel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) createPostModel(userID, channelID, message, hashtags, postType string, createAt int64, pinned bool) *model.Post {
|
||||
return &model.Post{
|
||||
Message: message,
|
||||
ChannelId: channelID,
|
||||
PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()),
|
||||
UserId: userID,
|
||||
Hashtags: hashtags,
|
||||
IsPinned: pinned,
|
||||
CreateAt: createAt,
|
||||
Type: postType,
|
||||
}
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) createPost(userID, channelID, message, hashtags, postType string, createAt int64, pinned bool) (*model.Post, error) {
|
||||
var creationTime int64 = 1000000
|
||||
if createAt > 0 {
|
||||
creationTime = createAt
|
||||
}
|
||||
postModel := th.createPostModel(userID, channelID, message, hashtags, postType, creationTime, pinned)
|
||||
post, appError := th.Store.Post().Save(postModel)
|
||||
if appError != nil {
|
||||
return nil, errors.New(appError.Error())
|
||||
}
|
||||
|
||||
return post, nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) createReply(userID, message, hashtags string, parent *model.Post, createAt int64, pinned bool) (*model.Post, error) {
|
||||
replyModel := th.createPostModel(userID, parent.ChannelId, message, hashtags, parent.Type, createAt, pinned)
|
||||
replyModel.ParentId = parent.Id
|
||||
replyModel.RootId = parent.Id
|
||||
reply, appError := th.Store.Post().Save(replyModel)
|
||||
if appError != nil {
|
||||
return nil, errors.New(appError.Error())
|
||||
}
|
||||
return reply, nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) deleteUserPosts(userID string) error {
|
||||
err := th.Store.Post().PermanentDeleteByUser(userID)
|
||||
if err != nil {
|
||||
return errors.New(err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) addUserToTeams(user *model.User, teamIDS []string) error {
|
||||
for _, teamID := range teamIDS {
|
||||
_, err := th.Store.Team().SaveMember(&model.TeamMember{TeamId: teamID, UserId: user.Id}, -1)
|
||||
if err != nil {
|
||||
return errors.New(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) addUserToChannels(user *model.User, channelIDS []string) ([]*model.ChannelMember, error) {
|
||||
|
||||
channelMembers := make([]*model.ChannelMember, len(channelIDS))
|
||||
for _, channelID := range channelIDS {
|
||||
cm, err := th.Store.Channel().SaveMember(&model.ChannelMember{
|
||||
ChannelId: channelID,
|
||||
UserId: user.Id,
|
||||
NotifyProps: model.GetDefaultChannelNotifyProps(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New(err.Error())
|
||||
}
|
||||
channelMembers = append(channelMembers, cm)
|
||||
}
|
||||
|
||||
return channelMembers, nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) assertUsersMatchInAnyOrder(t *testing.T, expected, actual []*model.User) {
|
||||
expectedUsernames := make([]string, 0, len(expected))
|
||||
for _, user := range expected {
|
||||
user.Sanitize(map[string]bool{})
|
||||
expectedUsernames = append(expectedUsernames, user.Username)
|
||||
}
|
||||
|
||||
actualUsernames := make([]string, 0, len(actual))
|
||||
for _, user := range actual {
|
||||
user.Sanitize(map[string]bool{})
|
||||
actualUsernames = append(actualUsernames, user.Username)
|
||||
}
|
||||
|
||||
if assert.ElementsMatch(t, expectedUsernames, actualUsernames) {
|
||||
assert.ElementsMatch(t, expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) checkPostInSearchResults(t *testing.T, postID string, searchResults map[string]*model.Post) {
|
||||
t.Helper()
|
||||
postIDS := make([]string, len(searchResults))
|
||||
for ID := range searchResults {
|
||||
postIDS = append(postIDS, ID)
|
||||
}
|
||||
assert.Contains(t, postIDS, postID, "Did not find expected post in search results.")
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) checkChannelIdsMatch(t *testing.T, expected []string, results *model.ChannelList) {
|
||||
t.Helper()
|
||||
channelIds := make([]string, len(*results))
|
||||
for i, channel := range *results {
|
||||
channelIds[i] = channel.Id
|
||||
}
|
||||
require.ElementsMatch(t, expected, channelIds)
|
||||
}
|
||||
|
||||
type ByChannelDisplayName model.ChannelList
|
||||
|
||||
func (s ByChannelDisplayName) Len() int { return len(s) }
|
||||
func (s ByChannelDisplayName) Swap(i, j int) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
func (s ByChannelDisplayName) Less(i, j int) bool {
|
||||
if s[i].DisplayName != s[j].DisplayName {
|
||||
return s[i].DisplayName < s[j].DisplayName
|
||||
}
|
||||
|
||||
return s[i].Id < s[j].Id
|
||||
}
|
||||
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@@ -4,14 +4,10 @@
|
||||
package searchtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/store"
|
||||
"github.com/mattermost/mattermost-server/v5/utils"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -28,9 +24,11 @@ type SearchTestEngine struct {
|
||||
}
|
||||
|
||||
type searchTest struct {
|
||||
Name string
|
||||
Fn func(*testing.T, store.Store)
|
||||
Tags []string
|
||||
Name string
|
||||
Fn func(*testing.T, *SearchTestHelper)
|
||||
Tags []string
|
||||
Skip bool
|
||||
SkipMessage string
|
||||
}
|
||||
|
||||
func filterTestsByTag(tests []searchTest, tags ...string) []searchTest {
|
||||
@@ -51,130 +49,29 @@ func filterTestsByTag(tests []searchTest, tags ...string) []searchTest {
|
||||
return filteredTests
|
||||
}
|
||||
|
||||
func runTestSearch(t *testing.T, s store.Store, testEngine *SearchTestEngine, tests []searchTest) {
|
||||
func runTestSearch(t *testing.T, testEngine *SearchTestEngine, tests []searchTest, th *SearchTestHelper) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping advanced search test")
|
||||
return
|
||||
}
|
||||
|
||||
filteredTests := filterTestsByTag(tests, testEngine.Driver)
|
||||
|
||||
for _, test := range filteredTests {
|
||||
test := test
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping advanced search test")
|
||||
|
||||
if test.Skip {
|
||||
t.Log("SKIPPED: " + test.Name + ". Reason: " + test.SkipMessage)
|
||||
continue
|
||||
}
|
||||
|
||||
if testEngine.BeforeTest != nil {
|
||||
testEngine.BeforeTest(t, s)
|
||||
testEngine.BeforeTest(t, th.Store)
|
||||
}
|
||||
t.Run(test.Name, func(t *testing.T) { test.Fn(t, s) })
|
||||
testName := test.Name
|
||||
testFn := test.Fn
|
||||
t.Run(testName, func(t *testing.T) { testFn(t, th) })
|
||||
if testEngine.AfterTest != nil {
|
||||
testEngine.AfterTest(t, s)
|
||||
testEngine.AfterTest(t, th.Store)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func makeEmail() string {
|
||||
return "success_" + model.NewId() + "@simulator.amazonses.com"
|
||||
}
|
||||
|
||||
func assertUsersMatchInAnyOrder(t *testing.T, expected, actual []*model.User) {
|
||||
expectedUsernames := make([]string, 0, len(expected))
|
||||
for _, user := range expected {
|
||||
expectedUsernames = append(expectedUsernames, user.Username)
|
||||
}
|
||||
|
||||
actualUsernames := make([]string, 0, len(actual))
|
||||
for _, user := range actual {
|
||||
actualUsernames = append(actualUsernames, user.Username)
|
||||
}
|
||||
|
||||
if assert.ElementsMatch(t, expectedUsernames, actualUsernames) {
|
||||
assert.ElementsMatch(t, expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func createUser(username, nickname, firstName, lastName string) *model.User {
|
||||
user := &model.User{
|
||||
Username: username,
|
||||
Password: username,
|
||||
Nickname: nickname,
|
||||
FirstName: firstName,
|
||||
LastName: lastName,
|
||||
Email: makeEmail(),
|
||||
}
|
||||
|
||||
return user
|
||||
}
|
||||
|
||||
func createPost(userId string, channelId string, message string) *model.Post {
|
||||
post := &model.Post{
|
||||
Message: message,
|
||||
ChannelId: channelId,
|
||||
PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()),
|
||||
UserId: userId,
|
||||
CreateAt: 1000000,
|
||||
}
|
||||
|
||||
return post
|
||||
}
|
||||
|
||||
func addUserToTeamsAndChannels(s store.Store, user *model.User, teamIds []string, channelIds []string) error {
|
||||
for _, teamId := range teamIds {
|
||||
_, err := s.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: user.Id}, -1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, channelId := range channelIds {
|
||||
_, err := s.Channel().SaveMember(&model.ChannelMember{ChannelId: channelId, UserId: user.Id, NotifyProps: model.GetDefaultChannelNotifyProps()})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkPostInSearchResults(t *testing.T, postId string, searchResults []string) {
|
||||
t.Helper()
|
||||
assert.Contains(t, searchResults, postId, "Did not find expected post in search results.")
|
||||
}
|
||||
|
||||
func checkPostNotInSearchResults(t *testing.T, postId string, searchResults []string) {
|
||||
t.Helper()
|
||||
assert.NotContains(t, searchResults, postId, "Found post in search results that should not be there.")
|
||||
}
|
||||
|
||||
func checkMatchesEqual(t *testing.T, expected model.PostSearchMatches, actual map[string][]string) {
|
||||
a := assert.New(t)
|
||||
|
||||
a.Len(actual, len(expected), "Received matches for a different number of posts")
|
||||
|
||||
for postId, expectedMatches := range expected {
|
||||
a.ElementsMatch(expectedMatches, actual[postId], fmt.Sprintf("%v: expected %v, got %v", postId, expectedMatches, actual[postId]))
|
||||
}
|
||||
}
|
||||
|
||||
func createPostWithHashtags(userId string, channelId string, message string, hashtags string) *model.Post {
|
||||
post := &model.Post{
|
||||
Message: message,
|
||||
ChannelId: channelId,
|
||||
PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()),
|
||||
UserId: userId,
|
||||
CreateAt: 1000000,
|
||||
Hashtags: hashtags,
|
||||
}
|
||||
|
||||
return post
|
||||
}
|
||||
|
||||
func createPostAtTime(userId string, channelId string, message string, createAt int64) *model.Post {
|
||||
post := &model.Post{
|
||||
Message: message,
|
||||
ChannelId: channelId,
|
||||
PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()),
|
||||
UserId: userId,
|
||||
CreateAt: createAt,
|
||||
}
|
||||
|
||||
return post
|
||||
}
|
||||
|
||||
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@@ -52,13 +52,6 @@ func StoreTestWithSearchTestEngine(t *testing.T, f func(*testing.T, store.Store,
|
||||
st := st
|
||||
searchTestEngine := &searchtest.SearchTestEngine{
|
||||
Driver: *st.SqlSettings.DriverName,
|
||||
AfterTest: func(t *testing.T, s store.Store) {
|
||||
t.Helper()
|
||||
|
||||
st.SqlSupplier.GetMaster().Exec("TRUNCATE Teams")
|
||||
st.SqlSupplier.GetMaster().Exec("TRUNCATE Channels")
|
||||
st.SqlSupplier.GetMaster().Exec("TRUNCATE Users")
|
||||
},
|
||||
}
|
||||
|
||||
t.Run(st.Name, func(t *testing.T) { f(t, st.Store, searchTestEngine) })
|
||||
|
||||
Ссылка в новой задаче
Block a user