Exemplo n.º 1
0
        public void Equals_NotSameType_Fails()
        {
            var one = ChangePath.Create("one");
            var two = new ChangeSet();

            Assert.False(Equals(one, two));
        }
Exemplo n.º 2
0
        public void GetChangeSet_CorrectWhenItemUpdate()
        {
            var expectedOldValue = "My old value";
            var expectedItem     = new TestItem
            {
                StringValue = expectedOldValue
            };
            var subject = new ChangeContainerItem <TestItem>(expectedItem, ChangeTarget.Collection);

            var expectedNewValue = "New value";

            expectedItem.StringValue = expectedNewValue;

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

            var expectedPath = path.AppendProperty(nameof(TestItem.StringValue));

            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(expectedOldValue, value.OldValue);
            Assert.Same(expectedNewValue, value.NewValue);
        }
Exemplo n.º 3
0
        public void GetChangeSet_CommitFalse_ContainsRemoves()
        {
            var expectedRemove = new TestItem();
            var expectedItems  = new List <TestItem>
            {
                new TestItem(),
                expectedRemove,
                new TestItem()
            };
            var subject  = new ChangeTrackingCollection <TestItem>(expectedItems);
            var basePath = ChangePath.Create("path");

            Assert.True(subject.Remove(expectedRemove));
            var result       = subject.GetChangeSet(basePath);
            var expectedPath = basePath.AppendIndex(1);

            Assert.True(subject.HasChanges);
            Assert.NotNull(result);
            var kvp = Assert.Single(result);

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

            Assert.Equal(ChangeAction.Remove, value.Action);
            Assert.Null(value.NewValue);
            Assert.Same(expectedRemove, value.OldValue);
        }
Exemplo n.º 4
0
        public void StartsWith_FalseWhenNotMatch()
        {
            var path1 = ChangePath.Create("Property1").AppendKey("123").AppendProperty("Property");
            var path2 = ChangePath.Create("Property1").AppendKey("321");

            Assert.False(path1.StartsWith(path2));
        }
Exemplo n.º 5
0
        public void Equals_Fails()
        {
            var one = ChangePath.Create("one");
            var two = ChangePath.Create("two");

            Assert.NotEqual(one, two);
        }
Exemplo n.º 6
0
        public void Equals_Success()
        {
            var one = ChangePath.Create("one");
            var two = ChangePath.Create("one");

            Assert.Equal(one, two);
        }
Exemplo n.º 7
0
        public void StartsWith_TrueWhenIndexMatch()
        {
            var path1 = ChangePath.Create("Property1").AppendIndex("123").AppendProperty("Property");
            var path2 = ChangePath.Create("Property1").AppendIndex("123");

            Assert.True(path1.StartsWith(path2));
        }
Exemplo n.º 8
0
        public void ToJsonPatch_Update_Complex_Success()
        {
            var path             = "Test";
            var expectedPath     = $"/{path.ToCamelCase()}";
            var expectedNewValue = new TestValue
            {
                StringValue = "My new complex value",
                IntValue    = 12345
            };
            var changes = ChangeSet.Create(
                ChangePath.Create(path),
                new ChangeValue
            {
                Action   = ChangeAction.Update,
                NewValue = expectedNewValue
            });

            var patch = changes.ToJsonPatch();

            Assert.Single(patch);
            var operation = patch[0];

            Assert.Equal(OperationType.Replace, operation.OperationType);
            Assert.Equal(expectedPath, operation.path);
            Assert.Equal(expectedNewValue, operation.value);
        }
