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
|
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'test_help'
class TestSchema < Test::Unit::TestCase
def test_default_namespace
schema = Avro::Schema.parse <<-SCHEMA
{"type": "record", "name": "OuterRecord", "fields": [
{"name": "field1", "type": {
"type": "record", "name": "InnerRecord", "fields": []
}},
{"name": "field2", "type": "InnerRecord"}
]}
SCHEMA
assert_equal schema.name, 'OuterRecord'
assert_equal schema.fullname, 'OuterRecord'
assert_nil schema.namespace
schema.fields.each do |field|
assert_equal field.type.name, 'InnerRecord'
assert_equal field.type.fullname, 'InnerRecord'
assert_nil field.type.namespace
end
end
def test_inherited_namespace
schema = Avro::Schema.parse <<-SCHEMA
{"type": "record", "name": "OuterRecord", "namespace": "my.name.space",
"fields": [
{"name": "definition", "type": {
"type": "record", "name": "InnerRecord", "fields": []
}},
{"name": "relativeReference", "type": "InnerRecord"},
{"name": "absoluteReference", "type": "my.name.space.InnerRecord"}
]}
SCHEMA
assert_equal schema.name, 'OuterRecord'
assert_equal schema.fullname, 'my.name.space.OuterRecord'
assert_equal schema.namespace, 'my.name.space'
schema.fields.each do |field|
assert_equal field.type.name, 'InnerRecord'
assert_equal field.type.fullname, 'my.name.space.InnerRecord'
assert_equal field.type.namespace, 'my.name.space'
end
end
def test_inherited_namespace_from_dotted_name
schema = Avro::Schema.parse <<-SCHEMA
{"type": "record", "name": "my.name.space.OuterRecord", "fields": [
{"name": "definition", "type": {
"type": "enum", "name": "InnerEnum", "symbols": ["HELLO", "WORLD"]
}},
{"name": "relativeReference", "type": "InnerEnum"},
{"name": "absoluteReference", "type": "my.name.space.InnerEnum"}
]}
SCHEMA
assert_equal schema.name, 'OuterRecord'
assert_equal schema.fullname, 'my.name.space.OuterRecord'
assert_equal schema.namespace, 'my.name.space'
schema.fields.each do |field|
assert_equal field.type.name, 'InnerEnum'
assert_equal field.type.fullname, 'my.name.space.InnerEnum'
assert_equal field.type.namespace, 'my.name.space'
end
end
def test_nested_namespaces
schema = Avro::Schema.parse <<-SCHEMA
{"type": "record", "name": "outer.OuterRecord", "fields": [
{"name": "middle", "type": {
"type": "record", "name": "middle.MiddleRecord", "fields": [
{"name": "inner", "type": {
"type": "record", "name": "InnerRecord", "fields": [
{"name": "recursive", "type": "MiddleRecord"}
]
}}
]
}}
]}
SCHEMA
assert_equal schema.name, 'OuterRecord'
assert_equal schema.fullname, 'outer.OuterRecord'
assert_equal schema.namespace, 'outer'
middle = schema.fields.first.type
assert_equal middle.name, 'MiddleRecord'
assert_equal middle.fullname, 'middle.MiddleRecord'
assert_equal middle.namespace, 'middle'
inner = middle.fields.first.type
assert_equal inner.name, 'InnerRecord'
assert_equal inner.fullname, 'middle.InnerRecord'
assert_equal inner.namespace, 'middle'
assert_equal inner.fields.first.type, middle
end
def test_to_avro_includes_namespaces
schema = Avro::Schema.parse <<-SCHEMA
{"type": "record", "name": "my.name.space.OuterRecord", "fields": [
{"name": "definition", "type": {
"type": "fixed", "name": "InnerFixed", "size": 16
}},
{"name": "reference", "type": "InnerFixed"}
]}
SCHEMA
assert_equal schema.to_avro, {
'type' => 'record', 'name' => 'OuterRecord', 'namespace' => 'my.name.space',
'fields' => [
{'name' => 'definition', 'type' => {
'type' => 'fixed', 'name' => 'InnerFixed', 'namespace' => 'my.name.space',
'size' => 16
}},
{'name' => 'reference', 'type' => 'my.name.space.InnerFixed'}
]
}
end
def test_unknown_named_type
error = assert_raise Avro::UnknownSchemaError do
Avro::Schema.parse <<-SCHEMA
{"type": "record", "name": "my.name.space.Record", "fields": [
{"name": "reference", "type": "MissingType"}
]}
SCHEMA
end
assert_equal '"MissingType" is not a schema we know about.', error.message
end
def test_to_avro_handles_falsey_defaults
schema = Avro::Schema.parse <<-SCHEMA
{"type": "record", "name": "Record", "namespace": "my.name.space",
"fields": [
{"name": "is_usable", "type": "boolean", "default": false}
]
}
SCHEMA
assert_equal schema.to_avro, {
'type' => 'record', 'name' => 'Record', 'namespace' => 'my.name.space',
'fields' => [
{'name' => 'is_usable', 'type' => 'boolean', 'default' => false}
]
}
end
end
|