コード例 #1
0
        public void TestReadInvalidAvroInput()
        {
            string Schema = @"
{""type"":""array"",
""items"":
{
""type"":""record"",
""name"":""TestNS.Person"",
""fields"":
    [
        { ""name"":""Id"", ""type"":""int"" },
        { ""name"":""Title"", ""type"":""string"" },
    ]
}
}";

            using (var stream = new MemoryStream())
            {
                using (var writer = AvroContainer.CreateGenericWriter(Schema, stream, /*leave open*/ true, Codec.Null))
                    using (var seqWriter = new SequentialWriter <object>(writer, 24))
                    {
                    }

                stream.Seek(0, SeekOrigin.Begin);

                var avroReader = new ODataAvroReader(this.CreateODataInputContext(stream), true);
                Assert.AreEqual(ODataReaderState.Start, avroReader.State);
                Assert.IsFalse(avroReader.Read());
                Assert.AreEqual(ODataReaderState.Exception, avroReader.State);
            }
        }
コード例 #2
0
        public void TestReadAvroAsODataFeed()
        {
            const string Schema     = @"
{""type"":""array"",
""items"":
{
""type"":""record"",
""name"":""TestNS.Person"",
""fields"":
    [
        { ""name"":""Id"", ""type"":""int"" },
        { ""name"":""Title"", ""type"":""string"" },
    ]
}
}";
            var          serializer = AvroSerializer.CreateGeneric(Schema);

            using (var stream = new MemoryStream())
            {
                var arraySchema  = (ArraySchema)serializer.WriterSchema;
                var recordSchema = arraySchema.ItemSchema;

                var rec1 = new AvroRecord(recordSchema);
                rec1["Id"]    = 1;
                rec1["Title"] = "s1";

                var rec2 = new AvroRecord(recordSchema);
                rec2["Id"]    = 2;
                rec2["Title"] = "s2";

                var array = new[] { rec1, rec2 };

                using (var writer = AvroContainer.CreateGenericWriter(Schema, stream, /*leave open*/ true, Codec.Null))
                    using (var seqWriter = new SequentialWriter <object>(writer, 24))
                    {
                        seqWriter.Write(array);
                    }

                stream.Seek(0, SeekOrigin.Begin);
                var avroReader = new ODataAvroReader(this.CreateODataInputContext(stream), true);
                Assert.AreEqual(ODataReaderState.Start, avroReader.State);
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.FeedStart, avroReader.State);
                Assert.IsTrue(avroReader.Read());

                // Entry 1
                Assert.AreEqual(ODataReaderState.EntryStart, avroReader.State);
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.EntryEnd, avroReader.State);
                var entry = avroReader.Item as ODataEntry;
                Assert.IsNotNull(entry);
                var properties = entry.Properties.ToList();
                Assert.AreEqual(2, properties.Count);
                Assert.AreEqual("Id", properties[0].Name);
                Assert.AreEqual(1, properties[0].Value);
                Assert.AreEqual("Title", properties[1].Name);
                Assert.AreEqual("s1", properties[1].Value);

                // Entry 2
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.EntryStart, avroReader.State);
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.EntryEnd, avroReader.State);
                entry = avroReader.Item as ODataEntry;
                Assert.IsNotNull(entry);
                properties = entry.Properties.ToList();
                Assert.AreEqual(2, properties.Count);
                Assert.AreEqual("Id", properties[0].Name);
                Assert.AreEqual(2, properties[0].Value);
                Assert.AreEqual("Title", properties[1].Name);
                Assert.AreEqual("s2", properties[1].Value);
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.FeedEnd, avroReader.State);

                Assert.IsFalse(avroReader.Read());
                Assert.AreEqual(ODataReaderState.Completed, avroReader.State);
            }
        }
