Improve test coverage

This commit is contained in:
Dan Jones 2025-01-27 11:41:32 -06:00
commit d7de194a90
4 changed files with 75 additions and 0 deletions

1
app.go
View file

@ -33,6 +33,7 @@ func NewApp(ver string, conf config.Config, db store.Store) (*App, error) {
app := App{
version: ver,
conf: conf,
storage: db,
}
selfIRI := boxap.DefaultServiceIRI(conf.BaseURL())

View file

@ -78,6 +78,31 @@ func TestService(t *testing.T) {
}
}
func TestAppStorage(t *testing.T) {
th := setupAppTest(t)
base := "http://localhost:1234/"
th.conf.EXPECT().BaseURL().Return(base).MinTimes(1)
th.conf.EXPECT().Name().Return("").AnyTimes()
a, er := NewApp("0.0.0.0", th.conf, th.store)
assert.NoError(t, er)
if assert.NotNil(t, a) {
assert.Equal(t, th.store, a.Storage())
}
}
func TestAppUser(t *testing.T) {
// TODO: Actually fill this out at some point
th := setupAppTest(t)
base := "http://localhost:1234/"
th.conf.EXPECT().BaseURL().Return(base).MinTimes(1)
th.conf.EXPECT().Name().Return("").AnyTimes()
a, er := NewApp("0.0.0.0", th.conf, th.store)
assert.NoError(t, er)
if assert.NotNil(t, a) {
assert.Zero(t, a.User())
}
}
func TestStrings(t *testing.T) {
cases := [...]struct {
given string

View file

@ -3,6 +3,7 @@ package config
import (
"testing"
"github.com/BurntSushi/toml"
"github.com/stretchr/testify/assert"
)
@ -24,3 +25,40 @@ func TestValidEnvReturnsCorrect(t *testing.T) {
})
}
}
func TestConfigBaseURL(t *testing.T) {
c := config{baseURL: "https://me.goodevilgenius.org"}
assert.Equal(t, c.baseURL, c.BaseURL())
}
func TestConfigStoreName(t *testing.T) {
c := config{stores: stores{store: "cockroachdb"}}
assert.Equal(t, c.stores.store, c.StoreName())
}
func TestStoresMissingStore(t *testing.T) {
ss := stores{}
st, er := ss.GetStore("cockroachdb")
assert.Nil(t, st)
assert.ErrorIs(t, er, ErrMissingStore)
}
var mockToml = `
[cockroachdb]
dsn = "cockroachdb://user:pass@127.0.0.1:26257/combluotion"
`
type mockConn struct {
CockroachDB toml.Primitive
}
func TestStoreMap(t *testing.T) {
var conn mockConn
md, _ := toml.Decode(mockToml, &conn)
st := store{"cockroachdb", conn.CockroachDB, md}
m, er := st.Map()
assert.NoError(t, er)
assert.Equal(t, "cockroachdb://user:pass@127.0.0.1:26257/combluotion", m["dsn"])
}

View file

@ -1,6 +1,7 @@
package store
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
@ -92,3 +93,13 @@ func TestMakeStoreNoName(t *testing.T) {
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)
}