-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added `IValidatableObject` to `CsvWriterSettings`. - Updated Culture to accept both `Invariant`/`Current` and `InvariantCulture`/`CurrentCulture`. - Specified only the latter in README. - Added unit testing for `CsvWriterSettings`.
- Loading branch information
1 parent
a9aa572
commit 7374087
Showing
3 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
Extensions/Csv/Cosmos.DataTransfer.CsvExtension.UnitTests/CsvWriterSettingsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System.Globalization; | ||
using Cosmos.DataTransfer.JsonExtension.UnitTests; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using System.ComponentModel.DataAnnotations; | ||
using Cosmos.DataTransfer.CsvExtension.Settings; | ||
using Cosmos.DataTransfer.Interfaces; | ||
using Cosmos.DataTransfer.JsonExtension; | ||
|
||
namespace Cosmos.DataTransfer.CsvExtension.UnitTests; | ||
|
||
[TestClass] | ||
public class CsvWriterSettingsTests | ||
{ | ||
|
||
|
||
[TestMethod] | ||
public void TestDefault(string culture) { | ||
var settings = new CsvWriterSettings() { }; | ||
|
||
Assert.AreEqual(settings.GetCultureInfo(), CultureInfo.InvariantCulture); | ||
Assert.AreEqual(settings.Validate(new ValidationContext(this)).Count(), 0); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow("invariant")] | ||
[DataRow("Invariant")] | ||
[DataRow("invariantCulture")] | ||
[DataRow("invariantculture")] | ||
public void TestInvariantCulture(string culture) { | ||
var settings = new CsvWriterSettings() { | ||
Culture = culture | ||
}; | ||
Assert.AreEqual(settings.GetCultureInfo(), CultureInfo.InvariantCulture); | ||
Assert.AreEqual(settings.Validate(new ValidationContext(this)).Count(), 0); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow("current")] | ||
[DataRow("Current")] | ||
[DataRow("currentCultuRE")] | ||
[DataRow("currentCulture")] | ||
public void TestCurrentCulture(string culture) { | ||
var settings = new CsvWriterSettings() { | ||
Culture = culture | ||
}; | ||
Assert.AreEqual(settings.GetCultureInfo(), CultureInfo.CurrentCulture); | ||
Assert.AreEqual(settings.Validate(new ValidationContext(this)).Count(), 0); | ||
} | ||
|
||
[TestMethod] | ||
public void TestCurrentCultureByName() { | ||
var settings = new CsvWriterSettings() { | ||
Culture = CultureInfo.CurrentCulture.Name | ||
}; | ||
Assert.AreEqual(settings.GetCultureInfo(), CultureInfo.CurrentCulture); | ||
Assert.AreEqual(settings.Validate(new ValidationContext(this)).Count(), 0); | ||
} | ||
|
||
[TestMethod] | ||
public void TestCultureFails() { | ||
var settings = new CsvWriterSettings() { | ||
Culture = "not a culture" | ||
}; | ||
var results = settings.Validate(new ValidationContext(this)); | ||
Assert.AreEqual(results.Count(), 1); | ||
Assert.AreEqual(results.First().ErrorMessage, "Could not find CultureInfo `not a culture` on this system."); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow(null)] | ||
[DataRow("")] | ||
public void TestCultureMissing(string culture) { | ||
var settings = new CsvWriterSettings() { | ||
Culture = culture | ||
}; | ||
var results = settings.Validate(new ValidationContext(this)); | ||
Assert.AreEqual(results.Count(), 1); | ||
Assert.AreEqual(results.First().ErrorMessage, "Culture missing."); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestDanishCulture() { | ||
var outputFile = Path.GetTempFileName(); | ||
var config = TestHelpers.CreateConfig(new Dictionary<string, string> | ||
{ | ||
{ "FilePath", outputFile }, | ||
{ "IncludeHeader", "false" }, | ||
{ "Culture", "da-DK" }, | ||
{ "Delimiter", ";" } | ||
}); | ||
|
||
var data = new List<DictionaryDataItem> | ||
{ | ||
new(new Dictionary<string, object?> | ||
{ | ||
{ "Value", 1.2 } | ||
}) | ||
}; | ||
|
||
var sink = new CsvFileSink(); | ||
|
||
await sink.WriteAsync(data.ToAsyncEnumerable(), config, new JsonFileSource(), NullLogger.Instance); | ||
var result = await File.ReadAllTextAsync(outputFile); | ||
Assert.AreEqual(result, "1,2"); | ||
} | ||
} |
38 changes: 33 additions & 5 deletions
38
Extensions/Csv/Cosmos.DataTransfer.CsvExtension/Settings/CsvWriterSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,47 @@ | ||
using System.Globalization; | ||
using Cosmos.DataTransfer.Interfaces; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Cosmos.DataTransfer.CsvExtension.Settings; | ||
|
||
public class CsvWriterSettings : IDataExtensionSettings | ||
public class CsvWriterSettings : IDataExtensionSettings, IValidatableObject | ||
{ | ||
public bool IncludeHeader { get; set; } = true; | ||
public string Delimiter { get; set; } = ","; | ||
public string Culture { get; set; } = "InvariantCulture"; | ||
public CultureInfo GetCultureInfo() { | ||
switch (this.Culture.ToLower()) | ||
switch (this.Culture?.ToLower()) | ||
{ | ||
case "invariantculture": return CultureInfo.InvariantCulture; | ||
case "current": return CultureInfo.CurrentCulture; | ||
default: return CultureInfo.GetCultureInfo(this.Culture); | ||
case "invariant": | ||
case "invariantculture": | ||
return CultureInfo.InvariantCulture; | ||
case "current": | ||
case "currentculture": | ||
return CultureInfo.CurrentCulture; | ||
default: return CultureInfo.GetCultureInfo(this.Culture!); | ||
} | ||
} | ||
|
||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | ||
{ | ||
ValidationResult? result = null; | ||
try { | ||
_ = this.GetCultureInfo(); | ||
} catch (CultureNotFoundException) { | ||
result = new ValidationResult( | ||
$"Could not find CultureInfo `{this.Culture}` on this system.", | ||
new string[] { "Culture" } | ||
); | ||
} catch (ArgumentNullException) { | ||
result = new ValidationResult( | ||
$"Culture missing.", | ||
new string[] { "Culture" } | ||
); | ||
} | ||
|
||
|
||
if (result != null) { | ||
yield return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters