2024-09-14 20:37:51 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2025-01-24 16:27:05 -06:00
|
|
|
"go.uber.org/mock/gomock"
|
|
|
|
|
|
|
|
|
|
"codeberg.org/danjones000/combluotion/config"
|
|
|
|
|
storeMock "codeberg.org/danjones000/combluotion/internal/testmocks/store"
|
2024-09-14 20:37:51 -05:00
|
|
|
)
|
|
|
|
|
|
2025-01-24 16:27:05 -06:00
|
|
|
func getStoreFactory(t *testing.T) (*gomock.Controller, StoreFactory) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
return ctrl, func(config.Config) (Store, error) {
|
|
|
|
|
return storeMock.NewMockStore(ctrl), nil
|
|
|
|
|
}
|
2024-09-14 20:37:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAddFactory(t *testing.T) {
|
2025-01-24 16:27:05 -06:00
|
|
|
_, f := getStoreFactory(t)
|
2024-09-14 20:37:51 -05:00
|
|
|
AddFactory("mock", f)
|
|
|
|
|
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) {
|
2025-01-24 16:27:05 -06:00
|
|
|
_, f := getStoreFactory(t)
|
2024-09-14 20:37:51 -05:00
|
|
|
AddFactory("mock", f)
|
|
|
|
|
defer delete(factories, "mock")
|
2025-01-24 16:27:05 -06:00
|
|
|
f = GetFactory("mock")
|
2024-09-14 20:37:51 -05:00
|
|
|
assert.NotNil(t, f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMakeStoreError(t *testing.T) {
|
|
|
|
|
s, e := MakeStore("mock", config.Config{})
|
|
|
|
|
assert.Nil(t, s)
|
|
|
|
|
assert.ErrorIs(t, e, ErrNoFactory)
|
|
|
|
|
assert.ErrorContains(t, e, ErrNoFactory.Error()+": mock")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMakeStoreNoError(t *testing.T) {
|
2025-01-24 16:27:05 -06:00
|
|
|
_, f := getStoreFactory(t)
|
2024-09-14 20:37:51 -05:00
|
|
|
AddFactory("mock", f)
|
|
|
|
|
defer delete(factories, "mock")
|
|
|
|
|
s, e := MakeStore("mock", config.Config{})
|
|
|
|
|
assert.NotNil(t, s)
|
|
|
|
|
assert.NoError(t, e)
|
|
|
|
|
}
|
2024-09-15 12:37:42 -05:00
|
|
|
|
|
|
|
|
func TestMakeStoreNoName(t *testing.T) {
|
2025-01-24 16:27:05 -06:00
|
|
|
_, f := getStoreFactory(t)
|
2024-09-15 12:37:42 -05:00
|
|
|
AddFactory("mock", f)
|
|
|
|
|
defer delete(factories, "mock")
|
|
|
|
|
s, e := MakeStore("", config.Config{
|
|
|
|
|
Conn: config.ConnSettings{Store: "mock"},
|
|
|
|
|
})
|
|
|
|
|
assert.NotNil(t, s)
|
|
|
|
|
assert.NoError(t, e)
|
|
|
|
|
}
|