Skip to content

Commit b05bb8f

Browse files
test: accepts meta in relationship of atomic UPDATE resource request
1 parent b083bd2 commit b05bb8f

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

test/JsonApiDotNetCoreTests/IntegrationTests/Meta/RequestMetaTests.cs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,80 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
506506
ValidateMetaData(relationship.Meta);
507507
}
508508

509+
[Fact]
510+
public async Task Accepts_meta_in_relationship_of_atomic_add_resource_operation()
511+
{
512+
// Arrange
513+
var store = _testContext.Factory.Services.GetRequiredService<RequestDocumentStore>();
514+
515+
SupportTicket existingTicket = _fakers.SupportTicket.GenerateOne();
516+
ProductFamily existingProductFamily = _fakers.ProductFamily.GenerateOne();
517+
518+
await _testContext.RunOnDatabaseAsync(async dbContext =>
519+
{
520+
dbContext.ProductFamilies.Add(existingProductFamily);
521+
await dbContext.SaveChangesAsync();
522+
});
523+
524+
var requestBody = new
525+
{
526+
atomic__operations = new[]
527+
{
528+
new
529+
{
530+
op = "add",
531+
data = new
532+
{
533+
type = "supportTickets",
534+
attributes = new
535+
{
536+
description = existingTicket.Description
537+
},
538+
relationships = new
539+
{
540+
productFamily = new
541+
{
542+
data = new
543+
{
544+
type = "productFamilies",
545+
id = existingProductFamily.StringId
546+
},
547+
meta = GetExampleMetaData()
548+
}
549+
}
550+
}
551+
}
552+
}
553+
};
554+
555+
string route = "/operations";
556+
557+
// Act
558+
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecutePostAtomicAsync<Document>(route, requestBody);
559+
560+
// Assert
561+
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
562+
563+
store.Document.Should().NotBeNull();
564+
store.Document.Operations.Should().NotBeNull();
565+
store.Document.Operations.Should().HaveCount(1);
566+
567+
var operation = store.Document.Operations[0];
568+
operation.Should().NotBeNull();
569+
operation.Data.Should().NotBeNull();
570+
operation.Data.SingleValue.Should().NotBeNull();
571+
572+
var relationships = operation.Data.SingleValue.Relationships;
573+
relationships.Should().NotBeNull();
574+
relationships.Should().ContainKey("productFamily");
575+
576+
var relationship = relationships["productFamily"];
577+
relationship.Should().NotBeNull();
578+
relationship.Meta.Should().NotBeNull();
579+
580+
ValidateMetaData(relationship.Meta);
581+
}
582+
509583
[Fact]
510584
public async Task Accepts_meta_in_data_of_atomic_add_resource_operation()
511585
{
@@ -555,6 +629,74 @@ public async Task Accepts_meta_in_data_of_atomic_add_resource_operation()
555629
ValidateMetaData(operation.Data.SingleValue.Meta);
556630
}
557631

632+
[Fact]
633+
public async Task Accepts_meta_in_relationship_of_atomic_update_resource_operation()
634+
{
635+
// Arrange
636+
var store = _testContext.Factory.Services.GetRequiredService<RequestDocumentStore>();
637+
638+
SupportTicket existingTicket = _fakers.SupportTicket.GenerateOne();
639+
ProductFamily existingProductFamily = _fakers.ProductFamily.GenerateOne();
640+
641+
await _testContext.RunOnDatabaseAsync(async dbContext =>
642+
{
643+
existingTicket.ProductFamily = existingProductFamily;
644+
dbContext.SupportTickets.Add(existingTicket);
645+
await dbContext.SaveChangesAsync();
646+
});
647+
648+
var requestBody = new
649+
{
650+
atomic__operations = new[]
651+
{
652+
new
653+
{
654+
op = "update",
655+
data = new
656+
{
657+
type = "supportTickets",
658+
id = existingTicket.StringId,
659+
relationships = new
660+
{
661+
productFamily = new
662+
{
663+
data = (object?)null
664+
}
665+
}
666+
},
667+
meta = GetExampleMetaData()
668+
}
669+
}
670+
};
671+
672+
string route = "/operations";
673+
674+
// Act
675+
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecutePostAtomicAsync<Document>(route, requestBody);
676+
677+
// Assert
678+
httpResponse.ShouldHaveStatusCode(HttpStatusCode.NoContent);
679+
680+
store.Document.Should().NotBeNull();
681+
store.Document.Operations.Should().NotBeNull();
682+
store.Document.Operations.Should().HaveCount(1);
683+
684+
var operation = store.Document.Operations[0];
685+
operation.Should().NotBeNull();
686+
operation.Meta.Should().NotBeNull();
687+
688+
ValidateMetaData(operation.Meta);
689+
690+
await _testContext.RunOnDatabaseAsync(async dbContext =>
691+
{
692+
var ticketInDatabase = await dbContext.SupportTickets
693+
.Include(supportTicket => supportTicket.ProductFamily)
694+
.FirstAsync(supportTicket => supportTicket.Id == existingTicket.Id);
695+
696+
ticketInDatabase.ProductFamily.Should().BeNull();
697+
});
698+
}
699+
558700
private static Object GetExampleMetaData()
559701
{
560702
return new

0 commit comments

Comments
 (0)