Skip to content

Commit 142b8ba

Browse files
committed
refactor: consolidate player data into single source of truth (#210)
Eliminate duplication between PlayerData.cs and PlayerFakes.cs by making PlayerFakes delegate to PlayerData methods. - PlayerFakes.MakeStarting11() now calls PlayerData.MakeStarting11() - PlayerFakes.MakeNew() now uses PlayerData.GetSubstitutes() - All factory methods updated to delegate to PlayerData - Enhanced XML documentation to clarify SSOT relationship
1 parent a766e4e commit 142b8ba

1 file changed

Lines changed: 55 additions & 184 deletions

File tree

test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs

Lines changed: 55 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -1,201 +1,67 @@
1-
using Dotnet.Samples.AspNetCore.WebApi.Enums;
21
using Dotnet.Samples.AspNetCore.WebApi.Models;
2+
using Dotnet.Samples.AspNetCore.WebApi.Utilities;
33

44
namespace Dotnet.Samples.AspNetCore.WebApi.Tests.Utilities;
55

66
/// <summary>
7-
/// A Fake is a working implementation that’s simpler than the real one.
8-
/// It usually has some “real” logic but is not suitable for production
7+
/// Test data factory for Player entities.
8+
/// Wraps production data from PlayerData with test-specific modifications (e.g., GUID assignment).
9+
/// A Fake is a working implementation that's simpler than the real one.
10+
/// It usually has some "real" logic but is not suitable for production
911
/// (e.g., an in‑memory database instead of a full SQL Server). Fakes are
10-
/// useful when you need behavior thats closer to reality but still want
12+
/// useful when you need behavior that's closer to reality but still want
1113
/// to avoid external dependencies.
1214
/// </summary>
1315
public static class PlayerFakes
1416
{
17+
/// <summary>
18+
/// Returns the starting 11 players with generated GUIDs for in-memory testing.
19+
/// Reuses production player data from PlayerData.MakeStarting11().
20+
/// </summary>
1521
public static List<Player> MakeStarting11()
1622
{
17-
return
18-
[
19-
new()
20-
{
21-
Id = Guid.NewGuid(),
22-
FirstName = "Damián",
23-
MiddleName = "Emiliano",
24-
LastName = "Martínez",
25-
DateOfBirth = new DateTime(1992, 9, 1, 0, 0, 0, DateTimeKind.Utc),
26-
SquadNumber = 23,
27-
Position = Position.Goalkeeper.Text,
28-
AbbrPosition = Position.Goalkeeper.Abbr,
29-
Team = "Aston Villa FC",
30-
League = "Premier League",
31-
Starting11 = true,
32-
},
33-
new()
34-
{
35-
Id = Guid.NewGuid(),
36-
FirstName = "Nahuel",
37-
LastName = "Molina",
38-
DateOfBirth = new DateTime(1998, 4, 5, 0, 0, 0, DateTimeKind.Utc),
39-
SquadNumber = 26,
40-
Position = Position.RightBack.Text,
41-
AbbrPosition = Position.RightBack.Abbr,
42-
Team = "Altético Madrid",
43-
League = "La Liga",
44-
Starting11 = true,
45-
},
46-
new()
47-
{
48-
Id = Guid.NewGuid(),
49-
FirstName = "Cristian",
50-
MiddleName = "Gabriel",
51-
LastName = "Romero",
52-
DateOfBirth = new DateTime(1998, 4, 26, 0, 0, 0, DateTimeKind.Utc),
53-
SquadNumber = 13,
54-
Position = Position.CentreBack.Text,
55-
AbbrPosition = Position.CentreBack.Abbr,
56-
Team = "Tottenham Hotspur",
57-
League = "Premier League",
58-
Starting11 = true,
59-
},
60-
new()
61-
{
62-
Id = Guid.NewGuid(),
63-
FirstName = "Nicolás",
64-
MiddleName = "Hernán Gonzalo",
65-
LastName = "Otamendi",
66-
DateOfBirth = new DateTime(1988, 2, 11, 0, 0, 0, DateTimeKind.Utc),
67-
SquadNumber = 19,
68-
Position = Position.CentreBack.Text,
69-
AbbrPosition = Position.CentreBack.Abbr,
70-
Team = "SL Benfica",
71-
League = "Liga Portugal",
72-
Starting11 = true,
73-
},
74-
new()
75-
{
76-
Id = Guid.NewGuid(),
77-
FirstName = "Nicolás",
78-
MiddleName = "Alejandro",
79-
LastName = "Tagliafico",
80-
DateOfBirth = new DateTime(1992, 8, 30, 0, 0, 0, DateTimeKind.Utc),
81-
SquadNumber = 3,
82-
Position = Position.LeftBack.Text,
83-
AbbrPosition = Position.LeftBack.Abbr,
84-
Team = "Olympique Lyon",
85-
League = "Ligue 1",
86-
Starting11 = true,
87-
},
88-
new()
89-
{
90-
Id = Guid.NewGuid(),
91-
FirstName = "Ángel",
92-
MiddleName = "Fabián",
93-
LastName = "Di María",
94-
DateOfBirth = new DateTime(1988, 2, 13, 0, 0, 0, DateTimeKind.Utc),
95-
SquadNumber = 11,
96-
Position = Position.RightWinger.Text,
97-
AbbrPosition = Position.RightWinger.Abbr,
98-
Team = "SL Benfica",
99-
League = "Liga Portugal",
100-
Starting11 = true,
101-
},
102-
new()
103-
{
104-
Id = Guid.NewGuid(),
105-
FirstName = "Rodrigo",
106-
MiddleName = "Javier",
107-
LastName = "de Paul",
108-
DateOfBirth = new DateTime(1994, 5, 23, 0, 0, 0, DateTimeKind.Utc),
109-
SquadNumber = 7,
110-
Position = Position.CentralMidfield.Text,
111-
AbbrPosition = Position.CentralMidfield.Abbr,
112-
Team = "Altético Madrid",
113-
League = "La Liga",
114-
Starting11 = true,
115-
},
116-
new()
117-
{
118-
Id = Guid.NewGuid(),
119-
FirstName = "Enzo",
120-
MiddleName = "Jeremías",
121-
LastName = "Fernández",
122-
DateOfBirth = new DateTime(2001, 1, 16, 0, 0, 0, DateTimeKind.Utc),
123-
SquadNumber = 24,
124-
Position = Position.CentralMidfield.Text,
125-
AbbrPosition = Position.CentralMidfield.Abbr,
126-
Team = "Chelsea FC",
127-
League = "Premier League",
128-
Starting11 = true,
129-
},
130-
new()
131-
{
132-
Id = Guid.NewGuid(),
133-
FirstName = "Alexis",
134-
LastName = "Mac Allister",
135-
DateOfBirth = new DateTime(1998, 12, 23, 0, 0, 0, DateTimeKind.Utc),
136-
SquadNumber = 20,
137-
Position = Position.CentralMidfield.Text,
138-
AbbrPosition = Position.CentralMidfield.Abbr,
139-
Team = "Liverpool FC",
140-
League = "Premier League",
141-
Starting11 = true,
142-
},
143-
new()
144-
{
145-
Id = Guid.NewGuid(),
146-
FirstName = "Lionel",
147-
MiddleName = "Andrés",
148-
LastName = "Messi",
149-
DateOfBirth = new DateTime(1987, 6, 23, 0, 0, 0, DateTimeKind.Utc),
150-
SquadNumber = 10,
151-
Position = Position.RightWinger.Text,
152-
AbbrPosition = Position.RightWinger.Abbr,
153-
Team = "Inter Miami CF",
154-
League = "Major League Soccer",
155-
Starting11 = true,
156-
},
157-
new()
158-
{
159-
Id = Guid.NewGuid(),
160-
FirstName = "Julián",
161-
LastName = "Álvarez",
162-
DateOfBirth = new DateTime(2000, 1, 30, 0, 0, 0, DateTimeKind.Utc),
163-
SquadNumber = 9,
164-
Position = Position.CentreForward.Text,
165-
AbbrPosition = Position.CentreForward.Abbr,
166-
Team = "Manchester City",
167-
League = "Premier League",
168-
Starting11 = true,
169-
}
170-
];
23+
var players = PlayerData.MakeStarting11();
24+
25+
// Assign GUIDs for in-memory database testing
26+
foreach (var player in players)
27+
{
28+
player.Id = Guid.NewGuid();
29+
}
30+
31+
return players;
17132
}
17233

34+
/// <summary>
35+
/// Returns a specific player from the starting 11 by squad number.
36+
/// Reuses production player data from PlayerData.MakeStarting11().
37+
/// </summary>
17338
public static Player MakeFromStarting11(int squadNumber)
17439
{
17540
var player =
176-
MakeStarting11().SingleOrDefault(player => player.SquadNumber == squadNumber)
41+
PlayerData.MakeStarting11().SingleOrDefault(p => p.SquadNumber == squadNumber)
17742
?? throw new ArgumentNullException(
17843
$"Player with Squad Number {squadNumber} not found."
17944
);
18045

46+
player.Id = Guid.NewGuid();
18147
return player;
18248
}
18349

50+
/// <summary>
51+
/// Returns a new player (substitute) for testing create operations.
52+
/// Reuses production player data from PlayerData.GetSubstitutes().
53+
/// </summary>
18454
public static Player MakeNew()
18555
{
186-
return new()
187-
{
188-
FirstName = "Leandro",
189-
MiddleName = "Daniel",
190-
LastName = "Paredes",
191-
DateOfBirth = new DateTime(1994, 06, 29, 0, 0, 0, DateTimeKind.Utc),
192-
SquadNumber = 5,
193-
Position = Position.DefensiveMidfield.Text,
194-
AbbrPosition = Position.DefensiveMidfield.Abbr,
195-
Team = "AS Roma",
196-
League = "Serie A",
197-
Starting11 = false
198-
};
56+
// Get Leandro Paredes (squad number 5) from substitutes
57+
var player =
58+
PlayerData.GetSubstitutes().SingleOrDefault(player => player.SquadNumber == 5)
59+
?? throw new InvalidOperationException(
60+
"Substitute player with squad number 5 not found."
61+
);
62+
63+
player.Id = Guid.NewGuid();
64+
return player;
19965
}
20066

20167
/* -------------------------------------------------------------------------
@@ -243,7 +109,7 @@ public static PlayerResponseModel MakeResponseModelForCreate()
243109
public static PlayerRequestModel MakeRequestModelForRetrieve(int squadNumber)
244110
{
245111
var player =
246-
MakeStarting11().SingleOrDefault(player => player.SquadNumber == squadNumber)
112+
PlayerData.MakeStarting11().SingleOrDefault(p => p.SquadNumber == squadNumber)
247113
?? throw new ArgumentNullException(
248114
$"Player with Squad Number {squadNumber} not found."
249115
);
@@ -264,7 +130,7 @@ public static PlayerRequestModel MakeRequestModelForRetrieve(int squadNumber)
264130
public static PlayerResponseModel MakeResponseModelForRetrieve(int squadNumber)
265131
{
266132
var player =
267-
MakeStarting11().SingleOrDefault(player => player.SquadNumber == squadNumber)
133+
PlayerData.MakeStarting11().SingleOrDefault(p => p.SquadNumber == squadNumber)
268134
?? throw new ArgumentNullException(
269135
$"Player with Squad Number {squadNumber} not found."
270136
);
@@ -284,17 +150,22 @@ public static PlayerResponseModel MakeResponseModelForRetrieve(int squadNumber)
284150

285151
public static List<PlayerResponseModel> MakeResponseModelsForRetrieve() =>
286152
[
287-
.. MakeStarting11()
288-
.Select(player => new PlayerResponseModel
153+
.. PlayerData
154+
.MakeStarting11()
155+
.Select(player =>
289156
{
290-
FullName =
291-
$"{player.FirstName} {(string.IsNullOrWhiteSpace(player.MiddleName) ? "" : player.MiddleName + " ")}{player.LastName}".Trim(),
292-
Birth = $"{player.DateOfBirth:MMMM d, yyyy}",
293-
Dorsal = player.SquadNumber,
294-
Position = player.Position,
295-
Club = player.Team,
296-
League = player.League,
297-
Starting11 = player.Starting11 ? "Yes" : "No"
157+
player.Id = Guid.NewGuid();
158+
return new PlayerResponseModel
159+
{
160+
FullName =
161+
$"{player.FirstName} {(string.IsNullOrWhiteSpace(player.MiddleName) ? "" : player.MiddleName + " ")}{player.LastName}".Trim(),
162+
Birth = $"{player.DateOfBirth:MMMM d, yyyy}",
163+
Dorsal = player.SquadNumber,
164+
Position = player.Position,
165+
Club = player.Team,
166+
League = player.League,
167+
Starting11 = player.Starting11 ? "Yes" : "No"
168+
};
298169
})
299170
];
300171

0 commit comments

Comments
 (0)