Skip to content

Commit

Permalink
Added Unit tests + updated FluentAssertions package.
Browse files Browse the repository at this point in the history
  • Loading branch information
brenordv committed Dec 17, 2024
1 parent f66fde0 commit 62da565
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 30 deletions.
7 changes: 7 additions & 0 deletions NightScout.Raccoon.sln
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "97. Pipelines", "97. Pipeli
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
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raccoon.Ninja.AzFn.DataApi.Tests", "Raccoon.Ninja.AzFn.DataApi.Tests\Raccoon.Ninja.AzFn.DataApi.Tests.csproj", "{A4B79A3C-396D-429F-BC30-012DF68C690A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -88,6 +90,10 @@ Global
{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
{A4B79A3C-396D-429F-BC30-012DF68C690A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4B79A3C-396D-429F-BC30-012DF68C690A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4B79A3C-396D-429F-BC30-012DF68C690A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4B79A3C-396D-429F-BC30-012DF68C690A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2B19A689-B703-450C-9E06-DFD0BE969053} = {11F72AFB-B94A-4E9A-8817-F1FB60E9DF6D}
Expand All @@ -100,5 +106,6 @@ Global
{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}
{A4B79A3C-396D-429F-BC30-012DF68C690A} = {BC1845AD-353C-4548-A86D-80A7EBC55C34}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Raccoon.Ninja.AzFn.DataApi.ExtensionMethods;

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

public class HttpRequestExtensionsTests
{
[Fact]
public void TryGetReadSinceParam_NoParameter_ReturnsNull()
{
// Arrange
var context = new DefaultHttpContext();
var request = context.Request;

// Act
var result = request.TryGetReadSinceParam();

// Assert
result.Should().BeNull();
}

[Fact]
public void TryGetReadSinceParam_InvalidParameter_ReturnsNull()
{
// Arrange
var context = new DefaultHttpContext();
var request = context.Request;
request.QueryString = new QueryString("?readSince=notANumber");

// Act
var result = request.TryGetReadSinceParam();

// Assert
result.Should().BeNull();
}

[Fact]
public void TryGetReadSinceParam_ValidParameter_ReturnsLongValue()
{
// Arrange
var context = new DefaultHttpContext();
var request = context.Request;
request.QueryString = new QueryString("?readSince=123456");

// Act
var result = request.TryGetReadSinceParam();

// Assert
result.Should().NotBeNull();
result.Should().Be(123456L);
}

[Fact]
public void TryGetReadSinceParam_ValidParameterSetThroughQueryProperty_ReturnsLongValue()
{
// Arrange
var context = new DefaultHttpContext();
var request = context.Request;

// Setting Query directly using a dictionary.
// Although Query is read-only, we can reassign it using the constructor of QueryCollection.
var queryDictionary = new Dictionary<string, StringValues>
{
{ "readSince", "987654" }
};
request.QueryString = QueryString.Create(queryDictionary);

// Act
var result = request.TryGetReadSinceParam();

// Assert
result.Should().NotBeNull();
result.Should().Be(987654L);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="FluentAssertions" Version="6.12.2"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>

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

</Project>
6 changes: 5 additions & 1 deletion Raccoon.Ninja.AzFn.DataApi/DataApiFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Raccoon.Ninja.AzFn.DataApi;

public class DataApiFunc
{
private const int maxItems = 1;
private readonly ILogger _logger;

public DataApiFunc(ILogger<DataApiFunc> logger)
Expand Down Expand Up @@ -69,6 +70,7 @@ public async Task<IActionResult> RunAsync(
new QueryDefinition("SELECT TOP 1 * FROM c ORDER BY c.readAt DESC")
);

var docsFetched = 0;
while (iteratorLatestReading.HasMoreResults)
{
var response = await iteratorLatestReading.ReadNextAsync();
Expand All @@ -80,7 +82,9 @@ public async Task<IActionResult> RunAsync(
return new OkObjectResult(results);
}

break;
docsFetched++;
if (docsFetched >= maxItems)
break;
}

return new NoContentResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="FluentAssertions" Version="6.12.2"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0"/>
<PackageReference Include="xunit" Version="2.8.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand All @@ -24,8 +24,8 @@
</ItemGroup>

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

</Project>
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<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.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Raccoon.Ninja.Domain.Core\Raccoon.Ninja.Domain.Core.csproj" />
<ProjectReference Include="..\Raccoon.Ninja.TestHelpers\Raccoon.Ninja.TestHelpers.csproj" />
</ItemGroup>
<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.2"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0"/>
<PackageReference Include="xunit" Version="2.8.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Raccoon.Ninja.Domain.Core\Raccoon.Ninja.Domain.Core.csproj"/>
<ProjectReference Include="..\Raccoon.Ninja.TestHelpers\Raccoon.Ninja.TestHelpers.csproj"/>
</ItemGroup>
</Project>

0 comments on commit 62da565

Please sign in to comment.