diff --git a/coster/.editorconfig b/coster/.editorconfig index 1e3cbd9..7767e6f 100644 --- a/coster/.editorconfig +++ b/coster/.editorconfig @@ -188,6 +188,10 @@ dotnet_diagnostic.IDE2003.severity = warning # csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental dotnet_diagnostic.IDE2004.severity = warning +# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1308 +# The data I'm matching to is in lower case +dotnet_diagnostic.CA1308.severity = none + # CSharp code style settings: [*.cs] # Newline settings diff --git a/coster/src/AzureVmCoster/Models/Csv/InputVmMap.cs b/coster/src/AzureVmCoster/Models/Csv/InputVmMap.cs index 6b37800..1dc7bdc 100644 --- a/coster/src/AzureVmCoster/Models/Csv/InputVmMap.cs +++ b/coster/src/AzureVmCoster/Models/Csv/InputVmMap.cs @@ -1,3 +1,6 @@ +using CsvHelper; +using CsvHelper.TypeConversion; + namespace AzureVmCoster.Models.Csv; public sealed class InputVmMap : ClassMap @@ -6,8 +9,16 @@ public InputVmMap() { Map(v => v.Cpu).Name("CPU"); Map(v => v.Ram).Name("RAM").TypeConverterOption.NumberStyles(NumberStyles.Currency); - Map(v => v.OperatingSystem).Name("Operating System"); + Map(v => v.OperatingSystem).Name("Operating System").TypeConverter(); Map(v => v.Name); - Map(v => v.Region); + Map(v => v.Region).TypeConverter(); + } + + private sealed class LowercaseConverter : DefaultTypeConverter + { + public override object? ConvertFromString(string? text, IReaderRow row, MemberMapData memberMapData) + { + return text?.ToLowerInvariant(); + } } } diff --git a/coster/tests/AzureVmCosterTests/SampleInputs/input-en-au-wrong-case.csv b/coster/tests/AzureVmCosterTests/SampleInputs/input-en-au-wrong-case.csv new file mode 100644 index 0000000..695d39e --- /dev/null +++ b/coster/tests/AzureVmCosterTests/SampleInputs/input-en-au-wrong-case.csv @@ -0,0 +1,2 @@ +RAM,Region,CPU,Name,Operating System +8,Us-West,4,name-1,Windows diff --git a/coster/tests/AzureVmCosterTests/Services/InputVmParserTests.cs b/coster/tests/AzureVmCosterTests/Services/InputVmParserTests.cs index 1363a07..822f4ae 100644 --- a/coster/tests/AzureVmCosterTests/Services/InputVmParserTests.cs +++ b/coster/tests/AzureVmCosterTests/Services/InputVmParserTests.cs @@ -71,4 +71,23 @@ public void GivenExactMatchInputAndCultureWithCommaDecimalPoint_WhenParse_ThenPa Assert.NotNull(actualVms); actualVms.Should().BeEquivalentTo(_expected); } + + [Fact] + public void GivenRegionAndOperatingSystemWithUnexpectedCase_WhenParse_ThenLowerCase() + { + // Arrange + var file = new FileInfo("SampleInputs/input-en-au-wrong-case.csv"); + var culture = new CultureInfo("en-au"); + + // Act + var actualVms = InputVmParser.Parse(file, culture); + + // Assert + var expected = new List + { + new() {Name = "name-1", Region = "us-west", Cpu = 4, Ram = 8, OperatingSystem = "windows"} + }; + Assert.NotNull(actualVms); + actualVms.Should().BeEquivalentTo(expected); + } }