Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/streaming #1

Merged
merged 5 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
- name: Build
run: dotnet build . --warnaserror
- name: Test
run: dotnet test . --configuration --no-build
run: dotnet test . --no-build
25 changes: 25 additions & 0 deletions CsvPortable/CsvPortable.Examples/CsvPortable.Examples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CsvPortable" Version="0.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions CsvPortable/CsvPortable.Examples/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ public void IgnoreProperty(string str, int number)

private class BaseClass
{
public virtual string String { get; set; }
public virtual string String { get; set; } = String.Empty;
public virtual int Int { get; set; }
}

private class IgnoreClass1 : BaseClass
{
[CsvIgnore] public override string String { get; set; }
[CsvIgnore] public override string String { get; set; } = String.Empty;
}

private class IgnoreClass2 : BaseClass
{
public override string String { get; set; }
public override string String { get; set; } = string.Empty;

[CsvIgnore] public override int Int { get; set; }
}
Expand Down
8 changes: 8 additions & 0 deletions CsvPortable/CsvPortable.Tests/CsvPortable.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Divergic.Logging.Xunit" Version="4.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand All @@ -32,6 +33,13 @@
<None Update="Files\SplitCsvRow\SplitCsvRowEncapsulation.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Files\Person\Person1.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Streamer\Files\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using CsvPortable.Exceptions;
using CsvPortable.Interfaces;
using Xunit.Abstractions;

namespace CsvPortable.Tests.Exceptions;

