From 25e145cce43a4788ab9ed062d42a8eb84843d562 Mon Sep 17 00:00:00 2001 From: j0nimost Date: Thu, 12 Oct 2023 07:43:14 +0300 Subject: [PATCH] added RFC-4180 Tests --- README.md | 5 ++- src/Kafa/Kafa.Stream.cs | 1 + src/KafaTests/RFC4180Tests.cs | 76 +++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/KafaTests/RFC4180Tests.cs diff --git a/README.md b/README.md index 8761449..e91a013 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Kafa A fast easy to use csv,tsv file parser. It has a low memory footprint with alot of optimizations to be done. +Kafa is also RFC-4180 Compliant [docs](https://www.rfc-editor.org/rfc/rfc4180) + ![Build Status](https://github.com/j0nimost/Kafa/actions/workflows/dotnet.yml/badge.svg) 🚧 UNDER ACTIVE DEVELOPMENT 🚧 @@ -97,7 +99,8 @@ You can simply convert a list of objects to a textwriter, stream or a file new CSVDataWithAttributes{ Date = DateTime.Parse("10/10/2023 4:08:38 PM"), Open=12.45, Close=12.99, High=13.00, Low=12.1, Name="AMZN", Volume=1233435512} }; - var rowmem = await Kafa.WriteAsync(csvs); + var textWriter = await Kafa.WriteAsync(csvs); + string result = textWriter.ToString(); // or using var stream = await Kafa.WriteToStreamAsync(csvs); // or diff --git a/src/Kafa/Kafa.Stream.cs b/src/Kafa/Kafa.Stream.cs index 02502a6..61f2507 100644 --- a/src/Kafa/Kafa.Stream.cs +++ b/src/Kafa/Kafa.Stream.cs @@ -101,6 +101,7 @@ public static async ValueTask WriteToStreamAsync(List entiti public static async ValueTask WriteToFileAsync(List entities, string path, KafaOptions options = null) { ArgumentNullException.ThrowIfNull(entities, nameof(entities)); + ArgumentNullException.ThrowIfNull(path, nameof(path)); var reflection = SetupOptions(options); using var fs = new FileStream(path, FileMode.Create); using var strWriter = new StreamWriter(fs, options.Encoding!, 512); diff --git a/src/KafaTests/RFC4180Tests.cs b/src/KafaTests/RFC4180Tests.cs new file mode 100644 index 0000000..e15e160 --- /dev/null +++ b/src/KafaTests/RFC4180Tests.cs @@ -0,0 +1,76 @@ +namespace KafaTests +{ + /// + /// CSV Standard https://www.rfc-editor.org/rfc/rfc4180 + /// + public class RFC4180Tests + { + private KafaOptions ReadEverythingOption => new KafaOptions() { HasHeader = false, FileType = FileType.CSV }; + + [Fact] + public void ReadRowsWithCRLF() + { + string csvString = "date,open,high,low,close,volume,Name\r\n2013-02-11,14.89,15.01,14.26,14.46,8882000,AAL\r\n2013-02-12,14.45,14.51,14.1,14.27,8126000,AAL\r\n"; + string expectedRow1 = "2013-02-11,14.89,15.01,14.26,14.46,8882000,AAL"; + string expectedRow2 = "2013-02-12,14.45,14.51,14.1,14.27,8126000,AAL"; + using var rows = Kafa.Read(csvString, ReadEverythingOption); + Assert.Equal(expectedRow1, rows[1].ToString()); + Assert.Equal(expectedRow2, rows[2].ToString()); + } + + [Fact] + public void ReadRowsWithNoCRLF() + { + string csvString = "date,open,high,low,close,volume,Name\r\n2013-02-11,14.89,15.01,14.26,14.46,8882000,AAL\r\n2013-02-12,14.45,14.51,14.1,14.27,8126000,AAL"; + string expectedRow1 = "2013-02-11,14.89,15.01,14.26,14.46,8882000,AAL"; + string expectedRow2 = "2013-02-12,14.45,14.51,14.1,14.27,8126000,AAL"; + using var rows = Kafa.Read(csvString, ReadEverythingOption); + Assert.Equal(expectedRow1, rows[1].ToString()); + Assert.Equal(expectedRow2, rows[2].ToString()); + } + + [Fact] + public void ReadRowsWithHeader() + { + string csvString = "date,open,high,low,close,volume,Name\r\n2013-02-11,14.89,15.01,14.26,14.46,8882000,AAL\r\n2013-02-12,14.45,14.51,14.1,14.27,8126000,AAL"; + string expectedRow1 = "date,open,high,low,close,volume,Name"; + string expectedRow2 = "2013-02-11,14.89,15.01,14.26,14.46,8882000,AAL"; + using var rows = Kafa.Read(csvString); + Assert.Equal(expectedRow1, rows[0].ToString()); + Assert.Equal(expectedRow2, rows[1].ToString()); + } + + + [Fact] + public void ReadRowsWithQuotes() + { + string csvString = "date,open,high,low,close,volume,Name\r\n2013-02-11,14.89,15.01,14.26,14.46,8882000,\"AAL\"\r\n2013-02-12,14.45,14.51,14.1,14.27,8126000,\"AAL\""; + string expectedRow1 = "2013-02-11,14.89,15.01,14.26,14.46,8882000,\"AAL\""; + string expectedRow2 = "2013-02-12,14.45,14.51,14.1,14.27,8126000,\"AAL\""; + using var rows = Kafa.Read(csvString, ReadEverythingOption); + Assert.Equal(expectedRow1, rows[1].ToString()); + Assert.Equal(expectedRow2, rows[2].ToString()); + } + + [Fact] + public void ReadRowsWithQuotesandCRLF() + { + string csvString = "date,open,high,low,close,volume,Name\r\n2013-02-11,14.89,15.01,14.26,14.46,8882000,\"AAL\r\n\"\r\n2013-02-12,14.45,14.51,14.1,14.27,8126000,\"AAL\r\n\""; + string expectedRow1 = "2013-02-11,14.89,15.01,14.26,14.46,8882000,\"AAL\r\n\""; + string expectedRow2 = "2013-02-12,14.45,14.51,14.1,14.27,8126000,\"AAL\r\n\""; + using var rows = Kafa.Read(csvString, ReadEverythingOption); + Assert.Equal(expectedRow1, rows[1].ToString()); + Assert.Equal(expectedRow2, rows[2].ToString()); + } + + [Fact] + public void ReadRowsWithDoubleQuotes() + { + string csvString = "date,open,high,low,\"close\"\"volume\",Name"; + + string expected = "\"close\"\"volume\""; + using var rows = Kafa.Read(csvString, ReadEverythingOption); + Assert.Equal(expected, rows[0].Cols[4].ToString()); + } + } +}