public async Task Can_Create_Article_With_Existing_Author() { // arrange var context = GetService <AppDbContext>(); 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 <OperationsDocument>("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); }
public async Task Can_Get_Article_Author() { // arrange var context = GetService <AppDbContext>(); var author = AuthorFactory.Get(); var article = ArticleFactory.Get(); article.Author = author; context.Articles.Add(article); context.SaveChanges(); var content = new { operations = new[] { new Dictionary <string, object> { { "op", "get" }, { "ref", new { type = "articles", id = article.StringId, relationship = nameof(article.Author) } } } } }; // act var(response, data) = await PatchAsync <OperationsDocument>("api/bulk", content); // assert Assert.NotNull(response); Assert.NotNull(data); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Single(data.Operations); var resourceObject = data.Operations.Single().DataObject; Assert.Equal(author.Id.ToString(), resourceObject.Id); Assert.Equal("authors", resourceObject.Type); }
public async Task Can_Create_Articles_With_Existing_Author() { // arrange var context = GetService <AppDbContext>(); 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 <object>() }; 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 <OperationsDocument>("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)); } }
public async Task Cannot_Create_Author_If_Article_Creation_Fails() { // arrange var context = GetService <AppDbContext>(); var author = AuthorFactory.Get(); var article = ArticleFactory.Get(); // do this so that the name is random enough for db validations author.Name = Guid.NewGuid().ToString("N"); article.Name = Guid.NewGuid().ToString("N"); var content = new { operations = new object[] { new { op = "add", data = new { type = "authors", attributes = new { name = author.Name }, } }, new { op = "add", data = new { type = "articles", attributes = new { name = article.Name }, // by not including the author, the article creation will fail // relationships = new { // author = new { // data = new { // type = "authors", // lid = authorLocalId // } // } // } } } } }; // act var(response, data) = await PatchAsync <ErrorCollection>("api/bulk", content); // assert Assert.NotNull(response); // for now, it is up to application implementations to perform validation and // provide the proper HTTP response code Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); Assert.Single(data.Errors); Assert.Contains("operation[1] (add)", data.Errors[0].Title); var dbAuthors = await context.Authors.Where(a => a.Name == author.Name).ToListAsync(); var dbArticles = await context.Articles.Where(a => a.Name == article.Name).ToListAsync(); Assert.Empty(dbAuthors); Assert.Empty(dbArticles); }