Skip to content

Commit

Permalink
docs: Add in process WeatherForecast example
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Oct 17, 2022
1 parent 4dab255 commit 1ce11d3
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Autodetect text files
* text=auto

# Definitively text files
*.cs text
*.cake text
Expand Down
2 changes: 1 addition & 1 deletion examples/WeatherForecast/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.idea
.vs
bin
build
bin
obj
1 change: 1 addition & 0 deletions examples/WeatherForecast/Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<PackageReference Update="Microsoft.Fast.Components.FluentUI" Version="1.5.3"/>
<PackageReference Update="System.ComponentModel.Annotations" Version="5.0.0"/>
<PackageReference Update="System.Text.Json" Version="6.0.6"/>
<PackageReference Update="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.10"/>
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.3.2"/>
<PackageReference Update="Selenium.WebDriver.ChromeDriver" Version="106.0.5249.6100"/>
<PackageReference Update="Selenium.WebDriver" Version="4.5.1"/>
Expand Down
20 changes: 20 additions & 0 deletions examples/WeatherForecast/WeatherForecast.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ ProjectSection(SolutionItems) = preProject
Dockerfile = Dockerfile
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{24FE2FC4-620F-4850-B115-9D1B4265839C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{CC7C97EE-5A60-4DAB-B07A-EFD3A73E6E3A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers", "..\..\src\Testcontainers\Testcontainers.csproj", "{490171F7-0781-40B1-B942-ACE759F8D51D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WeatherForecast", "src\WeatherForecast\WeatherForecast.csproj", "{40712E50-0FC0-47AA-A9BE-98AA2FB73E59}"
Expand All @@ -19,11 +23,23 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WeatherForecast.Repositorie
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WeatherForecast.Test", "tests\WeatherForecast.Test\WeatherForecast.Test.csproj", "{B8C371A9-6B6C-4595-9B5B-88071D6F3A71}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WeatherForecast.InProcess.Test", "tests\WeatherForecast.InProcess.Test\WeatherForecast.InProcess.Test.csproj", "{2D4474DB-9E16-4931-ADD0-569306C1C5AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2D4474DB-9E16-4931-ADD0-569306C1C5AE} = {CC7C97EE-5A60-4DAB-B07A-EFD3A73E6E3A}
{B8C371A9-6B6C-4595-9B5B-88071D6F3A71} = {CC7C97EE-5A60-4DAB-B07A-EFD3A73E6E3A}
{490171F7-0781-40B1-B942-ACE759F8D51D} = {24FE2FC4-620F-4850-B115-9D1B4265839C}
{40712E50-0FC0-47AA-A9BE-98AA2FB73E59} = {24FE2FC4-620F-4850-B115-9D1B4265839C}
{86938290-5D7D-43B5-8146-4614FB5820F4} = {24FE2FC4-620F-4850-B115-9D1B4265839C}
{53CAFB42-4AEB-43F9-B6A1-DAA8EE46F174} = {24FE2FC4-620F-4850-B115-9D1B4265839C}
{6582ADC8-7DE3-4183-95DF-75D63B784023} = {24FE2FC4-620F-4850-B115-9D1B4265839C}
{B56FE398-0CD9-4747-9B58-812EE659A61A} = {24FE2FC4-620F-4850-B115-9D1B4265839C}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{490171F7-0781-40B1-B942-ACE759F8D51D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{490171F7-0781-40B1-B942-ACE759F8D51D}.Debug|Any CPU.Build.0 = Debug|Any CPU
Expand Down Expand Up @@ -53,5 +69,9 @@ Global
{B8C371A9-6B6C-4595-9B5B-88071D6F3A71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8C371A9-6B6C-4595-9B5B-88071D6F3A71}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8C371A9-6B6C-4595-9B5B-88071D6F3A71}.Release|Any CPU.Build.0 = Release|Any CPU
{2D4474DB-9E16-4931-ADD0-569306C1C5AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D4474DB-9E16-4931-ADD0-569306C1C5AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D4474DB-9E16-4931-ADD0-569306C1C5AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D4474DB-9E16-4931-ADD0-569306C1C5AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public Temperature(Guid id, Guid belongsTo, string unitName, string unitSymbol,
Measured = measured;
}

public static Temperature AbsoluteZero { get; } = Temperature.Kelvin(Guid.Empty, 0, DateTime.MinValue);
public static Temperature AbsoluteZero { get; } = Kelvin(Guid.Empty, 0, DateTime.MinValue);

[JsonPropertyName("belongsTo")]
public Guid BelongsTo { get; }
Expand Down
4 changes: 4 additions & 0 deletions examples/WeatherForecast/src/WeatherForecast/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();

public sealed partial class Program
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<ProjectReference Include="$(SolutionDir)src/WeatherForecast.Interactors/WeatherForecast.Interactors.csproj"/>
<ProjectReference Include="$(SolutionDir)src/WeatherForecast.Repositories/WeatherForecast.Repositories.csproj"/>
</ItemGroup>
<ItemGroup>
<!-- https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory-1 -->
<InternalsVisibleTo Include="WeatherForecast.InProcess.Test" />
</ItemGroup>
<ItemGroup>
<None Update="certificate.crt" Visible="false">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<Sdk Name="Microsoft.Build.CentralPackageVersions" Version="2.1.3"/>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing"/>
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="xunit.runner.visualstudio"/>
<PackageReference Include="xunit"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../../../src/Testcontainers/Testcontainers.csproj"/>
<ProjectReference Include="$(SolutionDir)src/WeatherForecast/WeatherForecast.csproj"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Mvc.Testing;
using WeatherForecast.Entities;
using Xunit;

namespace WeatherForecast.InProcess.Test;

[UsedImplicitly]
public sealed class WeatherForecastTest : IAsyncLifetime
{
private readonly MsSqlTestcontainer _mssqlContainer;

public WeatherForecastTest()
{
var mssqlConfiguration = new MsSqlTestcontainerConfiguration();
mssqlConfiguration.Password = Guid.NewGuid().ToString("D");
mssqlConfiguration.Database = Guid.NewGuid().ToString("D");

_mssqlContainer = new TestcontainersBuilder<MsSqlTestcontainer>()
.WithDatabase(mssqlConfiguration)
.Build();
}

public Task InitializeAsync()
{
return _mssqlContainer.StartAsync();
}

public Task DisposeAsync()
{
return _mssqlContainer.DisposeAsync().AsTask();
}

public sealed class Api : IClassFixture<WeatherForecastTest>
{
private readonly HttpClient _httpClient;

public Api(WeatherForecastTest weatherForecastTest)
{
Environment.SetEnvironmentVariable("ASPNETCORE_URLS", "https://+");
Environment.SetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Path", "certificate.crt");
Environment.SetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Password", "password");
Environment.SetEnvironmentVariable("ConnectionStrings__DefaultConnection", weatherForecastTest._mssqlContainer.ConnectionString);
_httpClient = new WebApplicationFactory<Program>().CreateClient();
}

[Fact]
[Trait("Category", nameof(Api))]
public async Task Get_WeatherForecast_ReturnsSevenDays()
{
// Given
const string path = "api/WeatherForecast";

// When
var response = await _httpClient.GetAsync(path)
.ConfigureAwait(false);

var weatherForecastStream = await response.Content.ReadAsStreamAsync()
.ConfigureAwait(false);

var weatherForecast = await JsonSerializer.DeserializeAsync<IEnumerable<WeatherData>>(weatherForecastStream)
.ConfigureAwait(false);

// Then
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(7, weatherForecast!.Count());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public sealed class WeatherForecastContainer : HttpClient, IAsyncLifetime
public WeatherForecastContainer()
: base(new HttpClientHandler
{
// Trust the development certificate.
ServerCertificateCustomValidationCallback = (_, certificate, _, _) => Certificate.Equals(certificate)
})
{
Expand Down Expand Up @@ -62,6 +63,7 @@ public WeatherForecastContainer()

public async Task InitializeAsync()
{
// It is not necessary to clean up resources immediately (still good practice). The Resource Reaper will take care of orphaned resources.
await Image.InitializeAsync()
.ConfigureAwait(false);

Expand Down

0 comments on commit 1ce11d3

Please sign in to comment.