Skip to content

Commit 441ef62

Browse files
committed
openapi: support client-generated id
1 parent 69d91d6 commit 441ef62

File tree

8 files changed

+2344
-0
lines changed

8 files changed

+2344
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
using FluentAssertions;
2+
using OpenApiEndToEndTests.ClientGeneratedId.GeneratedCode;
3+
using OpenApiTests;
4+
using OpenApiTests.ClientGeneratedId;
5+
using TestBuildingBlocks;
6+
using Xunit;
7+
8+
namespace OpenApiEndToEndTests.ClientGeneratedId;
9+
10+
public sealed class PostTests : IClassFixture<IntegrationTestContext<OpenApiStartup<ClientGeneratedIdDbContext>, ClientGeneratedIdDbContext>>
11+
{
12+
private readonly IntegrationTestContext<OpenApiStartup<ClientGeneratedIdDbContext>, ClientGeneratedIdDbContext> _testContext;
13+
private readonly ClientGeneratedIdFakers _fakers = new();
14+
15+
public PostTests(IntegrationTestContext<OpenApiStartup<ClientGeneratedIdDbContext>, ClientGeneratedIdDbContext> testContext)
16+
{
17+
_testContext = testContext;
18+
19+
testContext.UseController<PlayersController>();
20+
testContext.UseController<GamesController>();
21+
}
22+
23+
[Fact]
24+
public async Task Returns_error_if_required_id_is_omitted()
25+
{
26+
// Arrange
27+
Player player = _fakers.Player.Generate();
28+
29+
using HttpClient httpClient = _testContext.Factory.CreateClient();
30+
ClientGeneratedIdClient apiClient = new(httpClient);
31+
32+
// Act
33+
Func<Task<PlayerPrimaryResponseDocument>> action = () => apiClient.PostPlayerAsync(null, new PlayerPostRequestDocument
34+
{
35+
Data = new PlayerDataInPostRequest
36+
{
37+
Id = null!, // FIXME: passing "" here works fine 🤔
38+
Attributes = new PlayerAttributesInPostRequest
39+
{
40+
Name = player.Name
41+
}
42+
}
43+
});
44+
45+
// 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.
49+
}
50+
51+
[Fact]
52+
public async Task Requires_passing_id()
53+
{
54+
// Arrange
55+
Player player = _fakers.Player.Generate();
56+
57+
using HttpClient httpClient = _testContext.Factory.CreateClient();
58+
ClientGeneratedIdClient apiClient = new(httpClient);
59+
60+
// Act
61+
Func<Task<PlayerPrimaryResponseDocument>> action = () => apiClient.PostPlayerAsync(null, new PlayerPostRequestDocument
62+
{
63+
Data = new PlayerDataInPostRequest
64+
{
65+
Id = player.StringId!,
66+
Attributes = new PlayerAttributesInPostRequest
67+
{
68+
Name = player.Name
69+
}
70+
}
71+
});
72+
73+
// Assert
74+
PlayerPrimaryResponseDocument doc = (await action.Should().NotThrowAsync()).Subject;
75+
doc.Data.Id.Should().Be(player.StringId);
76+
}
77+
78+
[Fact]
79+
public async Task Allows_passing_id()
80+
{
81+
// Arrange
82+
Game game = _fakers.Game.Generate();
83+
84+
using HttpClient httpClient = _testContext.Factory.CreateClient();
85+
ClientGeneratedIdClient apiClient = new(httpClient);
86+
87+
// Act
88+
Func<Task<GamePrimaryResponseDocument>> action = () => apiClient.PostGameAsync(null, new GamePostRequestDocument
89+
{
90+
Data = new GameDataInPostRequest
91+
{
92+
Id = game.StringId!, // FIXME: StringId is null, how to generate an id?
93+
Attributes = new GameAttributesInPostRequest
94+
{
95+
Name = game.Name,
96+
Price = (double)game.Price
97+
}
98+
}
99+
});
100+
101+
// Assert
102+
GamePrimaryResponseDocument doc = (await action.Should().NotThrowAsync()).Subject;
103+
doc.Data.Id.Should().Be(game.StringId);
104+
}
105+
106+
[Fact]
107+
public async Task Allow_omitting_id()
108+
{
109+
// Arrange
110+
Game game = _fakers.Game.Generate();
111+
112+
using HttpClient httpClient = _testContext.Factory.CreateClient();
113+
ClientGeneratedIdClient apiClient = new(httpClient);
114+
115+
// Act
116+
Func<Task<GamePrimaryResponseDocument>> action = () => apiClient.PostGameAsync(null, new GamePostRequestDocument
117+
{
118+
Data = new GameDataInPostRequest
119+
{
120+
Id = null!, // FIXME: incorrect nullability here
121+
Attributes = new GameAttributesInPostRequest
122+
{
123+
Name = game.Name,
124+
Price = (double)game.Price
125+
}
126+
}
127+
});
128+
129+
// Assert
130+
GamePrimaryResponseDocument doc = (await action.Should().NotThrowAsync()).Subject;
131+
doc.Data.Id.Should().NotBeNullOrEmpty();
132+
}
133+
}

0 commit comments

Comments
 (0)