Skip to content

Commit

Permalink
Merge pull request #13 from brenordv/feature/fix-sonar-issues
Browse files Browse the repository at this point in the history
Feature/fix sonar issues
  • Loading branch information
brenordv authored Feb 4, 2024
2 parents 552e248 + cfb4a25 commit f12ff65
Show file tree
Hide file tree
Showing 25 changed files with 468 additions and 182 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/qa-on-pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
projectKey: 'brenordv_nightscout-companion-apps'
organization: 'raccoon-ninja'
verbose: true
sonarExclusions: '**/*Usings.cs,Raccoon.Ninja.TestHelpers/*'
coverageExclusions: '**/*Usings.cs,Raccoon.Ninja.TestHelpers/*,**/*Program.cs,Raccoon.Ninja.WForm.GlucoseIcon/Utils/AppSettings.cs,Raccoon.Ninja.AzFn.DataApi/DataLatestHbA1cFunc.cs,Raccoon.Ninja.AzFn.DataTransfer/HbA1cCalcFunc.cs'

secrets:
githubToken: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/template-qa-sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
$sonarExclusions = "${{ inputs.sonarExclusions }}"
$coverageExclusions = "${{ inputs.coverageExclusions }}"
$sonarBeginCmd = ".\.sonar\scanner\dotnet-sonarscanner begin /k:`"${{ inputs.projectKey }}`" /o:`"${{ inputs.organization }}`" /d:sonar.login=`"${{ secrets.sonarToken }}`" /d:sonar.host.url=`"https://sonarcloud.io`" /d:`"sonar.verbose=$verboseFlag`""
$sonarBeginCmd = ".\.sonar\scanner\dotnet-sonarscanner begin /k:`"${{ inputs.projectKey }}`" /o:`"${{ inputs.organization }}`" /d:sonar.login=`"${{ secrets.sonarToken }}`" /d:sonar.host.url=`"https://sonarcloud.io`" /d:`"sonar.verbose=$verboseFlag`" /d:sonar.cs.vscoveragexml.reportsPaths=`"coverage.xml`""
if (-not [string]::IsNullOrWhiteSpace($sonarExclusions)) {
$sonarBeginCmd += " /d:`"sonar.exclusions=$sonarExclusions`""
}
Expand Down
10 changes: 5 additions & 5 deletions Raccoon.Ninja.AzFn.DataApi/DataLatestHbA1cFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ public static async Task<IActionResult> RunAsync(
CreateIfNotExists = false,
SqlQuery = "SELECT TOP 1 * FROM c WHERE c.docType = 1 and c.status = 1 ORDER BY c.createdAt DESC"
)]
IEnumerable<HbA1cCalculation> latestSuccessCalculations,
IEnumerable<HbA1CCalculation> latestSuccessCalculations,
[CosmosDB(databaseName: "%CosmosDatabaseName%", containerName: "%CosmosAggregateContainerName%",
Connection = "CosmosConnectionString",
CreateIfNotExists = false,
SqlQuery = "SELECT TOP 1 * FROM c WHERE c.docType = 1 and c.status = 2 ORDER BY c.createdAt DESC"
)]
IEnumerable<HbA1cCalculation> latestPartialSuccessCalculations,
IEnumerable<HbA1CCalculation> latestPartialSuccessCalculations,
ILogger log)
{
log.LogInformation("Data Latest HbA1c API call received. Request by IP: {Ip}",
req.HttpContext.Connection.RemoteIpAddress);
HbA1cCalculation latestSuccessful = null;
HbA1cCalculation latestPartialSuccessful = null;
HbA1CCalculation latestSuccessful = null;
HbA1CCalculation latestPartialSuccessful = null;

try
{
Expand All @@ -50,7 +50,7 @@ public static async Task<IActionResult> RunAsync(
if (latestSuccessful is null && latestPartialSuccessful is null)
return new NoContentResult();

return new OkObjectResult(new DataLatestHbA1cFuncResponse
return new OkObjectResult(new DataLatestHbA1CFuncResponse
{
LatestSuccessful = latestSuccessful,
LatestPartialSuccessful = latestPartialSuccessful
Expand Down
4 changes: 2 additions & 2 deletions Raccoon.Ninja.AzFn.DataApi/Raccoon.Ninja.AzFn.DataApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>V4</AzureFunctionsVersion>
<AssemblyVersion>1.1.0</AssemblyVersion>
<FileVersion>1.1.0</FileVersion>
<AssemblyVersion>1.1.1</AssemblyVersion>
<FileVersion>1.1.1</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
</PropertyGroup>
<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions Raccoon.Ninja.AzFn.DataTransfer/HbA1cCalcFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public static async Task RunAsync(
Connection = "CosmosConnectionString",
CreateIfNotExists = false,
SqlQuery = "SELECT TOP 1 * FROM c WHERE c.docType = 1 ORDER BY c.createdAt DESC"
)] IEnumerable<HbA1cCalculation> previousCalculations,
)] IEnumerable<HbA1CCalculation> previousCalculations,
[CosmosDB(databaseName: "%CosmosDatabaseName%", containerName: "%CosmosAggregateContainerName%",
Connection = "CosmosConnectionString",
CreateIfNotExists = false,
PartitionKey = "/id"
)]IAsyncCollector<HbA1cCalculation> calculationsOut,
)]IAsyncCollector<HbA1CCalculation> calculationsOut,
ILogger log)
{
var referenceDate = DateOnly.FromDateTime(DateTime.UtcNow);
Expand All @@ -42,7 +42,7 @@ public static async Task RunAsync(
{
var previousCalculation = previousCalculations.FirstOrDefault();

var hbA1c = readings.CalculateHbA1c(referenceDate);
var hbA1c = readings.CalculateHbA1C(referenceDate);

if (previousCalculation is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>V4</AzureFunctionsVersion>
<AssemblyVersion>1.1.0</AssemblyVersion>
<FileVersion>1.1.0</FileVersion>
<AssemblyVersion>1.1.1</AssemblyVersion>
<FileVersion>1.1.1</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
</PropertyGroup>
<ItemGroup>
Expand Down
19 changes: 18 additions & 1 deletion Raccoon.Ninja.Cli.CGMDataDisplayLauncher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
try
{
Logger.LogTrace("Starting CGM Data Display application");

// Construct the fully qualified path to the application
var appFullPath = GetFullPathToApp(appName);

Process.Start(new ProcessStartInfo
{
FileName = appName,
FileName = appFullPath,
WindowStyle = ProcessWindowStyle.Normal
});
}
Expand All @@ -25,3 +29,16 @@
{
Logger.LogTrace("Exiting Launcher");
}

return;

static string GetFullPathToApp(string appName)
{
var assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
var assemblyPath = Path.GetDirectoryName(assemblyLocation);

// Construct the fully qualified path to the application
return string.IsNullOrWhiteSpace(assemblyPath)
? appName
: Path.Combine(assemblyPath, appName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<AssemblyVersion>1.1.0</AssemblyVersion>
<FileVersion>1.1.0</FileVersion>
<AssemblyVersion>1.1.1</AssemblyVersion>
<FileVersion>1.1.1</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<AssemblyName>CGMDataDisplay.Launcher</AssemblyName>
</PropertyGroup>
Expand Down
111 changes: 111 additions & 0 deletions Raccoon.Ninja.Domain.Core.Tests/Entities/HbA1CCalculationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using Raccoon.Ninja.Domain.Core.Entities;
using Raccoon.Ninja.Domain.Core.Enums;
using Raccoon.Ninja.Domain.Core.ExtensionMethods;

namespace Raccoon.Ninja.Domain.Core.Tests.Entities;

public class HbA1CCalculationTests
{
[Fact]
public void PropInit_ShouldSetPropertiesCorrectly()
{
// Arrange
var referenceDate = DateOnly.FromDateTime(DateTime.UtcNow);
var id = Guid.NewGuid().ToString();
const HbA1CCalculationStatus status = HbA1CCalculationStatus.Error;
const string error = "Sample error";
const float value = 5.5f;
const float delta = 0.5f;

// Act
var calculation = new HbA1CCalculation
{
Id = id,
Value = value,
Delta = delta,
ReferenceDate = referenceDate,
Status = status,
Error = error
};

// Assert
calculation.DocType.Should().Be(AggregateType.HbA1CCalculation);
calculation.ReferenceDate.Should().Be(referenceDate);
calculation.CreatedAtUtc.Should().BeCloseTo(DateTime.UtcNow.ToUnixTimestamp(), 1000);
calculation.Status.Should().Be(status);
calculation.Error.Should().Be(error);
}

[Fact]
public void FromError_ShouldCreateErrorInstanceCorrectly()
{
// Arrange
const string error = "Error message";
var referenceDate = DateOnly.FromDateTime(DateTime.UtcNow);

// Act
var calculation = HbA1CCalculation.FromError(error, referenceDate);

// Assert
calculation.Id.Should().NotBeNullOrWhiteSpace();
calculation.Value.Should().Be(default);
calculation.Delta.Should().BeNull();
calculation.Status.Should().Be(HbA1CCalculationStatus.Error);
calculation.Error.Should().Be(error);
calculation.ReferenceDate.Should().Be(referenceDate);
calculation.DocType.Should().Be(AggregateType.HbA1CCalculation);
}

[Fact]
public void Records_WithSameValues_ShouldBeEqual()
{
// Arrange
var referenceDate = DateOnly.FromDateTime(DateTime.UtcNow);
var id = Guid.NewGuid().ToString();
const HbA1CCalculationStatus status = HbA1CCalculationStatus.Error; // Assuming Error is a valid enum value
const string error = "Error message";
const float value = 5.5f;
const float delta = 0.5f;


var calculation1 = new HbA1CCalculation
{
Id = id,
Value = value,
Delta = delta,
ReferenceDate = referenceDate,
Status = status,
Error = error
};

var calculation2 = new HbA1CCalculation
{
Id = id,
Value = value,
Delta = delta,
ReferenceDate = referenceDate,
Status = status,
Error = error
};

// Act & Assert
calculation1.Should().BeEquivalentTo(calculation2);
}

[Fact]
public void Constructor_WithNoArguments_ShouldSetDefaultValues()
{
// Act
var calculation = new HbA1CCalculation();

// Assert
calculation.Id.Should().NotBeNullOrWhiteSpace();
calculation.Value.Should().Be(default);
calculation.Delta.Should().BeNull();
calculation.DocType.Should().Be(AggregateType.HbA1CCalculation);
calculation.CreatedAtUtc.Should().BeCloseTo(DateTime.UtcNow.ToUnixTimestamp(), 1000);
calculation.Status.Should().Be(HbA1CCalculationStatus.NotCalculated);
calculation.Error.Should().BeNull();
calculation.ReferenceDate.Should().Be(default);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using FluentAssertions;
using Raccoon.Ninja.Domain.Core.Entities;
using Raccoon.Ninja.Domain.Core.Enums;
using Raccoon.Ninja.Domain.Core.ExtensionMethods;
using Raccoon.Ninja.TestHelpers;

namespace Raccoon.Ninja.Domain.Core.Tests.ExtensionMethods;

public class ListExtensionsTests
{
private static readonly DateOnly ReferenceDate = DateOnly.FromDateTime(DateTime.UtcNow);

[Fact]
public void HasElements_Should_Return_True_When_List_Has_Elements()
{
Expand All @@ -17,7 +22,7 @@ public void HasElements_Should_Return_True_When_List_Has_Elements()
//Assert
result.Should().BeTrue();
}

[Fact]
public void HasElements_Should_Return_False_When_List_Is_Null()
{
Expand All @@ -30,7 +35,7 @@ public void HasElements_Should_Return_False_When_List_Is_Null()
//Assert
result.Should().BeFalse();
}

[Fact]
public void HasElements_Should_Return_False_When_List_Is_Empty()
{
Expand All @@ -43,4 +48,71 @@ public void HasElements_Should_Return_False_When_List_Is_Empty()
//Assert
result.Should().BeFalse();
}

[Fact]
public void CalculateHbA1c_WhenListIsNull_ShouldReturnError()
{
// Arrange
IEnumerable<GlucoseReading> list = null;

// Act
var result = list.CalculateHbA1C(ReferenceDate);

// Assert
result.Status.Should().Be(HbA1CCalculationStatus.Error);
result.Error.Should().Be("No readings to calculate HbA1c");
}

[Fact]
public void CalculateHbA1c_WhenListIsEmpty_ShouldReturnError()
{
// Arrange
var list = new List<GlucoseReading>();

// Act
var result = list.CalculateHbA1C(ReferenceDate);

// Assert
result.Status.Should().Be(HbA1CCalculationStatus.Error);
result.Error.Should().Be("No readings returned from Db to calculate HbA1c");
}

[Fact]
public void CalculateHbA1c_WhenListHasMoreThanExpected_ShouldReturnError()
{
// Arrange
var actualReadingCount = Constants.ReadingsIn115Days + 1;
var readings = Generators.GlucoseReadingMockList(actualReadingCount, 100);

// Act
var result = readings.CalculateHbA1C(ReferenceDate);

// Assert
result.Status.Should().Be(HbA1CCalculationStatus.Error);
result.Error.Should().Be($"Too many readings to calculate HbA1c reliably. Expected (max) 33120 but got {actualReadingCount}");
}

[Theory]
[MemberData(nameof(TheoryGenerator.PartiallyValidHb1AcDataSets), MemberType = typeof(TheoryGenerator))]
public void CalculateHbA1c_WhenListHasOneReading_ShouldReturnPartialSuccess(List<GlucoseReading> readings, float expectedResult)
{
// Arrange & Act
var result = readings.CalculateHbA1C(ReferenceDate);

// Assert
result.Status.Should().Be(HbA1CCalculationStatus.SuccessPartial);
result.Value.Should().Be(expectedResult);
}

[Theory]
[MemberData(nameof(TheoryGenerator.ValidHb1AcDataSets), MemberType = typeof(TheoryGenerator))]
public void CalculateHbA1c_WhenListHasExactNumberOfReadings_ShouldReturnSuccess(List<GlucoseReading> readings, float expectedResult)
{
// Arrange & Act
var result = readings.CalculateHbA1C(ReferenceDate);

// Assert
result.Status.Should().Be(HbA1CCalculationStatus.Success);
result.Value.Should().Be(expectedResult);
}
}
Loading

0 comments on commit f12ff65

Please sign in to comment.