forked from linkedin/goavro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canonical_test.go
267 lines (258 loc) · 8.89 KB
/
canonical_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright [2019] LinkedIn Corp. Licensed under the Apache License, Version
// 2.0 (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
package goavro
import (
"testing"
)
func TestCanonicalSchema(t *testing.T) {
// Test cases are taken from the reference implementation here:
// https://github.com/apache/avro/blob/master/share/test/data/schema-tests.txt
cases := []struct {
Schema string
Canonical string
}{
{
Schema: `"null"`,
Canonical: `"null"`,
},
{
Schema: `{"type":"null"}`,
Canonical: `"null"`,
},
{
Schema: `"boolean"`,
Canonical: `"boolean"`,
},
{
Schema: `{"type":"boolean"}`,
Canonical: `"boolean"`,
},
{
Schema: `"int"`,
Canonical: `"int"`,
},
{
Schema: `{"type":"int"}`,
Canonical: `"int"`,
},
{
Schema: `"long"`,
Canonical: `"long"`,
},
{
Schema: `{"type":"long"}`,
Canonical: `"long"`,
},
{
Schema: `"float"`,
Canonical: `"float"`,
},
{
Schema: `{"type":"float"}`,
Canonical: `"float"`,
},
{
Schema: `"double"`,
Canonical: `"double"`,
},
{
Schema: `{"type":"double"}`,
Canonical: `"double"`,
},
{
Schema: `"bytes"`,
Canonical: `"bytes"`,
},
{
Schema: `{"type":"bytes"}`,
Canonical: `"bytes"`,
},
{
Schema: `"string"`,
Canonical: `"string"`,
},
{
Schema: `{"type":"string"}`,
Canonical: `"string"`,
},
/*
// Supported by the reference implementation but not by goavro at this point
{
Schema: "[ ]",
Canonical: "[]",
},
*/
{
Schema: `[ "int" ]`,
Canonical: `["int"]`,
},
{
Schema: `[ "int" , {"type":"boolean"} ]`,
Canonical: `["int","boolean"]`,
},
// The following 7 test cases differ from the reference implementation since goavro doesn't
// currently support empty fields array. A field name "dummy" is added since these tests are
// testing other aspects of canonicalization than empty field array.
{
Schema: `{"fields":[{"name":"dummy","type":"int"}], "type":"record", "name":"foo"}`,
Canonical: `{"name":"foo","type":"record","fields":[{"name":"dummy","type":"int"}]}`,
},
{
Schema: `{"fields":[{"name":"dummy","type":"int"}], "type":"record", "name":"foo", "namespace":"x.y"}`,
Canonical: `{"name":"x.y.foo","type":"record","fields":[{"name":"dummy","type":"int"}]}`,
},
{
Schema: `{"fields":[{"name":"dummy","type":"int"}], "type":"record", "name":"foo", "namespace":"x.y"}`,
Canonical: `{"name":"x.y.foo","type":"record","fields":[{"name":"dummy","type":"int"}]}`,
},
{
Schema: `{"fields":[{"name":"dummy","type":"int"}], "type":"record", "name":"a.b.foo", "namespace":"x.y"}`,
Canonical: `{"name":"a.b.foo","type":"record","fields":[{"name":"dummy","type":"int"}]}`,
},
{
Schema: `{"fields":[{"name":"dummy","type":"int"}], "type":"record", "name":"foo", "doc":"Useful info"}`,
Canonical: `{"name":"foo","type":"record","fields":[{"name":"dummy","type":"int"}]}`,
},
{
Schema: `{"fields":[{"name":"dummy","type":"int"}], "type":"record", "name":"foo", "aliases":["foo","bar"]}`,
Canonical: `{"name":"foo","type":"record","fields":[{"name":"dummy","type":"int"}]}`,
},
{
Schema: `{"fields":[{"name":"dummy","type":"int"}], "type":"record", "name":"foo", "doc":"foo", "aliases":["foo","bar"]}`,
Canonical: `{"name":"foo","type":"record","fields":[{"name":"dummy","type":"int"}]}`,
},
{
Schema: `{"fields":[{"type":{"type":"boolean"}, "name":"f1"}], "type":"record", "name":"foo"}`,
Canonical: `{"name":"foo","type":"record","fields":[{"name":"f1","type":"boolean"}]}`,
},
{
Schema: `{"fields": [
{"type": "boolean", "aliases": [], "name": "f1", "default": true},
{"order": "descending", "name": "f2", "doc": "Hello", "type": "int"}
],
"type": "record",
"name": "foo"}`,
Canonical: `{"name":"foo","type":"record","fields":[{"name":"f1","type":"boolean"},{"name":"f2","type":"int"}]}`,
},
{
Schema: `{"type":"enum", "name":"foo", "symbols":["A1"]}`,
Canonical: `{"name":"foo","type":"enum","symbols":["A1"]}`,
},
{
Schema: `{"namespace":"x.y.z", "type":"enum", "name":"foo", "doc":"foo bar", "symbols":["A1", "A2"]}`,
Canonical: `{"name":"x.y.z.foo","type":"enum","symbols":["A1","A2"]}`,
},
{
Schema: `{"name":"foo","type":"fixed","size":15}`,
Canonical: `{"name":"foo","type":"fixed","size":15}`,
},
{
Schema: `{"namespace":"x.y.z", "type":"fixed", "name":"foo", "doc":"foo bar", "size":32}`,
Canonical: `{"name":"x.y.z.foo","type":"fixed","size":32}`,
},
{
Schema: `{ "items":{"type":"null"}, "type":"array"}`,
Canonical: `{"type":"array","items":"null"}`,
},
{
Schema: `{ "values":"string", "type":"map"}`,
Canonical: `{"type":"map","values":"string"}`,
},
{
Schema: ` {"name":"PigValue","type":"record",
"fields":[{"name":"value", "type":["null", "int", "long", "PigValue"]}]}`,
Canonical: `{"name":"PigValue","type":"record","fields":[{"name":"value","type":["null","int","long","PigValue"]}]}`,
},
// [INTEGERS] Eliminate quotes around and any leading zeros in front of
// JSON integer literals (which appear in the size attributes of fixed
// schemas).
{
Schema: `{"size":"15","type":"fixed","name":"foo"}`,
Canonical: `{"name":"foo","type":"fixed","size":15}`,
},
// [STRINGS] For all JSON string literals in the schema text, replace
// any escaped characters (e.g., \uXXXX escapes) with their UTF-8
// equivalents.
{
// primitive
Schema: `"\u0069\u006e\u0074"`,
Canonical: `"int"`,
},
{
// primitive wrapped in JSON object
Schema: `{"type":"\u0069\u006e\u0074"}`,
Canonical: `"int"`,
},
{
// array items
Schema: `{"type":"array","items":"\u0069\u006e\u0074"}`,
Canonical: `{"type":"array","items":"int"}`,
},
{
// enum symbols
Schema: `{"type":"enum","symbols":["\u0047\u006f","\u0041\u0076\u0072\u006f"],"name":"\u0046\u006f\u006f"}`,
Canonical: `{"name":"Foo","type":"enum","symbols":["Go","Avro"]}`,
},
{
// fixed name
Schema: `{"size":16,"type":"fixed","name":"\u0046\u006f\u006f"}`,
Canonical: `{"name":"Foo","type":"fixed","size":16}`,
},
{
// map values
Schema: `{"values":"\u0069\u006e\u0074","type":"map"}`,
Canonical: `{"type":"map","values":"int"}`,
},
{
// record name
Schema: `{"fields":[{"name":"hi","type":"int"}], "type":"record", "name":"\u0046\u006f\u006f", "namespace":"x.y"}`,
Canonical: `{"name":"x.y.Foo","type":"record","fields":[{"name":"hi","type":"int"}]}`,
},
{
// record namespace
Schema: `{"fields":[{"name":"hi","type":"int"}], "type":"record", "name":"Foo", "namespace":"\u0078\u002e\u0079"}`,
Canonical: `{"name":"x.y.Foo","type":"record","fields":[{"name":"hi","type":"int"}]}`,
},
{
// record field name
Schema: `{"fields":[{"name":"\u0068\u0069","type":"int"}], "type":"record", "name":"Foo", "namespace":"x.y"}`,
Canonical: `{"name":"x.y.Foo","type":"record","fields":[{"name":"hi","type":"int"}]}`,
},
{
// record field type
Schema: `{"fields":[{"name":"hi","type":"\u0069\u006e\u0074"}], "type":"record", "name":"Foo", "namespace":"x.y"}`,
Canonical: `{"name":"x.y.Foo","type":"record","fields":[{"name":"hi","type":"int"}]}`,
},
{
// union children
Schema: `["\u006e\u0075\u006c\u006c","\u0069\u006e\u0074"]`,
Canonical: `["null","int"]`,
},
{
// propagate namespace "bar" to subtype "baz", i.e., "bar.baz"
Schema: `{"type":"record","name":"foo","namespace":"bar","fields":[{"type":"record","name":"baz","fields":[{"name":"hi","type":"int"}]}]}`,
Canonical: `{"name":"bar.foo","type":"record","fields":[{"name":"bar.baz","type":"record","fields":[{"name":"hi","type":"int"}]}]}`,
},
{
// replace the second reference to type "baz" in bye to "bar.baz"
Schema: `{"type":"record","name":"foo","namespace":"bar","fields":[{"type":"record","name":"baz","fields":[{"name":"hi","type":"int"}]},{"name":"bye", "type":["null","baz"]}]}`,
Canonical: `{"name":"bar.foo","type":"record","fields":[{"name":"bar.baz","type":"record","fields":[{"name":"hi","type":"int"}]},{"name":"bye","type":["null","bar.baz"]}]}`,
},
}
for _, c := range cases {
codec, err := NewCodec(c.Schema)
if err != nil {
t.Errorf("Unable to create codec for schema: %s\nwith error: %s", c.Schema, err)
} else {
if got, want := codec.CanonicalSchema(), c.Canonical; got != want {
t.Errorf("Test failed for schema: %s\n\tgot canonical:\t\t%s\n\texpected canonical:\t%s", c.Schema, got, want)
}
}
}
}