♻️ Refactor config

Make it easier to setup stores
This commit is contained in:
Dan Jones 2025-01-26 20:07:45 -06:00
commit 6f06adc37d
12 changed files with 623 additions and 81 deletions

View file

@ -9,32 +9,33 @@ import (
"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"
)
func getStore(t *testing.T) *storeMock.MockStore {
type appTest struct {
ctrl *gomock.Controller
store *storeMock.MockStore
conf *confMock.MockConfig
}
func setupAppTest(t *testing.T) appTest {
t.Helper()
ctrl := gomock.NewController(t)
return storeMock.NewMockStore(ctrl)
return appTest{ctrl, storeMock.NewMockStore(ctrl), confMock.NewMockConfig(ctrl)}
}
func TestEmptyBaseURL(t *testing.T) {
c := config.Config{}
a, er := NewApp("0.0.0", c, getStore(t))
th := setupAppTest(t)
th.conf.
EXPECT().
BaseURL().
Return("")
a, er := NewApp("0.0.0", th.conf, th.store)
assert.Nil(t, a)
assert.EqualError(t, er, "missing BaseURL")
}
func TestDefaultEnvironment(t *testing.T) {
store := getStore(t)
c := config.Config{BaseURL: "http://localhost:1234/"}
a, er := NewApp("0.0.0", c, store)
assert.NoError(t, er)
if assert.NotNil(t, a) {
assert.Equal(t, config.Dev, a.Environment())
}
}
func TestGivenEnvironment(t *testing.T) {
cases := [...]struct {
given config.Env
@ -43,18 +44,21 @@ func TestGivenEnvironment(t *testing.T) {
{config.Dev, config.Dev},
{config.Prod, config.Prod},
{config.Qa, config.Qa},
{config.Env("foo"), config.Dev},
{config.Env("✨"), config.Dev},
}
for _, c := range cases {
t.Run(string(c.given), func(t *testing.T) {
store := getStore(t)
conf := config.Config{BaseURL: "http://localhost:1234/", Env: c.given}
a, er := NewApp("0.0.0", conf, store)
th := setupAppTest(t)
th.conf.EXPECT().BaseURL().Return("http://localhost:1234/").Times(2)
th.conf.EXPECT().Name().Return("")
th.conf.
EXPECT().
Env().
Return(c.given)
a, er := NewApp("0.0.0", th.conf, th.store)
assert.NoError(t, er)
if assert.NotNil(t, a) {
assert.Equal(t, conf, a.Config())
assert.Equal(t, th.conf, a.Config())
assert.Equal(t, c.exp, a.Environment())
}
})
@ -62,10 +66,11 @@ func TestGivenEnvironment(t *testing.T) {
}
func TestService(t *testing.T) {
store := getStore(t)
th := setupAppTest(t)
base := "http://localhost:1234/"
conf := config.Config{BaseURL: base}
a, er := NewApp("0.0.0.0", conf, store)
th.conf.EXPECT().BaseURL().Return(base).Times(2)
th.conf.EXPECT().Name().Return("")
a, er := NewApp("0.0.0.0", th.conf, th.store)
assert.NoError(t, er)
if assert.NotNil(t, a) {
assert.Equal(t, vocab.IRI(base), a.ServiceIRI())
@ -86,10 +91,11 @@ func TestStrings(t *testing.T) {
for _, c := range cases {
t.Run(c.given, func(t *testing.T) {
store := getStore(t)
conf := config.Config{BaseURL: "http://localhost:1234/", Name: c.given}
th := setupAppTest(t)
th.conf.EXPECT().BaseURL().Return("http://localhost:1234/").AnyTimes()
th.conf.EXPECT().Name().Return(c.given).MinTimes(1)
expStr := fmt.Sprintf("%s (%s)", c.exp, "0.0.0.0")
a, er := NewApp("0.0.0.0", conf, store)
a, er := NewApp("0.0.0.0", th.conf, th.store)
assert.NoError(t, er)
if assert.NotNil(t, a) {
assert.Equal(t, c.exp, a.Name())