mattermost/server/public/pluginapi/store_test.go
Jesse Hallam e3fbf8711f
MM-68149: Upgrade to Go 1.26.2 (#36418)
* MM-68149: upgrade to Go 1.26.2

Update go directive in go.mod and .go-version.

* MM-68149: replace pointer helpers with Go 1.26 new()

Go 1.26 extends the built-in new() to accept an initial value expression,
making typed-pointer helpers like model.NewPointer(x), bToP(x), and boolPtr(x)
redundant. Replace every call site with new(x) and remove the now-unused
helper functions and their //go:fix inline directives.

* MM-68149: apply go fix for reflect API and format-string changes

- reflect.Ptr → reflect.Pointer (renamed in Go 1.18, deprecated alias removed in 1.26)
- reflect range-over-struct: for i := 0; i < t.NumField(); i++ → for field := range t.Fields()
  and the equivalent for Methods() and interface types
- Fix format-string concatenation and variadic-arg mismatches flagged by go vet

* MM-68149: update JPEG fixtures and test infrastructure for Go 1.26 encoder

Go 1.26 ships a new image/jpeg encoder that produces slightly different output.
Regenerate all JPEG fixture files and switch the comparison helpers from
byte-equality to pixel-level comparison with a small per-channel tolerance,
so minor encoder drift across patch versions is handled automatically.

Add -update-fixtures flag to make it easy to regenerate fixtures after future
major Go upgrades. Document the update procedure in tests/README.md.

* MM-68149: CI check that go fix ./... produces no changes

* Fix real bugs flagged by CodeRabbit review

- group.go: set newGroup.MemberCount not group.MemberCount (member count
  was populated on the wrong variable and lost before publish/return)
- file_test.go: guard compareImage(GetFilePreview) on the preview slice
  length, not the thumbnail slice length (copy-paste error)
- config_test.go: remove duplicate MinimumLength assignment

* fixup! Fix real bugs flagged by CodeRabbit review
2026-05-12 15:59:12 +00:00

135 lines
3.7 KiB
Go

package pluginapi_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
"github.com/mattermost/mattermost/server/public/pluginapi"
)
func TestStore(t *testing.T) {
t.Run("master db singleton", func(t *testing.T) {
api := &plugintest.API{}
driver := &plugintest.Driver{}
defer driver.AssertExpectations(t)
driver.On("Conn", true).Return("test", nil)
driver.On("ConnPing", "test").Return(nil)
driver.On("ConnClose", "test").Return(nil)
store := pluginapi.NewClient(api, driver).Store
db1, err := store.GetMasterDB()
require.NoError(t, err)
require.NotNil(t, db1)
db2, err := store.GetMasterDB()
require.NoError(t, err)
require.NotNil(t, db2)
require.Same(t, db1, db2)
require.NoError(t, store.Close())
})
t.Run("master db fallback", func(t *testing.T) {
config := &model.Config{
SqlSettings: model.SqlSettings{
DriverName: new("ramsql"),
DataSource: new("TestStore-master-db"),
ConnMaxLifetimeMilliseconds: new(2),
},
}
driver := &plugintest.Driver{}
defer driver.AssertExpectations(t)
driver.On("Conn", true).Return("test", nil)
driver.On("ConnPing", "test").Return(nil)
driver.On("ConnClose", "test").Return(nil)
api := &plugintest.API{}
defer api.AssertExpectations(t)
store := pluginapi.NewClient(api, driver).Store
api.On("GetUnsanitizedConfig").Return(config)
masterDB, err := store.GetMasterDB()
require.NoError(t, err)
require.NotNil(t, masterDB)
// No replica is set up, should fallback to master
replicaDB, err := store.GetReplicaDB()
require.NoError(t, err)
require.Same(t, replicaDB, masterDB)
require.NoError(t, store.Close())
})
t.Run("master db fallback without get master first", func(t *testing.T) {
config := &model.Config{
SqlSettings: model.SqlSettings{
DriverName: new("ramsql"),
DataSource: new("TestStore-master-db"),
ConnMaxLifetimeMilliseconds: new(2),
},
}
driver := &plugintest.Driver{}
defer driver.AssertExpectations(t)
driver.On("Conn", true).Return("test", nil)
driver.On("ConnPing", "test").Return(nil)
driver.On("ConnClose", "test").Return(nil)
api := &plugintest.API{}
defer api.AssertExpectations(t)
store := pluginapi.NewClient(api, driver).Store
api.On("GetUnsanitizedConfig").Return(config)
// No replica is set up, should fallback to master
replicaDB, err := store.GetReplicaDB()
require.NoError(t, err)
require.NotNil(t, replicaDB)
masterDB, err := store.GetMasterDB()
require.NoError(t, err)
require.NotNil(t, masterDB)
require.Same(t, replicaDB, masterDB)
require.NoError(t, store.Close())
})
t.Run("replica db singleton", func(t *testing.T) {
config := &model.Config{
SqlSettings: model.SqlSettings{
DriverName: new("ramsql"),
DataSource: new("TestStore-master-db"),
DataSourceReplicas: []string{"TestStore-master-db"},
ConnMaxLifetimeMilliseconds: new(2),
},
}
api := &plugintest.API{}
defer api.AssertExpectations(t)
api.On("GetUnsanitizedConfig").Return(config)
driver := &plugintest.Driver{}
defer driver.AssertExpectations(t)
driver.On("Conn", false).Return("test", nil)
driver.On("ConnPing", "test").Return(nil)
driver.On("ConnClose", "test").Return(nil)
store := pluginapi.NewClient(api, driver).Store
db1, err := store.GetReplicaDB()
require.NoError(t, err)
require.NotNil(t, db1)
db2, err := store.GetReplicaDB()
require.NoError(t, err)
require.NotNil(t, db2)
require.Same(t, db1, db2)
require.NoError(t, store.Close())
})
}