-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPlayerRepositoryTests.cs
More file actions
186 lines (152 loc) · 5.93 KB
/
PlayerRepositoryTests.cs
File metadata and controls
186 lines (152 loc) · 5.93 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System.Data.Common;
using Dotnet.Samples.AspNetCore.WebApi.Data;
using Dotnet.Samples.AspNetCore.WebApi.Models;
using Dotnet.Samples.AspNetCore.WebApi.Repositories;
using Dotnet.Samples.AspNetCore.WebApi.Tests.Utilities;
using FluentAssertions;
namespace Dotnet.Samples.AspNetCore.WebApi.Tests.Integration;
/// <summary>
/// Integration tests for <see cref="Repository{T}"/> and <see cref="PlayerRepository"/>.
/// Each test runs against an in-memory SQLite database with the full EF Core migration
/// chain applied via <see cref="DatabaseFakes.MigrateAsync"/>, which also validates
/// that the migration chain itself is healthy as a side effect.
/// </summary>
public class PlayerRepositoryTests : IAsyncLifetime
{
private DbConnection _connection = default!;
private PlayerDbContext _dbContext = default!;
private PlayerRepository _repository = default!;
public async Task InitializeAsync()
{
var (connection, options) = DatabaseFakes.CreateSqliteConnection();
_connection = connection;
_dbContext = DatabaseFakes.CreateDbContext(options);
await _dbContext.MigrateAsync();
_repository = new PlayerRepository(_dbContext);
}
public async Task DisposeAsync()
{
await _dbContext.DisposeAsync();
await _connection.DisposeAsync();
}
/* -------------------------------------------------------------------------
* GetAllAsync
* ---------------------------------------------------------------------- */
[Fact]
[Trait("Category", "Integration")]
public async Task GetAllAsync_WhenCalled_ReturnsAllSeededPlayers()
{
// Act
var players = await _repository.GetAllAsync();
// Assert
players.Should().HaveCount(26);
_dbContext.ChangeTracker.Entries<Player>().Should().BeEmpty();
}
/* -------------------------------------------------------------------------
* FindByIdAsync
* ---------------------------------------------------------------------- */
[Fact]
[Trait("Category", "Integration")]
public async Task FindByIdAsync_ExistingId_ReturnsPlayer()
{
// Arrange — resolve a real ID from the seeded database
var seeded = await _repository.GetAllAsync();
var existingId = seeded[0].Id;
// Act
var player = await _repository.FindByIdAsync(existingId);
// Assert
player.Should().NotBeNull();
player!.Id.Should().Be(existingId);
}
[Fact]
[Trait("Category", "Integration")]
public async Task FindByIdAsync_UnknownId_ReturnsNull()
{
// Act
var player = await _repository.FindByIdAsync(Guid.NewGuid());
// Assert
player.Should().BeNull();
}
/* -------------------------------------------------------------------------
* RemoveAsync
* ---------------------------------------------------------------------- */
[Fact]
[Trait("Category", "Integration")]
public async Task RemoveAsync_ExistingEntity_RemovesFromDatabase()
{
// Arrange
var seeded = await _repository.GetAllAsync();
var existingId = seeded[0].Id;
// Act
await _repository.RemoveAsync(existingId);
// Assert
var player = await _repository.FindByIdAsync(existingId);
player.Should().BeNull();
}
[Fact]
[Trait("Category", "Integration")]
public async Task RemoveAsync_UnknownId_NoExceptionThrown()
{
// Arrange
var countBefore = (await _repository.GetAllAsync()).Count;
// Act
var act = async () => await _repository.RemoveAsync(Guid.NewGuid());
// Assert
await act.Should().NotThrowAsync();
var countAfter = (await _repository.GetAllAsync()).Count;
countAfter.Should().Be(countBefore);
}
/* -------------------------------------------------------------------------
* FindBySquadNumberAsync
* ---------------------------------------------------------------------- */
[Fact]
[Trait("Category", "Integration")]
public async Task FindBySquadNumberAsync_ExistingSquadNumber_ReturnsPlayer()
{
// Arrange
var expected = PlayerFakes.MakeFromStarting11(23);
// Act
var player = await _repository.FindBySquadNumberAsync(expected.SquadNumber);
// Assert
player.Should().NotBeNull();
player!.SquadNumber.Should().Be(expected.SquadNumber);
}
[Fact]
[Trait("Category", "Integration")]
public async Task FindBySquadNumberAsync_UnknownSquadNumber_ReturnsNull()
{
// Arrange — derive a squad number guaranteed not to exist in the seeded data
var seeded = await _repository.GetAllAsync();
var unknownSquadNumber = seeded.Max(p => p.SquadNumber) + 1;
// Act
var player = await _repository.FindBySquadNumberAsync(unknownSquadNumber);
// Assert
player.Should().BeNull();
}
/* -------------------------------------------------------------------------
* SquadNumberExistsAsync
* ---------------------------------------------------------------------- */
[Fact]
[Trait("Category", "Integration")]
public async Task SquadNumberExistsAsync_ExistingSquadNumber_ReturnsTrue()
{
// Arrange
var expected = PlayerFakes.MakeFromStarting11(23);
// Act
var exists = await _repository.SquadNumberExistsAsync(expected.SquadNumber);
// Assert
exists.Should().BeTrue();
}
[Fact]
[Trait("Category", "Integration")]
public async Task SquadNumberExistsAsync_UnknownSquadNumber_ReturnsFalse()
{
// Arrange — derive a squad number guaranteed not to exist in the seeded data
var seeded = await _repository.GetAllAsync();
var unknownSquadNumber = seeded.Max(p => p.SquadNumber) + 1;
// Act
var exists = await _repository.SquadNumberExistsAsync(unknownSquadNumber);
// Assert
exists.Should().BeFalse();
}
}