11using System . IO . Compression ;
22using Microsoft . Extensions . Logging . Abstractions ;
3+ using Microsoft . Extensions . Configuration ;
34using System . Text ;
45using Microsoft . VisualStudio . TestTools . UnitTesting ;
56using Cosmos . DataTransfer . JsonExtension ;
67using Cosmos . DataTransfer . JsonExtension . UnitTests ;
78using Cosmos . DataTransfer . Interfaces ;
89using Moq ;
910
10- namespace Cosmos . DataTransfer . Common . UnitTest ;
11+ namespace Cosmos . DataTransfer . Common . UnitTests ;
1112
1213[ TestClass ]
1314public class FileSinkDataSinkTests {
1415
1516 public static IEnumerable < object [ ] > Test_WriteToTargetAsyncData { get {
16-
1717 yield return new object [ ] { CompressionEnum . None , "" , "" } ;
1818 yield return new object [ ] { CompressionEnum . None , ".txt" , ".txt" } ;
1919 yield return new object [ ] { CompressionEnum . Brotli , "" , ".br" } ;
@@ -26,15 +26,15 @@ public static IEnumerable<object[]> Test_WriteToTargetAsyncData { get {
2626 } }
2727
2828 [ TestMethod ]
29- [ DoNotParallelize ]
3029 [ DynamicData ( nameof ( Test_WriteToTargetAsyncData ) ) ]
3130 public async Task Test_WriteToTargetAsync ( CompressionEnum compression , string suffix , string expected_ext ) {
3231 var source = new Mock < IDataSourceExtension > ( ) ;
3332 FileDataSink sink = new ( ) ;
3433 var filePath = Path . GetTempFileName ( ) ;
3534 var config = TestHelpers . CreateConfig ( new Dictionary < string , string > ( ) {
3635 { "FilePath" , filePath + suffix } ,
37- { "Compression" , compression . ToString ( ) }
36+ { "Compression" , compression . ToString ( ) } ,
37+ { "Append" , "false" }
3838 } ) ;
3939
4040 var str = Encoding . UTF8 . GetBytes ( "Hello world!" ) ;
@@ -57,4 +57,44 @@ public void Test_GetSettings() {
5757 Assert . IsInstanceOfType ( response [ 0 ] , typeof ( FileSinkSettings ) ) ;
5858 }
5959
60+ public static IEnumerable < object [ ] > Test_WriteToTargetAsyncAppendData { get {
61+ yield return new object [ ] { "foobar.txt" , CompressionEnum . None } ;
62+ yield return new object [ ] { "foobar.txt.gz" , CompressionEnum . Gzip } ;
63+ yield return new object [ ] { "foobar.txt.br" , CompressionEnum . Brotli } ;
64+ yield return new object [ ] { "foobar.txt.zz" , CompressionEnum . Deflate } ;
65+ } }
66+
67+ [ TestMethod ]
68+ [ DynamicData ( nameof ( Test_WriteToTargetAsyncAppendData ) ) ]
69+ public async Task Test_WriteToTargetAsyncAppend ( string filePath , CompressionEnum compression ) {
70+ var source = new Mock < IDataSourceExtension > ( ) ;
71+ FileDataSink sink = new ( ) ;
72+ var tmpdir = FileSinkDataSourceTests . GetTemporaryDirectory ( ) ;
73+ var destfile = Path . Combine ( tmpdir , filePath ) ;
74+ File . Copy ( Path . Combine ( "Data" , filePath ) , destfile ) ;
75+ var config = TestHelpers . CreateConfig ( new Dictionary < string , string > ( ) {
76+ { "FilePath" , destfile } ,
77+ { "Compression" , compression . ToString ( ) } ,
78+ { "Append" , "true" }
79+ } ) ;
80+
81+ if ( compression != CompressionEnum . None ) {
82+ var e = Assert . ThrowsException < AggregateException > ( ( ) => {
83+ var settings = config . Get < FileSinkSettings > ( ) ;
84+ settings . Validate ( ) ;
85+ } ) ;
86+ return ;
87+ }
88+ var str = Encoding . UTF8 . GetBytes ( "\n It's Valentines day! Lovely!" ) ;
89+ await sink . WriteToTargetAsync ( writer => writer . WriteAsync ( str ) . AsTask ( ) ,
90+ config , source . Object , NullLogger . Instance ) ;
91+
92+ var dataSource = new FileDataSource ( ) ;
93+ var stream = dataSource . ReadFile ( destfile , CompressionEnum . None , NullLogger . Instance ) ! ;
94+ var buffer = new byte [ 100 ] ;
95+ await stream . ReadAsync ( buffer . AsMemory ( 0 , buffer . Length ) ) ;
96+ var result = Encoding . UTF8 . GetString ( buffer ) ;
97+ Assert . AreEqual ( "Hello world!\n It's Valentines day! Lovely!" , result . TrimEnd ( '\0 ' ) ) ;
98+ }
99+
60100}
0 commit comments