Files
mattermost/app/plugin_api_tests/test_db_driver/main.go
Agniva De Sarker 1f3e2d0c4f Added a sqlXPassThrough layer (#18238)
* Added a sqlXPassThrough layer

This layer embeds the original sqlx object and wraps some
key methods to avoid some boilerplate.

- Add timeout to all methods from the timeout in settings.
- Added trace functionality.
- Added auto-rebind capabilities so that each store method
doesn't need to call rebind every time.
- Added auto-lowering of query string to support NamedExec
for both MySQL and Postgres

All methods are not wrapped as all are likely not to be called
from our code. As and when they are called, support will be added.

```release-note
NONE
```

* fix lint issues

```release-note
NONE
```

* address review comments

```release-note
NONE
```

* incorporate suggestions

```release-note
NONE
```
2021-08-30 10:20:28 +05:30

65 lines
1.9 KiB
Go

// 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/v6/app/plugin_api_tests"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin"
"github.com/mattermost/mattermost-server/v6/shared/driver"
"github.com/mattermost/mattermost-server/v6/store/sqlstore"
"github.com/mattermost/mattermost-server/v6/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) {
settings := p.API.GetUnsanitizedConfig().SqlSettings
settings.Trace = model.NewBool(false)
store := sqlstore.New(settings, 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})
}