31 lines
709 B
Go
31 lines
709 B
Go
package health
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
type databaseStub struct{ err error }
|
|
|
|
func (s databaseStub) Ping(context.Context) error { return s.err }
|
|
|
|
func TestCheckReportsDatabaseReadiness(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
db Database
|
|
ready bool
|
|
}{
|
|
{name: "not connected", db: nil, ready: false},
|
|
{name: "ready", db: databaseStub{}, ready: true},
|
|
{name: "unavailable", db: databaseStub{err: errors.New("down")}, ready: false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := NewService(tc.db).Check(context.Background())
|
|
if got.Serving != tc.ready || got.DatabaseReady != tc.ready {
|
|
t.Fatalf("unexpected status: %+v", got)
|
|
}
|
|
})
|
|
}
|
|
}
|