Beispiel #1
0
        public async Task Cannot_remove_from_ManyToOne_relationship_for_unknown_relationship_ID()
        {
            // Arrange
            var existingDealership = new Dealership
            {
                Address = "Dam 1, 1012JS Amsterdam, the Netherlands"
            };

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                await dbContext.ClearTableAsync <Car>();
                dbContext.Dealerships.Add(existingDealership);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new[]
                {
                    new
                    {
                        type = "cars",
                        id   = "999:XX-YY-22"
                    }
                }
            };

            string route = $"/dealerships/{existingDealership.StringId}/relationships/inventory";

            // Act
            (HttpResponseMessage httpResponse, ErrorDocument responseDocument) = await _testContext.ExecuteDeleteAsync <ErrorDocument>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NotFound);

            responseDocument.Errors.Should().HaveCount(1);

            Error error = responseDocument.Errors[0];

            error.StatusCode.Should().Be(HttpStatusCode.NotFound);
            error.Title.Should().Be("A related resource does not exist.");
            error.Detail.Should().Be("Related resource of type 'cars' with ID '999:XX-YY-22' in relationship 'inventory' does not exist.");
        }
Beispiel #2
0
        public async Task Can_replace_OneToMany_relationship()
        {
            // Arrange
            var existingDealership = new Dealership
            {
                Address   = "Dam 1, 1012JS Amsterdam, the Netherlands",
                Inventory = new HashSet <Car>
                {
                    new Car
                    {
                        RegionId     = 123,
                        LicensePlate = "AA-BB-11"
                    },
                    new Car
                    {
                        RegionId     = 456,
                        LicensePlate = "CC-DD-22"
                    }
                }
            };

            var existingCar = new Car
            {
                RegionId     = 789,
                LicensePlate = "EE-FF-33"
            };

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                await dbContext.ClearTableAsync <Car>();
                dbContext.AddRange(existingDealership, existingCar);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new[]
                {
                    new
                    {
                        type = "cars",
                        id   = "123:AA-BB-11"
                    },
                    new
                    {
                        type = "cars",
                        id   = "789:EE-FF-33"
                    }
                }
            };

            string route = $"/dealerships/{existingDealership.StringId}/relationships/inventory";

            // Act
            (HttpResponseMessage httpResponse, string responseDocument) = await _testContext.ExecutePatchAsync <string>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                Dealership dealershipInDatabase = await dbContext.Dealerships
                                                  .Include(dealership => dealership.Inventory).FirstWithIdOrDefaultAsync(existingDealership.Id);

                dealershipInDatabase.Inventory.Should().HaveCount(2);
                dealershipInDatabase.Inventory.Should().ContainSingle(car => car.Id == existingCar.Id);
                dealershipInDatabase.Inventory.Should().ContainSingle(car => car.Id == existingDealership.Inventory.ElementAt(0).Id);
            });
        }