Skip to content

Commit 3fcb6f8

Browse files
author
Bart Koelman
committed
Fixed broken tests on EF Core 6
Apparently EF Core 5 did not always fail on missing required relationships, which was fixed in EF Core 6. I tried to update existing tests leaving the models intact, but that poluted lots of tests so I made them optional instead.
1 parent 6546063 commit 3fcb6f8

File tree

9 files changed

+40
-40
lines changed

9 files changed

+40
-40
lines changed

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/Blog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ public sealed class Blog : Identifiable<int>
2222
public IList<BlogPost> Posts { get; set; } = new List<BlogPost>();
2323

2424
[HasOne]
25-
public WebAccount Owner { get; set; } = null!;
25+
public WebAccount? Owner { get; set; }
2626
}
2727
}

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/Comment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed class Comment : Identifiable<int>
1515
public DateTime CreatedAt { get; set; }
1616

1717
[HasOne]
18-
public WebAccount Author { get; set; } = null!;
18+
public WebAccount? Author { get; set; }
1919

2020
[HasOne]
2121
public BlogPost Parent { get; set; } = null!;

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/Filtering/FilterDepthTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,13 @@ public async Task Can_filter_in_multiple_scopes()
502502
List<Blog> blogs = _fakers.Blog.Generate(2);
503503
blogs[1].Title = "Technology";
504504
blogs[1].Owner = _fakers.WebAccount.Generate();
505-
blogs[1].Owner.UserName = "Smith";
506-
blogs[1].Owner.Posts = _fakers.BlogPost.Generate(2);
507-
blogs[1].Owner.Posts[0].Caption = "One";
508-
blogs[1].Owner.Posts[1].Caption = "Two";
509-
blogs[1].Owner.Posts[1].Comments = _fakers.Comment.Generate(2).ToHashSet();
510-
blogs[1].Owner.Posts[1].Comments.ElementAt(0).CreatedAt = 1.January(2000);
511-
blogs[1].Owner.Posts[1].Comments.ElementAt(1).CreatedAt = 10.January(2010);
505+
blogs[1].Owner!.UserName = "Smith";
506+
blogs[1].Owner!.Posts = _fakers.BlogPost.Generate(2);
507+
blogs[1].Owner!.Posts[0].Caption = "One";
508+
blogs[1].Owner!.Posts[1].Caption = "Two";
509+
blogs[1].Owner!.Posts[1].Comments = _fakers.Comment.Generate(2).ToHashSet();
510+
blogs[1].Owner!.Posts[1].Comments.ElementAt(0).CreatedAt = 1.January(2000);
511+
blogs[1].Owner!.Posts[1].Comments.ElementAt(1).CreatedAt = 10.January(2010);
512512

513513
await _testContext.RunOnDatabaseAsync(async dbContext =>
514514
{
@@ -538,13 +538,13 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
538538
responseDocument.Included.ShouldHaveCount(3);
539539

540540
responseDocument.Included[0].Type.Should().Be("webAccounts");
541-
responseDocument.Included[0].Id.Should().Be(blogs[1].Owner.StringId);
541+
responseDocument.Included[0].Id.Should().Be(blogs[1].Owner!.StringId);
542542

543543
responseDocument.Included[1].Type.Should().Be("blogPosts");
544-
responseDocument.Included[1].Id.Should().Be(blogs[1].Owner.Posts[1].StringId);
544+
responseDocument.Included[1].Id.Should().Be(blogs[1].Owner!.Posts[1].StringId);
545545

546546
responseDocument.Included[2].Type.Should().Be("comments");
547-
responseDocument.Included[2].Id.Should().Be(blogs[1].Owner.Posts[1].Comments.ElementAt(1).StringId);
547+
responseDocument.Included[2].Id.Should().Be(blogs[1].Owner!.Posts[1].Comments.ElementAt(1).StringId);
548548
}
549549
}
550550
}

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/Includes/IncludeTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,10 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
416416
responseDocument.Included[2].Id.Should().Be(comment.Parent.Comments.ElementAt(0).StringId);
417417
responseDocument.Included[2].Attributes.ShouldContainKey("text").With(value => value.Should().Be(comment.Parent.Comments.ElementAt(0).Text));
418418

