test/test_io.rb in avro-1.7.7 vs test/test_io.rb in avro-1.8.0
- old
+ new
@@ -19,74 +19,87 @@
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 = <<EOS
{"type": "record",
"name": "Test",
"fields": [{"name": "f",
"type": "long"}]}
EOS
+ check(record_schema)
check_default(record_schema, '{"f": 11}', {"f" => 11})
end
def test_error
error_schema = <<EOS
{"type": "error",
"name": "TestError",
"fields": [{"name": "message",
"type": "string"}]}
EOS
+ check(error_schema)
check_default(error_schema, '{"message": "boom"}', {"message" => "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 = <<EOS
@@ -127,10 +140,11 @@
check(lisp_schema)
end
def test_fixed
fixed_schema = '{"type": "fixed", "name": "Test", "size": 1}'
+ check(fixed_schema)
check_default(fixed_schema, '"a"', "a")
end
def test_enum_with_duplicate
str = '{"type": "enum", "name": "Test","symbols" : ["AA", "AA"]}'
@@ -194,10 +208,55 @@
assert_equal hex_encoding, hex_val
end
end
+ def test_utf8_string_encoding
+ [
+ "\xC3".force_encoding('ISO-8859-1'),
+ "\xC3\x83".force_encoding('UTF-8')
+ ].each do |value|
+ output = ''.force_encoding('BINARY')
+ encoder = Avro::IO::BinaryEncoder.new(StringIO.new(output))
+ datum_writer = Avro::IO::DatumWriter.new(Avro::Schema.parse('"string"'))
+ datum_writer.write(value, encoder)
+
+ assert_equal "\x04\xc3\x83".force_encoding('BINARY'), output
+ end
+ end
+
+ def test_bytes_encoding
+ [
+ "\xC3\x83".force_encoding('BINARY'),
+ "\xC3\x83".force_encoding('ISO-8859-1'),
+ "\xC3\x83".force_encoding('UTF-8')
+ ].each do |value|
+ output = ''.force_encoding('BINARY')
+ encoder = Avro::IO::BinaryEncoder.new(StringIO.new(output))
+ datum_writer = Avro::IO::DatumWriter.new(Avro::Schema.parse('"bytes"'))
+ datum_writer.write(value, encoder)
+
+ assert_equal "\x04\xc3\x83".force_encoding('BINARY'), output
+ end
+ end
+
+ def test_fixed_encoding
+ [
+ "\xC3\x83".force_encoding('BINARY'),
+ "\xC3\x83".force_encoding('ISO-8859-1'),
+ "\xC3\x83".force_encoding('UTF-8')
+ ].each do |value|
+ output = ''.force_encoding('BINARY')
+ encoder = Avro::IO::BinaryEncoder.new(StringIO.new(output))
+ schema = '{"type": "fixed", "name": "TwoBytes", "size": 2}'
+ datum_writer = Avro::IO::DatumWriter.new(Avro::Schema.parse(schema))
+ datum_writer.write(value, encoder)
+
+ assert_equal "\xc3\x83".force_encoding('BINARY'), output
+ end
+ end
+
def test_skip_long
for value_to_skip, hex_encoding in BINARY_INT_ENCODINGS
value_to_read = 6253
# write some data in binary to string buffer
@@ -282,10 +341,9 @@
end
end
private
def check_default(schema_json, default_json, default_value)
- check(schema_json)
actual_schema = '{"type": "record", "name": "Foo", "fields": []}'
actual = Avro::Schema.parse(actual_schema)
expected_schema = <<EOS
{"type": "record",