Files
mostlymatter/store/searchtest/testlib.go
Miguel de la Cruz 4fe25b1cdd [MM-21551] Add search tests structure to test the search engines (#14031)
* WIP

* Adding bleve to go modules

* WIP

* Adding missing files from searchengine implementation

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* User and channel indexing and searches implemented

* Make bleve tests run with in-memory indexes

* Implement post index and deletion tests

* Initial commits for the search layer

* Removing unnecesary indexing

* WIP

* WIP

* More fixes for tests

* Adding the search layer

* Finishing the migration of searchers to the layer

* Removing unnecesary code

* Allowing multiple engines active at the same time

* WIP

* Add simple post search

* Print information when using bleve

* Adding some debugging to understand better how the searches are working

* Making more dynamic config of search engines

* Add post search basics

* Adding the Purge API endpoint

* Fixing bleve config updates

* Adding missed file

* Regenerating search engine mocks

* Adding missed v5 to modules imports

* fixing i18n

* Fixing some test around search engine

* Removing all bleve traces

* Cleaning up the vendors directory and go.mod/go.sum files

* Regenerating timer layer

* Adding properly the license

* Fixing govet shadow error

* Fixing some tests

* Fixing TestSearchPostsFromUser

* Fixing another test

* Fixing more tests

* Fixing more tests

* Removing SearchEngine redundant text from searchengine module code

* Fixing some reindexing problems in members updates

* Fixing tests

* Addressing PR comments

* Reverting go.mod and go.sum

* Addressing PR comments

* Fixing tests compilation

* Fixing govet

* Adding search engine stop method

* Being more explicit on where we use includeDeleted

* Adding GetSqlSupplier test helper method

* Mocking elasticsearch start function

* Fixing tests

* Search tests

* Fix tests

* Fix mod

* Fixing searchEngine for test helpers with store mocks

* Remove loglines

* Fix i18n strings

* Migrate search posts tests

* Fix linter

* Do not run search tests if -short flag is enabled

* Migrate back store tests that didn't belong to the searchlayer

* Fix scopelint issues

Co-authored-by: Jesús Espino <jespinog@gmail.com>
2020-03-30 19:17:40 +02:00

181 строка
4.7 KiB
Go

// 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/mattermost/mattermost-server/v5/utils"
"github.com/stretchr/testify/assert"
)
const (
ENGINE_ALL = "all"
ENGINE_MYSQL = "mysql"
ENGINE_POSTGRES = "postgres"
ENGINE_ELASTICSEARCH = "elasticsearch"
)
type SearchTestEngine struct {
Driver string
BeforeTest func(*testing.T, store.Store)
AfterTest func(*testing.T, store.Store)
}
type searchTest struct {
Name string
Fn func(*testing.T, store.Store)
Tags []string
}
func filterTestsByTag(tests []searchTest, tags ...string) []searchTest {
filteredTests := []searchTest{}
for _, test := range tests {
if utils.StringInSlice(ENGINE_ALL, test.Tags) {
filteredTests = append(filteredTests, test)
continue
}
for _, tag := range tags {
if utils.StringInSlice(tag, test.Tags) {
filteredTests = append(filteredTests, test)
break
}
}
}
return filteredTests
}
func runTestSearch(t *testing.T, s store.Store, testEngine *SearchTestEngine, tests []searchTest) {
filteredTests := filterTestsByTag(tests, testEngine.Driver)
for _, test := range filteredTests {
test := test
if testing.Short() {
t.Skip("Skipping advanced search test")
continue
}
if testEngine.BeforeTest != nil {
testEngine.BeforeTest(t, s)
}
t.Run(test.Name, func(t *testing.T) { test.Fn(t, s) })
if testEngine.AfterTest != nil {
testEngine.AfterTest(t, s)
}
}
}
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
}