Skip to content

Commit 0ab9403

Browse files
author
Bart Koelman
committed
Annotated test projects, use fakers for non-trivial models in integration tests, FluentAssertions everywhere
1 parent 268bc6c commit 0ab9403

File tree

366 files changed

+4635
-3720
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

366 files changed

+4635
-3720
lines changed

src/JsonApiDotNetCore/AtomicOperations/OperationProcessorAccessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public OperationProcessorAccessor(IServiceProvider serviceProvider)
3434

3535
protected virtual IOperationProcessor ResolveProcessor(OperationContainer operation)
3636
{
37-
Type processorInterface = GetProcessorInterface(operation.Request.WriteOperation.GetValueOrDefault());
37+
Type processorInterface = GetProcessorInterface(operation.Request.WriteOperation!.Value);
3838
ResourceType resourceType = operation.Request.PrimaryResourceType!;
3939

4040
Type processorType = processorInterface.MakeGenericType(resourceType.ClrType, resourceType.IdentityClrType);
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
#nullable disable
2-
31
using JetBrains.Annotations;
42
using JsonApiDotNetCore.Resources;
53

64
namespace DiscoveryTests
75
{
86
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
9-
public sealed class TestResource : Identifiable<int>
7+
public sealed class PrivateResource : Identifiable<int>
108
{
119
}
1210
}

test/DiscoveryTests/TestResourceDefinition.cs renamed to test/DiscoveryTests/PrivateResourceDefinition.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
#nullable disable
2-
31
using JetBrains.Annotations;
42
using JsonApiDotNetCore.Configuration;
53
using JsonApiDotNetCore.Resources;
64

75
namespace DiscoveryTests
86
{
97
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
10-
public sealed class TestResourceDefinition : JsonApiResourceDefinition<TestResource, int>
8+
public sealed class PrivateResourceDefinition : JsonApiResourceDefinition<PrivateResource, int>
119
{
12-
public TestResourceDefinition(IResourceGraph resourceGraph)
10+
public PrivateResourceDefinition(IResourceGraph resourceGraph)
1311
: base(resourceGraph)
1412
{
1513
}

test/DiscoveryTests/TestResourceRepository.cs renamed to test/DiscoveryTests/PrivateResourceRepository.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#nullable disable
2-
31
using System.Collections.Generic;
42
using JetBrains.Annotations;
53
using JsonApiDotNetCore.Configuration;
@@ -11,9 +9,9 @@
119
namespace DiscoveryTests
1210
{
1311
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
14-
public sealed class TestResourceRepository : EntityFrameworkCoreRepository<TestResource, int>
12+
public sealed class PrivateResourceRepository : EntityFrameworkCoreRepository<PrivateResource, int>
1513
{
16-
public TestResourceRepository(ITargetedFields targetedFields, IDbContextResolver dbContextResolver, IResourceGraph resourceGraph,
14+
public PrivateResourceRepository(ITargetedFields targetedFields, IDbContextResolver dbContextResolver, IResourceGraph resourceGraph,
1715
IResourceFactory resourceFactory, IEnumerable<IQueryConstraintProvider> constraintProviders, ILoggerFactory loggerFactory,
1816
IResourceDefinitionAccessor resourceDefinitionAccessor)
1917
: base(targetedFields, dbContextResolver, resourceGraph, resourceFactory, constraintProviders, loggerFactory, resourceDefinitionAccessor)

test/DiscoveryTests/TestResourceService.cs renamed to test/DiscoveryTests/PrivateResourceService.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#nullable disable
2-
31
using JetBrains.Annotations;
42
using JsonApiDotNetCore.Configuration;
53
using JsonApiDotNetCore.Middleware;
@@ -12,11 +10,11 @@
1210
namespace DiscoveryTests
1311
{
1412
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
15-
public sealed class TestResourceService : JsonApiResourceService<TestResource, int>
13+
public sealed class PrivateResourceService : JsonApiResourceService<PrivateResource, int>
1614
{
17-
public TestResourceService(IResourceRepositoryAccessor repositoryAccessor, IQueryLayerComposer queryLayerComposer, IPaginationContext paginationContext,
18-
IJsonApiOptions options, ILoggerFactory loggerFactory, IJsonApiRequest request, IResourceChangeTracker<TestResource> resourceChangeTracker,
19-
IResourceDefinitionAccessor resourceDefinitionAccessor)
15+
public PrivateResourceService(IResourceRepositoryAccessor repositoryAccessor, IQueryLayerComposer queryLayerComposer,
16+
IPaginationContext paginationContext, IJsonApiOptions options, ILoggerFactory loggerFactory, IJsonApiRequest request,
17+
IResourceChangeTracker<PrivateResource> resourceChangeTracker, IResourceDefinitionAccessor resourceDefinitionAccessor)
2018
: base(repositoryAccessor, queryLayerComposer, paginationContext, options, loggerFactory, request, resourceChangeTracker,
2119
resourceDefinitionAccessor)
2220
{

test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#nullable disable
2-
31
using FluentAssertions;
42
using JsonApiDotNetCore.Configuration;
53
using JsonApiDotNetCore.Middleware;
@@ -20,9 +18,8 @@ namespace DiscoveryTests
2018
{
2119
public sealed class ServiceDiscoveryFacadeTests
2220
{
23-
private static readonly NullLoggerFactory LoggerFactory = NullLoggerFactory.Instance;
21+
private static readonly ILoggerFactory LoggerFactory = NullLoggerFactory.Instance;
2422
private readonly IServiceCollection _services = new ServiceCollection();
25-
private readonly JsonApiOptions _options = new();
2623
private readonly ResourceGraphBuilder _resourceGraphBuilder;
2724

2825
public ServiceDiscoveryFacadeTests()
@@ -31,8 +28,10 @@ public ServiceDiscoveryFacadeTests()
3128
dbResolverMock.Setup(resolver => resolver.GetContext()).Returns(new Mock<DbContext>().Object);
3229
_services.AddScoped(_ => dbResolverMock.Object);
3330

34-
_services.AddSingleton<IJsonApiOptions>(_options);
35-
_services.AddSingleton<ILoggerFactory>(LoggerFactory);
31+
IJsonApiOptions options = new JsonApiOptions();
32+
33+
_services.AddSingleton(options);
34+
_services.AddSingleton(LoggerFactory);
3635
_services.AddScoped(_ => new Mock<IJsonApiRequest>().Object);
3736
_services.AddScoped(_ => new Mock<ITargetedFields>().Object);
3837
_services.AddScoped(_ => new Mock<IResourceGraph>().Object);
@@ -43,7 +42,7 @@ public ServiceDiscoveryFacadeTests()
4342
_services.AddScoped(_ => new Mock<IResourceRepositoryAccessor>().Object);
4443
_services.AddScoped(_ => new Mock<IResourceDefinitionAccessor>().Object);
4544

46-
_resourceGraphBuilder = new ResourceGraphBuilder(_options, LoggerFactory);
45+
_resourceGraphBuilder = new ResourceGraphBuilder(options, LoggerFactory);
4746
}
4847

4948
[Fact]
@@ -59,10 +58,10 @@ public void Can_add_resources_from_assembly_to_graph()
5958
// Assert
6059
IResourceGraph resourceGraph = _resourceGraphBuilder.Build();
6160

62-
ResourceType personType = resourceGraph.FindResourceType(typeof(Person));
61+
ResourceType? personType = resourceGraph.FindResourceType(typeof(Person));
6362
personType.ShouldNotBeNull();
6463

65-
ResourceType todoItemType = resourceGraph.FindResourceType(typeof(TodoItem));
64+
ResourceType? todoItemType = resourceGraph.FindResourceType(typeof(TodoItem));
6665
todoItemType.ShouldNotBeNull();
6766
}
6867

@@ -79,7 +78,7 @@ public void Can_add_resource_from_current_assembly_to_graph()
7978
// Assert
8079
IResourceGraph resourceGraph = _resourceGraphBuilder.Build();
8180

82-
ResourceType testResourceType = resourceGraph.FindResourceType(typeof(TestResource));
81+
ResourceType? testResourceType = resourceGraph.FindResourceType(typeof(PrivateResource));
8382
testResourceType.ShouldNotBeNull();
8483
}
8584

@@ -96,8 +95,8 @@ public void Can_add_resource_service_from_current_assembly_to_container()
9695
// Assert
9796
ServiceProvider services = _services.BuildServiceProvider();
9897

99-
var resourceService = services.GetRequiredService<IResourceService<TestResource, int>>();
100-
resourceService.Should().BeOfType<TestResourceService>();
98+
var resourceService = services.GetRequiredService<IResourceService<PrivateResource, int>>();
99+
resourceService.Should().BeOfType<PrivateResourceService>();
101100
}
102101

103102
[Fact]
@@ -113,8 +112,8 @@ public void Can_add_resource_repository_from_current_assembly_to_container()
113112
// Assert
114113
ServiceProvider services = _services.BuildServiceProvider();
115114

116-
var resourceRepository = services.GetRequiredService<IResourceRepository<TestResource, int>>();
117-
resourceRepository.Should().BeOfType<TestResourceRepository>();
115+
var resourceRepository = services.GetRequiredService<IResourceRepository<PrivateResource, int>>();
116+
resourceRepository.Should().BeOfType<PrivateResourceRepository>();
118117
}
119118

120119
[Fact]
@@ -130,8 +129,8 @@ public void Can_add_resource_definition_from_current_assembly_to_container()
130129
// Assert
131130
ServiceProvider services = _services.BuildServiceProvider();
132131

133-
var resourceDefinition = services.GetRequiredService<IResourceDefinition<TestResource, int>>();
134-
resourceDefinition.Should().BeOfType<TestResourceDefinition>();
132+
var resourceDefinition = services.GetRequiredService<IResourceDefinition<PrivateResource, int>>();
133+
resourceDefinition.Should().BeOfType<PrivateResourceDefinition>();
135134
}
136135
}
137136
}

test/JsonApiDotNetCoreTests/IntegrationTests/Archiving/ArchiveTests.cs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#nullable disable
2-
31
using System;
42
using System.Collections.Generic;
53
using System.Linq;
@@ -56,7 +54,9 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
5654

5755
responseDocument.Data.SingleValue.ShouldNotBeNull();
5856
responseDocument.Data.SingleValue.Id.Should().Be(broadcast.StringId);
59-
responseDocument.Data.SingleValue.Attributes["archivedAt"].As<DateTimeOffset?>().Should().BeCloseTo(broadcast.ArchivedAt.GetValueOrDefault());
57+
58+
responseDocument.Data.SingleValue.Attributes.ShouldContainKey("archivedAt")
59+
.With(value => value.As<DateTimeOffset?>().Should().BeCloseTo(broadcast.ArchivedAt!.Value));
6060
}
6161

6262
[Fact]
@@ -82,7 +82,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
8282

8383
responseDocument.Data.SingleValue.ShouldNotBeNull();
8484
responseDocument.Data.SingleValue.Id.Should().Be(broadcast.StringId);
85-
responseDocument.Data.SingleValue.Attributes["archivedAt"].As<DateTimeOffset?>().Should().BeNull();
85+
responseDocument.Data.SingleValue.Attributes.ShouldContainKey("archivedAt").With(value => value.As<DateTimeOffset?>().Should().BeNull());
8686
}
8787

8888
[Fact]
@@ -109,7 +109,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
109109

110110
responseDocument.Data.ManyValue.ShouldHaveCount(1);
111111
responseDocument.Data.ManyValue[0].Id.Should().Be(broadcasts[1].StringId);
112-
responseDocument.Data.ManyValue[0].Attributes["archivedAt"].As<DateTimeOffset?>().Should().BeNull();
112+
responseDocument.Data.ManyValue[0].Attributes.ShouldContainKey("archivedAt").With(value => value.As<DateTimeOffset?>().Should().BeNull());
113113
}
114114

