goshort/internal/config/config_test.go

102 lines
1.8 KiB
Go

package config_test
import (
"testing"
. "git.maronato.dev/maronato/goshort/internal/config"
"github.com/stretchr/testify/assert"
)
func TestGetDBTypeList(t *testing.T) {
dbTypeList := []string{
DBTypeMemory,
DBTypeSQLite,
}
results := GetDBTypeList()
assert.ElementsMatch(t, dbTypeList, results, "DBTypeList should be equal to the list of available DB types")
}
func TestValidate(t *testing.T) {
type test struct {
name string
config *Config
wantErr bool
}
tests := []test{
{
name: "Valid config",
config: &Config{
Prod: false,
DBType: DBTypeSQLite,
DBURL: "goshort.db",
Port: "8080",
Host: "localhost",
UIPort: "3000",
Debug: false,
SessionDuration: 7,
DisableRegistration: false,
},
wantErr: false,
},
{
name: "Minimum config",
config: &Config{},
wantErr: false,
},
{
name: "Invalid Host",
config: &Config{
Host: "invalid host",
Port: "8080",
},
wantErr: true,
},
{
name: "Invalid Port",
config: &Config{
Host: "localhost",
Port: "invalid port",
},
wantErr: true,
},
{
name: "Invalid UI Port",
config: &Config{
Host: "localhost",
Port: "8080",
UIPort: "invalid port",
},
wantErr: true,
},
{
name: "Valid DB Type",
config: &Config{
DBType: DBTypeMemory,
},
wantErr: false,
},
{
name: "Invalid DB Type",
config: &Config{
DBType: "invalid db type",
},
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := Validate(tc.config)
if tc.wantErr {
assert.Error(t, err, "Validate should return an error")
} else {
assert.NoError(t, err, "Validate should not return an error")
}
})
}
}