diff --git a/test/OperationsExampleTests/Add/AddTests.cs b/test/OperationsExampleTests/Add/AddTests.cs index 47aba439d6..0deaaa6a82 100644 --- a/test/OperationsExampleTests/Add/AddTests.cs +++ b/test/OperationsExampleTests/Add/AddTests.cs @@ -1,127 +1,257 @@ -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Threading.Tasks; -using Bogus; -using JsonApiDotNetCore.Models.Operations; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Threading.Tasks; +using Bogus; +using JsonApiDotNetCore.Models.Operations; using JsonApiDotNetCoreExample.Data; -using Microsoft.EntityFrameworkCore; -using OperationsExampleTests.Factories; -using Xunit; - -namespace OperationsExampleTests -{ - public class AddTests : Fixture - { - private readonly Faker _faker = new Faker(); - - [Fact] - public async Task Can_Create_Author() - { - // arrange - var context = GetService(); - var author = AuthorFactory.Get(); - var content = new - { - operations = new[] { - new { - op = "add", - data = new { - type = "authors", - attributes = new { - name = author.Name - } - } - } - } - }; - - // act - var (response, data) = await PatchAsync("api/bulk", content); - - // assert - Assert.NotNull(response); - Assert.Equal(HttpStatusCode.OK, response.StatusCode); - - var id = data.Operations.Single().DataObject.Id; - var lastAuthor = await context.Authors.SingleAsync(a => a.StringId == id); - Assert.Equal(author.Name, lastAuthor.Name); - } - - [Fact] - public async Task Can_Create_Authors() - { - // arrange - var expectedCount = _faker.Random.Int(1, 10); - var context = GetService(); - var authors = AuthorFactory.Get(expectedCount); - var content = new - { - operations = new List() - }; - - for (int i = 0; i < expectedCount; i++) - { - content.operations.Add( - new - { - op = "add", - data = new - { - type = "authors", - attributes = new - { - name = authors[i].Name - } - } - } - ); - } - - // act - var (response, data) = await PatchAsync("api/bulk", content); - - // assert - Assert.NotNull(response); - Assert.Equal(HttpStatusCode.OK, response.StatusCode); - Assert.Equal(expectedCount, data.Operations.Count); - - for (int i = 0; i < expectedCount; i++) - { - var dataObject = data.Operations[i].DataObject; - var author = context.Authors.Single(a => a.StringId == dataObject.Id); - Assert.Equal(authors[i].Name, author.Name); - } +using Microsoft.EntityFrameworkCore; +using OperationsExampleTests.Factories; +using Xunit; + +namespace OperationsExampleTests +{ + public class AddTests : Fixture + { + private readonly Faker _faker = new Faker(); + + [Fact] + public async Task Can_Create_Author() + { + // arrange + var context = GetService(); + var author = AuthorFactory.Get(); + var content = new + { + operations = new[] { + new { + op = "add", + data = new { + type = "authors", + attributes = new { + name = author.Name + } + } + } + } + }; + + // act + var (response, data) = await PatchAsync("api/bulk", content); + + // assert + Assert.NotNull(response); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + var id = data.Operations.Single().DataObject.Id; + var lastAuthor = await context.Authors.SingleAsync(a => a.StringId == id); + Assert.Equal(author.Name, lastAuthor.Name); + } + + [Fact] + public async Task Can_Create_Authors() + { + // arrange + var expectedCount = _faker.Random.Int(1, 10); + var context = GetService(); + var authors = AuthorFactory.Get(expectedCount); + var content = new + { + operations = new List() + }; + + for (int i = 0; i < expectedCount; i++) + { + content.operations.Add( + new + { + op = "add", + data = new + { + type = "authors", + attributes = new + { + name = authors[i].Name + } + } + } + ); + } + + // act + var (response, data) = await PatchAsync("api/bulk", content); + + // assert + Assert.NotNull(response); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedCount, data.Operations.Count); + + for (int i = 0; i < expectedCount; i++) + { + var dataObject = data.Operations[i].DataObject; + var author = context.Authors.Single(a => a.StringId == dataObject.Id); + Assert.Equal(authors[i].Name, author.Name); + } + } + + [Fact] + public async Task Can_Create_Article_With_Existing_Author() + { + // arrange + var context = GetService(); + var author = AuthorFactory.Get(); + var article = ArticleFactory.Get(); + + context.Authors.Add(author); + await context.SaveChangesAsync(); + + + //const string authorLocalId = "author-1"; + + var content = new + { + operations = new object[] { + new { + op = "add", + data = new { + type = "articles", + attributes = new { + name = article.Name + }, + relationships = new { + author = new { + data = new { + type = "authors", + id = author.Id + } + } + } + } + } + } + }; + + // act + var (response, data) = await PatchAsync("api/bulk", content); + + // assert + Assert.NotNull(response); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Single(data.Operations); + + + var lastAuthor = await context.Authors + .Include(a => a.Articles) + .SingleAsync(a => a.Id == author.Id); + var articleOperationResult = data.Operations[0]; + + // author validation: sanity checks + Assert.NotNull(lastAuthor); + Assert.Equal(author.Name, lastAuthor.Name); + + //// article validation + Assert.Single(lastAuthor.Articles); + Assert.Equal(article.Name, lastAuthor.Articles[0].Name); + Assert.Equal(articleOperationResult.DataObject.Id, lastAuthor.Articles[0].StringId); } - [Fact] - public async Task Can_Create_Author_With_Article() - { - // arrange - var context = GetService(); + [Fact] + public async Task Can_Create_Articles_With_Existing_Author() + { + + + // arrange + var context = GetService(); + var author = AuthorFactory.Get(); + context.Authors.Add(author); + await context.SaveChangesAsync(); + var expectedCount = _faker.Random.Int(1, 10); + var articles = ArticleFactory.Get(expectedCount); + + var content = new + { + operations = new List() + }; + + for (int i = 0; i < expectedCount; i++) + { + content.operations.Add( + new + { + op = "add", + data = new + { + type = "articles", + attributes = new + { + name = articles[i].Name + }, + relationships = new + { + author = new + { + data = new + { + type = "authors", + id = author.Id + } + } + } + } + } + ); + } + + // act + var (response, data) = await PatchAsync("api/bulk", content); + + // assert + Assert.NotNull(response); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedCount, data.Operations.Count); + + // author validation: sanity checks + var lastAuthor = context.Authors.Include(a => a.Articles).Single(a => a.Id == author.Id); + Assert.NotNull(lastAuthor); + Assert.Equal(author.Name, lastAuthor.Name); + + // articles validation + Assert.True(lastAuthor.Articles.Count == expectedCount); + for (int i = 0; i < expectedCount; i++) + { + var article = articles[i]; + Assert.NotNull(lastAuthor.Articles.FirstOrDefault(a => a.Name == article.Name)); + } + } + + [Fact] + public async Task Can_Create_Author_With_Article_Using_LocalId() + { + // arrange + var context = GetService(); var author = AuthorFactory.Get(); var article = ArticleFactory.Get(); const string authorLocalId = "author-1"; - var content = new - { - operations = new object[] { - new { - op = "add", + var content = new + { + operations = new object[] { + new { + op = "add", data = new { - lid = authorLocalId, - type = "authors", - attributes = new { - name = author.Name - }, - } + lid = authorLocalId, + type = "authors", + attributes = new { + name = author.Name + }, + } }, - new { - op = "add", - data = new { - type = "articles", - attributes = new { - name = article.Name + new { + op = "add", + data = new { + type = "articles", + attributes = new { + name = article.Name }, relationships = new { author = new { @@ -130,18 +260,18 @@ public async Task Can_Create_Author_With_Article() lid = authorLocalId } } - } - } - } - } - }; - - // act - var (response, data) = await PatchAsync("api/bulk", content); - - // assert - Assert.NotNull(response); - Assert.Equal(HttpStatusCode.OK, response.StatusCode); + } + } + } + } + }; + + // act + var (response, data) = await PatchAsync("api/bulk", content); + + // assert + Assert.NotNull(response); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(2, data.Operations.Count); var authorOperationResult = data.Operations[0]; @@ -151,14 +281,14 @@ public async Task Can_Create_Author_With_Article() .SingleAsync(a => a.StringId == id); var articleOperationResult = data.Operations[1]; - // author validation - Assert.Equal(authorLocalId, authorOperationResult.DataObject.LocalId); + // author validation + Assert.Equal(authorLocalId, authorOperationResult.DataObject.LocalId); Assert.Equal(author.Name, lastAuthor.Name); // article validation Assert.Single(lastAuthor.Articles); Assert.Equal(article.Name, lastAuthor.Articles[0].Name); - Assert.Equal(articleOperationResult.DataObject.Id, lastAuthor.Articles[0].StringId); - } - } -} + Assert.Equal(articleOperationResult.DataObject.Id, lastAuthor.Articles[0].StringId); + } + } +}