115115
[Fact]
@@ -136,9 +136,12 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
136136

137137
responseDocument.Data.ManyValue.ShouldHaveCount(2);
138138
responseDocument.Data.ManyValue[0].Id.Should().Be(broadcasts[0].StringId);
139-
responseDocument.Data.ManyValue[0].Attributes["archivedAt"].As<DateTimeOffset?>().Should().BeCloseTo(broadcasts[0].ArchivedAt.GetValueOrDefault());
139+
140+
responseDocument.Data.ManyValue[0].Attributes.ShouldContainKey("archivedAt")
141+
.With(value => value.As<DateTimeOffset?>().Should().BeCloseTo(broadcasts[0].ArchivedAt!.Value));
142+
140143
responseDocument.Data.ManyValue[1].Id.Should().Be(broadcasts[1].StringId);
141-
responseDocument.Data.ManyValue[1].Attributes["archivedAt"].Should().BeNull();
144+
responseDocument.Data.ManyValue[1].Attributes.ShouldContainKey("archivedAt").With(value => value.Should().BeNull());
142145
}
143146

144147
[Fact]
@@ -168,7 +171,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
168171

169172
responseDocument.Included.ShouldHaveCount(1);
170173
responseDocument.Included[0].Id.Should().Be(station.Broadcasts.ElementAt(1).StringId);
171-
responseDocument.Included[0].Attributes["archivedAt"].Should().BeNull();
174+
responseDocument.Included[0].Attributes.ShouldContainKey("archivedAt").With(value => value.Should().BeNull());
172175
}
173176

