1 // Licensed to the Apache Software Foundation (ASF) under one or more
2 // contributor license agreements. See the NOTICE file distributed with
3 // this work for additional information regarding copyright ownership.
4 // The ASF licenses this file to You under the Apache License, Version 2.0
5 // (the "License"); you may not use this file except in compliance with
6 // the License. You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 using Apache.Arrow.Ipc;
19 using System.Threading.Tasks;
22 namespace Apache.Arrow.Tests
24 public class ArrowFileWriterTests
27 public void Ctor_LeaveOpenDefault_StreamClosedOnDispose()
29 RecordBatch originalBatch = TestData.CreateSampleRecordBatch(length: 100);
30 var stream = new MemoryStream();
31 new ArrowFileWriter(stream, originalBatch.Schema).Dispose();
32 Assert.Throws<ObjectDisposedException>(() => stream.Position);
36 public void Ctor_LeaveOpenFalse_StreamClosedOnDispose()
38 RecordBatch originalBatch = TestData.CreateSampleRecordBatch(length: 100);
39 var stream = new MemoryStream();
40 new ArrowFileWriter(stream, originalBatch.Schema, leaveOpen: false).Dispose();
41 Assert.Throws<ObjectDisposedException>(() => stream.Position);
45 public void Ctor_LeaveOpenTrue_StreamValidOnDispose()
47 RecordBatch originalBatch = TestData.CreateSampleRecordBatch(length: 100);
48 var stream = new MemoryStream();
49 new ArrowFileWriter(stream, originalBatch.Schema, leaveOpen: true).Dispose();
50 Assert.Equal(0, stream.Position);
54 /// Tests that writing an arrow file will always align the Block lengths
55 /// to 8 bytes. There are asserts in both the reader and writer which will fail
56 /// if this isn't the case.
58 /// <returns></returns>
60 public async Task WritesFooterAlignedMulitpleOf8()
62 RecordBatch originalBatch = TestData.CreateSampleRecordBatch(length: 100);
64 var stream = new MemoryStream();
65 var writer = new ArrowFileWriter(
69 // use WriteLegacyIpcFormat, which only uses a 4-byte length prefix
70 // which causes the length prefix to not be 8-byte aligned by default
71 new IpcOptions() { WriteLegacyIpcFormat = true });
73 writer.WriteRecordBatch(originalBatch);
78 await ValidateRecordBatchFile(stream, originalBatch);
82 /// Tests that writing an arrow file will always align the Block lengths
83 /// to 8 bytes. There are asserts in both the reader and writer which will fail
84 /// if this isn't the case.
86 /// <returns></returns>
88 public async Task WritesFooterAlignedMulitpleOf8Async()
90 RecordBatch originalBatch = TestData.CreateSampleRecordBatch(length: 100);
92 var stream = new MemoryStream();
93 var writer = new ArrowFileWriter(
97 // use WriteLegacyIpcFormat, which only uses a 4-byte length prefix
98 // which causes the length prefix to not be 8-byte aligned by default
99 new IpcOptions() { WriteLegacyIpcFormat = true });
101 await writer.WriteRecordBatchAsync(originalBatch);
102 await writer.WriteEndAsync();
106 await ValidateRecordBatchFile(stream, originalBatch);
109 private async Task ValidateRecordBatchFile(Stream stream, RecordBatch recordBatch)
111 var reader = new ArrowFileReader(stream);
112 int count = await reader.RecordBatchCountAsync();
113 Assert.Equal(1, count);
114 RecordBatch readBatch = await reader.ReadRecordBatchAsync(0);
115 ArrowReaderVerifier.CompareBatches(recordBatch, readBatch);
119 /// Tests that writing an arrow file with no RecordBatches produces the correct
123 public async Task WritesEmptyFile()
125 RecordBatch originalBatch = TestData.CreateSampleRecordBatch(length: 1);
127 var stream = new MemoryStream();
128 var writer = new ArrowFileWriter(stream, originalBatch.Schema);
135 var reader = new ArrowFileReader(stream);
136 int count = await reader.RecordBatchCountAsync();
137 Assert.Equal(0, count);
138 RecordBatch readBatch = reader.ReadNextRecordBatch();
139 Assert.Null(readBatch);
140 SchemaComparer.Compare(originalBatch.Schema, reader.Schema);
144 /// Tests that writing an arrow file with no RecordBatches produces the correct
145 /// file when using WriteStartAsync and WriteEndAsync.
148 public async Task WritesEmptyFileAsync()
150 RecordBatch originalBatch = TestData.CreateSampleRecordBatch(length: 1);
152 var stream = new MemoryStream();
153 var writer = new ArrowFileWriter(stream, originalBatch.Schema);
155 await writer.WriteStartAsync();
156 await writer.WriteEndAsync();
160 var reader = new ArrowFileReader(stream);
161 int count = await reader.RecordBatchCountAsync();
162 Assert.Equal(0, count);
163 RecordBatch readBatch = reader.ReadNextRecordBatch();
164 Assert.Null(readBatch);
165 SchemaComparer.Compare(originalBatch.Schema, reader.Schema);