public void ShouldDetectChanges_Added() { var detector = new TestEntityChangeDetector(); EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector); TestEntity entity = new TestEntity() { BooleanValue = false, DateTimeValue = new DateTime(2015, 07, 29), GuidValue = Guid.NewGuid(), IntValue = 123, MoneyValue = 12.34m, PercentValue = 12.5m, StringValue = "Hello" }; tracker.Add(entity); var changes = tracker.DetectChanges(); Assert.AreEqual(1, changes.Count(), "The wrong number of changed entities were detected."); var change = changes.Single(); Assert.AreSame(entity, change.Entity, "The wrong entity was returned."); Assert.AreEqual(EntityState.Added, change.State, "The entity was not marked as modified."); Assert.AreEqual(7, change.GetChanges().Count(), "The wrong number of changes were detected."); Assert.IsTrue(change.HasChange(e => e.BooleanValue), "Boolean value should be changed."); Assert.IsTrue(change.HasChange(e => e.DateTimeValue), "DateTime value should be changed."); Assert.IsTrue(change.HasChange(e => e.GuidValue), "Guid value should be changed."); Assert.IsTrue(change.HasChange(e => e.IntValue), "Int value should be changed."); Assert.IsTrue(change.HasChange(e => e.MoneyValue), "Money value should be changed."); Assert.IsTrue(change.HasChange(e => e.PercentValue), "Percent value should be changed."); Assert.IsTrue(change.HasChange(e => e.StringValue), "String value should be changed."); }
public void ShouldRemainAddedAfterAddingTwice() { var detector = new TestEntityChangeDetector(); EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector); TestEntity entity = new TestEntity(); tracker.Add(entity); var before = tracker.DetectChanges(entity); Assert.AreEqual(EntityState.Added, before.State, "The entity should have been Added to start with."); tracker.Add(entity); var after = tracker.DetectChanges(entity); Assert.AreEqual(EntityState.Added, after.State, "The entity should have remained Added."); }
public void ShouldLeaveUnmodifedAfterAttachingAddedEntity() { var detector = new TestEntityChangeDetector(); EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector); TestEntity entity = new TestEntity(); tracker.Add(entity); tracker.Attach(entity); var change = tracker.DetectChanges(entity); Assert.AreEqual(EntityState.Unmodified, change.State, "Attaching an Added entity should make it Unmodified."); }
public void ShouldNotDetectAddedAfterCommitting() { var detector = new TestEntityChangeDetector(); EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector); TestEntity entity = new TestEntity(); tracker.Add(entity); tracker.CommitChanges(); var change = tracker.DetectChanges(entity); Assert.AreEqual(EntityState.Unmodified, change.State, "The entity should be Unmodified after committing."); }
public void ShouldDetatchAddedEntitiesThatAreRemoved() { var detector = new TestEntityChangeDetector(); EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector); TestEntity entity = new TestEntity(); tracker.Add(entity); tracker.Remove(entity); var change = tracker.DetectChanges(entity); Assert.AreEqual(EntityState.Detached, change.State, "The entity should have been Detached."); }
public void ShouldDetachAddedEntity() { var detector = new TestEntityChangeDetector(); EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector); TestEntity entity = new TestEntity(); tracker.Add(entity); tracker.Detach(entity); var change = tracker.DetectChanges(entity); Assert.AreEqual(EntityState.Detached, change.State, "Detaching an Added entity should make it Detached."); }
public void ShouldDetectAddedByDefault() { var detector = new TestEntityChangeDetector(); EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector); TestEntity entity = new TestEntity(); tracker.Add(entity); var changes = tracker.DetectChanges(); Assert.AreEqual(1, changes.Count(), "The entity should have been seen as changed."); var change = changes.Single(); Assert.AreSame(entity, change.Entity, "The wrong entity was returned."); Assert.AreEqual(EntityState.Added, change.State, "The entity was not added."); }
public void ShouldDetectChangeToDerivedPropertyWhenAdding() { var detector = new DerivedChangeDetector(); var tracker = new EntityChangeTracker <TestEntity>(detector); DerivedEntity entity = new DerivedEntity() { DerivedValue = 123 }; tracker.Add(entity); var changes = tracker.DetectChanges(); Assert.AreEqual(1, changes.Count(), "The wrong number of changes were detected."); IEntityChange <TestEntity> change = changes.Single(); Assert.AreSame(entity, change.Entity, "A change was detected on the wrong entity."); Assert.AreEqual(EntityState.Added, change.State, "The entity should have been modified."); Assert.AreEqual(1, change.GetChanges().Count(), "The wrong number of properties were seen as changed."); }
public void ShouldLeaveModifiedWhenAddingRemovedEntity() { var detector = new TestEntityChangeDetector(); EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector); TestEntity entity = new TestEntity() { IntValue = 123 }; tracker.Attach(entity); tracker.Remove(entity); tracker.Add(entity); entity.IntValue = 234; var change = tracker.DetectChanges(entity); Assert.AreEqual(EntityState.Modified, change.State, "Adding a Removed entity that was changed should make it Modified."); Assert.IsTrue(change.HasChange(x => x.IntValue), "The modified value does not reflect the latest value."); }
public void ShouldDetectMostRecentChangesForAdded() { var detector = new TestEntityChangeDetector(); EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector); TestEntity entity = new TestEntity() { IntValue = 123 }; tracker.Add(entity); entity.IntValue = 234; var after = tracker.DetectChanges(entity); Assert.AreEqual(EntityState.Added, after.State, "The entity should have remained Added."); Assert.IsTrue(after.HasChange(x => x.IntValue), "The IntValue should have changed."); var change = after.GetChange(x => x.IntValue); Assert.IsNotNull(change, "The change was null after detecting its prescence."); Assert.AreEqual(234, change.UpdatedValue, "The latest value was not returned."); }