コード例 #3
0
        public void TestReadAvroAsODataEntry()
        {
            const string Schema     = @"
{
""type"":""record"",
""name"":""TestNS.Person"",
""fields"":
    [
        { ""name"":""Id"", ""type"":""int"" },
        { ""name"":""Title"", ""type"":""string"" },
        { ""name"":""Address"", ""type"":{
                ""name"":""TestNS.Address"",
                ""type"":""record"",
                ""fields"":[
                    { ""name"":""ZipCode"", ""type"":""long"" },
                ]
            } 
        },
    ]
}";
            var          serializer = AvroSerializer.CreateGeneric(Schema);

            using (var stream = new MemoryStream())
            {
                var expected = new AvroRecord(serializer.WriterSchema);
                expected["Id"]    = -5;
                expected["Title"] = "set";

                var cpxSchema = ((RecordSchema)serializer.WriterSchema).GetField("Address").TypeSchema;
                var cpx       = new AvroRecord(cpxSchema);
                cpx["ZipCode"]      = 5L;
                expected["Address"] = cpx;

                using (var writer = AvroContainer.CreateGenericWriter(Schema, stream, /*leave open*/ true, Codec.Null))
                {
                    using (var streamWriter = new SequentialWriter <object>(writer, 24))
                    {
                        // Serialize the data to stream using the sequential writer
                        streamWriter.Write(expected);
                    }
                }

                stream.Seek(0, SeekOrigin.Begin);
                var avroReader = new ODataAvroReader(this.CreateODataInputContext(stream), false);
                Assert.AreEqual(ODataReaderState.Start, avroReader.State);
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.EntryStart, avroReader.State);
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.EntryEnd, avroReader.State);
                var entry = avroReader.Item as ODataEntry;
                Assert.IsNotNull(entry);
                Assert.AreEqual("TestNS.Person", entry.TypeName);
                var properties = entry.Properties.ToList();
                Assert.AreEqual(3, properties.Count);
                Assert.AreEqual("Id", properties[0].Name);
                Assert.AreEqual(-5, properties[0].Value);
                Assert.AreEqual("Title", properties[1].Name);
                Assert.AreEqual("set", properties[1].Value);
                Assert.AreEqual("Address", properties[2].Name);
                var address = properties[2].Value as ODataComplexValue;
                Assert.IsNotNull(address);
                Assert.AreEqual("TestNS.Address", address.TypeName);
                var zip = address.Properties.Single();
                Assert.AreEqual(5L, zip.Value);
                Assert.IsFalse(avroReader.Read());
                Assert.AreEqual(ODataReaderState.Completed, avroReader.State);
            }
        }
コード例 #4
0
        public void TestReadAvroAsODataFeed()
        {
            const string Schema = @"
{""type"":""array"",
""items"":
{
""type"":""record"",
""name"":""TestNS.Person"",
""fields"":
    [
        { ""name"":""Id"", ""type"":""int"" },
        { ""name"":""Title"", ""type"":""string"" },
    ]
}
}";
            var serializer = AvroSerializer.CreateGeneric(Schema);

            using (var stream = new MemoryStream())
            {
                var arraySchema = (ArraySchema)serializer.WriterSchema;
                var recordSchema = arraySchema.ItemSchema;

                var rec1 = new AvroRecord(recordSchema);
                rec1["Id"] = 1;
                rec1["Title"] = "s1";

                var rec2 = new AvroRecord(recordSchema);
                rec2["Id"] = 2;
                rec2["Title"] = "s2";

                var array = new[] { rec1, rec2 };

                using (var writer = AvroContainer.CreateGenericWriter(Schema, stream, /*leave open*/true, Codec.Null))
                using (var seqWriter = new SequentialWriter<object>(writer, 24))
                {
                    seqWriter.Write(array);
                }

                stream.Seek(0, SeekOrigin.Begin);
                var avroReader = new ODataAvroReader(this.CreateODataInputContext(stream), true);
                Assert.AreEqual(ODataReaderState.Start, avroReader.State);
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.FeedStart, avroReader.State);
                Assert.IsTrue(avroReader.Read());

                // Entry 1
                Assert.AreEqual(ODataReaderState.EntryStart, avroReader.State);
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.EntryEnd, avroReader.State);
                var entry = avroReader.Item as ODataEntry;
                Assert.IsNotNull(entry);
                var properties = entry.Properties.ToList();
                Assert.AreEqual(2, properties.Count);
                Assert.AreEqual("Id", properties[0].Name);
                Assert.AreEqual(1, properties[0].Value);
                Assert.AreEqual("Title", properties[1].Name);
                Assert.AreEqual("s1", properties[1].Value);

                // Entry 2
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.EntryStart, avroReader.State);
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.EntryEnd, avroReader.State);
                entry = avroReader.Item as ODataEntry;
                Assert.IsNotNull(entry);
                properties = entry.Properties.ToList();
                Assert.AreEqual(2, properties.Count);
                Assert.AreEqual("Id", properties[0].Name);
                Assert.AreEqual(2, properties[0].Value);
                Assert.AreEqual("Title", properties[1].Name);
                Assert.AreEqual("s2", properties[1].Value);
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.FeedEnd, avroReader.State);

                Assert.IsFalse(avroReader.Read());
                Assert.AreEqual(ODataReaderState.Completed, avroReader.State);
            }
        }
