public void ShouldSeeChangeToBasePropertyWhenBaseObject()
        {
            var           detector = new DerivedChangeDetector();
            var           tracker  = new EntityChangeTracker <TestEntity>(detector);
            DerivedEntity entity   = new DerivedEntity()
            {
                IntValue = 123
            };

            tracker.Attach(entity);

            entity.IntValue = 234;

            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.Modified, change.State, "The entity should have been modified.");

            Assert.AreEqual(1, change.GetChanges().Count(), "The wrong number of properties were seen as changed.");
            var propertyChange = change.GetChanges().Single();

            Assert.AreEqual(TestEntityChangeDetector.IntDescription, propertyChange.DisplayName, "The wrong property was recorded.");

            bool hasBaseChange = change.HasChange(x => x.IntValue);

            Assert.IsTrue(hasBaseChange, "The change was not detected.");

            bool hasDerivedChange = change.As <DerivedEntity>().HasChange(x => x.IntValue);

            Assert.IsTrue(hasDerivedChange, "The change was not detected.");
        }
        public void ShouldDetectMostRecentChangesForRemoved()
        {
            var detector = new TestEntityChangeDetector();
            EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector);

            TestEntity entity = new TestEntity()
            {
                IntValue = 123
            };

            tracker.Attach(entity);
            tracker.Remove(entity);

            entity.IntValue = 234;

            var change = tracker.DetectChanges(entity);

            Assert.AreEqual(EntityState.Removed, change.State, "The entity should have remained Added.");
            Assert.IsTrue(change.HasChange(x => x.IntValue), "The IntValue should have changed.");

            var propertyChange = change.GetChange(x => x.IntValue);

            Assert.IsNotNull(propertyChange, "The change was null after detecting its prescence.");
            Assert.AreEqual(234, propertyChange.OriginalValue, "The latest value was not returned.");
        }
        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 ShouldRetainModificationIfAttachedTwice()
        {
            var detector = new TestEntityChangeDetector();
            EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector);

            TestEntity entity = new TestEntity()
            {
                IntValue = 123
            };

            tracker.Attach(entity);
            var before = tracker.DetectChanges(entity);

            Assert.AreEqual(EntityState.Unmodified, before.State, "The entity should have been Unmodified to start with.");

            entity.IntValue = 234;

            tracker.Attach(entity); // Should not wipe out changes
            var after = tracker.DetectChanges(entity);

            Assert.AreEqual(EntityState.Modified, after.State, "The entity should have become Modified.");
        }
        public void ShouldDetachUnmodifiedEntity()
        {
            var detector = new TestEntityChangeDetector();
            EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector);

            TestEntity entity = new TestEntity();

            tracker.Attach(entity);
            tracker.Detach(entity);

            var change = tracker.DetectChanges(entity);

            Assert.AreEqual(EntityState.Detached, change.State, "Detaching an Unmodified entity should make it Detached.");
        }
        public void ShouldLeaveUnmodifedAfterAttachingRemovedEntity()
        {
            var detector = new TestEntityChangeDetector();
            EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector);

            TestEntity entity = new TestEntity();

            tracker.Remove(entity);
            tracker.Attach(entity);

            var change = tracker.DetectChanges(entity);

            Assert.AreEqual(EntityState.Unmodified, change.State, "Attaching a Removed 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 ShouldLeaveDetachedEntityDetached()
        {
            var detector = new TestEntityChangeDetector();
            EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector);

            TestEntity entity = new TestEntity();

            // Do not attach the entit, so Detach does nothing
            Assert.IsFalse(tracker.Detach(entity), "Should not be indicating the item was detached.");

            var change = tracker.DetectChanges(entity);

            Assert.AreEqual(EntityState.Detached, change.State, "Detaching a Detached entity should leave it Detached.");
        }
        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 ShouldMarkRemovedAsDetachedAfterCommitting()
        {
            var detector = new TestEntityChangeDetector();
            EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector);

            TestEntity entity = new TestEntity();

            tracker.Attach(entity);
            tracker.Remove(entity);

            tracker.CommitChanges();
            var change = tracker.DetectChanges(entity);

            Assert.AreEqual(EntityState.Detached, change.State, "The entity should be Detached after committing.");
        }
        public void ShouldAllowRetrievingUnmodifiedEntities()
        {
            var detector = new TestEntityChangeDetector();
            EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector);

            TestEntity entity = new TestEntity();

            tracker.Attach(entity);

            var changes = tracker.DetectChanges(EntityState.Unmodified);

            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.Unmodified, change.State, "The entity was not unmodified.");
        }
        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 ShouldNotDetectChangesAfterCommitting()
        {
            var detector = new TestEntityChangeDetector();
            EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector);

            TestEntity entity = new TestEntity()
            {
                IntValue = 1234
            };

            tracker.Attach(entity);
            entity.IntValue = 234;  // Should make the entity Modified

            tracker.CommitChanges();
            var change = tracker.DetectChanges(entity);

            Assert.AreEqual(EntityState.Unmodified, change.State, "The entity should be Unmodified after committing.");
        }
        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 ShouldIncludeChangeToDoubleDerivedProperty()
        {
            var detector = new DoubleDerivedChangeDetector();
            var tracker  = new EntityChangeTracker <TestEntity>(detector);
            DoubleDerivedEntity entity = new DoubleDerivedEntity()
            {
                DoubleDerivedValue = "John"
            };

            tracker.Attach(entity);

            entity.DoubleDerivedValue = "Tom";

            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.Modified, 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 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.");
        }