# 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 # # https://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 TestIO < Test::Unit::TestCase DATAFILE = 'tmp/test.rb.avro' Schema = Avro::Schema def test_null check('"null"') check_default('"null"', "null", nil) end def test_boolean check('"boolean"') check_default('"boolean"', "true", true) check_default('"boolean"', "false", false) end def test_string check('"string"') check_default('"string"', '"foo"', "foo") end def test_bytes check('"bytes"') check_default('"bytes"', '"foo"', "foo") end def test_int check('"int"') check_default('"int"', "5", 5) end def test_long check('"long"') check_default('"long"', "9", 9) end def test_float check('"float"') check_default('"float"', "1.2", 1.2) end def test_double check('"double"') check_default('"double"', "1.2", 1.2) end def test_array array_schema = '{"type": "array", "items": "long"}' check(array_schema) check_default(array_schema, "[1]", [1]) end def test_map map_schema = '{"type": "map", "values": "long"}' check(map_schema) check_default(map_schema, '{"a": 1}', {"a" => 1}) end def test_record record_schema = < 11}) end def test_record_with_logical_type record_schema = < "boom"}) end def test_enum enum_schema = '{"type": "enum", "name": "Test","symbols": ["A", "B"]}' check(enum_schema) check_default(enum_schema, '"B"', "B") end def test_recursive recursive_schema = < 1, 'bar' => 2 } writer, * = write_datum(datum_to_write, writers_schema) datum_read = read_datum(writer, writers_schema, readers_schema) assert_equal(datum_read, datum_to_write) end def test_snappy_backward_compat # a snappy-compressed block payload without the checksum # this has no back-references, just one literal so the last 9 # bytes are the uncompressed payload. old_snappy_bytes = "\x09\x20\x02\x06\x02\x0a\x67\x72\x65\x65\x6e" uncompressed_bytes = "\x02\x06\x02\x0a\x67\x72\x65\x65\x6e" snappy = Avro::DataFile::SnappyCodec.new assert_equal(uncompressed_bytes, snappy.decompress(old_snappy_bytes)) end private def check_no_default(schema_json) actual_schema = '{"type": "record", "name": "Foo", "fields": []}' actual = Avro::Schema.parse(actual_schema) expected_schema = <