From 8fd0eb292a4b5f88b5d685d4cc2fdc071bbfa305 Mon Sep 17 00:00:00 2001 From: Jonathan Marcantonio Date: Wed, 26 Jun 2024 15:07:01 -0400 Subject: [PATCH] Add tests Signed-off-by: Jonathan Marcantonio --- internal/data/spicedb_test.go | 27 +++++++++++++-- internal/service/health_test.go | 60 +++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 internal/service/health_test.go diff --git a/internal/data/spicedb_test.go b/internal/data/spicedb_test.go index 0e1942c..ac8449f 100644 --- a/internal/data/spicedb_test.go +++ b/internal/data/spicedb_test.go @@ -3,11 +3,12 @@ package data import ( "context" "fmt" - apiV0 "github.com/project-kessel/relations-api/api/relations/v0" - "github.com/project-kessel/relations-api/internal/biz" "os" "testing" + apiV0 "github.com/project-kessel/relations-api/api/relations/v0" + "github.com/project-kessel/relations-api/internal/biz" + "github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/middleware/tracing" "github.com/stretchr/testify/assert" @@ -117,6 +118,28 @@ func TestSecondCreateRelationshipSucceedsWithTouchTrue(t *testing.T) { assert.True(t, exists) } +func TestIsBackendAvailable(t *testing.T) { + t.Parallel() + + spiceDbrepo, err := container.CreateSpiceDbRepository() + assert.NoError(t, err) + + err = spiceDbrepo.IsBackendAvaliable() + assert.NoError(t, err) +} + +func TestIsBackendAvailableFailsWithError(t *testing.T) { + t.Parallel() + + spiceDbrepo, err := container.CreateSpiceDbRepository() + assert.NoError(t, err) + + container.Close() + + err = spiceDbrepo.IsBackendAvaliable() + assert.Error(t, err) +} + func TestCreateRelationshipFailsWithBadSubjectType(t *testing.T) { t.Parallel() diff --git a/internal/service/health_test.go b/internal/service/health_test.go new file mode 100644 index 0000000..d252124 --- /dev/null +++ b/internal/service/health_test.go @@ -0,0 +1,60 @@ +package service + +import ( + "context" + "testing" + + pb "github.com/project-kessel/relations-api/api/health/v1" + "github.com/project-kessel/relations-api/internal/biz" + "github.com/project-kessel/relations-api/internal/data" + + "github.com/stretchr/testify/assert" +) + +func TestHealthService_GetLivez(t *testing.T) { + t.Parallel() + + ctx := context.TODO() + spicedb, err := container.CreateSpiceDbRepository() + assert.NoError(t, err) + + service := createHealthService(spicedb) + resp, err := service.GetLivez(ctx, &pb.GetLivezRequest{}) + + assert.NoError(t, err) + assert.Equal(t, resp, &pb.GetLivezReply{Status: "OK", Code: 200}) +} + +func TestHealthService_GetReadyz_SpiceDBAvailable(t *testing.T) { + t.Parallel() + + ctx := context.TODO() + spicedb, err := container.CreateSpiceDbRepository() + assert.NoError(t, err) + + service := createHealthService(spicedb) + resp, err := service.GetReadyz(ctx, &pb.GetReadyzRequest{}) + + assert.NoError(t, err) + assert.Equal(t, resp, &pb.GetReadyzReply{Status: "OK", Code: 200}) +} + +func TestHealthService_GetReadyz_SpiceDBUnavailable(t *testing.T) { + t.Parallel() + + ctx := context.TODO() + spicedb, err := container.CreateSpiceDbRepository() + assert.NoError(t, err) + + container.Close() + + service := createHealthService(spicedb) + resp, err := service.GetReadyz(ctx, &pb.GetReadyzRequest{}) + + assert.NoError(t, err) + assert.Equal(t, resp, &pb.GetReadyzReply{Status: "Unavailable", Code: 503}) +} + +func createHealthService(spicedb *data.SpiceDbRepository) *HealthService { + return NewHealthService(biz.NewIsBackendAvailableUsecase(spicedb)) +}