combluotion/store/factory_test.go
2025-01-27 11:41:32 -06:00

105 lines
2.3 KiB
Go

package store
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"codeberg.org/danjones000/combluotion/config"
confMock "codeberg.org/danjones000/combluotion/internal/testmocks/config"
storeMock "codeberg.org/danjones000/combluotion/internal/testmocks/store"
)
type testHelp struct {
ctrl *gomock.Controller
store *storeMock.MockStore
fact StoreFactory
conf *confMock.MockConfig
}
func setupFactoryTest(t *testing.T) testHelp {
t.Helper()
ctrl := gomock.NewController(t)
store := storeMock.NewMockStore(ctrl)
fact := func(config.Store) (Store, error) {
return store, nil
}
return testHelp{ctrl, store, fact, confMock.NewMockConfig(ctrl)}
}
func TestAddFactory(t *testing.T) {
th := setupFactoryTest(t)
AddFactory("mock", th.fact)
defer delete(factories, "mock")
_, ok := factories["mock"]
assert.True(t, ok)
}
func TestGetFactoryNil(t *testing.T) {
f := GetFactory("mock")
assert.Nil(t, f)
}
func TestGetFactoryNotNil(t *testing.T) {
th := setupFactoryTest(t)
AddFactory("mock", th.fact)
defer delete(factories, "mock")
f := GetFactory("mock")
assert.NotNil(t, f)
}
func TestMakeStoreError(t *testing.T) {
th := setupFactoryTest(t)
th.conf.
EXPECT().
Store("mock").
Return(nil, nil)
s, e := MakeStore("mock", th.conf)
assert.Nil(t, s)
assert.ErrorIs(t, e, ErrNoFactory)
assert.ErrorContains(t, e, ErrNoFactory.Error()+": mock")
}
func TestMakeStoreNoError(t *testing.T) {
th := setupFactoryTest(t)
th.conf.
EXPECT().
Store("mock").
Return(nil, nil)
AddFactory("mock", th.fact)
defer delete(factories, "mock")
s, e := MakeStore("mock", th.conf)
assert.NotNil(t, s)
assert.NoError(t, e)
}
func TestMakeStoreNoName(t *testing.T) {
th := setupFactoryTest(t)
confStore := confMock.NewMockStore(th.ctrl)
th.conf.
EXPECT().
Store("").
Return(confStore, nil)
confStore.
EXPECT().
Name().
Return("mock")
AddFactory("mock", th.fact)
defer delete(factories, "mock")
s, e := MakeStore("", th.conf)
assert.NotNil(t, s)
assert.NoError(t, e)
}
func TestMakeStoreConfError(t *testing.T) {
th := setupFactoryTest(t)
mockError := errors.New("leave me alone")
th.conf.EXPECT().Store("mock").Return(nil, mockError)
s, e := MakeStore("mock", th.conf)
assert.Zero(t, s)
assert.ErrorIs(t, e, mockError)
}