|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Bogus; |
| 7 | +using JsonApiDotNetCore.Internal; |
| 8 | +using JsonApiDotNetCore.Models.Operations; |
| 9 | +using JsonApiDotNetCoreExample.Data; |
| 10 | +using OperationsExampleTests.Factories; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace OperationsExampleTests |
| 14 | +{ |
| 15 | + public class GetTests : Fixture, IDisposable |
| 16 | + { |
| 17 | + private readonly Faker _faker = new Faker(); |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public async Task Can_Get_Author_By_Id() |
| 21 | + { |
| 22 | + // arrange |
| 23 | + var context = GetService<AppDbContext>(); |
| 24 | + var author = AuthorFactory.Get(); |
| 25 | + context.Authors.Add(author); |
| 26 | + context.SaveChanges(); |
| 27 | + |
| 28 | + var content = new |
| 29 | + { |
| 30 | + operations = new[] { |
| 31 | + new Dictionary<string, object> { |
| 32 | + { "op", "get"}, |
| 33 | + { "ref", new { type = "authors", id = author.StringId } } |
| 34 | + } |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + // act |
| 39 | + var result = await PatchAsync<OperationsDocument>("api/bulk", content); |
| 40 | + |
| 41 | + // assert |
| 42 | + Assert.NotNull(result.response); |
| 43 | + Assert.NotNull(result.data); |
| 44 | + Assert.Equal(HttpStatusCode.OK, result.response.StatusCode); |
| 45 | + Assert.Equal(1, result.data.Operations.Count); |
| 46 | + Assert.Equal(author.Id.ToString(), result.data.Operations.Single().DataObject.Id); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async Task Get_Author_By_Id_Returns_404_If_NotFound() |
| 51 | + { |
| 52 | + // arrange |
| 53 | + var authorId = _faker.Random.Int(max: 0).ToString(); |
| 54 | + |
| 55 | + var content = new |
| 56 | + { |
| 57 | + operations = new[] { |
| 58 | + new Dictionary<string, object> { |
| 59 | + { "op", "get"}, |
| 60 | + { "ref", new { type = "authors", id = authorId } } |
| 61 | + } |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + // act |
| 66 | + var (response, data) = await PatchAsync<ErrorCollection>("api/bulk", content); |
| 67 | + |
| 68 | + // assert |
| 69 | + Assert.NotNull(response); |
| 70 | + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); |
| 71 | + Assert.NotNull(data); |
| 72 | + Assert.Equal(1, data.Errors.Count); |
| 73 | + Assert.True(data.Errors[0].Detail.Contains("authors"), "The error detail should contain the name of the entity that could not be found."); |
| 74 | + Assert.True(data.Errors[0].Detail.Contains(authorId), "The error detail should contain the entity id that could not be found"); |
| 75 | + Assert.True(data.Errors[0].Title.Contains("operation[0]"), "The error title should contain the operation identifier that failed"); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments