joist/internal/errors/errors_test.go

102 lines
2.3 KiB
Go

package errors
import (
"errors"
"path/filepath"
"testing"
)
func TestErrRouteNotFound(t *testing.T) {
tests := []struct {
name string
route string
expected string
}{
{
name: "empty route name",
route: "",
expected: "route not found: ",
},
{
name: "simple route name",
route: "home",
expected: "route not found: home",
},
{
name: "complex route name",
route: "/api/v1/resource",
expected: "route not found: /api/v1/resource",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := NewErrRouteNotFound(tt.route)
if err.Error() != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, err.Error())
}
// also check that errors.As works
var target *ErrRouteNotFound
if !errors.As(err, &target) {
t.Errorf("expected error to be of type ErrRouteNotFound")
}
})
}
}
func TestErrBadgerInit(t *testing.T) {
tests := []struct {
name string
baseDir string
subDir string
expected string
}{
{
name: "base dir only",
baseDir: "/tmp/data",
subDir: "",
expected: "failed to initialize badger store at base directory: /tmp/data",
},
{
name: "base dir with sub dir",
baseDir: "/tmp/data",
subDir: "auth",
expected: "failed to initialize badger store at directory: " + filepath.Join("/tmp/data", "auth"),
},
{
name: "empty paths",
baseDir: "",
subDir: "",
expected: "failed to initialize badger store at base directory: ",
},
{
name: "empty base dir with sub dir",
baseDir: "",
subDir: "cache",
expected: "failed to initialize badger store at directory: " + filepath.Join("", "cache"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := NewErrBadgerInit(tt.baseDir, tt.subDir)
if err.Error() != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, err.Error())
}
// also check that errors.As works
var target *ErrBadgerInit
if !errors.As(err, &target) {
t.Errorf("expected error to be of type ErrBadgerInit")
}
})
}
}
func TestErrCacheUninitialized(t *testing.T) {
if ErrCacheUninitialized.Error() != "the route cache is uninitialized" {
t.Errorf("unexpected error message: %q", ErrCacheUninitialized.Error())
}
}