Skip to content

Commit 5cabf90

Browse files
committed
Complete tests
1 parent 5785553 commit 5cabf90

File tree

9 files changed

+2903
-275
lines changed

9 files changed

+2903
-275
lines changed
Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using FluentAssertions;
2+
using FluentAssertions.Specialized;
3+
using JsonApiDotNetCore.OpenApi.Client;
4+
using Newtonsoft.Json;
25
using OpenApiEndToEndTests.ClientGeneratedId.GeneratedCode;
36
using OpenApiTests;
47
using OpenApiTests.ClientGeneratedId;
@@ -18,10 +21,11 @@ public PostTests(IntegrationTestContext<OpenApiStartup<ClientGeneratedIdDbContex
1821

1922
testContext.UseController<PlayersController>();
2023
testContext.UseController<GamesController>();
24+
testContext.UseController<GroupsController>();
2125
}
2226

2327
[Fact]
24-
public async Task Returns_error_if_required_id_is_omitted()
28+
public async Task Omit_required_id()
2529
{
2630
// Arrange
2731
Player player = _fakers.Player.Generate();
@@ -30,35 +34,35 @@ public async Task Returns_error_if_required_id_is_omitted()
3034
ClientGeneratedIdClient apiClient = new(httpClient);
3135

3236
// Act
33-
Func<Task<PlayerPrimaryResponseDocument>> action = () => apiClient.PostPlayerAsync(null, new PlayerPostRequestDocument
37+
Func<Task<PlayerPrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostPlayerAsync(null, new PlayerPostRequestDocument
3438
{
3539
Data = new PlayerDataInPostRequest
3640
{
37-
Id = null!, // FIXME: passing "" here works fine 🤔
41+
Id = null!,
3842
Attributes = new PlayerAttributesInPostRequest
3943
{
4044
Name = player.Name
4145
}
4246
}
43-
});
47+
}));
4448

4549
// Assert
46-
var exception = (await action.Should().ThrowAsync<Exception>()).Subject.First();
47-
// Exception is Newtonsoft.Json.JsonSerializationException: Cannot write a null value for property 'id'. Property requires a value. Path 'data'.
48-
// Probably not what we want.
50+
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
51+
assertion.Which.Message.Should().Be("Cannot write a null value for property 'id'. Property requires a value. Path 'data'.");
4952
}
5053

5154
[Fact]
52-
public async Task Requires_passing_id()
55+
public async Task Pass_required_id()
5356
{
5457
// Arrange
5558
Player player = _fakers.Player.Generate();
59+
player.Id = Guid.NewGuid();
5660

5761
using HttpClient httpClient = _testContext.Factory.CreateClient();
5862
ClientGeneratedIdClient apiClient = new(httpClient);
5963

6064
// Act
61-
Func<Task<PlayerPrimaryResponseDocument>> action = () => apiClient.PostPlayerAsync(null, new PlayerPostRequestDocument
65+
Func<Task<PlayerPrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostPlayerAsync(null, new PlayerPostRequestDocument
6266
{
6367
Data = new PlayerDataInPostRequest
6468
{
@@ -68,15 +72,15 @@ public async Task Requires_passing_id()
6872
Name = player.Name
6973
}
7074
}
71-
});
75+
}));
7276

7377
// Assert
74-
PlayerPrimaryResponseDocument doc = (await action.Should().NotThrowAsync()).Subject;
75-
doc.Data.Id.Should().Be(player.StringId);
78+
PlayerPrimaryResponseDocument? doc = (await action.Should().NotThrowAsync()).Subject;
79+
doc.Should().BeNull();
7680
}
7781

7882
[Fact]
79-
public async Task Allows_passing_id()
83+
public async Task Omit_allowed_id()
8084
{
8185
// Arrange
8286
Game game = _fakers.Game.Generate();
@@ -85,49 +89,76 @@ public async Task Allows_passing_id()
8589
ClientGeneratedIdClient apiClient = new(httpClient);
8690

8791
// Act
88-
Func<Task<GamePrimaryResponseDocument>> action = () => apiClient.PostGameAsync(null, new GamePostRequestDocument
92+
Func<Task<GamePrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostGameAsync(null, new GamePostRequestDocument
8993
{
9094
Data = new GameDataInPostRequest
9195
{
92-
Id = game.StringId!, // FIXME: StringId is null, how to generate an id?
96+
Id = null!,
9397
Attributes = new GameAttributesInPostRequest
9498
{
9599
Name = game.Name,
96100
Price = (double)game.Price
97101
}
98102
}
99-
});
103+
}));
100104

101105
// Assert
102-
GamePrimaryResponseDocument doc = (await action.Should().NotThrowAsync()).Subject;
103-
doc.Data.Id.Should().Be(game.StringId);
106+
GamePrimaryResponseDocument? doc = (await action.Should().NotThrowAsync()).Subject;
107+
doc?.Data.Id.Should().NotBeNullOrEmpty();
104108
}
105109

106110
[Fact]
107-
public async Task Allow_omitting_id()
111+
public async Task Pass_allowed_id()
108112
{
109113
// Arrange
110114
Game game = _fakers.Game.Generate();
115+
game.Id = Guid.NewGuid();
111116

112117
using HttpClient httpClient = _testContext.Factory.CreateClient();
113118
ClientGeneratedIdClient apiClient = new(httpClient);
114119

115120
// Act
116-
Func<Task<GamePrimaryResponseDocument>> action = () => apiClient.PostGameAsync(null, new GamePostRequestDocument
121+
Func<Task<GamePrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostGameAsync(null, new GamePostRequestDocument
117122
{
118123
Data = new GameDataInPostRequest
119124
{
120-
Id = null!, // FIXME: incorrect nullability here
125+
Id = game.StringId!,
121126
Attributes = new GameAttributesInPostRequest
122127
{
123128
Name = game.Name,
124129
Price = (double)game.Price
125130
}
126131
}
127-
});
132+
}));
128133

129134
// Assert
130-
GamePrimaryResponseDocument doc = (await action.Should().NotThrowAsync()).Subject;
131-
doc.Data.Id.Should().NotBeNullOrEmpty();
135+
GamePrimaryResponseDocument? doc = (await action.Should().NotThrowAsync()).Subject;
136+
doc.Should().BeNull();
132137
}
133-
}
138+
139+
[Fact]
140+
public async Task Omit_forbidden_id()
141+
{
142+
// Arrange
143+
Group group = _fakers.Group.Generate();
144+
145+
using HttpClient httpClient = _testContext.Factory.CreateClient();
146+
ClientGeneratedIdClient apiClient = new(httpClient);
147+
148+
// Act
149+
Func<Task<GroupPrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostGroupAsync(null, new GroupPostRequestDocument
150+
{
151+
Data = new GroupDataInPostRequest
152+
{
153+
Attributes = new GroupAttributesInPostRequest
154+
{
155+
Name = group.Name
156+
}
157+
}
158+
}));
159+
160+
// Assert
161+
GroupPrimaryResponseDocument? doc = (await action.Should().NotThrowAsync()).Subject;
162+
doc?.Data.Id.Should().NotBeNullOrEmpty();
163+
}
164+
}

0 commit comments

Comments
 (0)