-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from MrDave1999/sample/add-tests
test: Add integration test project for samples
- Loading branch information
Showing
16 changed files
with
265 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,5 @@ | |
app.MapControllers(); | ||
|
||
app.Run(); | ||
|
||
public partial class Program { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" /> | ||
<PackageReference Include="FluentAssertions" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="NUnit" /> | ||
<PackageReference Include="NUnit3TestAdapter" /> | ||
<PackageReference Include="NUnit.Analyzers" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\HostApplications\WebApi\Example.HostWebApi.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<!-- Copy the plugins directory from Example.HostWebApi to the output directory of Example.Test. --> | ||
<Content | ||
Include="..\HostApplications\WebApi\bin\$(Configuration)\$(TargetFramework)\plugins\**" | ||
CopyToOutputDirectory="PreserveNewest" | ||
TargetPath="plugins\%(RecursiveDir)\%(Filename)%(Extension)" | ||
/> | ||
|
||
<!-- Copy the plugins directory from Example.HostConsoleApp to the output directory of Example.Test. --> | ||
<Content | ||
Include="..\HostApplications\ConsoleApp\bin\$(Configuration)\$(TargetFramework)\plugins\**" | ||
CopyToOutputDirectory="PreserveNewest" | ||
TargetPath="plugins\%(RecursiveDir)\%(Filename)%(Extension)" | ||
/> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
global using NUnit.Framework; | ||
global using FluentAssertions; | ||
global using Microsoft.AspNetCore.Mvc.Testing; | ||
global using SimpleResults; | ||
global using System.Net; | ||
global using Example.WebApi; | ||
global using CPlugin.Net; | ||
global using Example.Contracts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
namespace Example.Test; | ||
|
||
public class PluginLoaderTest | ||
{ | ||
private class GetPluginInfo | ||
{ | ||
public string Name { get; set; } | ||
public string Description { get; set; } | ||
public string Version { get; set; } | ||
} | ||
|
||
[Test] | ||
public void Load_WhenPluginsAreFound_ShouldBeLoadedIntoMemory() | ||
{ | ||
// Arrange | ||
var plugins = | ||
""" | ||
Example.JsonPlugin.dll | ||
Example.OldJsonPlugin.dll | ||
"""; | ||
Environment.SetEnvironmentVariable("PLUGINS", plugins); | ||
var envConfiguration = new CPluginEnvConfiguration(); | ||
var expectedInfo = new GetPluginInfo[] | ||
{ | ||
new() | ||
{ | ||
Name = "json", | ||
Description = "Outputs JSON value.", | ||
Version = "Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" | ||
}, | ||
new() | ||
{ | ||
Name = "oldjson", | ||
Description = "Outputs JSON value.", | ||
Version = "Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" | ||
} | ||
}; | ||
|
||
// Act | ||
PluginLoader.Load(envConfiguration); | ||
var commands = TypeFinder.FindSubtypesOf<ICommand>(); | ||
var actualInfo = commands.Select(command => new GetPluginInfo | ||
{ | ||
Name = command.Name, | ||
Description = command.Description, | ||
Version = command.Version | ||
}).ToArray(); | ||
|
||
// Assert | ||
actualInfo.Should().BeEquivalentTo(expectedInfo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
namespace Example.Test.WebApi; | ||
|
||
public class Get | ||
{ | ||
[Test] | ||
public async Task Get_WhenAppointmentsAreObtained_ShouldReturnsHttpStatusCodeOk() | ||
{ | ||
// Arrange | ||
using var factory = new WebApplicationFactory<Program>(); | ||
var client = factory.CreateClient(); | ||
int expectedAppointments = 3; | ||
|
||
// Act | ||
var httpResponse = await client.GetAsync("/Appointment"); | ||
var result = await httpResponse | ||
.Content | ||
.ReadFromJsonAsync<ListedResult<GetAppointmentResponse>>(); | ||
|
||
// Asserts | ||
httpResponse.StatusCode.Should().Be(HttpStatusCode.OK); | ||
result.IsSuccess.Should().BeTrue(); | ||
result.Data.Should().HaveCount(expectedAppointments); | ||
} | ||
|
||
[Test] | ||
public async Task Get_WhenAppointmentIsObtained_ShouldReturnsHttpStatusCodeOk() | ||
{ | ||
// Arrange | ||
using var factory = new WebApplicationFactory<Program>(); | ||
var client = factory.CreateClient(); | ||
var requestUri = "/Appointment/5dace456-13d9-42c5-92ea-89869ac44b5c"; | ||
|
||
// Act | ||
var httpResponse = await client.GetAsync(requestUri); | ||
var result = await httpResponse | ||
.Content | ||
.ReadFromJsonAsync<Result<GetAppointmentResponse>>(); | ||
|
||
// Asserts | ||
httpResponse.StatusCode.Should().Be(HttpStatusCode.OK); | ||
result.IsSuccess.Should().BeTrue(); | ||
result.Data.Should().NotBeNull(); | ||
} | ||
|
||
[Test] | ||
public async Task Get_WhenAppointmentIsNotFound_ShouldReturnsHttpStatusCodeNotFound() | ||
{ | ||
// Arrange | ||
using var factory = new WebApplicationFactory<Program>(); | ||
var client = factory.CreateClient(); | ||
var requestUri = "/Appointment/5dace456"; | ||
|
||
// Act | ||
var httpResponse = await client.GetAsync(requestUri); | ||
var result = await httpResponse | ||
.Content | ||
.ReadFromJsonAsync<Result<GetAppointmentResponse>>(); | ||
|
||
// Asserts | ||
httpResponse.StatusCode.Should().Be(HttpStatusCode.NotFound); | ||
result.IsSuccess.Should().BeFalse(); | ||
result.Data.Should().BeNull(); | ||
} | ||
|
||
[TestCase("/People")] | ||
[TestCase("/Person")] | ||
public async Task Get_WhenPersonsAreObtained_ShouldReturnsHttpStatusCodeOk(string requestUri) | ||
{ | ||
// Arrange | ||
using var factory = new WebApplicationFactory<Program>(); | ||
var client = factory.CreateClient(); | ||
int expectedPersons = 3; | ||
|
||
// Act | ||
var httpResponse = await client.GetAsync(requestUri); | ||
var result = await httpResponse | ||
.Content | ||
.ReadFromJsonAsync<Result<IEnumerable<GetPersonResponse>>>(); | ||
|
||
// Asserts | ||
httpResponse.StatusCode.Should().Be(HttpStatusCode.OK); | ||
result.IsSuccess.Should().BeTrue(); | ||
result.Data.Should().HaveCount(expectedPersons); | ||
} | ||
|
||
[Test] | ||
public async Task Get_WhenWeatherForecastAreObtained_ShouldReturnsHttpStatusCodeOk() | ||
{ | ||
// Arrange | ||
using var factory = new WebApplicationFactory<Program>(); | ||
var client = factory.CreateClient(); | ||
int expectedWeatherForecast = 5; | ||
|
||
// Act | ||
var httpResponse = await client.GetAsync("/WeatherForecast"); | ||
var result = await httpResponse | ||
.Content | ||
.ReadFromJsonAsync<ListedResult<WeatherForecast>>(); | ||
|
||
// Asserts | ||
httpResponse.StatusCode.Should().Be(HttpStatusCode.OK); | ||
result.IsSuccess.Should().BeTrue(); | ||
result.Data.Should().HaveCount(expectedWeatherForecast); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Example.Test.WebApi; | ||
|
||
public class GetAppointmentResponse | ||
{ | ||
public string Id { get; set; } | ||
public string DoctorName { get; set; } | ||
public string PatientName { get; set; } | ||
} | ||
|
||
public class GetPersonResponse | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
public string Document { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace Example.Test.WebApi; | ||
|
||
public class Post | ||
{ | ||
[TestCase("/Person")] | ||
[TestCase("/People")] | ||
public async Task Post_WhenPersonIsCreated_ShouldReturnsHttpStatusCodeCreated(string requestUri) | ||
{ | ||
// Arrange | ||
using var factory = new WebApplicationFactory<Program>(); | ||
var client = factory.CreateClient(); | ||
|
||
// Act | ||
var httpResponse = await client.PostAsJsonAsync(requestUri, new {}); | ||
var result = await httpResponse | ||
.Content | ||
.ReadFromJsonAsync<Result>(); | ||
|
||
// Asserts | ||
httpResponse.StatusCode.Should().Be(HttpStatusCode.Created); | ||
result.IsSuccess.Should().BeTrue(); | ||
} | ||
} |