Beispiel #1
0
        public void HasChanges_TrueWhenAdd()
        {
            var subject = new ChangeTrackingCollection <TestItem>();

            subject.Add(ChangeTrackingObject.CreateTrackable <TestItem>());
            Assert.True(subject.HasChanges);
        }
Beispiel #2
0
        public void GetChangeSet_CommitTrue_MakesChangesPermanent()
        {
            var removeMe       = ChangeTrackingObject.CreateTrackable <TestItem>();
            var expectedUpdate = ChangeTrackingObject.CreateTrackable <TestItem>();
            var expectedItems  = new List <TestItem>
            {
                removeMe,
                expectedUpdate
            };
            var subject = new ChangeTrackingCollection <TestItem>(expectedItems);

            subject.Add(new TestItem());
            subject.Remove(removeMe);
            var expectedNewValue = "New Value";

            expectedUpdate.StringValue = expectedNewValue;

            var result = subject.GetChangeSet(ChangePath.Empty, commit: true);

            Assert.Equal(3, result.Count);
            Assert.False(subject.HasChanges);
            Assert.False(expectedUpdate.HasChanges);
            Assert.Equal(expectedNewValue, expectedUpdate.StringValue);
            Assert.DoesNotContain(removeMe, subject);
            Assert.Equal(2, subject.Count);
        }
Beispiel #3
0
        public void GetChangeSet_CorrectWhenDirectUpdate()
        {
            var originalItem = ChangeTrackingObject.CreateTrackable <TestItem>();

            originalItem.StringValue = "New value";
            var subject = new ChangeContainerItem <TestItem>(originalItem, ChangeTarget.Collection);

            var expectedItem = ChangeTrackingObject.CreateTrackable <TestItem>();

            subject.Item = expectedItem;

            var expectedPath = ChangePath.Create("Path");
            var result       = subject.GetChangeSet(expectedPath);

            var kvp = Assert.Single(result);

            Assert.Equal(ChangeTarget.Object, kvp.Key.Target);
            Assert.Contains(expectedPath, result as IDictionary <ChangePath, List <ChangeValue> >);
            var values = result[expectedPath];
            var value  = Assert.Single(values);

            Assert.Equal(ChangeAction.Update, value.Action);
            Assert.Same(originalItem, value.OldValue);
            Assert.Same(expectedItem, value.NewValue);
        }
        public void HasChanges_TrueWhenChanges()
        {
            var subject = ChangeTrackingObject.CreateTrackable <TestObject>();

            subject.StringValue = "NewValue";
            Assert.True(subject.HasChanges);
        }
Beispiel #5
0
        public void HasChanges_TrueWhenChangesToItem()
        {
            var item    = ChangeTrackingObject.CreateTrackable <TestItem>();
            var subject = new ChangeContainerItem <TestItem>(item, ChangeTarget.Object);

            item.StringValue = "Updated value";
            Assert.True(subject.HasChanges);
        }
Beispiel #6
0
        public void GetChangeSet_NullWhenNoChanges()
        {
            var expectedItem = ChangeTrackingObject.CreateTrackable <TestItem>();
            var subject      = new ChangeContainerItem <TestItem>(expectedItem, ChangeTarget.Collection);
            var result       = subject.GetChangeSet(ChangePath.Create("path"));

            Assert.Null(result);
        }
Beispiel #7
0
        public void Ctor_EntitySet()
        {
            var expectedEntity = ChangeTrackingObject.CreateTrackable <TestEntity>();
            var mockSubject    = new Mock <BaseEntityDataPackage <TestEntity> >(
                MockBehavior.Strict,
                expectedEntity);

            Assert.Same(expectedEntity, mockSubject.Object.Entity);
        }
        public void GetValue_Success()
        {
            var expectedValue = "My expected value";
            var subject       = ChangeTrackingObject.CreateTrackable <TestObject>();

            subject.StringValue = expectedValue;
            var result = subject.TestGetValue <string>(nameof(subject.StringValue));

            Assert.Equal(expectedValue, result);
        }
Beispiel #9
0
        public void HasChanges_FalseAtStart()
        {
            var expectedItems = new List <TestItem>
            {
                ChangeTrackingObject.CreateTrackable <TestItem>()
            };
            var subject = new ChangeTrackingCollection <TestItem>(expectedItems);

            Assert.False(subject.HasChanges);
        }
Beispiel #10
0
        public void Action_ItemHasChanges_ReturnsUpdate()
        {
            var expectedItem = ChangeTrackingObject.CreateTrackable <TestItem>();

            expectedItem.StringValue = "Updated value";
            Assert.True(expectedItem.HasChanges);

            var subject = new ChangeContainerItem <TestItem>(expectedItem, ChangeTarget.Object);

            Assert.Equal(ChangeAction.Update, subject.Action);
        }
Beispiel #11
0
        public void HasChanges_TrueWhenItemChange()
        {
            var item          = ChangeTrackingObject.CreateTrackable <TestItem>();
            var expectedItems = new List <TestItem>
            {
                item
            };
            var subject = new ChangeTrackingCollection <TestItem>(expectedItems);

            item.StringValue = "New value";
            Assert.True(subject.HasChanges);
        }
Beispiel #12
0
        public void GetChangeSet_HasChangeFalseWhenCommit()
        {
            var expectedItem = ChangeTrackingObject.CreateTrackable <TestItem>();
            var subject      = new ChangeContainerItem <TestItem>(expectedItem, ChangeTarget.Collection);

            expectedItem.StringValue = "New value";

            subject.GetChangeSet(ChangePath.Create("path"), true);

            Assert.False(subject.HasChanges);
            Assert.False(expectedItem.HasChanges);
        }
        public void Ctor_ItemsDictionaryParamWithChanges_HasChangesTrue()
        {
            var expectedUpdate = ChangeTrackingObject.CreateTrackable <TestItem>();
            var expectedItems  = new Dictionary <string, TestItem>
            {
                { "Test1", new TestItem() },
                { "Test2", expectedUpdate }
            };

            expectedUpdate.StringValue = "new value";
            var subject = new ChangeTrackingDictionary <string, TestItem>(expectedItems);

            Assert.True(subject.HasChanges);
        }
Beispiel #14
0
        public void GetChangeSet_GeneratesCorrectChangeSetForITracksChanges()
        {
            var path         = ChangePath.Create("Key");
            var expectedPath = path.AppendProperty(nameof(NestedObject.StringValue));
            var nested       = ChangeTrackingObject.CreateTrackable <NestedObject>();
            var subject      = new ChangeContainerValue(nested);

            nested.StringValue = "My new value;";

            var changes = subject.GetChangeSet(path);

            Assert.Single(changes);
            Assert.True(changes.ContainsKey(expectedPath));
        }
Beispiel #15
0
        public void Reset_RevertsItemChanges()
        {
            var expectedItem = ChangeTrackingObject.CreateTrackable <TestItem>();
            var subject      = new ChangeContainerItem <TestItem>(expectedItem, ChangeTarget.Collection);

            subject.Item.StringValue = "My new value";
            Assert.True(subject.HasChanges);
            Assert.Equal(ChangeAction.Update, subject.Action);

            subject.Reset();
            Assert.Same(expectedItem, subject.Item);
            Assert.False(subject.HasChanges);
            Assert.Equal(ChangeAction.None, subject.Action);
        }
Beispiel #16
0
        public void GetChangeSet_CorrectWhenRemove()
        {
            var expectedItem         = ChangeTrackingObject.CreateTrackable <TestItem>();
            var expectedChangeAction = ChangeAction.Remove;
            var subject      = new ChangeContainerItem <TestItem>(expectedItem, ChangeTarget.Collection, expectedChangeAction);
            var expectedPath = ChangePath.Create("Path");
            var result       = subject.GetChangeSet(expectedPath);

            var kvp = Assert.Single(result);

            Assert.Equal(ChangeTarget.Object, kvp.Key.Target);
            Assert.Contains(expectedPath, result as IDictionary <ChangePath, List <ChangeValue> >);
            var values = result[expectedPath];
            var value  = Assert.Single(values);

            Assert.Equal(expectedChangeAction, value.Action);
            Assert.Null(value.NewValue);
            Assert.Same(expectedItem, value.OldValue);
        }
        public void GetValue_ReturnsDefaultWhenNotSet()
        {
            var subject = ChangeTrackingObject.CreateTrackable <TestObject>();
            var result  = subject.TestGetValue <int>("someproperty");

            Assert.Equal(default, result);
        public void GetValue_ThrowsWhenNull()
        {
            var subject = ChangeTrackingObject.CreateTrackable <TestObject>();

            Assert.Throws <ArgumentNullException>(() => subject.TestGetValue <string>(null));
        }
Beispiel #19
0
        public async Task UpdateAsync_PipelineExecuted()
        {
            var updatingCalled      = false;
            var updatedCalled       = false;
            var executeUpdateCalled = false;

            var mockProcessorUpdating = new Mock <IProcessor <EntityUpdatingPackage <TestEntity> > >(MockBehavior.Strict);

            mockProcessorUpdating.SetupGet(p => p.Stage).Returns(new NumericPriorityStage(0));
            mockProcessorUpdating
            .Setup(p => p.ProcessAsync(It.IsAny <EntityUpdatingPackage <TestEntity> >(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask)
            .Callback(() =>
            {
                Assert.False(updatedCalled);
                Assert.False(executeUpdateCalled);
                updatingCalled = true;
            });

            var mockProcessorUpdated = new Mock <IProcessor <EntityUpdatedPackage <TestEntity> > >(MockBehavior.Strict);

            mockProcessorUpdated.SetupGet(p => p.Stage).Returns(new NumericPriorityStage(0));
            mockProcessorUpdated
            .Setup(p => p.ProcessAsync(It.IsAny <EntityUpdatedPackage <TestEntity> >(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask)
            .Callback(() =>
            {
                Assert.True(updatingCalled);
                Assert.True(executeUpdateCalled);
                updatedCalled = true;
            });

            var mockProcessorProvider = new Mock <IProcessorProvider>(MockBehavior.Strict);

            mockProcessorProvider
            .Setup(p => p.GetProcessors <EntityUpdatingPackage <TestEntity> >())
            .Returns(new List <IProcessor <EntityUpdatingPackage <TestEntity> > > {
                mockProcessorUpdating.Object
            });
            mockProcessorProvider
            .Setup(p => p.GetProcessors <EntityUpdatedPackage <TestEntity> >())
            .Returns(new List <IProcessor <EntityUpdatedPackage <TestEntity> > > {
                mockProcessorUpdated.Object
            });

            ChangeSet expectedChangeSet = null;

            var expectedPipeline = new ProcessorPipeline(mockProcessorProvider.Object);
            var mockSubject      = new Mock <Repository <TestEntity> >(MockBehavior.Strict, expectedPipeline);

            mockSubject
            .Protected()
            .Setup <Task <ChangeSet> >("ExecuteUpdateAsync", ItExpr.IsAny <TestEntity>(), ItExpr.IsAny <ChangeSet>(), It.IsAny <CancellationToken>())
            .Callback <TestEntity, ChangeSet, CancellationToken>((e, cs, ct) =>
            {
                expectedChangeSet = cs;
                Assert.True(updatingCalled);
                Assert.False(updatedCalled);
                executeUpdateCalled = true;
            })
            .Returns(Task.FromResult(expectedChangeSet));

            var expectedEntity = ChangeTrackingObject.CreateTrackable <TestEntity>();

            expectedEntity.StringValue = "NewValue";

            var result = await mockSubject.Object.UpdateAsync(expectedEntity, CancellationToken.None);

            mockProcessorProvider.Verify(p => p.GetProcessors <EntityUpdatingPackage <TestEntity> >(), Times.Once);
            mockProcessorProvider.Verify(p => p.GetProcessors <EntityUpdatedPackage <TestEntity> >(), Times.Once);

            mockProcessorUpdating.Verify(p => p.ProcessAsync(
                                             It.Is <EntityUpdatingPackage <TestEntity> >(v => ReferenceEquals(v.Entity, expectedEntity)),
                                             It.IsAny <CancellationToken>()),
                                         Times.Once());

            mockProcessorUpdated.Verify(p => p.ProcessAsync(
                                            It.Is <EntityUpdatedPackage <TestEntity> >(v => ReferenceEquals(v.Entity, expectedEntity)),
                                            It.IsAny <CancellationToken>()),
                                        Times.Once());

            mockSubject
            .Protected()
            .Verify("ExecuteUpdateAsync",
                    Times.Once(),
                    ItExpr.Is <TestEntity>(v => ReferenceEquals(v, expectedEntity)),
                    ItExpr.Is <ChangeSet>(v => ReferenceEquals(v, expectedChangeSet)),
                    ItExpr.IsAny <CancellationToken>());
        }
        public void HasChanges_FalseOnInitialize()
        {
            var subject = ChangeTrackingObject.CreateTrackable <TestObject>();

            Assert.False(subject.HasChanges);
        }