Skip to content

Commit

Permalink
require.Error
Browse files Browse the repository at this point in the history
Signed-off-by: sadath-12 <[email protected]>
  • Loading branch information
sadath-12 committed Dec 19, 2023
1 parent 36b78f5 commit 38bc154
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 57 deletions.
53 changes: 27 additions & 26 deletions service/grpc/topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/golang/protobuf/ptypes/empty"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

runtime "github.com/dapr/dapr/pkg/proto/runtime/v1"
"github.com/dapr/go-sdk/service/common"
Expand All @@ -28,29 +29,29 @@ import (
func TestTopicErrors(t *testing.T) {
server := getTestServer()
err := server.AddTopicEventHandler(nil, nil)
assert.Errorf(t, err, "expected error on nil sub with AddTopicEventHandler")
require.Errorf(t, err, "expected error on nil sub with AddTopicEventHandler")

err = server.AddBulkTopicEventHandler(nil, nil, 0, 0)
assert.Errorf(t, err, "expected error on nil sub with AddBulkTopicEventHandler")
require.Errorf(t, err, "expected error on nil sub with AddBulkTopicEventHandler")

sub := &common.Subscription{}
err = server.AddTopicEventHandler(sub, nil)
assert.Errorf(t, err, "expected error on invalid sub with AddTopicEventHandler")
require.Errorf(t, err, "expected error on invalid sub with AddTopicEventHandler")
err = server.AddBulkTopicEventHandler(sub, nil, 0, 0)
assert.Errorf(t, err, "expected error on invalid sub with AddBulkTopicEventHandler")
require.Errorf(t, err, "expected error on invalid sub with AddBulkTopicEventHandler")

sub.PubsubName = "messages"
err = server.AddTopicEventHandler(sub, nil)
assert.Errorf(t, err, "expected error on sub without topic with AddTopicEventHandler")
require.Errorf(t, err, "expected error on sub without topic with AddTopicEventHandler")
sub.PubsubName = "messages"
err = server.AddBulkTopicEventHandler(sub, nil, 0, 0)
assert.Errorf(t, err, "expected error on sub without topic with AddBulkTopicEventHandler")
require.Errorf(t, err, "expected error on sub without topic with AddBulkTopicEventHandler")

sub.Topic = "test"
err = server.AddTopicEventHandler(sub, nil)
assert.Errorf(t, err, "expected error on sub without handler")
require.Errorf(t, err, "expected error on sub without handler")
err = server.AddBulkTopicEventHandler(sub, nil, 0, 0)
assert.Errorf(t, err, "expected error on sub without handler")
require.Errorf(t, err, "expected error on sub without handler")
}

func TestTopicSubscriptionList(t *testing.T) {
Expand All @@ -65,7 +66,7 @@ func TestTopicSubscriptionList(t *testing.T) {
err := server.AddTopicEventHandler(sub1, eventHandler)
assert.Nil(t, err)
resp, err := server.ListTopicSubscriptions(context.Background(), &empty.Empty{})
assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, resp)
if assert.Lenf(t, resp.Subscriptions, 1, "expected 1 handlers") {

Check failure on line 71 in service/grpc/topic_test.go

View workflow job for this annotation

GitHub Actions / Test on 1.20

avoid direct access to proto field resp.Subscriptions, use resp.GetSubscriptions() instead (protogetter)

Check failure on line 71 in service/grpc/topic_test.go

View workflow job for this annotation

GitHub Actions / Test on 1.21

avoid direct access to proto field resp.Subscriptions, use resp.GetSubscriptions() instead (protogetter)
sub := resp.Subscriptions[0]

Check failure on line 72 in service/grpc/topic_test.go

View workflow job for this annotation

GitHub Actions / Test on 1.20

avoid direct access to proto field resp.Subscriptions, use resp.GetSubscriptions() instead (protogetter)

Check failure on line 72 in service/grpc/topic_test.go

View workflow job for this annotation

GitHub Actions / Test on 1.21

avoid direct access to proto field resp.Subscriptions, use resp.GetSubscriptions() instead (protogetter)
Expand All @@ -84,7 +85,7 @@ func TestTopicSubscriptionList(t *testing.T) {
err = server.AddTopicEventHandler(sub2, eventHandler)
assert.Nil(t, err)
resp, err = server.ListTopicSubscriptions(context.Background(), &empty.Empty{})
assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, resp)
if assert.Lenf(t, resp.Subscriptions, 1, "expected 1 handlers") {

Check failure on line 90 in service/grpc/topic_test.go

View workflow job for this annotation

GitHub Actions / Test on 1.20

avoid direct access to proto field resp.Subscriptions, use resp.GetSubscriptions() instead (protogetter)

Check failure on line 90 in service/grpc/topic_test.go

View workflow job for this annotation

GitHub Actions / Test on 1.21

avoid direct access to proto field resp.Subscriptions, use resp.GetSubscriptions() instead (protogetter)
sub := resp.Subscriptions[0]
Expand All @@ -110,7 +111,7 @@ func TestTopicSubscriptionList(t *testing.T) {
err := server.AddBulkTopicEventHandler(sub1, eventHandler, 10, 1000)
assert.Nil(t, err)
resp, err := server.ListTopicSubscriptions(context.Background(), &empty.Empty{})
assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, resp)
if assert.Lenf(t, resp.Subscriptions, 1, "expected 1 handlers") {
sub := resp.Subscriptions[0]
Expand All @@ -129,7 +130,7 @@ func TestTopicSubscriptionList(t *testing.T) {
err = server.AddBulkTopicEventHandler(sub2, eventHandler, 10, 1000)
assert.Nil(t, err)
resp, err = server.ListTopicSubscriptions(context.Background(), &empty.Empty{})
assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, resp)
if assert.Lenf(t, resp.Subscriptions, 1, "expected 1 handlers") {
sub := resp.Subscriptions[0]
Expand Down Expand Up @@ -165,15 +166,15 @@ func TestTopic(t *testing.T) {

t.Run("topic event without request", func(t *testing.T) {
_, err := server.OnTopicEvent(ctx, nil)
assert.Error(t, err)
require.Error(t, err)
})

t.Run("topic event for wrong topic", func(t *testing.T) {
in := &runtime.TopicEventRequest{
Topic: "invalid",
}
_, err := server.OnTopicEvent(ctx, in)
assert.Error(t, err)
require.Error(t, err)
})

t.Run("topic event for valid topic", func(t *testing.T) {
Expand All @@ -188,7 +189,7 @@ func TestTopic(t *testing.T) {
PubsubName: sub.PubsubName,
}
_, err := server.OnTopicEvent(ctx, in)
assert.NoError(t, err)
require.NoError(t, err)
})

stopTestServer(t, server)
Expand All @@ -209,15 +210,15 @@ func TestTopic(t *testing.T) {

t.Run("topic event without request", func(t *testing.T) {
_, err := server.OnTopicEvent(ctx, nil)
assert.Error(t, err)
require.Error(t, err)
})

t.Run("topic event for wrong topic", func(t *testing.T) {
in := &runtime.TopicEventRequest{
Topic: "invalid",
}
_, err := server.OnTopicEvent(ctx, in)
assert.Error(t, err)
require.Error(t, err)
})

t.Run("topic event for valid topic", func(t *testing.T) {
Expand All @@ -232,7 +233,7 @@ func TestTopic(t *testing.T) {
PubsubName: sub.PubsubName,
}
_, err := server.OnTopicEvent(ctx, in)
assert.NoError(t, err)
require.NoError(t, err)
})

stopTestServer(t, server)
Expand Down Expand Up @@ -267,7 +268,7 @@ func TestTopicWithValidationDisabled(t *testing.T) {
}

_, err = server.OnTopicEvent(ctx, in)
assert.NoError(t, err)
require.NoError(t, err)
})
t.Run("With bulk event handling", func(t *testing.T) {
ctx := context.Background()
Expand Down Expand Up @@ -296,7 +297,7 @@ func TestTopicWithValidationDisabled(t *testing.T) {
}

_, err = server.OnTopicEvent(ctx, in)
assert.NoError(t, err)
require.NoError(t, err)
})
}

Expand Down Expand Up @@ -335,7 +336,7 @@ func TestTopicWithErrors(t *testing.T) {
PubsubName: sub1.PubsubName,
}
resp, err := server.OnTopicEvent(ctx, in)
assert.Error(t, err)
require.Error(t, err)
assert.Equal(t, resp.GetStatus(), runtime.TopicEventResponse_RETRY)
})

Expand All @@ -351,7 +352,7 @@ func TestTopicWithErrors(t *testing.T) {
PubsubName: sub2.PubsubName,
}
resp, err := server.OnTopicEvent(ctx, in)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, resp.GetStatus(), runtime.TopicEventResponse_DROP)
})

