|
| 1 | +using System.Collections.Generic; |
1 | 2 | using System.Linq;
|
2 | 3 | using System.Net;
|
3 | 4 | using System.Threading.Tasks;
|
@@ -660,5 +661,107 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
|
660 | 661 | workItemInDatabase.Subscribers.Should().HaveCount(0);
|
661 | 662 | });
|
662 | 663 | }
|
| 664 | + |
| 665 | + [Fact] |
| 666 | + public async Task Can_add_self_to_cyclic_HasMany_relationship() |
| 667 | + { |
| 668 | + // Arrange |
| 669 | + var existingWorkItem = _fakers.WorkItem.Generate(); |
| 670 | + existingWorkItem.Children = _fakers.WorkItem.Generate(1); |
| 671 | + |
| 672 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 673 | + { |
| 674 | + dbContext.WorkItems.Add(existingWorkItem); |
| 675 | + await dbContext.SaveChangesAsync(); |
| 676 | + }); |
| 677 | + |
| 678 | + var requestBody = new |
| 679 | + { |
| 680 | + data = new[] |
| 681 | + { |
| 682 | + new |
| 683 | + { |
| 684 | + type = "workItems", |
| 685 | + id = existingWorkItem.StringId |
| 686 | + } |
| 687 | + } |
| 688 | + }; |
| 689 | + |
| 690 | + var route = $"/workItems/{existingWorkItem.StringId}/relationships/children"; |
| 691 | + |
| 692 | + // Act |
| 693 | + var (httpResponse, responseDocument) = await _testContext.ExecutePostAsync<string>(route, requestBody); |
| 694 | + |
| 695 | + // Assert |
| 696 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent); |
| 697 | + |
| 698 | + responseDocument.Should().BeEmpty(); |
| 699 | + |
| 700 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 701 | + { |
| 702 | + var workItemInDatabase = await dbContext.WorkItems |
| 703 | + .Include(workItem => workItem.Children) |
| 704 | + .FirstAsync(workItem => workItem.Id == existingWorkItem.Id); |
| 705 | + |
| 706 | + workItemInDatabase.Children.Should().HaveCount(2); |
| 707 | + workItemInDatabase.Children.Should().ContainSingle(workItem => workItem.Id == existingWorkItem.Children[0].Id); |
| 708 | + workItemInDatabase.Children.Should().ContainSingle(workItem => workItem.Id == existingWorkItem.Id); |
| 709 | + }); |
| 710 | + } |
| 711 | + |
| 712 | + [Fact] |
| 713 | + public async Task Can_add_self_to_cyclic_HasManyThrough_relationship() |
| 714 | + { |
| 715 | + // Arrange |
| 716 | + var existingWorkItem = _fakers.WorkItem.Generate(); |
| 717 | + existingWorkItem.RelatedToItems = new List<WorkItemToWorkItem> |
| 718 | + { |
| 719 | + new WorkItemToWorkItem |
| 720 | + { |
| 721 | + ToItem = _fakers.WorkItem.Generate() |
| 722 | + } |
| 723 | + }; |
| 724 | + |
| 725 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 726 | + { |
| 727 | + dbContext.WorkItems.Add(existingWorkItem); |
| 728 | + await dbContext.SaveChangesAsync(); |
| 729 | + }); |
| 730 | + |
| 731 | + var requestBody = new |
| 732 | + { |
| 733 | + data = new[] |
| 734 | + { |
| 735 | + new |
| 736 | + { |
| 737 | + type = "workItems", |
| 738 | + id = existingWorkItem.StringId |
| 739 | + } |
| 740 | + } |
| 741 | + }; |
| 742 | + |
| 743 | + var route = $"/workItems/{existingWorkItem.StringId}/relationships/relatedTo"; |
| 744 | + |
| 745 | + // Act |
| 746 | + var (httpResponse, responseDocument) = await _testContext.ExecutePostAsync<string>(route, requestBody); |
| 747 | + |
| 748 | + // Assert |
| 749 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent); |
| 750 | + |
| 751 | + responseDocument.Should().BeEmpty(); |
| 752 | + |
| 753 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 754 | + { |
| 755 | + var workItemInDatabase = await dbContext.WorkItems |
| 756 | + .Include(workItem => workItem.RelatedToItems) |
| 757 | + .ThenInclude(workItemToWorkItem => workItemToWorkItem.ToItem) |
| 758 | + .FirstAsync(workItem => workItem.Id == existingWorkItem.Id); |
| 759 | + |
| 760 | + workItemInDatabase.RelatedToItems.Should().HaveCount(2); |
| 761 | + workItemInDatabase.RelatedToItems.Should().OnlyContain(workItemToWorkItem => workItemToWorkItem.FromItem.Id == existingWorkItem.Id); |
| 762 | + workItemInDatabase.RelatedToItems.Should().ContainSingle(workItemToWorkItem => workItemToWorkItem.ToItem.Id == existingWorkItem.Id); |
| 763 | + workItemInDatabase.RelatedToItems.Should().ContainSingle(workItemToWorkItem => workItemToWorkItem.ToItem.Id == existingWorkItem.RelatedToItems[0].ToItem.Id); |
| 764 | + }); |
| 765 | + } |
663 | 766 | }
|
664 | 767 | }
|
0 commit comments