-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
main.go
62 lines (51 loc) · 1.88 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package main_test
import (
"database/sql"
"testing"
"github.com/mattermost/mattermost-server/v5/app/plugin_api_tests"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
"github.com/mattermost/mattermost-server/v5/shared/driver"
"github.com/mattermost/mattermost-server/v5/store/sqlstore"
"github.com/mattermost/mattermost-server/v5/store/storetest"
)
type MyPlugin struct {
plugin.MattermostPlugin
config plugin_api_tests.BasicConfig
t *testing.T
}
func (p *MyPlugin) OnConfigurationChange() error {
if err := p.API.LoadPluginConfiguration(&p.config); err != nil {
return err
}
return nil
}
func (p *MyPlugin) MessageWillBePosted(_ *plugin.Context, _ *model.Post) (*model.Post, string) {
store := sqlstore.New(p.API.GetUnsanitizedConfig().SqlSettings, nil)
store.GetMaster().Db.Close()
for _, isMaster := range []bool{true, false} {
// We replace the master DB with master and replica both just to make
// gorp APIs work.
store.GetMaster().Db = sql.OpenDB(driver.NewConnector(p.Driver, isMaster))
// Testing with a handful of stores
storetest.TestPostStore(p.t, store, store)
storetest.TestUserStore(p.t, store, store)
storetest.TestTeamStore(p.t, store)
storetest.TestChannelStore(p.t, store, store)
storetest.TestBotStore(p.t, store, store)
store.GetMaster().Db.Close()
}
// Use the API to instantiate the driver
// And then run the full suite of tests.
return nil, "OK"
}
// TestDBAPI is a test function which actually runs a plugin. The objective
// is to run the storetest suite from inside a plugin.
//
// The test runner compiles the test code to a binary, and runs it as a normal
// binary. But under the hood, a test runs.
func TestDBAPI(t *testing.T) {
plugin.ClientMain(&MyPlugin{t: t})
}