Skip to content

Commit

Permalink
add assertion helper for extension keys
Browse files Browse the repository at this point in the history
Signed-off-by: myan <myan@redhat.com>
  • Loading branch information
yanmxa committed Jul 14, 2023
1 parent fdcb2d2 commit 91d4c02
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
4 changes: 1 addition & 3 deletions test/integration/kafka_sarama/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ func TestSendEvent(t *testing.T) {
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}),
)
})
})
Expand Down
4 changes: 1 addition & 3 deletions test/integration/kafka_sarama_binding/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ func TestSendBinaryMessageToBinary(t *testing.T) {
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}),
)
})
})
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 contain 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

0 comments on commit 91d4c02

Please sign in to comment.