Merge pull request #71 from Tiglas/refactor-tests
refactor: group related tests into subtests
This commit is contained in:
@@ -22,9 +22,9 @@ func (s *Server) ListEntities(ctx context.Context, req *pb.ListEntitiesRequest)
|
||||
return nil, status.Error(codes.Internal, "could not list entities")
|
||||
}
|
||||
|
||||
result := make([]*pb.Entity, len(entities))
|
||||
for i, v := range entities {
|
||||
result[i] = &pb.Entity{Uri: v}
|
||||
var result []*pb.Entity
|
||||
for _, v := range entities {
|
||||
result = append(result, &pb.Entity{Uri: v})
|
||||
}
|
||||
|
||||
return &pb.ListEntitiesReply{Entities: result}, nil
|
||||
|
||||
@@ -13,12 +13,14 @@ import (
|
||||
pb "github.com/spotify/backstage/proto/inventory/v1"
|
||||
)
|
||||
|
||||
var entityURI = "boss://test/test"
|
||||
|
||||
func TestServerListEntities(t *testing.T) {
|
||||
testStorage := NewTestStorage()
|
||||
defer testStorage.Close()
|
||||
s := Server{Storage: testStorage.Storage}
|
||||
|
||||
entity := &pb.Entity{Uri: "boss://test/test"}
|
||||
entity := &pb.Entity{Uri: entityURI}
|
||||
|
||||
_, err := s.CreateEntity(context.Background(), &pb.CreateEntityRequest{Entity: entity})
|
||||
if err != nil {
|
||||
@@ -32,7 +34,7 @@ func TestServerListEntities(t *testing.T) {
|
||||
if len(list.GetEntities()) != 1 {
|
||||
t.Errorf("ServerTest(TestServerListEntities) expected %v items, got %v", 1, len(list.GetEntities()))
|
||||
}
|
||||
if list.GetEntities()[0].GetUri() != "boss://test/test" {
|
||||
if list.GetEntities()[0].GetUri() != entityURI {
|
||||
t.Errorf("ServerTest(TestServerListEntities) expected uri %v, got %v", "boss://test/test", list.GetEntities()[0].GetUri())
|
||||
}
|
||||
|
||||
@@ -50,7 +52,7 @@ func TestServerCreateEntity(t *testing.T) {
|
||||
defer testStorage.Close()
|
||||
s := Server{Storage: testStorage.Storage}
|
||||
|
||||
entity := &pb.Entity{Uri: "boss://test/test"}
|
||||
entity := &pb.Entity{Uri: entityURI}
|
||||
createReq := &pb.CreateEntityRequest{Entity: entity}
|
||||
resp, err := s.CreateEntity(context.Background(), createReq)
|
||||
if err != nil {
|
||||
@@ -62,93 +64,97 @@ func TestServerCreateEntity(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestServerGetEntity(t *testing.T) {
|
||||
testStorage := NewTestStorage()
|
||||
defer testStorage.Close()
|
||||
s := Server{Storage: testStorage.Storage}
|
||||
t.Run("Get entity", func(t *testing.T) {
|
||||
testStorage := NewTestStorage()
|
||||
defer testStorage.Close()
|
||||
s := Server{Storage: testStorage.Storage}
|
||||
|
||||
entity := &pb.Entity{Uri: "boss://test/test"}
|
||||
createReq := &pb.CreateEntityRequest{Entity: entity}
|
||||
s.CreateEntity(context.Background(), createReq)
|
||||
entity := &pb.Entity{Uri: entityURI}
|
||||
createReq := &pb.CreateEntityRequest{Entity: entity}
|
||||
s.CreateEntity(context.Background(), createReq)
|
||||
|
||||
req := &pb.GetEntityRequest{Entity: entity}
|
||||
resp, err := s.GetEntity(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Errorf("ServerTest(GetEntity) got unexpected error %v", err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Errorf("ServerTest(GetEntity) returned nil")
|
||||
}
|
||||
if resp.GetEntity().GetUri() != entity.GetUri() {
|
||||
t.Errorf("ServerTest(GetEntity) got %v, wanted %v", resp.GetEntity().GetUri(), entity.GetUri())
|
||||
}
|
||||
}
|
||||
req := &pb.GetEntityRequest{Entity: entity}
|
||||
resp, err := s.GetEntity(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Errorf("ServerTest(GetEntity) got unexpected error %v", err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Errorf("ServerTest(GetEntity) returned nil")
|
||||
}
|
||||
if resp.GetEntity().GetUri() != entity.GetUri() {
|
||||
t.Errorf("ServerTest(GetEntity) got %v, wanted %v", resp.GetEntity().GetUri(), entity.GetUri())
|
||||
}
|
||||
})
|
||||
|
||||
func TestServerGetEntityWithIncludedFacts(t *testing.T) {
|
||||
testStorage := NewTestStorage()
|
||||
defer testStorage.Close()
|
||||
s := Server{Storage: testStorage.Storage}
|
||||
t.Run("Get entity with included facts", func(t *testing.T) {
|
||||
testStorage := NewTestStorage()
|
||||
defer testStorage.Close()
|
||||
s := Server{Storage: testStorage.Storage}
|
||||
setFactReq := &pb.SetFactRequest{EntityUri: entityURI, Name: "test-name", Value: "test-value"}
|
||||
s.SetFact(context.Background(), setFactReq)
|
||||
|
||||
entityUri := "boss://test/test"
|
||||
setFactReq := &pb.SetFactRequest{EntityUri: entityUri, Name: "test-name", Value: "test-value"}
|
||||
s.SetFact(context.Background(), setFactReq)
|
||||
entity := &pb.Entity{Uri: entityURI}
|
||||
req := &pb.GetEntityRequest{Entity: entity, IncludeFacts: []string{"test-name"}}
|
||||
|
||||
entity := &pb.Entity{Uri: entityUri}
|
||||
req := &pb.GetEntityRequest{Entity: entity, IncludeFacts: []string{"test-name"}}
|
||||
|
||||
resp, err := s.GetEntity(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Errorf("ServerTest(GetEntity) got unexpected error %v", err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Errorf("ServerTest(GetEntity) returned nil")
|
||||
}
|
||||
expectedFacts := []*pb.Fact{{Name: "test-name", Value: "test-value"}}
|
||||
if !reflect.DeepEqual(resp.GetFacts(), expectedFacts) {
|
||||
t.Errorf("ServerTest(GetEntity) got %v, wanted %v", resp.GetFacts(), expectedFacts)
|
||||
}
|
||||
resp, err := s.GetEntity(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Errorf("ServerTest(GetEntity) got unexpected error %v", err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Errorf("ServerTest(GetEntity) returned nil")
|
||||
}
|
||||
expectedFacts := []*pb.Fact{{Name: "test-name", Value: "test-value"}}
|
||||
if !reflect.DeepEqual(resp.GetFacts(), expectedFacts) {
|
||||
t.Errorf("ServerTest(GetEntity) got %v, wanted %v", resp.GetFacts(), expectedFacts)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestServerSetFactForExistingEntity(t *testing.T) {
|
||||
testStorage := NewTestStorage()
|
||||
defer testStorage.Close()
|
||||
s := Server{Storage: testStorage.Storage}
|
||||
t.Run("Set fact for existing entity", func(t *testing.T) {
|
||||
testStorage := NewTestStorage()
|
||||
defer testStorage.Close()
|
||||
s := Server{Storage: testStorage.Storage}
|
||||
|
||||
entity := &pb.Entity{Uri: "boss://test/test"}
|
||||
createReq := &pb.CreateEntityRequest{Entity: entity}
|
||||
s.CreateEntity(context.Background(), createReq)
|
||||
entity := &pb.Entity{Uri: entityURI}
|
||||
createReq := &pb.CreateEntityRequest{Entity: entity}
|
||||
s.CreateEntity(context.Background(), createReq)
|
||||
|
||||
req := &pb.SetFactRequest{EntityUri: "boss://test/test", Name: "test-name", Value: "test-value"}
|
||||
resp, err := s.SetFact(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Errorf("ServerTest(SetFact) got unexpected error %v", err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Errorf("ServerTest(SetFact) returned nil")
|
||||
}
|
||||
fact := &pb.Fact{Name: req.GetName(), Value: req.GetValue()}
|
||||
if !reflect.DeepEqual(resp.GetFact(), fact) {
|
||||
t.Errorf("ServerTest(SetFact) got %v, wanted %v", resp.GetFact(), fact)
|
||||
}
|
||||
req := &pb.SetFactRequest{EntityUri: entityURI, Name: "test-name", Value: "test-value"}
|
||||
resp, err := s.SetFact(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Errorf("ServerTest(SetFact) got unexpected error %v", err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Errorf("ServerTest(SetFact) returned nil")
|
||||
}
|
||||
|
||||
fact := &pb.Fact{Name: req.GetName(), Value: req.GetValue()}
|
||||
assertFact(t, resp.GetFact(), fact)
|
||||
})
|
||||
|
||||
t.Run("Set fact for non-existing entity", func(t *testing.T) {
|
||||
testStorage := NewTestStorage()
|
||||
defer testStorage.Close()
|
||||
s := Server{Storage: testStorage.Storage}
|
||||
|
||||
req := &pb.SetFactRequest{EntityUri: entityURI, Name: "test-name", Value: "test-value"}
|
||||
resp, err := s.SetFact(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Errorf("ServerTest(SetFact) got unexpected error %v", err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Errorf("ServerTest(SetFact) returned nil")
|
||||
}
|
||||
|
||||
fact := &pb.Fact{Name: req.GetName(), Value: req.GetValue()}
|
||||
assertFact(t, resp.GetFact(), fact)
|
||||
})
|
||||
}
|
||||
|
||||
func TestServerSetFactForNonExistingEntity(t *testing.T) {
|
||||
testStorage := NewTestStorage()
|
||||
defer testStorage.Close()
|
||||
s := Server{Storage: testStorage.Storage}
|
||||
|
||||
entityUri := "boss://test/test"
|
||||
req := &pb.SetFactRequest{EntityUri: entityUri, Name: "test-name", Value: "test-value"}
|
||||
resp, err := s.SetFact(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Errorf("ServerTest(SetFact) got unexpected error %v", err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Errorf("ServerTest(SetFact) returned nil")
|
||||
}
|
||||
|
||||
fact := &pb.Fact{Name: req.GetName(), Value: req.GetValue()}
|
||||
if !reflect.DeepEqual(resp.GetFact(), fact) {
|
||||
t.Errorf("ServerTest(SetFact) got %v, wanted %v", resp.GetFact(), fact)
|
||||
func assertFact(t *testing.T, got, want *pb.Fact) {
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("ServerTest(SetFact) got %v, wanted %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user