コード例 #5
0
        public void TestReadAvroAsODataEntry()
        {
            const string Schema = @"
{
""type"":""record"",
""name"":""TestNS.Person"",
""fields"":
    [
        { ""name"":""Id"", ""type"":""int"" },
        { ""name"":""Title"", ""type"":""string"" },
        { ""name"":""Address"", ""type"":{
                ""name"":""TestNS.Address"",
                ""type"":""record"",
                ""fields"":[
                    { ""name"":""ZipCode"", ""type"":""long"" },
                ]
            } 
        },
    ]
}";
            var serializer = AvroSerializer.CreateGeneric(Schema);

            using (var stream = new MemoryStream())
            {
                var expected = new AvroRecord(serializer.WriterSchema);
                expected["Id"] = -5;
                expected["Title"] = "set";

                var cpxSchema = ((RecordSchema)serializer.WriterSchema).GetField("Address").TypeSchema;
                var cpx = new AvroRecord(cpxSchema);
                cpx["ZipCode"] = 5L;
                expected["Address"] = cpx;

                using (var writer = AvroContainer.CreateGenericWriter(Schema, stream, /*leave open*/true, Codec.Null))
                {
                    using (var streamWriter = new SequentialWriter<object>(writer, 24))
                    {
                        // Serialize the data to stream using the sequential writer
                        streamWriter.Write(expected);
                    }
                }

                stream.Seek(0, SeekOrigin.Begin);
                var avroReader = new ODataAvroReader(this.CreateODataInputContext(stream), false);
                Assert.AreEqual(ODataReaderState.Start, avroReader.State);
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.EntryStart, avroReader.State);
                Assert.IsTrue(avroReader.Read());
                Assert.AreEqual(ODataReaderState.EntryEnd, avroReader.State);
                var entry = avroReader.Item as ODataEntry;
                Assert.IsNotNull(entry);
                Assert.AreEqual("TestNS.Person", entry.TypeName);
                var properties = entry.Properties.ToList();
                Assert.AreEqual(3, properties.Count);
                Assert.AreEqual("Id", properties[0].Name);
                Assert.AreEqual(-5, properties[0].Value);
                Assert.AreEqual("Title", properties[1].Name);
                Assert.AreEqual("set", properties[1].Value);
                Assert.AreEqual("Address", properties[2].Name);
                var address = properties[2].Value as ODataComplexValue;
                Assert.IsNotNull(address);
                Assert.AreEqual("TestNS.Address", address.TypeName);
                var zip = address.Properties.Single();
                Assert.AreEqual(5L, zip.Value);
                Assert.IsFalse(avroReader.Read());
                Assert.AreEqual(ODataReaderState.Completed, avroReader.State);
            }
        }
コード例 #6
0
        public void TestReadInvalidAvroInput()
        {
            string Schema = @"
{""type"":""array"",
""items"":
{
""type"":""record"",
""name"":""TestNS.Person"",
""fields"":
    [
        { ""name"":""Id"", ""type"":""int"" },
        { ""name"":""Title"", ""type"":""string"" },
    ]
}
}";

            using (var stream = new MemoryStream())
            {
                using (var writer = AvroContainer.CreateGenericWriter(Schema, stream, /*leave open*/true, Codec.Null))
                using (var seqWriter = new SequentialWriter<object>(writer, 24))
                {
                }

                stream.Seek(0, SeekOrigin.Begin);

                var avroReader = new ODataAvroReader(this.CreateODataInputContext(stream), true);
                Assert.AreEqual(ODataReaderState.Start, avroReader.State);
                Assert.IsFalse(avroReader.Read());
                Assert.AreEqual(ODataReaderState.Exception, avroReader.State);
            }
        }