Adding changes to separate unit tests and integration tests (#13670)

* Introducing unit (not integration) tests for the app layer

* Initial support for unit tests at the API

* Adding unit tests support to the store layer

* Add unit tests support in commands

* Adding last tests needed for run unit tests properly

* Fixing govet

* Removing some duplication

* Fixing tests

* Fixing tests

* Not compiling test helpers with the main module for api

* Revert "Not compiling test helpers with the main module for api"

This reverts commit 36a199bbe0f7503f665f5d6c7b41c53aabc2e54f.

* Fixing tests

* Fixing unit tests

* More consistency between api4/apiteslib.go and app/helper_test.go

* Renaming things to make more obvious the new Setup functions purpose

* Reverting change in go.sum

* Start with empty mock for app layer

* Start with empty mock for api layer

* Start with empty mock for web layer

* Renaming SetupWithStoreMockConfig to SetupConfigWithStoreMock

* Fixing tests on web package

* Removing unnecesary function
Этот коммит содержится в:
Jesús Espino
2020-03-02 17:13:39 +01:00
коммит произвёл GitHub
родитель cd36c9f041
Коммит 7035e09fe9
27 изменённых файлов: 720 добавлений и 187 удалений

Просмотреть файл

@@ -19,6 +19,8 @@ import (
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store"
"github.com/mattermost/mattermost-server/v5/store/storetest/mocks"
"github.com/mattermost/mattermost-server/v5/testlib"
"github.com/mattermost/mattermost-server/v5/utils"
"github.com/mattermost/mattermost-server/v5/web"
"github.com/mattermost/mattermost-server/v5/wsapi"
@@ -51,19 +53,9 @@ type TestHelper struct {
tempWorkspace string
}
// testStore tracks the active test store.
// This is a bridge between the new testlib ownership of the test store and the existing usage
// of the api4 test helper by many packages. In the future, this test helper would ideally belong
// to the testlib altogether.
var testStore store.Store
func UseTestStore(store store.Store) {
testStore = store
}
func setupTestHelper(enterprise bool, updateConfig func(*model.Config)) *TestHelper {
testStore.DropAllTables()
var mainHelper *testlib.MainHelper
func setupTestHelper(dbStore store.Store, enterprise bool, updateConfig func(*model.Config)) *TestHelper {
tempWorkspace, err := ioutil.TempDir("", "apptest")
if err != nil {
panic(err)
@@ -84,7 +76,7 @@ func setupTestHelper(enterprise bool, updateConfig func(*model.Config)) *TestHel
var options []app.Option
options = append(options, app.ConfigStore(memoryStore))
options = append(options, app.StoreOverride(testStore))
options = append(options, app.StoreOverride(dbStore))
s, err := app.NewServer(options...)
if err != nil {
@@ -117,7 +109,6 @@ func setupTestHelper(enterprise bool, updateConfig func(*model.Config)) *TestHel
Init(th.Server, th.Server.AppOptions, th.App.Srv().Router)
web.New(th.Server, th.Server.AppOptions, th.App.Srv().Router)
wsapi.Init(th.App, th.App.Srv().WebSocketRouter)
th.App.Srv().Store.MarkSystemRanUnitTests()
th.App.DoAppMigrations()
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true })
@@ -148,15 +139,72 @@ func setupTestHelper(enterprise bool, updateConfig func(*model.Config)) *TestHel
}
func SetupEnterprise(tb testing.TB) *TestHelper {
return setupTestHelper(true, nil)
if testing.Short() {
tb.SkipNow()
}
if mainHelper == nil {
tb.SkipNow()
}
dbStore := mainHelper.GetStore()
dbStore.DropAllTables()
dbStore.MarkSystemRanUnitTests()
return setupTestHelper(dbStore, true, nil)
}
func Setup(tb testing.TB) *TestHelper {
return setupTestHelper(false, nil)
if testing.Short() {
tb.SkipNow()
}
if mainHelper == nil {
tb.SkipNow()
}
dbStore := mainHelper.GetStore()
dbStore.DropAllTables()
dbStore.MarkSystemRanUnitTests()
return setupTestHelper(dbStore, false, nil)
}
func SetupConfig(tb testing.TB, updateConfig func(cfg *model.Config)) *TestHelper {
return setupTestHelper(false, updateConfig)
if testing.Short() {
tb.SkipNow()
}
if mainHelper == nil {
tb.SkipNow()
}
dbStore := mainHelper.GetStore()
dbStore.DropAllTables()
dbStore.MarkSystemRanUnitTests()
return setupTestHelper(dbStore, false, updateConfig)
}
func SetupConfigWithStoreMock(tb testing.TB, updateConfig func(cfg *model.Config)) *TestHelper {
th := setupTestHelper(testlib.GetMockStoreForSetupFunctions(), false, updateConfig)
emptyMockStore := mocks.Store{}
emptyMockStore.On("Close").Return(nil)
th.App.Srv().Store = &emptyMockStore
return th
}
func SetupWithStoreMock(tb testing.TB) *TestHelper {
th := setupTestHelper(testlib.GetMockStoreForSetupFunctions(), false, nil)
emptyMockStore := mocks.Store{}
emptyMockStore.On("Close").Return(nil)
th.App.Srv().Store = &emptyMockStore
return th
}
func SetupEnterpriseWithStoreMock(tb testing.TB) *TestHelper {
th := setupTestHelper(testlib.GetMockStoreForSetupFunctions(), true, nil)
emptyMockStore := mocks.Store{}
emptyMockStore.On("Close").Return(nil)
th.App.Srv().Store = &emptyMockStore
return th
}
func (me *TestHelper) ShutdownApp() {

Просмотреть файл

@@ -119,7 +119,7 @@ func TestCORSRequestHandling(t *testing.T) {
},
} {
t.Run(name, func(t *testing.T) {
th := SetupConfig(t, func(cfg *model.Config) {
th := SetupConfigWithStoreMock(t, func(cfg *model.Config) {
*cfg.ServiceSettings.AllowCorsFrom = testcase.AllowCorsFrom
*cfg.ServiceSettings.CorsExposedHeaders = testcase.CorsExposedHeaders
*cfg.ServiceSettings.CorsAllowCredentials = testcase.CorsAllowCredentials

Просмотреть файл

@@ -9,8 +9,6 @@ import (
"github.com/mattermost/mattermost-server/v5/testlib"
)
var mainHelper *testlib.MainHelper
func TestMain(m *testing.M) {
var options = testlib.HelperOptions{
EnableStore: true,
@@ -20,6 +18,5 @@ func TestMain(m *testing.M) {
mainHelper = testlib.NewMainHelperWithOptions(&options)
defer mainHelper.Close()
UseTestStore(mainHelper.GetStore())
mainHelper.Main(m)
}