174177
[Fact]
@@ -194,16 +197,16 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
194197
// Assert
195198
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
196199

197-
DateTimeOffset archivedAt0 = station.Broadcasts.ElementAt(0).ArchivedAt.GetValueOrDefault();
200+
DateTimeOffset archivedAt0 = station.Broadcasts.ElementAt(0).ArchivedAt!.Value;
198201

199202
responseDocument.Data.SingleValue.ShouldNotBeNull();
200203
responseDocument.Data.SingleValue.Id.Should().Be(station.StringId);
201204

202205
responseDocument.Included.ShouldHaveCount(2);
203206
responseDocument.Included[0].Id.Should().Be(station.Broadcasts.ElementAt(0).StringId);
204-
responseDocument.Included[0].Attributes["archivedAt"].As<DateTimeOffset?>().Should().BeCloseTo(archivedAt0);
207+
responseDocument.Included[0].Attributes.ShouldContainKey("archivedAt").With(value => value.As<DateTimeOffset?>().Should().BeCloseTo(archivedAt0));
205208
responseDocument.Included[1].Id.Should().Be(station.Broadcasts.ElementAt(1).StringId);
206-
responseDocument.Included[1].Attributes["archivedAt"].Should().BeNull();
209+
responseDocument.Included[1].Attributes.ShouldContainKey("archivedAt").With(value => value.Should().BeNull());
207210
}
208211

