From 2be7f612ee703f49ef10b66bc73316d96c7dfa1c Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Sun, 22 Jul 2018 23:09:06 -0700 Subject: [PATCH] test(acceptance): Can_Create_And_Set_HasOne_Relationships_From_Independent_Side --- .../Controllers/PersonRolesController.cs | 17 +++++++ .../Data/AppDbContext.cs | 1 + .../JsonApiDotNetCoreExample/Models/Person.cs | 15 +++++- .../Acceptance/Spec/CreatingDataTests.cs | 51 +++++++++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs new file mode 100644 index 0000000000..dbc3b482f5 --- /dev/null +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs @@ -0,0 +1,17 @@ +using JsonApiDotNetCore.Controllers; +using JsonApiDotNetCore.Services; +using JsonApiDotNetCoreExample.Models; +using Microsoft.Extensions.Logging; + +namespace JsonApiDotNetCoreExample.Controllers +{ + public class PersonRolesController : JsonApiController + { + public PersonRolesController( + IJsonApiContext jsonApiContext, + IResourceService resourceService, + ILoggerFactory loggerFactory) + : base(jsonApiContext, resourceService, loggerFactory) + { } + } +} diff --git a/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs b/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs index 6f50f9aa5b..0ccf59d162 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs @@ -39,5 +39,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) public DbSet Authors { get; set; } public DbSet NonJsonApiResources { get; set; } public DbSet Users { get; set; } + public DbSet PersonRoles { get; set; } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Models/Person.cs b/src/Examples/JsonApiDotNetCoreExample/Models/Person.cs index d140b23b6b..0a72ce2bd1 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Models/Person.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Models/Person.cs @@ -1,9 +1,16 @@ +using System; using System.Collections.Generic; using JsonApiDotNetCore.Models; using JsonApiDotNetCore.Services; namespace JsonApiDotNetCoreExample.Models { + public class PersonRole : Identifiable + { + [HasOne("person")] + public Person Person { get; set; } + } + public class Person : Identifiable, IHasMeta { [Attr("first-name")] @@ -17,10 +24,14 @@ public class Person : Identifiable, IHasMeta [HasMany("assigned-todo-items")] public virtual List AssignedTodoItems { get; set; } - + [HasMany("todo-collections")] public virtual List TodoItemCollections { get; set; } - + + [HasOne("role")] + public virtual PersonRole Role { get; set; } + public int? PersonRoleId { get; set; } + [HasOne("unincludeable-item", Link.All, canInclude: false)] public virtual TodoItem UnIncludeableItem { get; set; } diff --git a/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/CreatingDataTests.cs b/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/CreatingDataTests.cs index 015d79cdbd..e65278db12 100644 --- a/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/CreatingDataTests.cs +++ b/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/CreatingDataTests.cs @@ -342,6 +342,57 @@ public async Task Can_Create_And_Set_HasOne_Relationships() Assert.Equal(owner.Id, todoItemResult.OwnerId); } + [Fact] + public async Task Can_Create_And_Set_HasOne_Relationships_From_Independent_Side() + { + // arrange + var builder = new WebHostBuilder() + .UseStartup(); + var httpMethod = new HttpMethod("POST"); + var server = new TestServer(builder); + var client = server.CreateClient(); + + var context = _fixture.GetService(); + + var person = new JsonApiDotNetCoreExample.Models.Person(); + context.People.Add(person); + await context.SaveChangesAsync(); + + var route = "/api/v1/person-roles"; + var request = new HttpRequestMessage(httpMethod, route); + var clientDefinedId = Guid.NewGuid(); + var content = new + { + data = new + { + type = "person-roles", + relationships = new + { + person = new + { + data = new + { + type = "people", + id = person.Id.ToString() + } + } + } + } + }; + + request.Content = new StringContent(JsonConvert.SerializeObject(content)); + request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json"); + + // act + var response = await client.SendAsync(request); + var body = await response.Content.ReadAsStringAsync(); + + // assert + Assert.Equal(HttpStatusCode.Created, response.StatusCode); + var deserializedBody = (PersonRole)_fixture.GetService().Deserialize(body); + Assert.Equal(person.Id, deserializedBody.Person.Id); + } + [Fact] public async Task ShouldReceiveLocationHeader_InResponse() {