public void Assign_ShouldAssignIdOfBlog()
        {
            // Arrange
            var blog = new Blog {Id = Guid.Empty};

            // Act
            target.Assign(blog);

            // Assert
            blog.Id.Should().NotBe(Guid.Empty);
        }
        public void ShouldPopulateCollectionBasedReference()
        {
            var context = new InMemoryDataContext();

            var child = new Post();
            var blog = new Blog("Test");
            child.Blog = blog;

            context.Add(child);
            context.Commit();

            Assert.AreEqual(1, blog.Posts.Count(x => x == child));
        }
        public void ShouldPopulateSingleReference()
        {
            var context = new InMemoryDataContext();

            var child = new Post();
            var blog = new Blog("Test");
            blog.Posts.Add(child);

            context.Add(blog);
            context.Commit();

            Assert.IsNotNull(child.Blog);
        }
        public void Add_ShouldStoreBlogWithAuthor()
        {
            //arrange 
            var blog = new Blog
            {
                Author = new Author()
            };

            //act
            _context.Add(blog);

            //assert
            _context.AsQueryable<Author>().Count().Should().Be(1);
            _context.AsQueryable<Author>().First().Should().BeSameAs(blog.Author);
        }
        public void ShouldPopulateBackReferenceCollections()
        {
            // Arrange
            var context = new InMemoryDataContext();
            var blog = new Blog();
            var post = new Post { Blog = blog };
            context.Add(post);
            context.Commit();

            // Act
            var fetchedBlog = context.AsQueryable<Blog>().First();

            // Assert
            fetchedBlog.Posts.Count().Should().Be(1);
        }
 public void ShouldPerformCommitsBetterThan10ms()
 {
     //Arrange
     Stopwatch sw = Stopwatch.StartNew();
     for (int i = 0; i < 100; i++)
     {
         var blog = new Blog();
         _context.Add(blog);
         blog.Posts.Add(new Post());
         _context.Commit();
     }
     sw.Stop();
     var averageInsert = sw.ElapsedMilliseconds/1000;
     averageInsert.Should().BeLessOrEqualTo(10);
     Console.WriteLine("Average Time for insert of graph is {0}", averageInsert);
 }
        public void ShouldPopulateBackReferenceSingleProperty()
        {
            // Arrange
            var context = new InMemoryDataContext();
            var blog = new Blog()
            {
                Posts = new List<Post>()
                {
                    new Post()
                }
            };
            context.Add(blog);
            context.Commit();

            // Act
            var fetchedPost = context.AsQueryable<Post>().First();

            // Assert
            fetchedPost.Blog.Should().NotBeNull();
        }
        public void Commit_ShouldAddNewLeafMembers()
        {
            //Arrange
            var blog = new Blog();
            _context.Add(blog);
            _context.Commit();

            //Act
            blog.Posts.Add(new Post());
            _context.Commit();

            //Assert
            _context.AsQueryable<Post>().Count().Should().Be(1);
        }
        public void Commit_ShouldRemoveOrphanedMembers()
        {
            // Arrange
            var blog = new Blog();
            var site = new Site {Blog = blog};
            _context.Add(site);
            _context.Commit();
            site.Blog = null;

            // Act
            _context.Commit();

            // Assert
            _context.AsQueryable<Blog>().Should().NotContain(blog);
            _context.AsQueryable<Blog>().Count().Should().Be(0);
        }
        public void Commit_ShouldRemoveOrphanedCollectionMembersWhenWholeCollectionRemoved()
        {
            // Arrange
            var post1 = new Post();
            var post2 = new Post();
            var blog = new Blog
            {
                Posts = new List<Post> {post1, post2}
            };
            _context.Add(blog);
            _context.Commit();
            blog.Posts = null;

            // Act
            _context.Commit();

            // Assert
            _context.AsQueryable<Post>().Should().NotContain(post1);
            _context.AsQueryable<Post>().Should().NotContain(post2);
            _context.AsQueryable<Post>().Count().Should().Be(0);
        }
        public void Remove_ShouldRemoveFromParentButNotDeleteChildObjectsThatAreReferencedMoreThanOne()
        {
            //arrange 

            var post1 = new Post();
            var post2 = new Post();
            var blog1 = new Blog
            {
                Posts = new List<Post> {post1, post2}
            };
            var blog2 = new Blog
            {
                Posts = new List<Post> {post1}
            };
            var site = new Site
            {
                Blog = blog2
            };
            _context.Add(blog1);
            _context.Add(site);

            // Act
            _context.Remove(blog2);

            // Assert
            _context.AsQueryable<Post>().Count().Should().Be(2);
            _context.AsQueryable<Post>().First().Should().BeSameAs(post1);
            _context.AsQueryable<Blog>().Single().Posts.Count().Should().Be(2);
            site.Blog.Should().BeNull();
        }
        public void Remove_ShouldNotRemoveIfReferencedByAnotherCollection()
        {
            // Arrange
            var post1 = new Post();
            var post2 = new Post();
            var blog1 = new Blog
            {
                Posts = new List<Post> {post1, post2}
            };
            var blog2 = new Blog
            {
                Posts = new List<Post> {post1}
            };
            _context.Add(blog1);
            _context.Add(blog2);

            // Act
            _context.Remove(post2);

            // Assert
            _context.AsQueryable<Post>().Count().Should().Be(1);
            _context.AsQueryable<Post>().First().Should().BeSameAs(post1);
            _context.AsQueryable<Blog>().Where(b => b.Posts.Count > 1).Count().Should().Be(0);
        }
        public void Remove_ShouldRemoveObjectFromRelatedCollection()
        {
            //arrange 
            var post = new Post();
            var blog = new Blog {Posts = new List<Post> {post}};
            _context.Add(blog);

            //act
            _context.Remove(post);

            //assert
            _context.AsQueryable<Blog>().Count().Should().Be(1);
            _context.AsQueryable<Blog>().First().Posts.Count().Should().Be(0);
            _context.AsQueryable<Post>().Count().Should().Be(0);
        }
        public void Add_ShouldIgnoreNullCollections()
        {
            //arrange 
            var blog = new Blog
            {
                Posts = null
            };

            //act
            _context.Add(blog);

            //assert
            _context.AsQueryable<Post>().Count().Should().Be(0);
        }
        public void Add_ShouldNotAddAnItemTwiceForMultipleReferences()
        {
            //arrange 
            var post = new Post();
            var blog = new Blog
            {
                Posts = new List<Post> {post, new Post()}
            };
            var blog2 = new Blog
            {
                Posts = new List<Post> {post}
            };
            _context.Add(blog);

            //act
            _context.Add(blog2);

            //assert
            _context.AsQueryable<Post>().Count().Should().Be(2);
        }
        public void Add_ShouldUseIdentityForRelatedTypes()
        {
            //Arrange
            _context.RegisterIdentityStrategy(new GuidIdentityStrategy<Author>(x => x.Id));
            var blog = new Blog();
            blog.Author = new Author {Id = Guid.Empty};

            //Act
            _context.Add(blog);

            //Assert
            blog.Author.Id.Should().NotBe(Guid.Empty);
        }
        public void Commit_ShouldUseIdentityForRelatedCollectionTypes()
        {
            //Arrange
            _context.RegisterIdentityStrategy(new IntegerIdentityStrategy<Post>(x => x.Id));
            var blog = new Blog();
            _context.Add(blog);
            _context.Commit();
            blog.Posts.Add(new Post {Id = 0});

            //Act
            _context.Commit();

            //Assert
            blog.Posts.Single().Id.Should().NotBe(0);
        }
        public void Remove_ShouldRemoveDependentGraphOnBranchRemoval()
        {
            //arrange 
            var post = new Post();
            var blog = new Blog
            {
                Posts = new List<Post> {new Post(), post}
            };
            var site = new Site
            {
                Blog = blog
            };
            _context.Add(site);

            //act
            _context.Remove(blog);

            //assert
            IQueryable<Post> posts = _context.AsQueryable<Post>();
            posts.Count().Should().Be(0);
        }
        public void Add_ShouldIncludeAllRelatedItemsFromRelatedCollections()
        {
            //arrange 
            var blog = new Blog
            {
                Posts = new List<Post> {new Post(), new Post()}
            };

            //act
            _context.Add(blog);

            //assert
            _context.AsQueryable<Post>().Count().Should().Be(2);
        }
        public void Remove_ShouldNotRemoveIfReferencedByAnotherObject()
        {
            // Arrange
            var blog = new Blog();
            var site1 = new Site {Blog = blog};
            var site2 = new Site {Blog = blog};
            _context.Add(site1);
            _context.Add(site2);

            // Act
            _context.Remove(site1);

            // Assert
            _context.AsQueryable<Site>().Count().Should().Be(1);
            _context.AsQueryable<Site>().First().Should().BeSameAs(site2);
            _context.AsQueryable<Blog>().Count().Should().Be(1);
            _context.AsQueryable<Blog>().First().Should().BeSameAs(blog);
        }