public void When_object_changed_modified_event_should_be_called()
        {
            var subscriber = Substitute.For <IDummySubscriber>();
            var item       = new
            {
                Id  = AutoFixture.Create <string>(),
                Str = AutoFixture.Create <string>(),
                Int = AutoFixture.Create <int>(),
                Obj = new
                {
                    Value = AutoFixture.Create <string>()
                }
            };
            var jObject    = JObject.FromObject(item);
            var collection = new StreamCollection(_name);

            collection.Added(item.Id, jObject);
            collection.Modified += subscriber.React;

            // Act
            collection.Changed(item.Id, jObject);

            // Assert
            subscriber.Received()
            .React(Arg.Any <object>(), Arg.Is <StreamCollectionEventArgs>(args =>
                                                                          args.ModificationType == ModificationType.Changed && args.Result == jObject
                                                                          ));
        }
        public void When_an_empty_object_is_given_for_change_update_nothing()
        {
            var item = new
            {
                Id  = AutoFixture.Create <string>(),
                Str = AutoFixture.Create <string>(),
                Int = AutoFixture.Create <int>(),
                Obj = new
                {
                    Value = AutoFixture.Create <string>()
                }
            };

            var other = new
            {
            };

            var collection = new StreamCollection(_name);

            // Act
            collection.Added(item.Id, JObject.FromObject(item));
            collection.Changed(item.Id, JObject.FromObject(other));

            // Assert
            var result = collection.GetAnonymousTypeById(item.Id, item);

            result.Should().Be(item);
        }
        public void When_object_is_changed_merge_existing_values()
        {
            var item = new
            {
                Id  = AutoFixture.Create <string>(),
                Str = AutoFixture.Create <string>(),
                Int = AutoFixture.Create <int>(),
                Obj = new
                {
                    OldValue = AutoFixture.Create <string>(),
                    Value    = AutoFixture.Create <string>()
                }
            };

            var other = new
            {
                Str = AutoFixture.Create <string>(),
                Int = AutoFixture.Create <int>(),
                Obj = new
                {
                    OldVaue  = item.Obj.OldValue,
                    Value    = AutoFixture.Create <string>(),
                    NewValue = AutoFixture.Create <string>()
                },
                NewValue = AutoFixture.Create <string>()
            };

            var collection = new StreamCollection(_name);

            // Act
            collection.Added(item.Id, JObject.FromObject(item));
            collection.Changed(item.Id, JObject.FromObject(other));

            // Assert
            var result = collection.GetAnonymousTypeById(item.Id, other);

            result.Int.Should().Be(other.Int);
            result.Str.Should().Be(other.Str);
            result.Obj.Should().Be(other.Obj);

            other.Should().NotBeSameAs(result);

            var result2 = collection.GetAnonymousTypeById(item.Id, item);

            result2.Id.Should().Be(item.Id);

            item.Should().NotBeSameAs(result2);
        }
        public void When_object_changed_modified_event_should_be_called()
        {
            var subscriber = Substitute.For<IDummySubscriber>();
            var item = new
            {
                Id = AutoFixture.Create<string>(),
                Str = AutoFixture.Create<string>(),
                Int = AutoFixture.Create<int>(),
                Obj = new
                {
                    Value = AutoFixture.Create<string>()
                }
            };
            var jObject = JObject.FromObject(item);
            var collection = new StreamCollection(_name);
            collection.Added(item.Id, jObject);
            collection.Modified += subscriber.React;

            // Act
            collection.Changed(item.Id, jObject);

            // Assert
            subscriber.Received()
                      .React(Arg.Any<object>(), Arg.Is<StreamCollectionEventArgs>(args =>
                          args.ModificationType == ModificationType.Changed && args.Result == jObject
                          ));
        }
        public void When_an_empty_object_is_given_for_change_update_nothing()
        {
            var item = new
            {
                Id = AutoFixture.Create<string>(),
                Str = AutoFixture.Create<string>(),
                Int = AutoFixture.Create<int>(),
                Obj = new
                {
                    Value = AutoFixture.Create<string>()
                }
            };

            var other = new
            {
            };

            var collection = new StreamCollection(_name);

            // Act
            collection.Added(item.Id, JObject.FromObject(item));
            collection.Changed(item.Id, JObject.FromObject(other));

            // Assert
            var result = collection.GetAnonymousTypeById(item.Id, item);

            result.Should().Be(item);
        }
        public void When_object_is_changed_merge_existing_values()
        {
            var item = new
            {
                Id = AutoFixture.Create<string>(),
                Str = AutoFixture.Create<string>(),
                Int = AutoFixture.Create<int>(),
                Obj = new
                {
                    OldValue = AutoFixture.Create<string>(),
                    Value = AutoFixture.Create<string>()
                }
            };

            var other = new
            {
                Str = AutoFixture.Create<string>(),
                Int = AutoFixture.Create<int>(),
                Obj = new
                {
                    OldVaue = item.Obj.OldValue,
                    Value = AutoFixture.Create<string>(),
                    NewValue = AutoFixture.Create<string>()
                },
                NewValue = AutoFixture.Create<string>()
            };

            var collection = new StreamCollection(_name);

            // Act
            collection.Added(item.Id, JObject.FromObject(item));
            collection.Changed(item.Id, JObject.FromObject(other));

            // Assert
            var result = collection.GetAnonymousTypeById(item.Id, other);

            result.Int.Should().Be(other.Int);
            result.Str.Should().Be(other.Str);
            result.Obj.Should().Be(other.Obj);

            other.Should().NotBeSameAs(result);

            var result2 = collection.GetAnonymousTypeById(item.Id, item);
            result2.Id.Should().Be(item.Id);

            item.Should().NotBeSameAs(result2);
        }