Skip to content
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
10 changes: 10 additions & 0 deletions CsvHelperClient.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NHSISL.CsvHelperClient.Test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NHSISL.CsvHelperClient.Tests.Unit", "NHSISL.CsvHelperClient.Tests.Unit\NHSISL.CsvHelperClient.Tests.Unit.csproj", "{62E04B25-66F1-4D46-9F5E-4967DACC9CA6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NHSISL.CsvHelperClient.Tests.Integration", "NHSISL.CsvHelperClient.Tests.Integration\NHSISL.CsvHelperClient.Tests.Integration.csproj", "{8DB4ADA5-29D3-40A6-A0D3-0F5A8B96674E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Acceptance|Any CPU = Acceptance|Any CPU
Expand Down Expand Up @@ -53,6 +55,14 @@ Global
{62E04B25-66F1-4D46-9F5E-4967DACC9CA6}.Release|Any CPU.Build.0 = Release|Any CPU
{62E04B25-66F1-4D46-9F5E-4967DACC9CA6}.Test|Any CPU.ActiveCfg = Test|Any CPU
{62E04B25-66F1-4D46-9F5E-4967DACC9CA6}.Test|Any CPU.Build.0 = Test|Any CPU
{8DB4ADA5-29D3-40A6-A0D3-0F5A8B96674E}.Acceptance|Any CPU.ActiveCfg = Debug|Any CPU
{8DB4ADA5-29D3-40A6-A0D3-0F5A8B96674E}.Acceptance|Any CPU.Build.0 = Debug|Any CPU
{8DB4ADA5-29D3-40A6-A0D3-0F5A8B96674E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8DB4ADA5-29D3-40A6-A0D3-0F5A8B96674E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8DB4ADA5-29D3-40A6-A0D3-0F5A8B96674E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8DB4ADA5-29D3-40A6-A0D3-0F5A8B96674E}.Release|Any CPU.Build.0 = Release|Any CPU
{8DB4ADA5-29D3-40A6-A0D3-0F5A8B96674E}.Test|Any CPU.ActiveCfg = Debug|Any CPU
{8DB4ADA5-29D3-40A6-A0D3-0F5A8B96674E}.Test|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
16 changes: 16 additions & 0 deletions NHSISL.CsvHelperClient.Tests.Integration/Models/Car.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NHSISL.CsvHelperClient.Tests.Integration.Models
{
internal class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<IsPackable>false</IsPackable>
<Configurations>Debug;Release;Test;Integration</Configurations>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Tynamix.ObjectFiller" Version="1.5.9" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NHSISL.CsvHelperClient\NHSISL.CsvHelperClient.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using FluentAssertions;
using Force.DeepCloner;
using NHSISL.CsvHelperClient.Tests.Integration.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;

namespace NHSISL.CsvHelperClient.Tests.Integration.Services.Foundations.CsvHelpers
{
public partial class CsvHelperTests
{
[Fact]
[Trait("Category", "Integration")]
public async Task ShouldMapCsvToObject()
{
// given
List<Car> randomCars = CreateRandomCars();
List<Car> expectedObjects = randomCars.DeepClone();

string randomCsvFormattedObjects = GetCsvRepresentationOfCar(
cars: randomCars,
hasHeaderRow: true,
shouldAddTrailingComma: false);

// when
List<Car> retrievedObjects =
await this.csvClient.MapCsvToObjectAsync<Car>(randomCsvFormattedObjects, hasHeaderRecord: true);

// then
retrievedObjects.Should().BeEquivalentTo(expectedObjects);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// ---------------------------------------------------------
// Copyright (c) North East London ICB. All rights reserved.
// ---------------------------------------------------------

using NHSISL.CsvHelperClient.Clients;
using NHSISL.CsvHelperClient.Tests.Integration.Models;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text;
using Tynamix.ObjectFiller;
using Xunit;

namespace NHSISL.CsvHelperClient.Tests.Integration.Services.Foundations.CsvHelpers
{
public partial class CsvHelperTests
{
private readonly CsvClient csvClient;

public CsvHelperTests()
{
this.csvClient = new CsvClient();
}

private static int GetRandomNumber() =>
new IntRange(min: 2, max: 10).GetValue();

private static DateTimeOffset GetRandomDateTimeOffset() =>
new DateTimeRange(earliestDate: new DateTime()).GetValue();

private static List<Car> CreateRandomCars()
{
return CreateCarFiller()
.Create(count: GetRandomNumber())
.ToList();
}

private static Filler<Car> CreateCarFiller()
{
var filler = new Filler<Car>();
filler.Setup();

return filler;
}

private string WrapInQuotesIfContainsComma(string value)
{
if (value.Contains(","))
{
return $"\"{value}\"";
}
return value;
}

private string GetCsvRepresentationOfCar(
List<Car> cars,
bool hasHeaderRow,
bool shouldAddTrailingComma)
{
StringBuilder csvBuilder = new StringBuilder();

if (hasHeaderRow)
{
csvBuilder.AppendLine("Make,Model,Year,Color");
}

foreach (var car in cars)
{
string line = $"{WrapInQuotesIfContainsComma(car.Make)}," +
$"{WrapInQuotesIfContainsComma(car.Model)}," +
$"{WrapInQuotesIfContainsComma(car.Year.ToString())}," +
$"{WrapInQuotesIfContainsComma(car.Color)}";

if (shouldAddTrailingComma)
{
line += ",";
}

csvBuilder.AppendLine(line);
}

return csvBuilder.ToString();
}
}
}
Loading