Skip to content

Lower case Operating System and region when parsing VMs #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2024
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
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);
}
}