Skip to content
This repository has been archived by the owner on Jun 19, 2022. It is now read-only.

Commit

Permalink
increase testing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Lopez committed Jan 24, 2020
1 parent e40884d commit abf2bb3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
12 changes: 6 additions & 6 deletions pkg/pubsub/adapter/converters/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func convertPubSub(ctx context.Context, msg *cepubsub.Message, sendMode ModeType
logger := logging.FromContext(ctx).With(zap.Any("event.id", event.ID()))
// set the content type to something that can be handled by codec.go
event.SetDataContentType(cloudevents.ApplicationJSON)
msg := &PubSubMessage{
msg := &pubSubMessage{
ID: event.ID(),
Attributes: msg.Attributes,
PublishTime: event.Time(),
Expand All @@ -73,7 +73,7 @@ func convertPubSub(ctx context.Context, msg *cepubsub.Message, sendMode ModeType
msg.Data = raw
}

if err := event.SetData(&PushMessage{
if err := event.SetData(&pushMessage{
Subscription: tx.Subscription,
Message: msg,
}); err != nil {
Expand All @@ -94,16 +94,16 @@ func convertPubSub(ctx context.Context, msg *cepubsub.Message, sendMode ModeType
return &event, nil
}

// PushMessage represents the format Pub/Sub uses to push events.
type PushMessage struct {
// pushMessage represents the format Pub/Sub uses to push events.
type pushMessage struct {
// Subscription is the subscription ID that received this Message.
Subscription string `json:"subscription"`
// Message holds the Pub/Sub message contents.
Message *PubSubMessage `json:"message,omitempty"`
Message *pubSubMessage `json:"message,omitempty"`
}

// PubSubMessage matches the inner message format used by Push Subscriptions.
type PubSubMessage struct {
type pubSubMessage struct {
// ID identifies this message. This ID is assigned by the server and is
// populated for Messages obtained from a subscription.
// This field is read-only.
Expand Down
28 changes: 24 additions & 4 deletions pkg/pubsub/adapter/converters/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestConvertCloudPubSub(t *testing.T) {
return pubSubCloudEvent(map[string]string{
"attribute1": "value1",
"attribute2": "value2",
})
}, "")
},
}, {
name: "upper case attributes",
Expand All @@ -68,7 +68,7 @@ func TestConvertCloudPubSub(t *testing.T) {
return pubSubCloudEvent(map[string]string{
"attribute1": "value1",
"attribute2": "value2",
})
}, "")
},
}, {
name: "only setting valid alphanumeric attribute",
Expand All @@ -83,7 +83,24 @@ func TestConvertCloudPubSub(t *testing.T) {
wantEventFn: func() *cloudevents.Event {
return pubSubCloudEvent(map[string]string{
"attribute1": "value1",
})
}, "")
},
}, {
name: "schema as attribute",
message: &cepubsub.Message{
Data: []byte("test data"),
Attributes: map[string]string{
"attribute1": "value1",
"attribute2": "value2",
"schema": "schema_val",
},
},
sendMode: Binary,
wantEventFn: func() *cloudevents.Event {
return pubSubCloudEvent(map[string]string{
"attribute1": "value1",
"attribute2": "value2",
}, "schema_val")
},
}, {
name: "Push mode with non valid alphanumeric attribute",
Expand Down Expand Up @@ -139,7 +156,7 @@ func TestConvertCloudPubSub(t *testing.T) {
}
}

func pubSubCloudEvent(extensions map[string]string) *cloudevents.Event {
func pubSubCloudEvent(extensions map[string]string, schema string) *cloudevents.Event {
e := cloudevents.NewEvent(cloudevents.VersionV1)
e.SetID("id")
e.SetSource(v1alpha1.CloudPubSubSourceEventSource("testproject", "testtopic"))
Expand All @@ -151,6 +168,9 @@ func pubSubCloudEvent(extensions map[string]string) *cloudevents.Event {
for k, v := range extensions {
e.SetExtension(k, v)
}
if schema != "" {
e.SetDataSchema(schema)
}
return &e
}

Expand Down

0 comments on commit abf2bb3

Please sign in to comment.