-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathWeatherForecastTest.cs
76 lines (59 loc) · 2.23 KB
/
WeatherForecastTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
namespace WeatherForecast.InProcess.Tests;
[UsedImplicitly]
public sealed class WeatherForecastTest : IAsyncLifetime
{
private readonly PostgreSqlContainer _postgreSqlContainer = new PostgreSqlBuilder().Build();
public Task InitializeAsync()
{
return _postgreSqlContainer.StartAsync();
}
public Task DisposeAsync()
{
return _postgreSqlContainer.DisposeAsync().AsTask();
}
public sealed class Api : WebApplicationFactory<Program>, IClassFixture<WeatherForecastTest>
{
private readonly string _postgreSqlConnectionString;
public Api(WeatherForecastTest weatherForecastTest)
{
_postgreSqlConnectionString = weatherForecastTest._postgreSqlContainer.GetConnectionString();
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseSetting("URLS", "https://+");
builder.UseSetting("ConnectionStrings:PostgreSQL", _postgreSqlConnectionString);
}
[Fact]
[Trait("Category", nameof(Api))]
public async Task Get_WeatherForecast_ReturnsSevenDays()
{
// Given
const string path = "api/WeatherForecast";
using var httpClient = CreateClient();
// When
var response = await httpClient.GetAsync(path)
.ConfigureAwait(true);
var weatherForecastStream = await response.Content.ReadAsStreamAsync()
.ConfigureAwait(true);
var weatherForecast = await JsonSerializer.DeserializeAsync<IEnumerable<WeatherData>>(weatherForecastStream)
.ConfigureAwait(true);
// Then
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(7, weatherForecast!.Count());
}
[Fact]
[Trait("Category", nameof(Api))]
public async Task Get_WeatherForecast_ReturnsThreeDays()
{
// Given
const int threeDays = 3;
using var serviceScope = Services.CreateScope();
var weatherDataReadOnlyRepository = serviceScope.ServiceProvider.GetRequiredService<IWeatherDataReadOnlyRepository>();
// When
var weatherForecast = await weatherDataReadOnlyRepository.GetAllAsync(string.Empty, string.Empty, DateTime.Today, DateTime.Today.AddDays(threeDays))
.ConfigureAwait(true);
// Then
Assert.Equal(threeDays, weatherForecast.Count());
}
}
}