Skip to content

test(spec): can create hasOne relationship with resource #296

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
Jun 10, 2018
Merged
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
Expand Up @@ -106,7 +106,7 @@ public async Task Cannot_Create_Entity_With_Client_Generate_Id()
attributes = new
{
description = todoItem.Description,
ordinal = todoItem.Ordinal,
ordinal = todoItem.Ordinal,
createdDate = DateTime.Now
}
}
Expand Down Expand Up @@ -174,7 +174,7 @@ public async Task Can_Create_Guid_Identifiable_Entity_With_Client_Defined_Id_If_
var httpMethod = new HttpMethod("POST");
var server = new TestServer(builder);
var client = server.CreateClient();

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

var owner = new JsonApiDotNetCoreExample.Models.Person();
Expand Down Expand Up @@ -285,6 +285,63 @@ public async Task Can_Create_And_Set_HasMany_Relationships()
Assert.NotEmpty(contextCollection.TodoItems);
}

[Fact]
public async Task Can_Create_And_Set_HasOne_Relationships()
{
// arrange
var builder = new WebHostBuilder()
.UseStartup<Startup>();
var httpMethod = new HttpMethod("POST");
var server = new TestServer(builder);
var client = server.CreateClient();

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

var todoItem = new TodoItem();
var owner = new JsonApiDotNetCoreExample.Models.Person();
context.People.Add(owner);
await context.SaveChangesAsync();

var route = "/api/v1/todo-items";
var request = new HttpRequestMessage(httpMethod, route);
var content = new
{
data = new
{
type = "todo-items",
relationships = new Dictionary<string, dynamic>
{
{ "owner", new {
data = new
{
type = "people",
id = owner.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 = (TodoItem)_fixture.GetService<IJsonApiDeSerializer>().Deserialize(body);
var newId = deserializedBody.Id;

context = _fixture.GetService<AppDbContext>();
var todoItemResult = context.TodoItems
.Include(c => c.Owner)
.SingleOrDefault(c => c.Id == newId);

Assert.Equal(owner.Id, todoItemResult.OwnerId);
}

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