Skip to content

Commit

Permalink
Added tests to the extension methods in the ScheduledTasks project.
Browse files Browse the repository at this point in the history
  • Loading branch information
Breno RdV committed Feb 11, 2024
1 parent 041447b commit e4ec0cb
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
7 changes: 7 additions & 0 deletions NightScout.Raccoon.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "97. Pipelines", "97. Pipeli
.github\workflows\template-test-build-and-publish-to-azure.yml = .github\workflows\template-test-build-and-publish-to-azure.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raccoon.Ninja.AzFn.ScheduledTasks.Tests", "Raccoon.Ninja.AzFn.ScheduledTasks.Tests\Raccoon.Ninja.AzFn.ScheduledTasks.Tests.csproj", "{D4D62A6B-724A-4499-B462-A9E95E7896CD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -82,6 +84,10 @@ Global
{1CAC242F-719D-4E31-BEF8-7E3972F12482}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1CAC242F-719D-4E31-BEF8-7E3972F12482}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1CAC242F-719D-4E31-BEF8-7E3972F12482}.Release|Any CPU.Build.0 = Release|Any CPU
{D4D62A6B-724A-4499-B462-A9E95E7896CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D4D62A6B-724A-4499-B462-A9E95E7896CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4D62A6B-724A-4499-B462-A9E95E7896CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D4D62A6B-724A-4499-B462-A9E95E7896CD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2B19A689-B703-450C-9E06-DFD0BE969053} = {11F72AFB-B94A-4E9A-8817-F1FB60E9DF6D}
Expand All @@ -93,5 +99,6 @@ Global
{734F0969-C86D-449F-80FF-DC8129C07A61} = {11F72AFB-B94A-4E9A-8817-F1FB60E9DF6D}
{7BC539F4-D458-40C0-8277-0E6D24416E8C} = {0F4894B1-AE2F-4119-94D4-82B7B33A7C39}
{1CAC242F-719D-4E31-BEF8-7E3972F12482} = {11F72AFB-B94A-4E9A-8817-F1FB60E9DF6D}
{D4D62A6B-724A-4499-B462-A9E95E7896CD} = {BC1845AD-353C-4548-A86D-80A7EBC55C34}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using FluentAssertions;
using Raccoon.Ninja.AzFn.ScheduledTasks.ExtensionMethods;
using Raccoon.Ninja.TestHelpers;

namespace Raccoon.Ninja.AzFn.ScheduledTasks.Tests.ExtensionMethods;

public class ListExtensionsTests
{
[Fact]
public void ToGlucoseReadings_WhenCalled_ShouldReturnCorrectResult()
{
// Arrange
const int quantityDocuments = 2;
var documents = Generators.NightScoutMongoDocumentMockList(quantityDocuments);

var previousReading = Generators.GlucoseReadingMockSingle();

// Act
var result = documents.ToGlucoseReadings(previousReading).ToList();

// Assert
result.Should().NotBeNull();
result.Should().HaveCount(quantityDocuments);
result[0].Value.Should().Be(documents[0].Value);
result[1].Value.Should().Be(documents[1].Value);
}

[Fact]
public void ToGlucoseReadings_WhenCalledWillIgnoreNulls_ShouldReturnCorrectResult()
{
// Arrange
const int quantityDocuments = 2;
var documents = Generators.NightScoutMongoDocumentMockList(quantityDocuments);
documents.Add(null);
var previousReading = Generators.GlucoseReadingMockSingle();

// Act
var result = documents.ToGlucoseReadings(previousReading).ToList();

// Assert
result.Should().NotBeNull();
result.Should().HaveCount(quantityDocuments);
result[0].Value.Should().Be(documents[0].Value);
result[1].Value.Should().Be(documents[1].Value);
}
}
1 change: 1 addition & 0 deletions Raccoon.Ninja.AzFn.ScheduledTasks.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Raccoon.Ninja.AzFn.ScheduledTasks\Raccoon.Ninja.AzFn.ScheduledTasks.csproj" />
<ProjectReference Include="..\Raccoon.Ninja.TestHelpers\Raccoon.Ninja.TestHelpers.csproj" />
</ItemGroup>

</Project>
31 changes: 28 additions & 3 deletions Raccoon.Ninja.TestHelpers/Generators.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
using Bogus;
using MongoDB.Bson;
using Raccoon.Ninja.Domain.Core.Entities;
using Raccoon.Ninja.Domain.Core.Enums;
using Raccoon.Ninja.Domain.Core.Models;
using Raccoon.Ninja.Extensions.MongoDb.Models;

namespace Raccoon.Ninja.TestHelpers;

public static class Generators
{
public static IList<GlucoseReading> GlucoseReadingMockList(int qty, float value)
public static IList<NightScoutMongoDocument> NightScoutMongoDocumentMockList(int qty, int? value = null)
{
var faker = new Faker<NightScoutMongoDocument>()
.RuleFor(x => x.Id, f => new ObjectId())
.RuleFor(x => x.Value, f => value ?? f.Random.Number(60, 399))
.RuleFor(x => x.ReadingTimestamp, f => f.Date.Past().ToUnixTimestamp())
.RuleFor(x => x.ReadingTimestampAsString, f => f.Date.Past().ToString("yyyy-MM-ddTHH:mm:ssZ"))
.RuleFor(x => x.Trend, f => f.Random.Enum<Trend>())
.RuleFor(x => x.Direction, f => f.Random.String2(5))
.RuleFor(x => x.Device, f => f.Random.String2(5))
.RuleFor(x => x.Type, f => f.Random.String2(5))
.RuleFor(x => x.UtcOffset, f => f.Random.Number(-12, 12))
.RuleFor(x => x.SystemTime, f => f.Date.Past().ToString("yyyy-MM-ddTHH:mm:ssZ"));


return faker.Generate(qty).OrderBy(doc => doc.ReadingTimestamp).ToList();
}

public static IList<GlucoseReading> GlucoseReadingMockList(int qty, float? value = null)
{
var faker = new Faker<GlucoseReading>()
.RuleFor(x => x.Id, f => f.Random.Guid().ToString())
.RuleFor(x => x.Value, f => value)
.RuleFor(x => x.Value, f => value ?? f.Random.Number(60, 399))
.RuleFor(x => x.Trend, f => f.Random.Enum<Trend>())
.RuleFor(x => x.ReadTimestampUtc, f => f.Date.Past().ToUnixTimestamp());

return faker.Generate(qty);
}


public static GlucoseReading GlucoseReadingMockSingle(float? value = null)
{
return GlucoseReadingMockList(1, value)[0];
}

public static IList<HbA1CCalculationResponse> HbA1CCalculationResponseMockList(int qty, float? value = null,
float? delta = null, HbA1CCalculationStatus? status = null, string error = null)
{
Expand Down
1 change: 1 addition & 0 deletions Raccoon.Ninja.TestHelpers/Raccoon.Ninja.TestHelpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Raccoon.Ninja.Domain.Core\Raccoon.Ninja.Domain.Core.csproj" />
<ProjectReference Include="..\Raccoon.Ninja.Extensions.MongoDb\Raccoon.Ninja.Extensions.MongoDb.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Bogus" Version="35.4.0" />
Expand Down

0 comments on commit e4ec0cb

Please sign in to comment.