public class CsvDeserializationExceptionTest : TestBase
{
public CsvDeserializationExceptionTest(ITestOutputHelper output) : base(output)
{
}

[Theory]
[InlineData("", null, null)]
[InlineData("This is a error Message", null, null)]
[InlineData("This is a error Message", "This is a faulty CSV Row", null)]
[InlineData("This is a error Message", "This is a faulty CSV Row", 1)]
[InlineData("This is a another error Message", "This is a another faulty CSV Row", 4)]
public void CsvExceptionTest(string message, string? csvLine, int? rowIndex)
{
CsvDeserializationException Instance() => rowIndex is not null ? new CsvDeserializationException(message, csvLine, rowIndex.Value) : new CsvDeserializationException(message, csvLine);

Assert.Equal(message, Instance().Message);
Assert.Equal(csvLine, Instance().CsvLine);
Assert.Equal(rowIndex ?? CsvDeserializationException.DefaultRowIndex, Instance().RowIndex);

var instance = Instance();
// Test change of Row
const string newCsvLine = "New CSV Line";
instance.CsvLine = newCsvLine;
Assert.NotEqual(csvLine, instance.CsvLine);
Assert.Equal(newCsvLine, instance.CsvLine);

// Test change of RowIndex


var rowIndexNew = new Random().Next(100, 1000);
instance.RowIndex = rowIndexNew;
Assert.NotEqual(rowIndex, instance.RowIndex);
Assert.Equal(rowIndexNew, instance.RowIndex);
}

private class ExceptionTestDto
{
public DateTime Time { get; set; }
}

public struct ExceptionTestDtoStruct
{
public ExceptionTestDtoStruct(string? row, bool faulty)
{
Row = row;
Faulty = faulty;
}

public string? Row { get; set; }
public bool Faulty { get; set; }
}
public static IEnumerable<object[]> CsvExceptionTestCases()
{
return new List<object[]>
{
new object[] { null!, true} ,
new object[] {"", true},
new object[] {" ", true},
new object[]{"--", true},
new object[]{"199999.19.12", true},
new object[]{"31.31.2002", true},
new object[]{"2003.03.12 25.16.12", true},
new object[]{"2003.04.08 12.05.34", true},
new object[]{"2003.04.08 12.05.34", true},
};
}


[Theory]
[MemberData(nameof(CsvExceptionTestCases))]
public void TestTryFromRow(string row, bool faulty)
{
var ob = ICsvPortable.TryFromCsvRow<ExceptionTestDto>(row);
if (faulty)
{
Assert.Null(ob);
}
else
{
Assert.NotNull(ob);
}
}
[Theory]
[MemberData(nameof(CsvExceptionTestCases))]
public void TestFromRow(string row, bool faulty)
{
ExceptionTestDto Instance() => ICsvPortable.FromCsvRow<ExceptionTestDto>(row);
if (faulty)
{
Assert.Throws<CsvDeserializationException>(Instance);
}
else
{
Assert.NotNull(Instance());
}
}

[Theory]
[MemberData(nameof(CsvExceptionTestCases))]
public void TestFromRowErrorAction(string row, bool faulty)
{
ExceptionTestDto Instance() => ICsvPortable.FromCsvRow<ExceptionTestDto>(row);
if (faulty)
{
Assert.Throws<CsvDeserializationException>(Instance);
}
else
{
Assert.NotNull(Instance());
}
}
}
21 changes: 21 additions & 0 deletions CsvPortable/CsvPortable.Tests/Files/Person/Person1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Id;Given name;Sur Name;Age;Birthday;Street;City;ZipCode;Country;ActiveAddressSince;Street;City;ZipCode;Country;ActiveAddressSince
1;John;Doe;30;1993-05-15;123 Main St;Anytown;12345;USA;2010-02-20;456 Elm St;Sometown;54321;USA;2022-01-10
2;Jane;Smith;25;1998-09-22;789 Oak Ave;Othercity;98765;Canada;2015-08-12;321 Pine Rd;Yourtown;56789;Canada;2020-04-05
3;Michael;Johnson;40;1983-12-10;456 Birch Ln;Somewhere;23456;UK;2005-06-18;789 Maple Dr;Newplace;87654;UK;2018-09-30
4;Emily;Williams;28;1995-11-08;567 Cedar St;Townsville;34567;Australia;2012-03-25;890 Willow Rd;Differenttown;45678;Australia;2019-07-15
5;Daniel;Brown;35;1986-08-03;678 Pine Ave;Cityville;56789;USA;2008-09-14;123 Oak Rd;Anothertown;67890;USA;2021-02-28
6;Olivia;Davis;22;2001-04-18;789 Elm St;Villagetown;78901;Canada;2019-01-05;234 Maple Ave;Hometown;78901;Canada;2022-06-20
7;James;Miller;45;1978-07-30;890 Oak Ln;Countryside;89012;UK;2000-05-12;345 Cedar Rd;Smalltown;89012;UK;2015-10-08
8;Sophia;Wilson;31;1990-02-12;901 Pine Dr;Suburbia;90123;Australia;2011-08-30;456 Birch St;Othertown;90123;Australia;2020-03-02
9;David;Taylor;29;1992-10-25;123 Cedar Ave;Outskirts;01234;USA;2010-07-15;789 Elm Rd;Towncenter;01234;USA;2021-11-18
10;Ava;Johnson;27;1994-06-29;234 Maple St;Metropolis;12345;Canada;2013-04-05;890 Oak Ave;Uptown;12345;Canada;2022-09-10
11;Liam;Anderson;33;1989-03-12;345 Willow Rd;Villagetown;34567;USA;2012-05-20;901 Elm St;Hometown;34567;USA;2020-08-15
12;Emma;Martinez;24;1999-07-08;456 Birch Ave;Cityville;45678;Canada;2018-04-15;123 Cedar Rd;Anothertown;45678;Canada;2021-10-10
13;Noah;Garcia;29;1992-11-18;567 Oak Ln;Countryside;56789;UK;2010-09-12;234 Pine Ave;Smalltown;56789;UK;2022-02-25
14;Isabella;Lopez;26;1997-09-01;678 Pine Dr;Suburbia;67890;Australia;2015-06-30;345 Cedar St;Othertown;67890;Australia;2020-12-05
15;Ethan;Harris;39;1984-12-28;789 Elm Ave;Outskirts;78901;USA;2006-08-22;456 Oak Rd;Towncenter;78901;USA;2019-03-18
16;Mia;Clark;23;2000-05-10;890 Cedar St;Metropolis;89012;Canada;2017-03-02;901 Maple Ave;Uptown;89012;Canada;2022-07-15
17;Aiden;Lee;42;1979-08-15;901 Birch Rd;Villagetown;90123;UK;2001-06-12;123 Oak Ln;Hometown;90123;UK;2018-10-28
18;Avery;Rodriguez;30;1991-01-29;123 Cedar Dr;Cityville;01234;Australia;2010-04-20;234 Pine Rd;Anothertown;01234;Australia;2021-09-02
19;Benjamin;Scott;28;1994-04-03;234 Maple Ave;Countryside;12345;USA;2012-07-08;345 Willow St;Smalltown;12345;USA;2020-10-15
20;Ella;Young;25;1998-02-14;345 Birch Ln;Suburbia;23456;Canada;2015-01-10;456 Elm Ave;Othertown;23456;Canada;2022-04-25
26 changes: 26 additions & 0 deletions CsvPortable/CsvPortable.Tests/StreamTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace CsvPortable.Tests;