419-
string userName = comment.Parent.Comments.ElementAt(0).Author.UserName;
419+
string userName = comment.Parent.Comments.ElementAt(0).Author!.UserName;
420420

421421
responseDocument.Included[3].Type.Should().Be("webAccounts");
422-
responseDocument.Included[3].Id.Should().Be(comment.Parent.Comments.ElementAt(0).Author.StringId);
422+
responseDocument.Included[3].Id.Should().Be(comment.Parent.Comments.ElementAt(0).Author!.StringId);
423423
responseDocument.Included[3].Attributes.ShouldContainKey("userName").With(value => value.Should().Be(userName));
424424

425425
responseDocument.Included[4].Type.Should().Be("comments");
@@ -437,7 +437,7 @@ public async Task Can_include_chain_of_relationships_with_multiple_paths()
437437
blog.Posts[0].Author!.Preferences = _fakers.AccountPreferences.Generate();
438438
blog.Posts[0].Comments = _fakers.Comment.Generate(2).ToHashSet();
439439
blog.Posts[0].Comments.ElementAt(0).Author = _fakers.WebAccount.Generate();
440-
blog.Posts[0].Comments.ElementAt(0).Author.Posts = _fakers.BlogPost.Generate(1);
440+
blog.Posts[0].Comments.ElementAt(0).Author!.Posts = _fakers.BlogPost.Generate(1);
441441

442442
await _testContext.RunOnDatabaseAsync(async dbContext =>
443443
{
@@ -493,7 +493,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
493493
value.ShouldNotBeNull();
494494
value.Data.SingleValue.ShouldNotBeNull();
495495
value.Data.SingleValue.Type.Should().Be("accountPreferences");
496-
value.Data.SingleValue.Id.Should().Be(blog.Posts[0].Author!.Preferences.StringId);
496+
value.Data.SingleValue.Id.Should().Be(blog.Posts[0].Author!.Preferences!.StringId);
497497
});
498498

499499
responseDocument.Included[1].Relationships.ShouldContainKey("posts").With(value =>
@@ -503,7 +503,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
503503
});
504504

505505
responseDocument.Included[2].Type.Should().Be("accountPreferences");
506-
responseDocument.Included[2].Id.Should().Be(blog.Posts[0].Author!.Preferences.StringId);
506+
responseDocument.Included[2].Id.Should().Be(blog.Posts[0].Author!.Preferences!.StringId);
507507

508508
responseDocument.Included[3].Type.Should().Be("comments");
509509
responseDocument.Included[3].Id.Should().Be(blog.Posts[0].Comments.ElementAt(0).StringId);
@@ -513,18 +513,18 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
513513
value.ShouldNotBeNull();
514514
value.Data.SingleValue.ShouldNotBeNull();
515515
value.Data.SingleValue.Type.Should().Be("webAccounts");
516-
value.Data.SingleValue.Id.Should().Be(blog.Posts[0].Comments.ElementAt(0).Author.StringId);
516+
value.Data.SingleValue.Id.Should().Be(blog.Posts[0].Comments.ElementAt(0).Author!.StringId);
517517
});
518518

519519
responseDocument.Included[4].Type.Should().Be("webAccounts");
520-
responseDocument.Included[4].Id.Should().Be(blog.Posts[0].Comments.ElementAt(0).Author.StringId);
520+
responseDocument.Included[4].Id.Should().Be(blog.Posts[0].Comments.ElementAt(0).Author!.StringId);
521521

