public async Task when_remove_item_from_owned_collection__item_is_deleted() { using (TestDbContext context = new TestDbContext()) { IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(context); // GIVEN an enity root with an owned list: context.Add(new Entity { OwnedList = new[] { new OwnedListItem { Id = 1, Name = "Owned Item A" }, new OwnedListItem { Id = 2, Name = "Owned Item B" } } }); context.SaveChanges(); // WHEN the collection is modified: Entity detachedEntity = new Entity { Id = 1, OwnedList = new[] { new OwnedListItem { Id = 2, Name = "Owned Item B" }, } }; await detachedContext.Set<Entity>().UpdateAsync(detachedEntity); await detachedContext.SaveChangesAsync(); // THEN the items is added to the the database. Entity persistedEntity = await detachedContext.Set<Entity>().LoadAsync(1); Assert.False(persistedEntity.OwnedList.Any(s => s.Name == "Owned Item A")); Assert.True(persistedEntity.OwnedList.Any(s => s.Name == "Owned Item B")); } }
public async Task when_entity_is_saved__many_to_many_is_saved() { using (ManyToManyContext dbContext = new ManyToManyContext()) { IDetachedContext <ManyToManyContext> detachedContext = new DetachedContext <ManyToManyContext>(dbContext); dbContext.AddRange(new[] { new Role { Name = "Role 1" }, new Role { Name = "Role 2" } }); await dbContext.SaveChangesAsync(); await detachedContext.Set <User>().UpdateAsync(new User { Name = "Test", Roles = new[] { new Role { Id = 1 } } }); await detachedContext.SaveChangesAsync(); User persisted = await detachedContext.Set <User>().LoadAsync(1); Assert.Equal(1, persisted.Roles.Count); Assert.Equal(1, persisted.UserRoles.Count); Assert.True(persisted.Roles.Any(r => r.Name == "Role 1")); } }
public async Task when_an_entity_with_audit_is_modified__the_modified_audit_fields_are_persisted() { // GIVEN a persisted entity: using (IDetachedContext <AuditingContext> detachedContext = new DetachedContext <AuditingContext>()) { await detachedContext.Set <EntityForAuditing>().UpdateAsync(new EntityForAuditing { Name = "Test" }); await detachedContext.SaveChangesAsync(); EntityForAuditing persisted = await detachedContext.Set <EntityForAuditing>().LoadAsync(1); // WHEN the entity is modified SessionInfoProvider.Default.CurrentUser = "******"; await detachedContext.Set <EntityForAuditing>().UpdateAsync(new EntityForAuditing { Id = 1, Name = "Test Modified" }); await detachedContext.SaveChangesAsync(); EntityForAuditing persisted2 = await detachedContext.Set <EntityForAuditing>().LoadAsync(1); // THEN 'created' audit fields are not modified. Assert.Equal(persisted.CreatedDate, persisted2.CreatedDate); Assert.Equal(persisted.CreatedBy, persisted2.CreatedBy); // and 'modified' audit fields are set. Assert.Equal("Test User 2", persisted2.ModifiedBy); Assert.Equal(DateTime.Now.Date, persisted2.ModifiedDate.Date); } }
public async Task when_entity_is_edited__many_to_many_is_merged() { using (ManyToManyContext dbContext = new ManyToManyContext()) { // GIVEN some entities of type B: dbContext.AddRange(new[] { new Role { Name = "EndB 1" }, new Role { Name = "EndB 2" }, new Role { Name = "EndB 3" } }); await dbContext.SaveChangesAsync(); // AND a root A containing two associations of B. IDetachedContext <ManyToManyContext> detached = new DetachedContext <ManyToManyContext>(dbContext); await detached.Set <User>().UpdateAsync(new User { Name = "Test Root", Roles = new[] { new Role { Id = 1, Name = "Role 1" }, new Role { Id = 2, Name = "Role 2" } } }); await detached.SaveChangesAsync(); // WHEN a root with only one association of B is saved: await detached.Set <User>().UpdateAsync(new User { Id = 1, Name = "Test Root", Roles = new[] { new Role { Id = 3, Name = "Role 3" }, } }); await detached.SaveChangesAsync(); // THEN the association to the second item is removed: User persisted = await detached.Set <User>().LoadAsync(1); } }
public async Task when_2_entities_same_type_persisted_values_are_updated() { TwoReferencesSameTypeEntity entity = new TwoReferencesSameTypeEntity(); entity.ReferenceA = new TwoReferencesSameTypeReference { Name = "Reference A", Items = new[] { new TwoReferencesSameTypeItem { Name = "Reference A Item 1" }, new TwoReferencesSameTypeItem { Name = "Reference A Item 2" }, new TwoReferencesSameTypeItem { Name = "Reference A Item 3" } } }; entity.ReferenceB = new TwoReferencesSameTypeReference { Name = "Reference B", Items = new[] { new TwoReferencesSameTypeItem { Name = "Reference B Item 1" }, new TwoReferencesSameTypeItem { Name = "Reference B Item 2" }, new TwoReferencesSameTypeItem { Name = "Reference B Item 3" } } }; entity.ReferenceC = new TwoReferencesSameTypeReference { Name = "Reference C", Items = new[] { new TwoReferencesSameTypeItem { Name = "Reference C Item 1" }, new TwoReferencesSameTypeItem { Name = "Reference C Item 2" }, new TwoReferencesSameTypeItem { Name = "Reference C Item 3" } } }; using (IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>()) { await detachedContext.Set<TwoReferencesSameTypeEntity>().UpdateAsync(entity); await detachedContext.SaveChangesAsync(); TwoReferencesSameTypeEntity entity2 = await detachedContext.Set<TwoReferencesSameTypeEntity>().LoadAsync(1); Assert.Equal("Reference A Item 1", entity2.ReferenceA.Items[0].Name); Assert.Equal("Reference A Item 2", entity2.ReferenceA.Items[1].Name); Assert.Equal("Reference A Item 3", entity2.ReferenceA.Items[2].Name); Assert.Equal("Reference B Item 1", entity2.ReferenceB.Items[0].Name); Assert.Equal("Reference B Item 2", entity2.ReferenceB.Items[1].Name); Assert.Equal("Reference B Item 3", entity2.ReferenceB.Items[2].Name); Assert.Equal("Reference C Item 1", entity2.ReferenceC.Items[0].Name); Assert.Equal("Reference C Item 2", entity2.ReferenceC.Items[1].Name); Assert.Equal("Reference C Item 3", entity2.ReferenceC.Items[2].Name); } }
public async Task when_associated_reference_set_to_null__entity_is_preserved() { using (TestDbContext context = new TestDbContext()) { IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(context); // GIVEN an enity root with references: AssociatedReference[] references = new[] { new AssociatedReference { Id = 1, Name = "Associated Reference 1" }, new AssociatedReference { Id = 2, Name = "Associated Reference 2" } }; context.AddRange(references); context.Add(new Entity { AssociatedReference = references[0], }); context.SaveChanges(); // WHEN the owned and the associated references are set to null: Entity detachedEntity = new Entity { Id = 1, AssociatedReference = null, OwnedReference = null }; await detachedContext.Set<Entity>().UpdateAsync(detachedEntity); await detachedContext.SaveChangesAsync(); // THEN the associated reference still exsits: Assert.True(context.AssociatedReferences.Any(a => a.Name == "Associated Reference 1")); } }
public async Task when_owned_reference_set_to_null__entity_is_deleted() { using (TestDbContext context = new TestDbContext()) { IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(context); // GIVEN an enity root with references: context.Add(new Entity { OwnedReference = new OwnedReference { Id = 1, Name = "Owned Reference 1" } }); context.SaveChanges(); // WHEN the owned and the associated references are set to null: Entity detachedEntity = new Entity { Id = 1, OwnedReference = null }; await detachedContext.Set<Entity>().UpdateAsync(detachedEntity); await detachedContext.SaveChangesAsync(); // THEN the owned reference is removed: Assert.False(context.OwnedReferences.Any(o => o.Name == "Owned Reference 1")); } }
public async Task when_owned_reference_set_to_entity__entity_is_created() { using (TestDbContext context = new TestDbContext()) { IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(context); // GIVEN an enity root with references: context.Add(new Entity { OwnedReference = new OwnedReference { Id = 1, Name = "Owned Reference 1" } }); context.SaveChanges(); // WHEN the owned reference is set: await detachedContext.Set<Entity>().UpdateAsync(new Entity { Id = 1, OwnedReference = new OwnedReference { Id = 2, Name = "Owned Reference 2" } }); await detachedContext.SaveChangesAsync(); // THEN the owned reference is replaced, the old reference is deleted: Assert.False(context.OwnedReferences.Any(o => o.Name == "Owned Reference 1")); } }
public async Task when_a_transformed_page_is_requested__the_page_is_loaded() { using (PaginationContext dbContext = new PaginationContext()) { // GIVEN a list of entities: for (int i = 1; i <= 50; i++) { dbContext.Entities.Add(new PaginationEntity { Name = "Entity " + i.ToString("D2") }); } await dbContext.SaveChangesAsync(); // WHEN a page of size 10 is loaded: DetachedContext <PaginationContext> detached = new DetachedContext <PaginationContext>(dbContext); IPage <Item> result = await detached.Set <PaginationEntity>().LoadPageAsync(1, 10, q => q.Where(e => string.Compare(e.Name, "Entity 20") > 0) .Select(e => new Item { Id = e.Id, Description = e.Name })); // THEN page contains 10 transformed items and correct indexes and count values: Assert.Equal(10, result.Items.Count); Assert.Equal(10, result.PageSize); Assert.Equal(1, result.PageIndex); Assert.Equal(30, result.RowCount); Assert.Equal(3, result.PageCount); Assert.Equal("Entity 21", result.Items.First().Description); } }
public async Task when_an_ordered_page_is_requested__the_page_is_loaded() { using (PaginationContext dbContext = new PaginationContext()) { // GIVEN a list of entities: for (int i = 1; i <= 50; i++) { dbContext.Entities.Add(new PaginationEntity { Name = "Entity " + i.ToString("D2") }); } await dbContext.SaveChangesAsync(); // WHEN a page of size 10 is loaded: DetachedContext <PaginationContext> detached = new DetachedContext <PaginationContext>(dbContext); IPage <PaginationEntity> result = await detached.Set <PaginationEntity>().LoadPageAsync(1, 10, q => q.OrderByDescending(e => e.Name)); // THEN page contains 10 items and correct indexes and count values: Assert.Equal(10, result.Items.Count); Assert.Equal(10, result.PageSize); Assert.Equal(1, result.PageIndex); Assert.Equal(50, result.RowCount); Assert.Equal(5, result.PageCount); Assert.Equal("Entity 50", result.Items.First().Name); } }
public async Task when_an_entity_with_audit_is_created__the_created_audit_fields_are_persisted() { using (IDetachedContext <AuditingContext> detachedContext = new DetachedContext <AuditingContext>()) { SessionInfoProvider.Default.CurrentUser = "******"; await detachedContext.Set <EntityForAuditing>().UpdateAsync(new EntityForAuditing { Name = "Test" }); await detachedContext.SaveChangesAsync(); EntityForAuditing persisted = await detachedContext.Set <EntityForAuditing>().LoadAsync(1); Assert.Equal("Test User", persisted.CreatedBy); Assert.Equal(DateTime.Now.Date, persisted.CreatedDate.Date); } }
public async Task when_load_by_key__type_conversion_is_made() { using (TestDbContext context = new TestDbContext()) { IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(context); await detachedContext.Set<Entity>().UpdateAsync(new Entity { Name = "Test entity" }); await detachedContext.SaveChangesAsync(); Entity persisted = await detachedContext.Set<Entity>().LoadAsync("1"); Assert.NotNull(persisted); persisted = await detachedContext.Set<Entity>().LoadAsync(1); Assert.NotNull(persisted); } }
public async Task when_root_persisted__children_are_persisted() { using (IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>()) { // GIVEN a context: detachedContext.DbContext.AddRange(new[] { new AssociatedListItem { Id = 1, Name = "Associated 1" }, new AssociatedListItem { Id = 2, Name = "Associated 2" } }); detachedContext.DbContext.Add(new AssociatedReference { Id = 1, Name = "Associated 1" }); await detachedContext.DbContext.SaveChangesAsync(); // WHEN an entity is persisted: await detachedContext.Set<Entity>().UpdateAsync(new Entity { Name = "Test entity", AssociatedList = new[] { new AssociatedListItem { Id = 1, Name = "Sarlanga" }, new AssociatedListItem { Id = 2 } }, AssociatedReference = new AssociatedReference { Id = 1 }, OwnedList = new[] { new OwnedListItem { Name = "Owned 1" }, new OwnedListItem { Name = "Owned 2" } }, OwnedReference = new OwnedReference { Name = "Owned Reference 1" } }); await detachedContext.SaveChangesAsync(); // THEN the entity should be loaded correctly: Entity persisted = await detachedContext.Set<Entity>().LoadAsync("1"); Assert.NotNull(persisted); Assert.Equal(2, persisted.AssociatedList.Count); Assert.NotNull(persisted.AssociatedReference); Assert.Equal(2, persisted.OwnedList.Count); Assert.NotNull(persisted.OwnedReference); } }
public async Task when_deleting_multiple_ids__entities_are_deleted() { using (TestDbContext context = new TestDbContext()) { // GIVEN: 3 entities IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(context); await detachedContext.Set<Entity>().UpdateAsync(new Entity { Name = "Entity 1" }); await detachedContext.Set<Entity>().UpdateAsync(new Entity { Name = "Entity 2" }); await detachedContext.Set<Entity>().UpdateAsync(new Entity { Name = "Entity 3" }); await detachedContext.SaveChangesAsync(); List<Entity> all = context.Entities.ToList(); Assert.Equal(3, all.Count); // WHEN: two are deleted in batch await detachedContext.Set<Entity>().DeleteAsync(new KeyValue(1), new KeyValue(2)); await detachedContext.SaveChangesAsync(); // THEN: only one remains all = context.Entities.ToList(); Assert.Equal(1, all.Count); } }
public async Task when_entity_is_loaded__many_to_many_is_loaded() { using (ManyToManyContext dbContext = new ManyToManyContext()) { dbContext.AddRange(new[] { new Role { Name = "Role 1" }, new Role { Name = "Role 2" } }); dbContext.SaveChanges(); dbContext.Add(new User { Name = "Test Root" }); dbContext.SaveChanges(); dbContext.AddRange(new[] { new UserRoles { User = dbContext.User.First(), Role = dbContext.Roles.First() }, new UserRoles { User = dbContext.User.First(), Role = dbContext.Roles.Last() } }); dbContext.SaveChanges(); IDetachedContext <ManyToManyContext> detachedContext = new DetachedContext <ManyToManyContext>(dbContext); User persisted = await detachedContext.Set <User>().LoadAsync(1); Assert.Equal(2, persisted.UserRoles.Count); Assert.Equal(2, persisted.Roles.Count); Assert.True(persisted.Roles.Any(r => r.Name == "Role 1")); Assert.True(persisted.Roles.Any(r => r.Name == "Role 2")); } }
public async Task when_entity_deleted__owned_properties_are_deleted() { using (TestDbContext context = new TestDbContext()) { IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(context); // GIVEN an enity root with an owned list: var associatedItems = new List<AssociatedListItem>(new[] { new AssociatedListItem { Name = "Associated Item 1" }, new AssociatedListItem { Name = "Associated Item 2" } }); context.AddRange(associatedItems); await context.SaveChangesAsync(); context.Add(new Entity { OwnedList = new List<OwnedListItem>(new[] { new OwnedListItem { Name = "Owned Item A" }, new OwnedListItem { Name = "Owned Item B" }, new OwnedListItem { Name = "Owned Item C" } }), AssociatedList = associatedItems }); context.SaveChanges(); // WHEN the entity is deleted: await detachedContext.Set<Entity>().DeleteAsync(1); await detachedContext.SaveChangesAsync(); // THEN owned items are removed: Assert.False(context.OwnedListItems.Any(e => e.Name == "Owned Item A")); Assert.False(context.OwnedListItems.Any(e => e.Name == "Owned Item B")); Assert.False(context.OwnedListItems.Any(e => e.Name == "Owned Item C")); // and the associated items are not removed: Assert.True(context.AssociatedListItems.Any(e => e.Name == "Associated Item 1")); Assert.True(context.AssociatedListItems.Any(e => e.Name == "Associated Item 2")); } }