forked from mostafa/xk6-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avro_test.go
107 lines (100 loc) · 3.07 KB
/
avro_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package kafka
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestSerializeAvro serializes a JSON object into Avro binary.
func TestSerializeAvro(t *testing.T) {
avroSerde := &AvroSerde{}
expected := []byte{0x0a, 0x76, 0x61, 0x6c, 0x75, 0x65}
schema := &Schema{
ID: 2,
Schema: `{
"type":"record",
"name":"Schema",
"namespace":"io.confluent.kafka.avro",
"fields":[{"name":"key","type":"string"}]}`,
Version: 1,
Subject: "avro-schema",
}
actual, err := avroSerde.Serialize(map[string]interface{}{"key": "value"}, schema)
assert.Nil(t, err)
assert.Equal(t, expected, actual)
}
// TestSerializeAvroFailsOnInvalidDataType tests the serialization of a JSON object
// into Avro binary and fails on invalid data type.
func TestSerializeAvroFailsOnInvalidDataType(t *testing.T) {
avroSerde := &AvroSerde{}
schema := &Schema{
ID: 2,
Schema: `{
"type":"record",
"name":"Schema",
"namespace":"io.confluent.kafka.avro",
"fields":[{"name":"key","type":"string"}]}`,
Version: 1,
Subject: "avro-schema",
}
actual, err := avroSerde.Serialize(`{"key":"value"}`, schema)
assert.Nil(t, actual)
assert.NotNil(t, err)
assert.Equal(t, ErrInvalidDataType, err)
}
// TestSerializeAvroFailsOnValidation tests the serialization of a JSON object
// into Avro binary and fails on schema validation error.
func TestSerializeAvroFailsOnValidation(t *testing.T) {
avroSerde := &AvroSerde{}
schema := &Schema{
ID: 2,
Schema: `{
"type":"record",
"name":"Schema",
"namespace":"io.confluent.kafka.avro",
"fields":[{"name":"key","type":"string"}]}`,
Version: 1,
Subject: "avro-schema",
}
actual, err := avroSerde.Serialize(map[string]interface{}{"value": "key"}, schema)
assert.Nil(t, actual)
assert.NotNil(t, err)
assert.Equal(t, "Failed to encode data", err.Message)
assert.Equal(t, failedToEncode, err.Code)
}
// TestDeserializeAvro tests the deserialization of a JSON object from Avro binary.
func TestDeserializeAvro(t *testing.T) {
avroSerde := &AvroSerde{}
schema := &Schema{
ID: 2,
Schema: `{
"type":"record",
"name":"Schema",
"namespace":"io.confluent.kafka.avro",
"fields":[{"name":"key","type":"string"}]}`,
Version: 1,
Subject: "avro-schema",
}
expected := map[string]interface{}{"key": "value"}
actual, err := avroSerde.Deserialize([]byte{0x0a, 0x76, 0x61, 0x6c, 0x75, 0x65}, schema)
assert.Nil(t, err)
assert.Equal(t, expected, actual)
}
// TestDeserializeFailsOnDecodingBinary tests the deserialization of a JSON object
// from Avro binary and fails on decoding the received binary (invalid data).
func TestDeserializeFailsOnDecodingBinary(t *testing.T) {
avroSerde := &AvroSerde{}
schema := &Schema{
ID: 2,
Schema: `{
"type":"record",
"name":"Schema",
"namespace":"io.confluent.kafka.avro",
"fields":[{"name":"key","type":"string"}]}`,
Version: 1,
Subject: "avro-schema",
}
actual, err := avroSerde.Deserialize([]byte{0x76, 0x61, 0x6c, 0x75, 0x65}, schema)
assert.Nil(t, actual)
assert.NotNil(t, err)
assert.Equal(t, "Failed to decode data", err.Message)
assert.Equal(t, failedToDecodeFromBinary, err.Code)
}