public async Task Can_patch_resource_with_to_one_relationship_through_relationship_link() { // Arrange var man = new Man(); var insurance = new CompanyHealthInsurance(); await _testContext.RunOnDatabaseAsync(async dbContext => { await dbContext.ClearTablesAsync <Man, CompanyHealthInsurance>(); dbContext.AddRange(man, insurance); await dbContext.SaveChangesAsync(); }); var route = $"/men/{man.Id}/relationships/healthInsurance"; var requestBody = new { data = new { type = "companyHealthInsurances", id = insurance.StringId } }; // Act var(httpResponse, _) = await _testContext.ExecutePatchAsync <Document>(route, requestBody); // Assert httpResponse.Should().HaveStatusCode(HttpStatusCode.OK); await _testContext.RunOnDatabaseAsync(async dbContext => { var assertMan = await dbContext.Men .Include(m => m.HealthInsurance) .SingleAsync(h => h.Id == man.Id); assertMan.HealthInsurance.Should().BeOfType <CompanyHealthInsurance>(); }); }
public async Task Can_create_resource_with_ToOne_relationship() { // Arrange var existingInsurance = new CompanyHealthInsurance(); await _testContext.RunOnDatabaseAsync(async dbContext => { await dbContext.ClearTableAsync <CompanyHealthInsurance>(); dbContext.CompanyHealthInsurances.Add(existingInsurance); await dbContext.SaveChangesAsync(); }); var requestBody = new { data = new { type = "men", relationships = new { healthInsurance = new { data = new { type = "companyHealthInsurances", id = existingInsurance.StringId } } } } }; var route = "/men"; // Act var(httpResponse, responseDocument) = await _testContext.ExecutePostAsync <Document>(route, requestBody); // Assert httpResponse.Should().HaveStatusCode(HttpStatusCode.Created); responseDocument.SingleData.Should().NotBeNull(); var newManId = int.Parse(responseDocument.SingleData.Id); await _testContext.RunOnDatabaseAsync(async dbContext => { var manInDatabase = await dbContext.Men .Include(man => man.HealthInsurance) .FirstAsync(man => man.Id == newManId); manInDatabase.HealthInsurance.Should().BeOfType <CompanyHealthInsurance>(); manInDatabase.HealthInsurance.Id.Should().Be(existingInsurance.Id); }); }
public async Task Can_create_resource_with_to_one_relationship() { // Arrange var insurance = new CompanyHealthInsurance(); await _testContext.RunOnDatabaseAsync(async dbContext => { await dbContext.ClearTableAsync <CompanyHealthInsurance>(); dbContext.CompanyHealthInsurances.Add(insurance); await dbContext.SaveChangesAsync(); }); var route = "/men"; var requestBody = new { data = new { type = "men", relationships = new Dictionary <string, object> { { "healthInsurance", new { data = new { type = "companyHealthInsurances", id = insurance.StringId } } } } } }; // Act var(httpResponse, responseDocument) = await _testContext.ExecutePostAsync <Document>(route, requestBody); // Assert httpResponse.Should().HaveStatusCode(HttpStatusCode.Created); await _testContext.RunOnDatabaseAsync(async dbContext => { var assertMan = await dbContext.Men .Include(m => m.HealthInsurance) .SingleAsync(m => m.Id == int.Parse(responseDocument.SingleData.Id)); assertMan.HealthInsurance.Should().BeOfType <CompanyHealthInsurance>(); }); }
public async Task Can_update_resource_with_ToOne_relationship_through_relationship_endpoint() { // Arrange var existingMan = new Man(); var existingInsurance = new CompanyHealthInsurance(); await _testContext.RunOnDatabaseAsync(async dbContext => { await dbContext.ClearTablesAsync <Man, CompanyHealthInsurance>(); dbContext.AddRange(existingMan, existingInsurance); await dbContext.SaveChangesAsync(); }); var requestBody = new { data = new { type = "companyHealthInsurances", id = existingInsurance.StringId } }; var route = $"/men/{existingMan.StringId}/relationships/healthInsurance"; // Act var(httpResponse, responseDocument) = await _testContext.ExecutePatchAsync <string>(route, requestBody); // Assert httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent); responseDocument.Should().BeEmpty(); await _testContext.RunOnDatabaseAsync(async dbContext => { var manInDatabase = await dbContext.Men .Include(man => man.HealthInsurance) .FirstAsync(man => man.Id == existingMan.Id); manInDatabase.HealthInsurance.Should().BeOfType <CompanyHealthInsurance>(); manInDatabase.HealthInsurance.Id.Should().Be(existingInsurance.Id); }); }