Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e15d03d

Browse files
authored
add assertion helper for extension keys (#920)
* add assertion helper for extension keys Signed-off-by: myan <[email protected]> * fixed the fmt issue Signed-off-by: myan <[email protected]> * avoid race condition Signed-off-by: myan <[email protected]> * wording Signed-off-by: myan <[email protected]> --------- Signed-off-by: myan <[email protected]>
1 parent c1482af commit e15d03d

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

test/integration/kafka_sarama/kafka_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,11 @@ func TestSendEvent(t *testing.T) {
3535
clienttest.SendReceive(t, func() interface{} {
3636
return protocolFactory(t)
3737
}, eventIn, func(eventOut event.Event) {
38-
eventOut = test.ConvertEventExtensionsToString(t, eventOut)
39-
40-
require.Equal(t, TopicName, eventOut.Extensions()[KAFKA_TOPIC])
41-
require.NotNil(t, eventOut.Extensions()[KAFKA_PARTITION])
42-
require.NotNil(t, eventOut.Extensions()[KAFKA_OFFSET])
43-
4438
test.AllOf(
4539
test.HasExactlyAttributesEqualTo(eventIn.Context),
4640
test.HasData(eventIn.Data()),
41+
test.HasExtensionKeys([]string{KAFKA_OFFSET, KAFKA_PARTITION}),
42+
test.HasExtension(KAFKA_TOPIC, TopicName),
4743
)
4844
})
4945
})

test/integration/kafka_sarama_binding/kafka_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,12 @@ func TestSendBinaryMessageToBinary(t *testing.T) {
5656

5757
in := MustCreateMockBinaryMessage(eventIn)
5858
test.SendReceive(t, binding.WithPreferredEventEncoding(context.TODO(), binding.EncodingBinary), in, s, r, func(out binding.Message) {
59-
eventOut := MustToEvent(t, context.Background(), out)
6059
assert.Equal(t, binding.EncodingBinary, out.ReadEncoding())
61-
eventOut = ConvertEventExtensionsToString(t, eventOut)
62-
63-
require.Equal(t, topicName, eventOut.Extensions()[KAFKA_TOPIC])
64-
require.NotNil(t, eventOut.Extensions()[KAFKA_PARTITION])
65-
require.NotNil(t, eventOut.Extensions()[KAFKA_OFFSET])
66-
6760
AllOf(
6861
HasExactlyAttributesEqualTo(eventIn.Context),
6962
HasData(eventIn.Data()),
63+
HasExtensionKeys([]string{KAFKA_OFFSET, KAFKA_PARTITION}),
64+
HasExtension(KAFKA_TOPIC, topicName),
7065
)
7166
})
7267
})

v2/test/event_matchers.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ func HasExtensions(ext map[string]interface{}) EventMatcher {
184184
}
185185
}
186186

187+
// HasExtensionKeys checks if the event contains the provided keys from its extensions
188+
func HasExtensionKeys(keys []string) EventMatcher {
189+
return func(have event.Event) error {
190+
for _, k := range keys {
191+
if _, ok := have.Extensions()[k]; !ok {
192+
return fmt.Errorf("expecting extension key %q", k)
193+
}
194+
}
195+
return nil
196+
}
197+
}
198+
187199
// HasExtension checks if the event contains the provided extension
188200
func HasExtension(key string, value interface{}) EventMatcher {
189201
return HasExtensions(map[string]interface{}{key: value})
@@ -277,7 +289,6 @@ func HasAttributeKind(kind spec.Kind, value interface{}) EventMatcher {
277289
// LICENSE: MIT License
278290

279291
func isEmpty(object interface{}) bool {
280-
281292
// get nil case out of the way
282293
if object == nil {
283294
return true

0 commit comments

Comments
 (0)