mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* 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 36a199bbe0.
* 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
118 lines
2.3 KiB
Go
118 lines
2.3 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package sqlstore
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/mattermost/mattermost-server/v5/model"
|
|
"github.com/mattermost/mattermost-server/v5/store"
|
|
"github.com/mattermost/mattermost-server/v5/store/storetest"
|
|
)
|
|
|
|
type storeType struct {
|
|
Name string
|
|
SqlSettings *model.SqlSettings
|
|
SqlSupplier *SqlSupplier
|
|
Store store.Store
|
|
}
|
|
|
|
var storeTypes []*storeType
|
|
|
|
func StoreTest(t *testing.T, f func(*testing.T, store.Store)) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
tearDownStores()
|
|
panic(err)
|
|
}
|
|
}()
|
|
for _, st := range storeTypes {
|
|
st := st
|
|
t.Run(st.Name, func(t *testing.T) {
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
f(t, st.Store)
|
|
})
|
|
}
|
|
}
|
|
|
|
func StoreTestWithSqlSupplier(t *testing.T, f func(*testing.T, store.Store, storetest.SqlSupplier)) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
tearDownStores()
|
|
panic(err)
|
|
}
|
|
}()
|
|
for _, st := range storeTypes {
|
|
st := st
|
|
t.Run(st.Name, func(t *testing.T) {
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
f(t, st.Store, st.SqlSupplier)
|
|
})
|
|
}
|
|
}
|
|
|
|
func initStores() {
|
|
if testing.Short() {
|
|
return
|
|
}
|
|
storeTypes = append(storeTypes, &storeType{
|
|
Name: "MySQL",
|
|
SqlSettings: storetest.MakeSqlSettings(model.DATABASE_DRIVER_MYSQL),
|
|
})
|
|
storeTypes = append(storeTypes, &storeType{
|
|
Name: "PostgreSQL",
|
|
SqlSettings: storetest.MakeSqlSettings(model.DATABASE_DRIVER_POSTGRES),
|
|
})
|
|
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
tearDownStores()
|
|
panic(err)
|
|
}
|
|
}()
|
|
var wg sync.WaitGroup
|
|
for _, st := range storeTypes {
|
|
st := st
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
st.SqlSupplier = NewSqlSupplier(*st.SqlSettings, nil)
|
|
st.Store = st.SqlSupplier
|
|
st.Store.DropAllTables()
|
|
st.Store.MarkSystemRanUnitTests()
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
var tearDownStoresOnce sync.Once
|
|
|
|
func tearDownStores() {
|
|
if testing.Short() {
|
|
return
|
|
}
|
|
tearDownStoresOnce.Do(func() {
|
|
var wg sync.WaitGroup
|
|
wg.Add(len(storeTypes))
|
|
for _, st := range storeTypes {
|
|
st := st
|
|
go func() {
|
|
if st.Store != nil {
|
|
st.Store.Close()
|
|
}
|
|
if st.SqlSettings != nil {
|
|
storetest.CleanupSqlSettings(st.SqlSettings)
|
|
}
|
|
wg.Done()
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
})
|
|
}
|