Expand Down Expand Up @@ -391,7 +392,7 @@ func TestTopicWithErrors(t *testing.T) {
PubsubName: sub1.PubsubName,
}
resp, err := server.OnTopicEvent(ctx, in)
assert.Error(t, err)
require.Error(t, err)
assert.Equal(t, resp.GetStatus(), runtime.TopicEventResponse_RETRY)
})

Expand All @@ -407,7 +408,7 @@ func TestTopicWithErrors(t *testing.T) {
PubsubName: sub2.PubsubName,
}
resp, err := server.OnTopicEvent(ctx, in)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, resp.GetStatus(), runtime.TopicEventResponse_DROP)
})

Expand Down Expand Up @@ -482,7 +483,7 @@ func TestEventDataHandling(t *testing.T) {
return false, nil
}
err := s.AddTopicEventHandler(sub, handler)
assert.NoErrorf(t, err, "error adding event handler")
require.NoErrorf(t, err, "error adding event handler")

startTestServer(s)

Expand Down Expand Up @@ -556,7 +557,7 @@ func TestEventDataHandling(t *testing.T) {
return false, nil
}
err := s.AddBulkTopicEventHandler(sub, handler, 10, 1000)
assert.NoErrorf(t, err, "error adding event handler")
require.NoErrorf(t, err, "error adding event handler")

