Skip to content

Commit

Permalink
Improved test suite for bindings (#323)
Browse files Browse the repository at this point in the history
* Changed UpdateAttribute signature to support failures

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Changed signature of RunTranscoderTest + made public CopyEventContext

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Moved RunTranscoderTest

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Fixed wrong stuff in tests

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Now MockBinaryMessage and MockStructuredMessage implements BinaryEncoder and StructuredEncoder

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
  • Loading branch information
slinkydeveloper authored Mar 2, 2020
1 parent b72bc2a commit bb1235c
Show file tree
Hide file tree
Showing 10 changed files with 338 additions and 278 deletions.
28 changes: 28 additions & 0 deletions pkg/binding/test/mock_binary_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package test
import (
"bytes"
"context"
"io"
"io/ioutil"

cloudevents "github.com/cloudevents/sdk-go"
"github.com/cloudevents/sdk-go/pkg/binding"
Expand All @@ -16,6 +18,31 @@ type MockBinaryMessage struct {
Body []byte
}

func (bm *MockBinaryMessage) Start(ctx context.Context) error {
bm.Metadata = make(map[spec.Attribute]interface{})
bm.Extensions = make(map[string]interface{})
return nil
}

func (bm *MockBinaryMessage) SetAttribute(attribute spec.Attribute, value interface{}) error {
bm.Metadata[attribute] = value
return nil
}

func (bm *MockBinaryMessage) SetExtension(name string, value interface{}) error {
bm.Extensions[name] = value
return nil
}

func (bm *MockBinaryMessage) SetData(data io.Reader) (err error) {
bm.Body, err = ioutil.ReadAll(data)
return err
}

func (bm *MockBinaryMessage) End() error {
return nil
}

var versions = spec.New()

func NewMockBinaryMessage(e cloudevents.Event) binding.Message {
Expand Down Expand Up @@ -89,3 +116,4 @@ func (bm *MockBinaryMessage) Encoding() binding.Encoding {
func (bm *MockBinaryMessage) Finish(error) error { return nil }

var _ binding.Message = (*MockBinaryMessage)(nil) // Test it conforms to the interface
var _ binding.BinaryEncoder = (*MockBinaryMessage)(nil)
11 changes: 10 additions & 1 deletion pkg/binding/test/mock_structured_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package test
import (
"bytes"
"context"
"io"
"io/ioutil"

cloudevents "github.com/cloudevents/sdk-go"
"github.com/cloudevents/sdk-go/pkg/binding"
Expand All @@ -15,7 +17,13 @@ type MockStructuredMessage struct {
Bytes []byte
}

func (s *MockStructuredMessage) GetParent() binding.Message {
func (s *MockStructuredMessage) SetStructuredEvent(ctx context.Context, format format.Format, event io.Reader) (err error) {
s.Format = format
s.Bytes, err = ioutil.ReadAll(event)
if err != nil {
return
}

return nil
}

Expand Down Expand Up @@ -45,3 +53,4 @@ func (bm *MockStructuredMessage) Encoding() binding.Encoding {
func (s *MockStructuredMessage) Finish(error) error { return nil }

var _ binding.Message = (*MockStructuredMessage)(nil) // Test it conforms to the interface
var _ binding.StructuredEncoder = (*MockStructuredMessage)(nil)
9 changes: 9 additions & 0 deletions pkg/binding/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,12 @@ func MustToEvent(ctx context.Context, m binding.Message) (e ce.Event, encoding b
}
return
}

func CopyEventContext(e ce.Event) ce.Event {
newE := ce.Event{}
newE.Context = e.Context.Clone()
newE.DataEncoded = e.DataEncoded
newE.Data = e.Data
newE.DataBinary = e.DataBinary
return newE
}
45 changes: 45 additions & 0 deletions pkg/binding/test/transcoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package test

import (
"context"
"testing"

"github.com/stretchr/testify/require"

cloudevents "github.com/cloudevents/sdk-go"
"github.com/cloudevents/sdk-go/pkg/binding"
)

type TranscoderTestArgs struct {
Name string
InputMessage binding.Message
WantEvent cloudevents.Event
Transformers []binding.TransformerFactory
}

func RunTranscoderTests(t *testing.T, ctx context.Context, tests []TranscoderTestArgs) {
for _, tt := range tests {
tt := tt // Don't use range variable inside scope
t.Run(tt.Name, func(t *testing.T) {

mockStructured := MockStructuredMessage{}
mockBinary := MockBinaryMessage{}

enc, err := binding.Encode(ctx, tt.InputMessage, &mockStructured, &mockBinary, tt.Transformers)
require.NoError(t, err)

var e cloudevents.Event
if enc == binding.EncodingStructured {
e, _, err = binding.ToEvent(ctx, &mockStructured)
require.NoError(t, err)
} else if enc == binding.EncodingBinary {
e, _, err = binding.ToEvent(ctx, &mockBinary)
require.NoError(t, err)
} else {
t.Fatalf("Unexpected encoding %v", enc)
}
require.NoError(t, err)
AssertEventEquals(t, tt.WantEvent, e)
})
}
}
130 changes: 65 additions & 65 deletions pkg/binding/transcoder/add_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,68 +18,68 @@ func TestAddAttribute(t *testing.T) {
e.Context = e.Context.AsV1()

subject := "aaa"
expectedEventWithSubject := copyEventContext(e)
expectedEventWithSubject := test.CopyEventContext(e)
require.NoError(t, expectedEventWithSubject.Context.SetSubject(subject))

timestamp, err := types.ToTime(time.Now())
require.NoError(t, err)
expectedEventWithTime := copyEventContext(e)
expectedEventWithTime := test.CopyEventContext(e)
require.NoError(t, expectedEventWithTime.Context.SetTime(timestamp))

RunTranscoderTests(t, context.Background(), []TranscoderTestArgs{
test.RunTranscoderTests(t, context.Background(), []test.TranscoderTestArgs{
{
name: "No change to id to Mock Structured message",
inputMessage: test.NewMockStructuredMessage(copyEventContext(e)),
wantEvent: copyEventContext(e),
transformer: AddAttribute(spec.ID, "new-id"),
Name: "No change to id to Mock Structured message",
InputMessage: test.NewMockStructuredMessage(test.CopyEventContext(e)),
WantEvent: test.CopyEventContext(e),
Transformers: binding.TransformerFactories{AddAttribute(spec.ID, "new-id")},
},
{
name: "No change to id to Mock Binary message",
inputMessage: test.NewMockBinaryMessage(copyEventContext(e)),
wantEvent: copyEventContext(e),
transformer: AddAttribute(spec.ID, "new-id"),
Name: "No change to id to Mock Binary message",
InputMessage: test.NewMockBinaryMessage(test.CopyEventContext(e)),
WantEvent: test.CopyEventContext(e),
Transformers: binding.TransformerFactories{AddAttribute(spec.ID, "new-id")},
},
{
name: "No change to id to Event message",
inputMessage: binding.EventMessage(copyEventContext(e)),
wantEvent: copyEventContext(e),
transformer: AddAttribute(spec.ID, "new-id"),
Name: "No change to id to Event message",
InputMessage: binding.EventMessage(test.CopyEventContext(e)),
WantEvent: test.CopyEventContext(e),
Transformers: binding.TransformerFactories{AddAttribute(spec.ID, "new-id")},
},
{
name: "Add subject to Mock Structured message",
inputMessage: test.NewMockStructuredMessage(copyEventContext(e)),
wantEvent: expectedEventWithSubject,
transformer: AddAttribute(spec.Subject, subject),
Name: "Add subject to Mock Structured message",
InputMessage: test.NewMockStructuredMessage(test.CopyEventContext(e)),
WantEvent: expectedEventWithSubject,
Transformers: binding.TransformerFactories{AddAttribute(spec.Subject, subject)},
},
{
name: "Add subject to Mock Binary message",
inputMessage: test.NewMockBinaryMessage(copyEventContext(e)),
wantEvent: expectedEventWithSubject,
transformer: AddAttribute(spec.Subject, subject),
Name: "Add subject to Mock Binary message",
InputMessage: test.NewMockBinaryMessage(test.CopyEventContext(e)),
WantEvent: expectedEventWithSubject,
Transformers: binding.TransformerFactories{AddAttribute(spec.Subject, subject)},
},
{
name: "Add subject to Event message",
inputMessage: binding.EventMessage(copyEventContext(e)),
wantEvent: expectedEventWithSubject,
transformer: AddAttribute(spec.Subject, subject),
Name: "Add subject to Event message",
InputMessage: binding.EventMessage(test.CopyEventContext(e)),
WantEvent: expectedEventWithSubject,
Transformers: binding.TransformerFactories{AddAttribute(spec.Subject, subject)},
},
{
name: "Add time to Mock Structured message",
inputMessage: test.NewMockStructuredMessage(copyEventContext(e)),
wantEvent: expectedEventWithTime,
transformer: AddAttribute(spec.Time, timestamp),
Name: "Add time to Mock Structured message",
InputMessage: test.NewMockStructuredMessage(test.CopyEventContext(e)),
WantEvent: expectedEventWithTime,
Transformers: binding.TransformerFactories{AddAttribute(spec.Time, timestamp)},
},
{
name: "Add time to Mock Binary message",
inputMessage: test.NewMockBinaryMessage(copyEventContext(e)),
wantEvent: expectedEventWithTime,
transformer: AddAttribute(spec.Time, timestamp),
Name: "Add time to Mock Binary message",
InputMessage: test.NewMockBinaryMessage(test.CopyEventContext(e)),
WantEvent: expectedEventWithTime,
Transformers: binding.TransformerFactories{AddAttribute(spec.Time, timestamp)},
},
{
name: "Add time to Event message",
inputMessage: binding.EventMessage(copyEventContext(e)),
wantEvent: expectedEventWithTime,
transformer: AddAttribute(spec.Time, timestamp),
Name: "Add time to Event message",
InputMessage: binding.EventMessage(test.CopyEventContext(e)),
WantEvent: expectedEventWithTime,
Transformers: binding.TransformerFactories{AddAttribute(spec.Time, timestamp)},
},
})
}
Expand All @@ -90,45 +90,45 @@ func TestAddExtension(t *testing.T) {

extName := "aaa"
extValue := "bbb"
expectedEventWithExtension := copyEventContext(e)
expectedEventWithExtension := test.CopyEventContext(e)
require.NoError(t, expectedEventWithExtension.Context.SetExtension(extName, extValue))

RunTranscoderTests(t, context.Background(), []TranscoderTestArgs{
test.RunTranscoderTests(t, context.Background(), []test.TranscoderTestArgs{
{
name: "No change to extension 'aaa' to Mock Structured message",
inputMessage: test.NewMockStructuredMessage(copyEventContext(expectedEventWithExtension)),
wantEvent: copyEventContext(expectedEventWithExtension),
transformer: AddExtension(extName, extValue),
Name: "No change to extension 'aaa' to Mock Structured message",
InputMessage: test.NewMockStructuredMessage(test.CopyEventContext(expectedEventWithExtension)),
WantEvent: test.CopyEventContext(expectedEventWithExtension),
Transformers: binding.TransformerFactories{AddExtension(extName, extValue)},
},
{
name: "No change to extension 'aaa' to Mock Binary message",
inputMessage: test.NewMockBinaryMessage(copyEventContext(expectedEventWithExtension)),
wantEvent: copyEventContext(expectedEventWithExtension),
transformer: AddExtension(extName, extValue),
Name: "No change to extension 'aaa' to Mock Binary message",
InputMessage: test.NewMockBinaryMessage(test.CopyEventContext(expectedEventWithExtension)),
WantEvent: test.CopyEventContext(expectedEventWithExtension),
Transformers: binding.TransformerFactories{AddExtension(extName, extValue)},
},
{
name: "No change to extension 'aaa' to Event message",
inputMessage: binding.EventMessage(copyEventContext(expectedEventWithExtension)),
wantEvent: copyEventContext(expectedEventWithExtension),
transformer: AddExtension(extName, extValue),
Name: "No change to extension 'aaa' to Event message",
InputMessage: binding.EventMessage(test.CopyEventContext(expectedEventWithExtension)),
WantEvent: test.CopyEventContext(expectedEventWithExtension),
Transformers: binding.TransformerFactories{AddExtension(extName, extValue)},
},
{
name: "Add extension 'aaa' to Mock Structured message",
inputMessage: test.NewMockStructuredMessage(copyEventContext(e)),
wantEvent: copyEventContext(expectedEventWithExtension),
transformer: AddExtension(extName, extValue),
Name: "Add extension 'aaa' to Mock Structured message",
InputMessage: test.NewMockStructuredMessage(test.CopyEventContext(e)),
WantEvent: test.CopyEventContext(expectedEventWithExtension),
Transformers: binding.TransformerFactories{AddExtension(extName, extValue)},
},
{
name: "Add extension 'aaa' to Mock Binary message",
inputMessage: test.NewMockBinaryMessage(copyEventContext(e)),
wantEvent: copyEventContext(expectedEventWithExtension),
transformer: AddExtension(extName, extValue),
Name: "Add extension 'aaa' to Mock Binary message",
InputMessage: test.NewMockBinaryMessage(test.CopyEventContext(e)),
WantEvent: test.CopyEventContext(expectedEventWithExtension),
Transformers: binding.TransformerFactories{AddExtension(extName, extValue)},
},
{
name: "Add extension 'aaa' to Event message",
inputMessage: binding.EventMessage(copyEventContext(e)),
wantEvent: copyEventContext(expectedEventWithExtension),
transformer: AddExtension(extName, extValue),
Name: "Add extension 'aaa' to Event message",
InputMessage: binding.EventMessage(test.CopyEventContext(e)),
WantEvent: test.CopyEventContext(expectedEventWithExtension),
Transformers: binding.TransformerFactories{AddExtension(extName, extValue)},
},
})
}
Loading

0 comments on commit bb1235c

Please sign in to comment.