Skip to content

Commit

Permalink
Lower case VM OS and region
Browse files Browse the repository at this point in the history
Better to be lenient on user input, e.g. 'Windows' should match
'windows'.
  • Loading branch information
gabrielweyer committed Oct 13, 2024
1 parent b04f767 commit cce0cda
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions coster/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions coster/src/AzureVmCoster/Models/Csv/InputVmMap.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using CsvHelper;
using CsvHelper.TypeConversion;

namespace AzureVmCoster.Models.Csv;

public sealed class InputVmMap : ClassMap<InputVm>
Expand All @@ -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<LowercaseConverter>();
Map(v => v.Name);
Map(v => v.Region);
Map(v => v.Region).TypeConverter<LowercaseConverter>();
}

private sealed class LowercaseConverter : DefaultTypeConverter
{
public override object? ConvertFromString(string? text, IReaderRow row, MemberMapData memberMapData)
{
return text?.ToLowerInvariant();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RAM,Region,CPU,Name,Operating System
8,Us-West,4,name-1,Windows
19 changes: 19 additions & 0 deletions coster/tests/AzureVmCosterTests/Services/InputVmParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputVm>
{
new() {Name = "name-1", Region = "us-west", Cpu = 4, Ram = 8, OperatingSystem = "windows"}
};
Assert.NotNull(actualVms);
actualVms.Should().BeEquivalentTo(expected);
}
}

0 comments on commit cce0cda

Please sign in to comment.