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 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.");
        }
Exemplo n.º 3
0
        public void ShouldThrowExceptionIfAccessorRefersToNonProperty_HasChanges()
        {
            var        detector = new TestEntityChangeDetector();
            TestEntity original = new TestEntity();
            TestEntity updated  = new TestEntity();

            detector.HasChange(e => e.Field, original, updated);
        }
        public void ShouldLeaveDetachedIfRemovingDetachedEntity()
        {
            var detector = new TestEntityChangeDetector();
            EntityChangeTracker <TestEntity> tracker = new EntityChangeTracker <TestEntity>(detector);

            TestEntity entity = new TestEntity();

            Assert.IsFalse(tracker.Remove(entity), "Should not have indicated the entity was removed when it was Detached.");
        }
Exemplo n.º 5
0
        public void ShouldReturnFalseIfCheckingIfUnconfiguredPropertyChanged()
        {
            var        detector = new TestEntityChangeDetector();
            TestEntity original = new TestEntity();
            TestEntity updated  = new TestEntity();

            bool hasChange = detector.HasChange(a => a.NotConfigured, original, updated);

            Assert.IsFalse(hasChange, "A change should not be detected if the property is not recognized.");
        }
Exemplo n.º 6
0
        public void ShouldThrowExceptionIfAccessorNull_HasChanges()
        {
            var        detector = new TestEntityChangeDetector();
            TestEntity original = new TestEntity();
            TestEntity updated  = new TestEntity();

            Expression <Func <TestEntity, int> > accessor = null;

            detector.HasChange(accessor, original, updated);
        }
Exemplo n.º 7
0
        public void ShouldNotDetectChange_OriginalAndUpdatedNull()
        {
            var        detector = new TestEntityChangeDetector();
            TestEntity original = null;
            TestEntity updated  = null;

            Assert.IsFalse(detector.HasChange(a => a.StringValue, original, updated), "Change detected for the property.");

            var changes = detector.GetChanges(original, updated);

            Assert.AreEqual(0, changes.Count(), "The wrong number of changes were detected.");
        }
        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 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 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 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 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.");
        }
Exemplo n.º 18
0
        public void ShouldDetectChange_UpdatedNull()
        {
            var        detector = new TestEntityChangeDetector();
            TestEntity original = new TestEntity()
            {
                StringValue = "After"
            };
            TestEntity updated = null;

            Assert.IsTrue(detector.HasChange(a => a.StringValue, original, updated), "No change detected for the property.");

            var changes = detector.GetChanges(original, updated);

            Assert.AreEqual(1, changes.Count(), "The wrong number of changes were detected.");
            IPropertyChange change = changes.Single();

            Assert.AreEqual(TestEntityChangeDetector.StringDescription, change.DisplayName, "The wrong property was recorded.");
            Assert.AreEqual(Formatters.FormatString(original.StringValue), change.FormatOriginalValue(), "The old value was not recorded.");
            Assert.AreEqual(null, change.FormatUpdatedValue(), "The new value was not recorded.");
        }
        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 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 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.");
        }
Exemplo n.º 22
0
        public void ShouldDetectAllChanges()
        {
            var        detector = new TestEntityChangeDetector();
            TestEntity original = new TestEntity()
            {
                BooleanValue  = false,
                DateTimeValue = new DateTime(2015, 07, 29),
                GuidValue     = Guid.NewGuid(),
                IntValue      = 123,
                MoneyValue    = 12.34m,
                PercentValue  = 12.5m,
                StringValue   = "Hello"
            };
            TestEntity updated = new TestEntity()
            {
                BooleanValue  = true,
                DateTimeValue = new DateTime(2015, 07, 30),
                GuidValue     = Guid.NewGuid(),
                IntValue      = 234,
                MoneyValue    = 10m,
                PercentValue  = 15m,
                StringValue   = "Goodbye"
            };

            Assert.IsTrue(detector.HasChange(e => e.BooleanValue, original, updated), "Boolean value should be changed.");
            Assert.IsTrue(detector.HasChange(e => e.DateTimeValue, original, updated), "DateTime value should be changed.");
            Assert.IsTrue(detector.HasChange(e => e.GuidValue, original, updated), "Guid value should be changed.");
            Assert.IsTrue(detector.HasChange(e => e.IntValue, original, updated), "Int value should be changed.");
            Assert.IsTrue(detector.HasChange(e => e.MoneyValue, original, updated), "Money value should be changed.");
            Assert.IsTrue(detector.HasChange(e => e.PercentValue, original, updated), "Percent value should be changed.");
            Assert.IsTrue(detector.HasChange(e => e.StringValue, original, updated), "String value should be changed.");

            var changes = detector.GetChanges(original, updated);

            Assert.AreEqual(7, changes.Count(), "The wrong number of changes were detected.");
        }