522522
responseDocument.Included[4].Relationships.ShouldContainKey("posts").With(value =>
523523
{
524524
value.ShouldNotBeNull();
525525
value.Data.ManyValue.ShouldNotBeEmpty();
526526
value.Data.ManyValue[0].Type.Should().Be("blogPosts");
527-
value.Data.ManyValue[0].Id.Should().Be(blog.Posts[0].Comments.ElementAt(0).Author.Posts[0].StringId);
527+
value.Data.ManyValue[0].Id.Should().Be(blog.Posts[0].Comments.ElementAt(0).Author!.Posts[0].StringId);
528528
});
529529

530530
responseDocument.Included[4].Relationships.ShouldContainKey("preferences").With(value =>
@@ -534,7 +534,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
534534
});
535535

536536
responseDocument.Included[5].Type.Should().Be("blogPosts");
537-
responseDocument.Included[5].Id.Should().Be(blog.Posts[0].Comments.ElementAt(0).Author.Posts[0].StringId);
537+
responseDocument.Included[5].Id.Should().Be(blog.Posts[0].Comments.ElementAt(0).Author!.Posts[0].StringId);
538538

539539
responseDocument.Included[5].Relationships.ShouldContainKey("author").With(value =>
540540
{

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/Pagination/PaginationWithTotalCountTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ public async Task Can_paginate_in_multiple_scopes()
416416
// Arrange
417417
List<Blog> blogs = _fakers.Blog.Generate(2);
418418
blogs[1].Owner = _fakers.WebAccount.Generate();
419-
blogs[1].Owner.Posts = _fakers.BlogPost.Generate(2);
420-
blogs[1].Owner.Posts[1].Comments = _fakers.Comment.Generate(2).ToHashSet();
419+
blogs[1].Owner!.Posts = _fakers.BlogPost.Generate(2);
420+
blogs[1].Owner!.Posts[1].Comments = _fakers.Comment.Generate(2).ToHashSet();
421421

422422
await _testContext.RunOnDatabaseAsync(async dbContext =>
423423
{
@@ -441,13 +441,13 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
441441
responseDocument.Included.ShouldHaveCount(3);
442442

443443
responseDocument.Included[0].Type.Should().Be("webAccounts");
444-
responseDocument.Included[0].Id.Should().Be(blogs[1].Owner.StringId);
444+
responseDocument.Included[0].Id.Should().Be(blogs[1].Owner!.StringId);
445445

446446
responseDocument.Included[1].Type.Should().Be("blogPosts");
447-
responseDocument.Included[1].Id.Should().Be(blogs[1].Owner.Posts[1].StringId);
447+
responseDocument.Included[1].Id.Should().Be(blogs[1].Owner!.Posts[1].StringId);
448448

449449
responseDocument.Included[2].Type.Should().Be("comments");
450-
responseDocument.Included[2].Id.Should().Be(blogs[1].Owner.Posts[1].Comments.ElementAt(1).StringId);
450+
responseDocument.Included[2].Id.Should().Be(blogs[1].Owner!.Posts[1].Comments.ElementAt(1).StringId);
451451

452452
string linkPrefix = $"{HostPrefix}/blogs?include=owner.posts.comments";
453453

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/Sorting/SortTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,13 @@ public async Task Can_sort_in_multiple_scopes()
407407
blogs[1].Title = "Technology";
408408

409409
blogs[1].Owner = _fakers.WebAccount.Generate();
410-
blogs[1].Owner.Posts = _fakers.BlogPost.Generate(2);
411-
blogs[1].Owner.Posts[0].Caption = "One";
412-
blogs[1].Owner.Posts[1].Caption = "Two";
410+
blogs[1].Owner!.Posts = _fakers.BlogPost.Generate(2);
411+
blogs[1].Owner!.Posts[0].Caption = "One";
412+
blogs[1].Owner!.Posts[1].Caption = "Two";
413413

414-
blogs[1].Owner.Posts[1].Comments = _fakers.Comment.Generate(2).ToHashSet();
415-
blogs[1].Owner.Posts[1].Comments.ElementAt(0).CreatedAt = 1.January(2000);
416-
blogs[1].Owner.Posts[1].Comments.ElementAt(0).CreatedAt = 10.January(2010);
414+
blogs[1].Owner!.Posts[1].Comments = _fakers.Comment.Generate(2).ToHashSet();
415+
blogs[1].Owner!.Posts[1].Comments.ElementAt(0).CreatedAt = 1.January(2000);
416+
blogs[1].Owner!.Posts[1].Comments.ElementAt(0).CreatedAt = 10.January(2010);
417417

418418
await _testContext.RunOnDatabaseAsync(async dbContext =>
419419
{
@@ -437,19 +437,19 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
437437
responseDocument.Included.ShouldHaveCount(5);
438438

439439
responseDocument.Included[0].Type.Should().Be("webAccounts");
440-
responseDocument.Included[0].Id.Should().Be(blogs[1].Owner.StringId);
440+
responseDocument.Included[0].Id.Should().Be(blogs[1].Owner!.StringId);
441441

442442
responseDocument.Included[1].Type.Should().Be("blogPosts");
443-
responseDocument.Included[1].Id.Should().Be(blogs[1].Owner.Posts[1].StringId);
443+
responseDocument.Included[1].Id.Should().Be(blogs[1].Owner!.Posts[1].StringId);
444444

445445
responseDocument.Included[2].Type.Should().Be("comments");
446-
responseDocument.Included[2].Id.Should().Be(blogs[1].Owner.Posts[1].Comments.ElementAt(1).StringId);
446+
responseDocument.Included[2].Id.Should().Be(blogs[1].Owner!.Posts[1].Comments.ElementAt(1).StringId);
447447

448448
responseDocument.Included[3].Type.Should().Be("comments");
449-
responseDocument.Included[3].Id.Should().Be(blogs[1].Owner.Posts[1].Comments.ElementAt(0).StringId);
449+
responseDocument.Included[3].Id.Should().Be(blogs[1].Owner!.Posts[1].Comments.ElementAt(0).StringId);
450450

451451
responseDocument.Included[4].Type.Should().Be("blogPosts");
452-
responseDocument.Included[4].Id.Should().Be(blogs[1].Owner.Posts[0].StringId);
452+
responseDocument.Included[4].Id.Should().Be(blogs[1].Owner!.Posts[0].StringId);
453453
}
454454

455455
[Fact]

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/SparseFieldSets/SparseFieldSetTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
540540
blogCaptured.Title.Should().Be(blog.Title);
541541
blogCaptured.PlatformName.Should().BeNull();
542542

543-
blogCaptured.Owner.UserName.Should().Be(blog.Owner.UserName);
543+
blogCaptured.Owner!.UserName.Should().Be(blog.Owner.UserName);
544544
blogCaptured.Owner.DisplayName.Should().Be(blog.Owner.DisplayName);
545545
blogCaptured.Owner.DateOfBirth.Should().BeNull();
546546

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/WebAccount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed class WebAccount : Identifiable<int>
2828
public IList<BlogPost> Posts { get; set; } = new List<BlogPost>();
2929

3030
[HasOne]
31-
public AccountPreferences Preferences { get; set; } = null!;
31+
public AccountPreferences? Preferences { get; set; }
3232

3333
[HasMany]
3434
public IList<LoginAttempt> LoginAttempts { get; set; } = new List<LoginAttempt>();

test/JsonApiDotNetCoreTests/IntegrationTests/ResourceDefinitions/Serialization/Scholarship.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public sealed class Scholarship : Identifiable<int>
1818
public IList<Student> Participants { get; set; } = new List<Student>();
1919

2020
[HasOne]
21-
public Student PrimaryContact { get; set; } = null!;
21+
public Student? PrimaryContact { get; set; }
2222
}
2323
}

0 commit comments

Comments
 (0)