Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Marcantonio <[email protected]>
  • Loading branch information
lennysgarage committed Jun 26, 2024
1 parent 9b887a1 commit 8fd0eb2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
27 changes: 25 additions & 2 deletions internal/data/spicedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down
60 changes: 60 additions & 0 deletions internal/service/health_test.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 8fd0eb2

Please sign in to comment.