Skip to content

test(acceptance): Can_Create_And_Set_HasOne_Relationships_From_Indepe… #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExample.Controllers
{
public class PersonRolesController : JsonApiController<PersonRole>
{
public PersonRolesController(
IJsonApiContext jsonApiContext,
IResourceService<PersonRole> resourceService,
ILoggerFactory loggerFactory)
: base(jsonApiContext, resourceService, loggerFactory)
{ }
}
}
1 change: 1 addition & 0 deletions src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
public DbSet<Author> Authors { get; set; }
public DbSet<NonJsonApiResource> NonJsonApiResources { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<PersonRole> PersonRoles { get; set; }
}
}
15 changes: 13 additions & 2 deletions src/Examples/JsonApiDotNetCoreExample/Models/Person.cs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -17,10 +24,14 @@ public class Person : Identifiable, IHasMeta

[HasMany("assigned-todo-items")]
public virtual List<TodoItem> AssignedTodoItems { get; set; }

[HasMany("todo-collections")]
public virtual List<TodoItemCollection> 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; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClientGeneratedIdsStartup>();
var httpMethod = new HttpMethod("POST");
var server = new TestServer(builder);
var client = server.CreateClient();

var context = _fixture.GetService<AppDbContext>();

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<IJsonApiDeSerializer>().Deserialize(body);
Assert.Equal(person.Id, deserializedBody.Person.Id);
}

[Fact]
public async Task ShouldReceiveLocationHeader_InResponse()
{
Expand Down