public async Task UpdateAsync_ShouldReturnCollectionWithOnlyIncludedPropertiesUpdated_WhenEntitiesMatchAndIncludedPropertyExpresionsArePassed(DbProvider provider, TestConfiguration testConfiguration = TestConfiguration.Default) { TestEntityCompositeKey[] existingEntities = new[] { new TestEntityCompositeKey { IdPartA = "Should not be updated 1", IdPartB = "B", IntTestValue = 561645, BoolTestValue = false, DateTimeTestValue = DateTime.UtcNow, LongTestValue = 54123 }, new TestEntityCompositeKey { IdPartA = "Should not be updated 2", IdPartB = "B", IntTestValue = 56123, BoolTestValue = true, DateTimeTestValue = DateTime.UtcNow, LongTestValue = 1231 }, new TestEntityCompositeKey { IdPartA = "Should be updated 3", IdPartB = "B", IntTestValue = 111, BoolTestValue = true, DateTimeTestValue = DateTime.UtcNow, LongTestValue = 65465132165 }, }; TestEntityCompositeKey[] expectedEntities = new[] { new TestEntityCompositeKey { IdPartA = "Should not be updated 1", IdPartB = "B", IntTestValue = -1, BoolTestValue = true, DateTimeTestValue = DateTime.UtcNow.AddDays(1), LongTestValue = 781 }, new TestEntityCompositeKey { IdPartA = "Should be updated 3", IdPartB = "B", IntTestValue = 561235164, BoolTestValue = false, DateTimeTestValue = DateTime.UtcNow.AddDays(1), LongTestValue = 165465132165 }, }; using TestDbContext context = await ContextFactory.GetDbContextAsync(provider, seedData : existingEntities, testConfiguration : testConfiguration); // Include long and datetime values - they are the only items expected to be updated based on the mocked data. InclusionBuilder <TestEntityCompositeKey> inclusionBuilder = new InclusionBuilder <TestEntityCompositeKey>() .Include(x => x.LongTestValue) .Include(nameof(TestEntityCompositeKey.DateTimeTestValue)); // Invoke the method and check that the result the updated expected entities IReadOnlyCollection <TestEntityCompositeKey> result = await context.UpdateAsync( expectedEntities, condition : x => x.Incoming.IntTestValue > x.Current.IntTestValue, // Only update if IntTestValue is greater than the incoming value, which rules out "Should not be updated 1" clusivityBuilder : inclusionBuilder); var expectedUpdatedEntity = new TestEntityCompositeKey { IdPartA = expectedEntities[1].IdPartA, IdPartB = expectedEntities[1].IdPartB, IntTestValue = existingEntities[2].IntTestValue, // We did not include this field in the update => it should have its original value BoolTestValue = existingEntities[2].BoolTestValue, // We did not include this field in the update => it should have its original value DateTimeTestValue = expectedEntities[1].DateTimeTestValue, LongTestValue = expectedEntities[1].LongTestValue, }; result.Should().BeEquivalentTo(new[] { expectedUpdatedEntity }); // Validate that the DB is updated context.TestEntitiesWithCompositeKey.Should().BeEquivalentTo(new[] { existingEntities[0], existingEntities[1], expectedUpdatedEntity }); }
public async Task UpdateAsync_ShouldReturnCollectionWithOnlyIncludedPropertiesUpdated_WhenEntitiesMatchAndIncludedPropertyNamesArePassed(DbProvider provider) { var existingEntities = new[] { new TestEntityCompositeKey { IdPartA = "Should not be updated 1", IdPartB = "B", IntTestValue = 561645, BoolTestValue = false, DateTimeTestValue = DateTime.UtcNow, LongTestValue = 54123 }, new TestEntityCompositeKey { IdPartA = "Should not be updated 2", IdPartB = "B", IntTestValue = 56123, BoolTestValue = true, DateTimeTestValue = DateTime.UtcNow, LongTestValue = 1231 }, new TestEntityCompositeKey { IdPartA = "Should be updated 3", IdPartB = "B", IntTestValue = 111, BoolTestValue = true, DateTimeTestValue = DateTime.UtcNow, LongTestValue = 65465132165 }, }; var expectedEntities = new[] { new TestEntityCompositeKey { IdPartA = "Should not be updated 1", IdPartB = "B", IntTestValue = -1, BoolTestValue = true, DateTimeTestValue = DateTime.UtcNow.AddDays(1), LongTestValue = 781 }, new TestEntityCompositeKey { IdPartA = "Should be updated 3", IdPartB = "B", IntTestValue = 561235164, BoolTestValue = false, DateTimeTestValue = DateTime.UtcNow.AddDays(1), LongTestValue = 165465132165 }, }; using TestDbContext context = await ContextFactory.GetDbContextAsync(provider, seedData : existingEntities); // Invoke the method and check that the result the updated expected entities var result = await context.UpdateAsync( expectedEntities, condition : x => x.Incoming.IntTestValue > x.Current.IntTestValue, // Only update if IntTestValue is greater than the incoming value, which rules out "Should not be updated 1" includedProperties : new[] { nameof(TestEntityCompositeKey.LongTestValue), nameof(TestEntityCompositeKey.DateTimeTestValue) }); var expectedUpdatedEntity = new TestEntityCompositeKey { IdPartA = expectedEntities[1].IdPartA, IdPartB = expectedEntities[1].IdPartB, IntTestValue = existingEntities[2].IntTestValue, // We did not include this field in the update => it should have its original value BoolTestValue = existingEntities[2].BoolTestValue, // We did not include this field in the update => it should have its original value DateTimeTestValue = expectedEntities[1].DateTimeTestValue, LongTestValue = expectedEntities[1].LongTestValue, }; result.Should().BeEquivalentTo(new[] { expectedUpdatedEntity }); // Validate that the DB is updated context.TestEntitiesWithCompositeKey.Should().BeEquivalentTo(new[] { existingEntities[0], existingEntities[1], expectedUpdatedEntity }); }