Skip to content

Commit

Permalink
add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasKim committed Jul 15, 2023
1 parent b8bdcb0 commit c7bf164
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 15 deletions.
7 changes: 7 additions & 0 deletions SharpLibrary.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lending.Infrastructure", "s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lending.UnitTests", "src\Lending\Tests\Lending.Tests.UnitTests\Lending.UnitTests.csproj", "{F2570521-0973-49E8-98E4-9502529B1600}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lending.IntegrationTests", "src\Lending\Tests\Lending.IntegrationTests\Lending.IntegrationTests.csproj", "{47686A90-666E-4DB1-AF9C-5AC364EE6F74}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -81,6 +83,10 @@ Global
{F2570521-0973-49E8-98E4-9502529B1600}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2570521-0973-49E8-98E4-9502529B1600}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2570521-0973-49E8-98E4-9502529B1600}.Release|Any CPU.Build.0 = Release|Any CPU
{47686A90-666E-4DB1-AF9C-5AC364EE6F74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47686A90-666E-4DB1-AF9C-5AC364EE6F74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47686A90-666E-4DB1-AF9C-5AC364EE6F74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47686A90-666E-4DB1-AF9C-5AC364EE6F74}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -99,6 +105,7 @@ Global
{9BEE17AA-197D-4C88-BB39-A1AFCEE952CB} = {93037DB4-D7D7-41B3-97C2-B8AA0E2CE7BF}
{FF082753-BD37-4BFE-8514-9CA89C4AB225} = {9BEE17AA-197D-4C88-BB39-A1AFCEE952CB}
{F2570521-0973-49E8-98E4-9502529B1600} = {9376673B-42BC-4B24-9F12-723FE1D6DC64}
{47686A90-666E-4DB1-AF9C-5AC364EE6F74} = {9376673B-42BC-4B24-9F12-723FE1D6DC64}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {403C711D-BF79-4140-AD10-15ABC0952005}
Expand Down
32 changes: 19 additions & 13 deletions src/Lending/API/Lending.API/Features/PatronHold/Endpoint.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using FastEndpoints;
using Lending.Domain.BookAggregate;
using Lending.Domain.PatronAggregate;
using Lending.Infrastructure;

namespace Lending.API.Features.PatronHold;

public class PatronHoldRequest
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public Guid PatronId { get; set; }
public Guid BookId { get; set; }
}

public class PatronHoldResponse
Expand All @@ -17,22 +19,26 @@ public class PatronHoldResponse

public class PatronHoldEndpoint : Endpoint<PatronHoldRequest>
{
private readonly IRepository _repository;

public PatronHoldEndpoint(IRepository repository)
{
_repository = repository;
}

public override void Configure()
{
Post("myevent");
Post("api/patronhold");
AllowAnonymous();
}

public override async Task HandleAsync(PatronHoldRequest req, CancellationToken ct)
{
Console.WriteLine($"Hello world {req.FirstName}");
public override async Task HandleAsync(PatronHoldRequest request, CancellationToken ct)
{
var patron = await _repository.Get<Patron>(request.PatronId);
var book = await _repository.Get<Book>(request.BookId);

var response = new PatronHoldResponse()
{
FullName = req.FirstName + " " + req.LastName,
IsOver18 = req.Age > 18
};
patron.HoldBook(book);

await SendAsync(response, cancellation: ct);
await SendOkAsync(ct);
}
}
2 changes: 2 additions & 0 deletions src/Lending/API/Lending.API/Lending.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

<ItemGroup>
<ProjectReference Include="..\..\..\Core\Core.Application\Core.Application.csproj" />
<ProjectReference Include="..\..\Domain\Patron\Patron\PatronAggregate.csproj" />
<ProjectReference Include="..\..\Infrastructure\Lending.Infrastructure\Lending.Infrastructure.csproj" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions src/Lending/API/Lending.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
.AddControllers();

var app = builder.Build();

app.UseFastEndpoints();
app.UseDefaultExceptionHandler();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<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="AutoFixture.Xunit2" Version="4.18.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\API\Lending.API\Lending.API.csproj" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions src/Lending/Tests/Lending.IntegrationTests/PatronHoldE2ETests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoFixture.Xunit2;
using FastEndpoints;
using Lending.API;
using Lending.API.Features.PatronHold;
using Lending.Infrastructure;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;

namespace Lending.IntegrationTests
{
public class PatronHoldE2ETests
{
public static HttpClient Client { get; } = new WebApplicationFactory<AppSettings>()
.WithWebHostBuilder(b =>
b.ConfigureServices(s =>
s.AddScoped<IRepository, Repository>()))
.CreateClient();

[Theory, AutoData]
public async Task Test(PatronHoldRequest request, Guid PatronId, Guid BookId)
{
var result = await Client.POSTAsync<PatronHoldEndpoint, PatronHoldRequest, PatronHoldResponse >(new() { PatronId = PatronId, BookId = BookId});
Console.WriteLine();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoFixture.Xunit2;
using Lending.API.Features.PatronHold;
using Lending.Infrastructure;

namespace Lending.IntegrationTests
{
public class PatronHoldIntergrationsTests
{

[Theory, AutoData]
public async Task Test(PatronHoldRequest request)
{
var endpoint = new PatronHoldEndpoint(new Repository());
await endpoint.HandleAsync(request, default);
Console.WriteLine();
}
}
}
1 change: 1 addition & 0 deletions src/Lending/Tests/Lending.IntegrationTests/Usings.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
Expand Up @@ -25,8 +25,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Domain\Patron\Patron\PatronAggregate.csproj" />
<ProjectReference Include="..\..\..\Infrastructure\Lending.Infrastructure\Lending.Infrastructure.csproj" />
<ProjectReference Include="..\..\Domain\Patron\Patron\PatronAggregate.csproj" />
<ProjectReference Include="..\..\Infrastructure\Lending.Infrastructure\Lending.Infrastructure.csproj" />
</ItemGroup>
Expand Down
13 changes: 13 additions & 0 deletions src/Lending/Tests/Lending.Tests.UnitTests/RepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,18 @@ public async Task Get(Patron patron, Repository repo)

success.Should().BeTrue();
result.Should().BeEquivalentTo(patron);
}

[Theory, AutoData]
public async Task Upsert_Update(Patron patron, Patron patron2, Repository repo)
{
bool initial = await repo.Upsert(patron.Id, patron);
bool final = await repo.Upsert(patron.Id, patron2);

var result = await repo.Get<Patron>(patron.Id);

initial.Should().BeTrue();
final.Should().BeTrue();
result.Should().BeEquivalentTo(patron2);
}
}

0 comments on commit c7bf164

Please sign in to comment.