Exemplo n.º 9
0
        public void GetChangeSet_CommitFalse_ContainsUpdates()
        {
            var expectedOldValue = "My old value";
            var expectedNewValue = "The new value";
            var expectedUpdate   = new TestItem
            {
                StringValue = expectedOldValue
            };
            var expectedItems = new List <TestItem>
            {
                new TestItem(),
                expectedUpdate,
                new TestItem()
            };
            var subject      = new ChangeTrackingCollection <TestItem>(expectedItems);
            var path         = ChangePath.Create("path");
            var expectedPath = path.AppendIndex(1).AppendProperty(nameof(TestItem.StringValue));

            expectedUpdate.StringValue = expectedNewValue;
            var result = subject.GetChangeSet(path);

            Assert.True(subject.HasChanges);
            Assert.NotNull(result);
            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.Equal(expectedNewValue, value.NewValue);
            Assert.Equal(expectedOldValue, value.OldValue);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        public void StartsWith_FalseWhenPathIsLonger()
        {
            var path1 = ChangePath.Create("Property1");
            var path2 = ChangePath.Create("Property1").AppendKey("321");

            Assert.False(path1.StartsWith(path2));
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
        public void Create_Part_Success()
        {
            var expectedPart = new ChangePathProperty("value");
            var path         = ChangePath.Create(expectedPart);

            Assert.Single(path.Parts);
            Assert.Same(expectedPart, path.Parts[0]);
        }
Exemplo n.º 14
0
        public void GetHashCode_EqualsTrue()
        {
            var one     = ChangePath.Create("one").AppendProperty("two");
            var two     = ChangePath.Create("one").AppendProperty("two");
            var hashOne = one.GetHashCode();
            var hashTwo = two.GetHashCode();

            Assert.Equal(hashOne, hashTwo);
        }
Exemplo n.º 15
0
        public void Create_Property_Success()
        {
            var expectedValue = "test";
            var path          = ChangePath.Create(expectedValue);

            Assert.Single(path.Parts);
            Assert.True(path.Parts[0] is ChangePathProperty);
            var cpp = path.Parts[0] as ChangePathProperty;

            Assert.Equal(expectedValue, cpp.Property);
        }
Exemplo n.º 16
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);
        }
Exemplo n.º 17
0
        public void ToJsonPatch_IgnoresActionNone()
        {
            var changes = ChangeSet.Create(
                ChangePath.Create("Test"),
                new ChangeValue
            {
                Action = ChangeAction.None
            });

            var patch = changes.ToJsonPatch();

            Assert.Empty(patch);
        }
Exemplo n.º 18
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));
        }
Exemplo n.º 19
0
        public void GetChangeSet_GeneratesCorrectChangeSet()
        {
            var expectedOldValue = "Initial Value";
            var expectedNewValue = "New Value";
            var expectedPath     = ChangePath.Create("Key");
            var subject          = new ChangeContainerValue(expectedOldValue);

            subject.CurrentValue = expectedNewValue;
            var changes = subject.GetChangeSet(expectedPath);

            Assert.Single(changes);
            Assert.True(changes.ContainsKey(expectedPath));
            Assert.Single(changes[expectedPath]);
            Assert.Equal(ChangeAction.Update, changes[expectedPath][0].Action);
            Assert.Equal(expectedOldValue, changes[expectedPath][0].OldValue);
            Assert.Equal(expectedNewValue, changes[expectedPath][0].NewValue);
        }
Exemplo n.º 20
0
        public void Merge_MergesEntries()
        {
            var expectetPathOne = ChangePath.Create("one");
            var expectedPathTwo = ChangePath.Create("two");
            var subject         = new ChangeSet();

            subject.Append(expectetPathOne, new ChangeValue());
            var mergeMe = new ChangeSet();

            mergeMe.Append(expectedPathTwo, new ChangeValue());

            subject.Merge(mergeMe);

            Assert.Equal(2, subject.Count);
            Assert.Single(mergeMe);
            Assert.Contains(expectetPathOne, subject as IDictionary <ChangePath, List <ChangeValue> >);
            Assert.Contains(expectedPathTwo, subject as IDictionary <ChangePath, List <ChangeValue> >);
        }
Exemplo n.º 21
0
        public void CommitChanges_CallsITracksChangesGetChangeSet()
        {
            var expectedPath      = ChangePath.Create("MyPath");
            var expectedChangeSet = new ChangeSet();
            var mock = new Mock <ITracksChanges>(MockBehavior.Strict);

            mock
            .Setup(tc => tc.GetChangeSet(It.IsAny <ChangePath>(), It.IsAny <bool>()))
            .Returns(expectedChangeSet);

            var result = ITracksChangesExtensions.CommitChanges(mock.Object, expectedPath);

            Assert.Same(expectedChangeSet, result);

            mock.Verify(tc => tc.GetChangeSet(
                            It.Is <ChangePath>(v => v == expectedPath),
                            It.Is <bool>(v => v)),
                        Times.Once);
        }
Exemplo n.º 22
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);
        }
Exemplo n.º 23
0
        public void ToJsonPatch_Remove_Success()
        {
            var path             = "Test";
            var expectedPath     = $"/{path.ToCamelCase()}";
            var expectedNewValue = "My new value";
            var changes          = ChangeSet.Create(
                ChangePath.Create(path),
                new ChangeValue
            {
                Action   = ChangeAction.Remove,
                NewValue = expectedNewValue
            });

            var patch = changes.ToJsonPatch();

            Assert.Single(patch);
            var operation = patch[0];

            Assert.Equal(OperationType.Remove, operation.OperationType);
            Assert.Equal(expectedPath, operation.path);
            Assert.Null(operation.value);
        }