Skip to content

Commit

Permalink
backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mlapaglia committed Nov 23, 2023
1 parent 8574ee3 commit bfc05e8
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 2 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@ jobs:
- uses: actions/checkout@v2
- name: Run build
run: docker build . --file ./OpenAlprWebhookProcessor/Dockerfile

back-end-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run backend tests
run: dotnet test --configuration Release --no-build --collect:"XPlat Code Coverage" --results-directory ./coverage
- name: Code Coverage Report
uses: irongut/[email protected]
with:
filename: coverage/**/coverage.cobertura.xml
badge: true
fail_below_min: true
format: markdown
hide_branch_rate: false
hide_complexity: true
indicators: true
output: both
thresholds: '60 80'

- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@v2
if: github.event_name == 'pull_request'
with:
recreate: true
path: code-coverage-results.md

front-end-tests:
runs-on: ubuntu-latest
Expand Down
10 changes: 8 additions & 2 deletions OpenAlprWebhookProcessor.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
# Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{350E0804-C372-49AB-B670-75E037878FAF}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenAlprWebhookProcessor", "OpenAlprWebhookProcessor\OpenAlprWebhookProcessor.csproj", "{7C3E9804-0A13-4060-B5FD-91A613647146}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{DD123D3E-8A09-4104-AF20-D5FBFAA6F52E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -20,6 +22,10 @@ Global
{7C3E9804-0A13-4060-B5FD-91A613647146}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C3E9804-0A13-4060-B5FD-91A613647146}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C3E9804-0A13-4060-B5FD-91A613647146}.Release|Any CPU.Build.0 = Release|Any CPU
{DD123D3E-8A09-4104-AF20-D5FBFAA6F52E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD123D3E-8A09-4104-AF20-D5FBFAA6F52E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD123D3E-8A09-4104-AF20-D5FBFAA6F52E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD123D3E-8A09-4104-AF20-D5FBFAA6F52E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
57 changes: 57 additions & 0 deletions Tests/AgentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using OpenAlprWebhookProcessor.Data;
using OpenAlprWebhookProcessor.Settings;

namespace Tests
{
public class AgentTests
{
private EfContextCreator _contextCreator;

[SetUp]
public void Setup()
{
_contextCreator = new EfContextCreator();
}

[Test]
public async Task AgentRequestSucceeds()
{
using (var processorContext = _contextCreator.CreateContext())
{
if (processorContext.Database.EnsureCreated())
{
processorContext.Agents.Add(new OpenAlprWebhookProcessor.Data.Agent()
{
EndpointUrl = "http://google.com",
Hostname = "localhost",
Id = Guid.NewGuid(),
IsDebugEnabled = true,
IsImageCompressionEnabled = true,
LastHeartbeatEpochMs = 0,
LastSuccessfulScrapeEpoch = 0,
Latitude = 0,
Longitude = 0,
NextScrapeEpochMs = 0,
OpenAlprWebServerApiKey = null,
OpenAlprWebServerUrl = null,
ScheduledScrapingIntervalMinutes = 0,
SunriseOffset = 0,
SunsetOffset = 0,
TimeZoneOffset = 0,
Uid = null,
Version = null,
});

await processorContext.SaveChangesAsync();
var handler = new GetAgentRequestHandler(processorContext);

var agentResult = await handler.HandleAsync(default);

Assert.That(agentResult, Is.Not.Null);
}
}
}
}
}
31 changes: 31 additions & 0 deletions Tests/EfContextCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using OpenAlprWebhookProcessor.Data;

namespace Tests
{
public class EfContextCreator
{
readonly SqliteConnection _connection;

readonly DbContextOptions<ProcessorContext> _contextOptions;

public EfContextCreator()
{
_connection = new SqliteConnection("Filename=:memory:");
_connection.Open();

_contextOptions = new DbContextOptionsBuilder<ProcessorContext>()
.UseSqlite(_connection)
.Options;

using var context = new ProcessorContext(_contextOptions);

context.Database.EnsureCreated();
}

public ProcessorContext CreateContext() => new(_contextOptions);

public void Dispose() => _connection.Dispose();
}
}
1 change: 1 addition & 0 deletions Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
24 changes: 24 additions & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OpenAlprWebhookProcessor\OpenAlprWebhookProcessor.csproj" />
</ItemGroup>

</Project>

0 comments on commit bfc05e8

Please sign in to comment.