Skip to content

Commit

Permalink
added RFC-4180 Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
j0nimost committed Oct 12, 2023
1 parent 1f998d7 commit 25e145c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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 🚧
Expand Down Expand Up @@ -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<CsvData>(csvs);
var textWriter = await Kafa.WriteAsync<CsvData>(csvs);
string result = textWriter.ToString();
// or
using var stream = await Kafa.WriteToStreamAsync<CsvData>(csvs);
// or
Expand Down
1 change: 1 addition & 0 deletions src/Kafa/Kafa.Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static async ValueTask<MemoryStream> WriteToStreamAsync<T>(List<T> entiti
public static async ValueTask WriteToFileAsync<T>(List<T> entities, string path, KafaOptions options = null)

Check warning on line 101 in src/Kafa/Kafa.Stream.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
ArgumentNullException.ThrowIfNull(entities, nameof(entities));
ArgumentNullException.ThrowIfNull(path, nameof(path));
var reflection = SetupOptions<T>(options);
using var fs = new FileStream(path, FileMode.Create);
using var strWriter = new StreamWriter(fs, options.Encoding!, 512);
Expand Down
76 changes: 76 additions & 0 deletions src/KafaTests/RFC4180Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace KafaTests
{
/// <summary>
/// CSV Standard https://www.rfc-editor.org/rfc/rfc4180
/// </summary>
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());
}
}
}

0 comments on commit 25e145c

Please sign in to comment.