using Deserialize;
using Interfaces;
using Xunit.Abstractions;

public class StreamTests
{
private readonly ITestOutputHelper output;

public StreamTests(ITestOutputHelper output)
{
this.output = output;
}

[Fact]
public void TestStreamInputOutput()
{
var loader = ICsvPortable.CreateStreamer(output.BuildLogger());

var stream = File.OpenRead("Files/Person/Person1.csv");
var persons = loader.FromStream<Person>(stream);


}
}
45 changes: 45 additions & 0 deletions CsvPortable/CsvPortable.Tests/Streamer/StreamTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using CsvPortable.Interfaces;
using Xunit.Abstractions;

namespace CsvPortable.Tests.Deserialize;

using System.Text;

public class StreamTest
{
private readonly ITestOutputHelper output;

public StreamTest(ITestOutputHelper output)
{
this.output = output;
}

private const int Count = 20;

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task DeserializeSerializeStream(bool loggerNull)
{
var streamer = ICsvPortable.CreateStreamer(loggerNull ? null : output.BuildLogger());

var stream = File.OpenRead("Files/Person/Person1.csv");
var entries = streamer.FromStream<Person>(stream: stream).ToList();

Assert.Equal(Count, entries.Count());

var outputStream = new MemoryStream();

await streamer.ToStream(entries, outputStream);

outputStream.Position = 0;
byte[] buffer = new byte[outputStream.Length];
await outputStream.ReadAsync(buffer);
var t = Encoding.UTF8.GetString(buffer);
outputStream.Position = 0;
var reEntries = streamer.FromStream<Person>(stream: outputStream);


Assert.Equal(entries.Count, reEntries.Count());
}
}
13 changes: 13 additions & 0 deletions CsvPortable/CsvPortable.Tests/TestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Xunit.Abstractions;

namespace CsvPortable.Tests;

public class TestBase
{
protected readonly ITestOutputHelper output;

public TestBase(ITestOutputHelper output)
{
this.output = output;
}
}
12 changes: 12 additions & 0 deletions CsvPortable/CsvPortable.Tests/TestDtos/Address.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace CsvPortable.Tests.Deserialize;

using Interfaces;

public class Address : ICsvPortable
{
public string Street { get; set; } = string.Empty;
public string City { get; set; } = string.Empty;
public int ZipCode { get; set; }
public string Country { get; set; } = string.Empty;
public DateTime ActiveAddressSince { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CsvPortable.Attributes;
using CsvPortable.Interfaces;

namespace CsvPortable.Tests.TestDto;

Expand Down
17 changes: 17 additions & 0 deletions CsvPortable/CsvPortable.Tests/TestDtos/PersonDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace CsvPortable.Tests.Deserialize;

public class Person
{
public int Id { get; set; }

public string GiveName { get; set; } = string.Empty;

public string SurName { get; set; } = string.Empty;

public int Age { get; set; }
public DateTime Birthday { get; set; }

public Address? LivingAddress { get; set; }

public Address? WorkAddress { get; set; }
}
5 changes: 3 additions & 2 deletions CsvPortable/CsvPortable/Configuration/CsvConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public class CsvConfiguration

public DateTime Date { get; }

public static List<CsvConfiguration> Configurations { get; set; }
// ReSharper disable once CollectionNeverUpdated.Global
public static List<CsvConfiguration> Configurations { get; set; } = new List<CsvConfiguration>();

public CsvConfiguration(int type, string date)
{
Expand Down Expand Up @@ -37,7 +38,7 @@ public CsvConfiguration(int type, string date)

public static implicit operator CsvConfiguration (int configurationType)
{
return Configurations.First( k => k.Type == configurationType);
return Configurations.First( k => k.Type == configurationType);
}

public override bool Equals(object? obj)
Expand Down
2 changes: 1 addition & 1 deletion CsvPortable/CsvPortable/Configuration/CsvParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public CsvParameter(CsvConfiguration? configuration, CsvDelimiter delimiter, boo
public bool CloseEnd { get; set; } = true;


public CsvParameter? ParameterToUse(Type type, bool closeEnd)
public CsvParameter ParameterToUse(Type type, bool closeEnd)
{
bool MatchType((Type Type, CsvConfiguration Configuration) t)
{
Expand Down
1 change: 1 addition & 0 deletions CsvPortable/CsvPortable/CsvPortable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading