Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add assertion helper for extension keys #920

Merged
merged 4 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions test/integration/kafka_sarama/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ func TestSendEvent(t *testing.T) {
clienttest.SendReceive(t, func() interface{} {
return protocolFactory(t)
}, eventIn, func(eventOut event.Event) {
eventOut = test.ConvertEventExtensionsToString(t, eventOut)

require.Equal(t, TopicName, eventOut.Extensions()[KAFKA_TOPIC])
require.NotNil(t, eventOut.Extensions()[KAFKA_PARTITION])
require.NotNil(t, eventOut.Extensions()[KAFKA_OFFSET])

test.AllOf(
test.HasExactlyAttributesEqualTo(eventIn.Context),
test.HasData(eventIn.Data()),
test.HasExtensionKeys([]string{KAFKA_OFFSET, KAFKA_PARTITION}),
test.HasExtension(KAFKA_TOPIC, TopicName),
)
})
})
Expand Down
9 changes: 2 additions & 7 deletions test/integration/kafka_sarama_binding/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,12 @@ func TestSendBinaryMessageToBinary(t *testing.T) {

in := MustCreateMockBinaryMessage(eventIn)
test.SendReceive(t, binding.WithPreferredEventEncoding(context.TODO(), binding.EncodingBinary), in, s, r, func(out binding.Message) {
eventOut := MustToEvent(t, context.Background(), out)
assert.Equal(t, binding.EncodingBinary, out.ReadEncoding())
eventOut = ConvertEventExtensionsToString(t, eventOut)

require.Equal(t, topicName, eventOut.Extensions()[KAFKA_TOPIC])
require.NotNil(t, eventOut.Extensions()[KAFKA_PARTITION])
require.NotNil(t, eventOut.Extensions()[KAFKA_OFFSET])

AllOf(
HasExactlyAttributesEqualTo(eventIn.Context),
HasData(eventIn.Data()),
HasExtensionKeys([]string{KAFKA_OFFSET, KAFKA_PARTITION}),
HasExtension(KAFKA_TOPIC, topicName),
)
})
})
Expand Down
13 changes: 12 additions & 1 deletion v2/test/event_matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ func HasExtensions(ext map[string]interface{}) EventMatcher {
}
}

// HasExtensionKeys checks if the event contains the provided keys from its extensions
func HasExtensionKeys(keys []string) EventMatcher {
return func(have event.Event) error {
for _, k := range keys {
if _, ok := have.Extensions()[k]; !ok {
return fmt.Errorf("expecting extension key %q", k)
}
}
return nil
}
}

// HasExtension checks if the event contains the provided extension
func HasExtension(key string, value interface{}) EventMatcher {
return HasExtensions(map[string]interface{}{key: value})
Expand Down Expand Up @@ -277,7 +289,6 @@ func HasAttributeKind(kind spec.Kind, value interface{}) EventMatcher {
// LICENSE: MIT License

func isEmpty(object interface{}) bool {

// get nil case out of the way
if object == nil {
return true
Expand Down