From 2600a321356d16cf3f30b91892baa6fa5624bc26 Mon Sep 17 00:00:00 2001 From: Alex Jackson Date: Tue, 13 Jul 2021 12:21:31 +0100 Subject: [PATCH 1/2] HEEDLS-467 Change units and remove decimal place below gigabytes --- .../Helpers/DisplayStringHelperTests.cs | 6 +++--- .../TrackingSystem/Centre/ContractDetailsViewModelTests.cs | 2 +- DigitalLearningSolutions.Web/Helpers/DisplayStringHelper.cs | 6 ++++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/DigitalLearningSolutions.Web.Tests/Helpers/DisplayStringHelperTests.cs b/DigitalLearningSolutions.Web.Tests/Helpers/DisplayStringHelperTests.cs index 0379118d26..ca768d1ec0 100644 --- a/DigitalLearningSolutions.Web.Tests/Helpers/DisplayStringHelperTests.cs +++ b/DigitalLearningSolutions.Web.Tests/Helpers/DisplayStringHelperTests.cs @@ -46,7 +46,7 @@ public void FormatBytesWithLimit_returns_expected_string_for_kilobytes() var result = DisplayStringHelper.FormatBytesWithLimit(12, 1200); // Then - result.Should().Be("12B / 1.2KiB"); + result.Should().Be("12B / 1KB"); } [Test] @@ -56,7 +56,7 @@ public void FormatBytesWithLimit_returns_expected_string_for_gibibytes() var result = DisplayStringHelper.FormatBytesWithLimit(12, Gibibyte); // Then - result.Should().Be("12B / 1GiB"); + result.Should().Be("12B / 1GB"); } [Test] @@ -69,7 +69,7 @@ public void FormatBytesWithLimit_returns_expected_string_when_less_than_next_siz var result = DisplayStringHelper.FormatBytesWithLimit(12, bytes); // Then - result.Should().Be("12B / 1024MiB"); + result.Should().Be("12B / 1024MB"); } [Test] diff --git a/DigitalLearningSolutions.Web.Tests/ViewModels/TrackingSystem/Centre/ContractDetailsViewModelTests.cs b/DigitalLearningSolutions.Web.Tests/ViewModels/TrackingSystem/Centre/ContractDetailsViewModelTests.cs index 57219d1ac8..34f571f0fe 100644 --- a/DigitalLearningSolutions.Web.Tests/ViewModels/TrackingSystem/Centre/ContractDetailsViewModelTests.cs +++ b/DigitalLearningSolutions.Web.Tests/ViewModels/TrackingSystem/Centre/ContractDetailsViewModelTests.cs @@ -40,7 +40,7 @@ public void AdminUsers_and_Centre_populate_expected_values() viewModel.TrainersColour.Should().Be("green"); viewModel.CustomCourses.Should().Be("10 / 12"); viewModel.CustomCoursesColour.Should().Be("yellow"); - viewModel.ServerSpace.Should().Be("1KiB / 1GiB"); + viewModel.ServerSpace.Should().Be("1KB / 1GB"); viewModel.ServerSpaceColour.Should().Be("green"); } diff --git a/DigitalLearningSolutions.Web/Helpers/DisplayStringHelper.cs b/DigitalLearningSolutions.Web/Helpers/DisplayStringHelper.cs index 5071e48ed7..a845171121 100644 --- a/DigitalLearningSolutions.Web/Helpers/DisplayStringHelper.cs +++ b/DigitalLearningSolutions.Web/Helpers/DisplayStringHelper.cs @@ -5,7 +5,7 @@ public static class DisplayStringHelper { private const string Divider = " / "; - private static readonly string[] Units = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" }; + private static readonly string[] Units = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; public static string FormatNumberWithLimit(int number, int limit) { @@ -30,7 +30,9 @@ private static string GenerateBytesDisplayString(long byteCount) } var place = Convert.ToInt32(Math.Floor(Math.Log(byteCount, 1024))); - var number = Math.Round(byteCount / Math.Pow(1024, place), 1); + // Do not include decimal place below GB + var decimalPlaces = place <= 2 ? 0 : 1; + var number = Math.Round(byteCount / Math.Pow(1024, place), decimalPlaces); return (Math.Sign(byteCount) * number) + Units[place]; } } From 4cf488660c162dfa15ba7d54b10ceb03b437c804 Mon Sep 17 00:00:00 2001 From: Alex Jackson Date: Tue, 13 Jul 2021 12:53:48 +0100 Subject: [PATCH 2/2] HEEDLS-467 Add unit tests for decimal values --- .../Helpers/DisplayStringHelperTests.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/DigitalLearningSolutions.Web.Tests/Helpers/DisplayStringHelperTests.cs b/DigitalLearningSolutions.Web.Tests/Helpers/DisplayStringHelperTests.cs index ca768d1ec0..22ba9eaec8 100644 --- a/DigitalLearningSolutions.Web.Tests/Helpers/DisplayStringHelperTests.cs +++ b/DigitalLearningSolutions.Web.Tests/Helpers/DisplayStringHelperTests.cs @@ -8,6 +8,7 @@ public class DisplayStringHelperTests { private const long Gibibyte = 1073741824; + private const long Mebibyte = 1048576; [Test] public void GenerateNumberWithLimitDisplayString_returns_expected_string_with_limit() @@ -59,6 +60,26 @@ public void FormatBytesWithLimit_returns_expected_string_for_gibibytes() result.Should().Be("12B / 1GB"); } + [Test] + public void FormatBytesWithLimit_returns_expected_string_for_gibibytes_with_decimal() + { + // When + var result = DisplayStringHelper.FormatBytesWithLimit(12, Gibibyte + 500000000); + + // Then + result.Should().Be("12B / 1.5GB"); + } + + [Test] + public void FormatBytesWithLimit_returns_expected_string_for_mebibytes_with_decimal() + { + // When + var result = DisplayStringHelper.FormatBytesWithLimit(12, Mebibyte + 800000); + + // Then + result.Should().Be("12B / 2MB"); + } + [Test] public void FormatBytesWithLimit_returns_expected_string_when_less_than_next_size() {