Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Medications.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Project Path="src\Medications.AppHost\Medications.AppHost.csproj" />
</Folder>
<Folder Name="/tests/">
<File Path="test.runsettings" />
<File Path="tests\test.runsettings" />
<Project Path="tests\Medications.Unit.Tests\Medications.Unit.Tests.csproj" />
</Folder>
</Solution>
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![CI](https://github.com/fernandotonacoder/medications-rest-api/actions/workflows/ci.yml/badge.svg)](https://github.com/fernandotonacoder/medications-rest-api/actions/workflows/ci.yml)
[![.NET](https://img.shields.io/badge/.NET-10-512BD4?logo=dotnet&logoColor=white)](https://dotnet.microsoft.com/)
[![Aspire](https://img.shields.io/badge/Aspire-512BD4?logo=dotnet&logoColor=white)](https://learn.microsoft.com/en-us/dotnet/aspire/)
[![SQL Server](https://img.shields.io/badge/SQL%20Server-2022-CC2927)](https://www.microsoft.com/sql-server)
[![SQL Server](https://img.shields.io/badge/SQL%20Server-2022-0078D4?logo=microsoftsqlserver&logoColor=white)](https://www.microsoft.com/sql-server)
[![Scalar](https://img.shields.io/badge/Scalar-API%20Reference-1F2937)](https://scalar.com/)

[![Quality gate](https://sonarcloud.io/api/project_badges/quality_gate?project=fernandotonacoder_medications-rest-api)](https://sonarcloud.io/summary/overall?id=fernandotonacoder_medications-rest-api)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Medications.Api.Data;
using Medications.Api.Endpoints;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace Medications.Unit.Tests
{
public class MedicationEndpointsRegistrationUnitTests
{
[Theory]
[InlineData("GET", "/api/medications/")]
[InlineData("GET", "/api/medications/{id}")]
[InlineData("POST", "/api/medications/")]
[InlineData("DELETE", "/api/medications/{id}")]
public void MapMedicationEndpoints_RegistersRoute(string httpMethod, string pattern)
{
var endpoints = MapEndpoints();

Assert.Contains(endpoints, endpoint =>
endpoint.RoutePattern.RawText == pattern
&& endpoint.Metadata.GetMetadata<HttpMethodMetadata>()!.HttpMethods.Contains(httpMethod));
}

[Fact]
public void MapMedicationEndpoints_RegistersOnlyTheFourEndpoints()
{
Assert.Equal(4, MapEndpoints().Count);
}

[Fact]
public void GetMedicationById_IsNamed_SoCreatedAtRouteResolvesTheLocationHeader()
{
var endpoints = MapEndpoints();

var named = Assert.Single(endpoints, endpoint =>
endpoint.Metadata.GetMetadata<IEndpointNameMetadata>()?.EndpointName == "GetMedication");
Assert.Equal("/api/medications/{id}", named.RoutePattern.RawText);
}

private static List<RouteEndpoint> MapEndpoints()
{
var builder = WebApplication.CreateSlimBuilder();
builder.Services.AddDbContext<MedicationsDbContext>(
options => options.UseInMemoryDatabase(nameof(MedicationEndpointsRegistrationUnitTests)));
builder.Services.AddSingleton(TimeProvider.System);

var app = builder.Build();

app.MapMedicationEndpoints();

return ((IEndpointRouteBuilder)app).DataSources
.SelectMany(dataSource => dataSource.Endpoints)
.OfType<RouteEndpoint>()
.ToList();
}
}
}
2 changes: 0 additions & 2 deletions tests/Medications.Unit.Tests/Medications.Unit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

<PropertyGroup>
<IsPackable>false</IsPackable>
<!-- Visual Studio only auto-detects a .runsettings in the solution root,
so pointing at it explicitly is needed. -->
<RunSettingsFilePath>$(MSBuildThisFileDirectory)..\test.runsettings</RunSettingsFilePath>
</PropertyGroup>

Expand Down