startTestServer(s)

Expand Down
48 changes: 24 additions & 24 deletions service/http/topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestEventNilHandler(t *testing.T) {
Metadata: map[string]string{},
}
err := s.AddTopicEventHandler(sub, nil)
assert.Error(t, err, "expected error adding event handler")
require.Error(t, err, "expected error adding event handler")
})
t.Run("With bulk event handling", func(t *testing.T) {
s := newServer("", nil)
Expand All @@ -69,7 +69,7 @@ func TestEventNilHandler(t *testing.T) {
Metadata: map[string]string{},
}
err := s.AddBulkTopicEventHandler(sub, nil, 10, 1000)
assert.Error(t, err, "expected error adding event handler")
require.Error(t, err, "expected error adding event handler")
})
}

Expand All @@ -96,7 +96,7 @@ func TestEventHandler(t *testing.T) {
Metadata: map[string]string{},
}
err := s.AddTopicEventHandler(sub, testTopicFunc)
assert.NoError(t, err, "error adding event handler")
require.NoError(t, err, "error adding event handler")

sub2 := &common.Subscription{
PubsubName: "messages",
Expand All @@ -105,7 +105,7 @@ func TestEventHandler(t *testing.T) {
Metadata: map[string]string{},
}
err = s.AddTopicEventHandler(sub2, testErrorTopicFunc)
assert.NoError(t, err, "error adding error event handler")
require.NoError(t, err, "error adding error event handler")

sub3 := &common.Subscription{
PubsubName: "messages",
Expand All @@ -115,7 +115,7 @@ func TestEventHandler(t *testing.T) {
Priority: 1,
}
err = s.AddTopicEventHandler(sub3, testTopicFunc)
assert.NoError(t, err, "error adding error event handler")
require.NoError(t, err, "error adding error event handler")

s.registerBaseHandler()

Expand Down Expand Up @@ -182,7 +182,7 @@ func TestBulkEventHandler(t *testing.T) {
Metadata: map[string]string{},
}
err := s.AddBulkTopicEventHandler(sub, testTopicFunc, 10, 1000)
assert.NoError(t, err, "error adding event handler")
require.NoError(t, err, "error adding event handler")

sub2 := &common.Subscription{
PubsubName: "messages",
Expand All @@ -191,7 +191,7 @@ func TestBulkEventHandler(t *testing.T) {
Metadata: map[string]string{},
}
err = s.AddBulkTopicEventHandler(sub2, testErrorTopicFunc, 10, 1000)
assert.NoError(t, err, "error adding error event handler")
require.NoError(t, err, "error adding error event handler")

sub3 := &common.Subscription{
PubsubName: "messages",
Expand All @@ -201,7 +201,7 @@ func TestBulkEventHandler(t *testing.T) {
Priority: 1,
}
err = s.AddBulkTopicEventHandler(sub3, testTopicFunc, 10, 1000)
assert.NoError(t, err, "error adding error event handler")
require.NoError(t, err, "error adding error event handler")

s.registerBaseHandler()

Expand Down Expand Up @@ -355,7 +355,7 @@ func TestEventDataHandling(t *testing.T) {
return false, nil
}
err := s.AddTopicEventHandler(sub, handler)
assert.NoError(t, err, "error adding event handler")
require.NoError(t, err, "error adding event handler")

s.registerBaseHandler()

Expand Down Expand Up @@ -523,7 +523,7 @@ func TestBulkEventDataHandling(t *testing.T) {
return false, nil
}
err := s.AddBulkTopicEventHandler(sub, handler, 5, 1000)
assert.NoError(t, err, "error adding event handler")
require.NoError(t, err, "error adding event handler")

s.registerBaseHandler()

Expand Down Expand Up @@ -605,61 +605,61 @@ func makeRequest(t *testing.T, s *Server, route, data, method string, expectedSt
t.Helper()

req, err := http.NewRequest(method, route, strings.NewReader(data))
assert.NoErrorf(t, err, "error creating request: %s", data)
require.NoErrorf(t, err, "error creating request: %s", data)
testRequest(t, s, req, expectedStatusCode)
}

func makeRequestWithExpectedBody(t *testing.T, s *Server, route, data, method string, expectedStatusCode int, expectedBody []byte) {
t.Helper()

req, err := http.NewRequest(method, route, strings.NewReader(data))
assert.NoErrorf(t, err, "error creating request: %s", data)
require.NoErrorf(t, err, "error creating request: %s", data)
testRequestWithResponseBody(t, s, req, expectedStatusCode, expectedBody)
}

func makeEventRequest(t *testing.T, s *Server, route, data string, expectedStatusCode int) {
t.Helper()

req, err := http.NewRequest(http.MethodPost, route, strings.NewReader(data))
assert.NoErrorf(t, err, "error creating request: %s", data)
require.NoErrorf(t, err, "error creating request: %s", data)
req.Header.Set("Content-Type", "application/json")
testRequest(t, s, req, expectedStatusCode)
}

func TestAddingInvalidEventHandlers(t *testing.T) {
s := newServer("", nil)
err := s.AddTopicEventHandler(nil, testTopicFunc)
assert.Error(t, err, "expected error adding no sub event handler")
require.Error(t, err, "expected error adding no sub event handler")

sub := &common.Subscription{Metadata: map[string]string{}}
err = s.AddTopicEventHandler(sub, testTopicFunc)
assert.Error(t, err, "expected error adding empty sub event handler")
require.Error(t, err, "expected error adding empty sub event handler")

sub.Topic = "test"
err = s.AddTopicEventHandler(sub, testTopicFunc)
assert.Error(t, err, "expected error adding sub without component event handler")
require.Error(t, err, "expected error adding sub without component event handler")

sub.PubsubName = "messages"
err = s.AddTopicEventHandler(sub, testTopicFunc)
assert.Error(t, err, "expected error adding sub without route event handler")
require.Error(t, err, "expected error adding sub without route event handler")
}

func TestAddingInvalidBulkEventHandlers(t *testing.T) {
s := newServer("", nil)
err := s.AddBulkTopicEventHandler(nil, testTopicFunc, 10, 1000)
assert.Error(t, err, "expected error adding no sub event handler")
require.Error(t, err, "expected error adding no sub event handler")

sub := &common.Subscription{Metadata: map[string]string{}}
err = s.AddBulkTopicEventHandler(sub, testTopicFunc, 10, 1000)
assert.Error(t, err, "expected error adding empty sub event handler")
require.Error(t, err, "expected error adding empty sub event handler")

sub.Topic = "test"
err = s.AddBulkTopicEventHandler(sub, testTopicFunc, 10, 1000)
assert.Error(t, err, "expected error adding sub without component event handler")
require.Error(t, err, "expected error adding sub without component event handler")

sub.PubsubName = "messages"
err = s.AddBulkTopicEventHandler(sub, testTopicFunc, 10, 1000)
assert.Error(t, err, "expected error adding sub without route event handler")
require.Error(t, err, "expected error adding sub without route event handler")
}

func TestRawPayloadDecode(t *testing.T) {
Expand All @@ -671,7 +671,7 @@ func TestRawPayloadDecode(t *testing.T) {
err = errors.New("error decode data_base64")
}
if err != nil {
assert.NoError(t, err, "error rawPayload decode")
require.NoError(t, err, "error rawPayload decode")
}
return
}
Expand All @@ -693,7 +693,7 @@ func TestRawPayloadDecode(t *testing.T) {
},
}
err := s.AddTopicEventHandler(sub3, testRawTopicFunc)
assert.NoError(t, err, "error adding raw event handler")
require.NoError(t, err, "error adding raw event handler")

s.registerBaseHandler()
makeEventRequest(t, s, "/raw", rawData, http.StatusOK)
Expand All @@ -710,7 +710,7 @@ func TestRawPayloadDecode(t *testing.T) {
},
}
err := s.AddBulkTopicEventHandler(sub3, testRawTopicFunc, 10, 1000)
assert.NoError(t, err, "error adding raw event handler")
require.NoError(t, err, "error adding raw event handler")

s.registerBaseHandler()
makeEventRequest(t, s, "/raw", rawData, http.StatusOK)
Expand Down
Loading

0 comments on commit 38bc154

Please sign in to comment.