209212
[Fact]
@@ -227,11 +230,11 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
227230
// Assert
228231
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
229232

230-
DateTimeOffset archivedAt = comment.AppliesTo.ArchivedAt.GetValueOrDefault();
231-
232233
responseDocument.Data.SingleValue.ShouldNotBeNull();
233234
responseDocument.Data.SingleValue.Id.Should().Be(comment.AppliesTo.StringId);
234-
responseDocument.Data.SingleValue.Attributes["archivedAt"].As<DateTimeOffset?>().Should().BeCloseTo(archivedAt);
235+
236+
responseDocument.Data.SingleValue.Attributes.ShouldContainKey("archivedAt")
237+
.With(value => value.As<DateTimeOffset?>().Should().BeCloseTo(comment.AppliesTo.ArchivedAt!.Value));
235238
}
236239

237240
[Fact]
@@ -258,7 +261,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
258261

259262
responseDocument.Data.ManyValue.ShouldHaveCount(1);
260263
responseDocument.Data.ManyValue[0].Id.Should().Be(station.Broadcasts.ElementAt(1).StringId);
261-
responseDocument.Data.ManyValue[0].Attributes["archivedAt"].Should().BeNull();
264+
responseDocument.Data.ManyValue[0].Attributes.ShouldContainKey("archivedAt").With(value => value.Should().BeNull());
262265
}
263266

264267
[Fact]
@@ -283,13 +286,14 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
283286
// Assert
284287
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
285288

286-
DateTimeOffset archivedAt0 = station.Broadcasts.ElementAt(0).ArchivedAt.GetValueOrDefault();
287-
288289
responseDocument.Data.ManyValue.ShouldHaveCount(2);
289290
responseDocument.Data.ManyValue[0].Id.Should().Be(station.Broadcasts.ElementAt(0).StringId);
290-
responseDocument.Data.ManyValue[0].Attributes["archivedAt"].As<DateTimeOffset?>().Should().BeCloseTo(archivedAt0);
291+
292+
responseDocument.Data.ManyValue[0].Attributes.ShouldContainKey("archivedAt").With(value =>
293+
value.As<DateTimeOffset?>().Should().BeCloseTo(station.Broadcasts.ElementAt(0).ArchivedAt!.Value));
294+
291295
responseDocument.Data.ManyValue[1].Id.Should().Be(station.Broadcasts.ElementAt(1).StringId);
292-
responseDocument.Data.ManyValue[1].Attributes["archivedAt"].Should().BeNull();
296+
responseDocument.Data.ManyValue[1].Attributes.ShouldContainKey("archivedAt").With(value => value.Should().BeNull());
293297
}
294298

295299
[Fact]
@@ -320,7 +324,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
320324

