public MockFamily()
 {
     var child1 = new Child("Child1") { Children = new List<Child>(CreateGrandChildren("Child1")) };
     var child2 = new Child("Child2") { Children = new List<Child>(CreateGrandChildren("Child2")) };
     var child3 = new Child("Child3") { Children = new List<Child>(CreateGrandChildren("Child3")) };
     Parent = new Parent("Parent")
         {
             Children = new List<Child> { child1, child2, child3 }
         };
 }
        public void Apply_Changes_Should_Mark_Deleted_Parent()
        {
            // Arrange
            var context = TestsHelper.CreateFamilyDbContext(CreateFamilyDbOptions);
            var parent = new Parent("Parent");
            parent.TrackingState = TrackingState.Deleted;

            // Act
            context.ApplyChanges(parent);

            // Assert
            Assert.AreEqual(EntityState.Deleted, context.Entry(parent).State);
        }
        public void Apply_Changes_Should_Mark_Modified_Parent_Properties()
        {
            // Arrange
            var context = TestsHelper.CreateFamilyDbContext(CreateFamilyDbOptions);
            var parent = new Parent("Parent");
            parent.Name += "_Changed";
            parent.TrackingState = TrackingState.Modified;
            parent.ModifiedProperties = new List<string> {"Name"};

            // Act
            context.ApplyChanges(parent);

            // Assert
            Assert.AreEqual(EntityState.Modified, context.Entry(parent).State);
            Assert.IsTrue(context.Entry(parent).Property("Name").IsModified);
        }
        public void Apply_Changes_Should_Mark_Added_Parent_With_Children()
        {
            // Arrange
            var context = TestsHelper.CreateFamilyDbContext(CreateFamilyDbOptions);
            var parent = new Parent("Parent")
            {
                Children = new List<Child>
                    {
                        new Child("Child1"), 
                        new Child("Child2"),
                        new Child("Child3")
                    }
            };
            parent.TrackingState = TrackingState.Added;
            parent.Children[0].TrackingState = TrackingState.Added;
            parent.Children[1].TrackingState = TrackingState.Added;
            parent.Children[2].TrackingState = TrackingState.Added;

            // Act
            context.ApplyChanges(parent);

            // Assert
            Assert.AreEqual(EntityState.Added, context.Entry(parent).State);
            Assert.AreEqual(EntityState.Added, context.Entry(parent.Children[0]).State);
            Assert.AreEqual(EntityState.Added, context.Entry(parent.Children[1]).State);
            Assert.AreEqual(EntityState.Added, context.Entry(parent.Children[2]).State);
        }
        public void Apply_Changes_Should_Mark_Parent_With_Added_Modified_Deleted_Children()
        {
            // Arrange
            var context = TestsHelper.CreateFamilyDbContext(CreateFamilyDbOptions);
            var child1 = new Child("Child1");
            var child2 = new Child("Child2");
            var child3 = new Child("Child3");
            var parent = new Parent("Parent")
            {
                Children = new List<Child> { child1, child2, child3 }
            };
            child1.TrackingState = TrackingState.Added;
            child2.TrackingState = TrackingState.Modified;
            child3.TrackingState = TrackingState.Deleted;

            // Act
            context.ApplyChanges(parent);

            // Assert
            Assert.AreEqual(EntityState.Unchanged, context.Entry(parent).State);
            Assert.AreEqual(EntityState.Added, context.Entry(child1).State);
            Assert.AreEqual(EntityState.Modified, context.Entry(child2).State);
            Assert.AreEqual(EntityState.Deleted, context.Entry(child3).State);
        }