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
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,27 @@ public async Task ShouldMapCsvToObject()
// then
retrievedObjects.Should().BeEquivalentTo(expectedObjects);
}

[Fact]
[Trait("Category", "Integration")]
public async Task ShouldMapCsvToDynamicObject()
{
// given
List<Car> randomCars = CreateRandomCars();
List<dynamic> anonCars = CreateDynamicCars(randomCars);
List<dynamic> expectedObjects = anonCars.DeepClone();

string randomCsvFormattedObjects = GetCsvRepresentationOfDynamicObject(
cars: anonCars,
hasHeaderRow: true,
shouldAddTrailingComma: false);

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

// then
retrievedObjects.Should().BeEquivalentTo(expectedObjects);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@ private static int GetRandomNumber() =>
private static DateTimeOffset GetRandomDateTimeOffset() =>
new DateTimeRange(earliestDate: new DateTime()).GetValue();

private static List<dynamic> CreateDynamicCars(List<Car> cars)
{
return cars
.Select(car =>
{
dynamic item = new ExpandoObject();
item.Make = car.Make;
item.Model = car.Model;
item.Year = car.Year.ToString();
item.Color = car.Color;

return item;
})
.ToList<dynamic>();
}

private static List<object> CreateAnonymousObjectCars(List<Car> cars)
{
return cars
.Select(car => new
{
Make = car.Make,
Model = car.Model,
Year = car.Year,
Color = car.Color
})
.ToList<object>();
}


private static List<Car> CreateRandomCars()
{
return CreateCarFiller()
Expand Down Expand Up @@ -83,5 +113,35 @@ private string GetCsvRepresentationOfCar(

return csvBuilder.ToString();
}

private string GetCsvRepresentationOfDynamicObject(
List<dynamic> 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