321325
responseDocument.Included.ShouldHaveCount(1);
322326
responseDocument.Included[0].Id.Should().Be(network.Stations.ElementAt(0).Broadcasts.ElementAt(1).StringId);
323-
responseDocument.Included[0].Attributes["archivedAt"].Should().BeNull();
327+
responseDocument.Included[0].Attributes.ShouldContainKey("archivedAt").With(value => value.Should().BeNull());
324328
}
325329

326330
[Fact]
@@ -346,16 +350,16 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
346350
// Assert
347351
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
348352

349-
DateTimeOffset archivedAt0 = network.Stations.ElementAt(0).Broadcasts.ElementAt(0).ArchivedAt.GetValueOrDefault();
353+
DateTimeOffset archivedAt0 = network.Stations.ElementAt(0).Broadcasts.ElementAt(0).ArchivedAt!.Value;
350354

351355
responseDocument.Data.ManyValue.ShouldHaveCount(1);
352356
responseDocument.Data.ManyValue[0].Id.Should().Be(network.Stations.ElementAt(0).StringId);
353357

354358
responseDocument.Included.ShouldHaveCount(2);
355359
responseDocument.Included[0].Id.Should().Be(network.Stations.ElementAt(0).Broadcasts.ElementAt(0).StringId);
356-
responseDocument.Included[0].Attributes["archivedAt"].As<DateTimeOffset?>().Should().BeCloseTo(archivedAt0);
360+
responseDocument.Included[0].Attributes.ShouldContainKey("archivedAt").With(value => value.As<DateTimeOffset?>().Should().BeCloseTo(archivedAt0));
357361
responseDocument.Included[1].Id.Should().Be(network.Stations.ElementAt(0).Broadcasts.ElementAt(1).StringId);
358-
responseDocument.Included[1].Attributes["archivedAt"].Should().BeNull();
362+
responseDocument.Included[1].Attributes.ShouldContainKey("archivedAt").With(value => value.Should().BeNull());
359363
}
360364

361365
[Fact]
@@ -439,9 +443,12 @@ public async Task Can_create_unarchived_resource()
439443
httpResponse.Should().HaveStatusCode(HttpStatusCode.Created);
440444

441445
responseDocument.Data.SingleValue.ShouldNotBeNull();
442-
responseDocument.Data.SingleValue.Attributes["title"].Should().Be(newBroadcast.Title);
443-
responseDocument.Data.SingleValue.Attributes["airedAt"].As<DateTimeOffset>().Should().BeCloseTo(newBroadcast.AiredAt);
444-
responseDocument.Data.SingleValue.Attributes["archivedAt"].Should().BeNull();
446+
responseDocument.Data.SingleValue.Attributes.ShouldContainKey("title").With(value => value.Should().Be(newBroadcast.Title));
447+
448+
responseDocument.Data.SingleValue.Attributes.ShouldContainKey("airedAt")
449+
.With(value => value.As<DateTimeOffset>().Should().BeCloseTo(newBroadcast.AiredAt));
450+
451+
responseDocument.Data.SingleValue.Attributes.ShouldContainKey("archivedAt").With(value => value.Should().BeNull());
445452
}
446453

447454
[Fact]
@@ -636,7 +643,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
636643

637644
await _testContext.RunOnDatabaseAsync(async dbContext =>
638645
{
639-
TelevisionBroadcast broadcastInDatabase = await dbContext.Broadcasts.FirstWithIdOrDefaultAsync(broadcast.Id);
646+
TelevisionBroadcast? broadcastInDatabase = await dbContext.Broadcasts.FirstWithIdOrDefaultAsync(broadcast.Id);
640647

641648
broadcastInDatabase.Should().BeNull();
642649
});
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#nullable disable
2-
31
using System;
42
using JetBrains.Annotations;
53
using JsonApiDotNetCore.Resources;
@@ -11,12 +9,12 @@ namespace JsonApiDotNetCoreTests.IntegrationTests.Archiving
119
public sealed class BroadcastComment : Identifiable<int>
1210
{
1311
[Attr]
14-
public string Text { get; set; }
12+
public string Text { get; set; } = null!;
1513

1614
[Attr]
1715
public DateTimeOffset CreatedAt { get; set; }
1816

1917
[HasOne]
20-
public TelevisionBroadcast AppliesTo { get; set; }
18+
public TelevisionBroadcast AppliesTo { get; set; } = null!;
2119
}
2220
}

0 commit comments

Comments
 (0)