示例#1
0
        private BlogPost CreatePost(int index, Blog blog, IList<User> users, Random random)
        {
            var month = 12 - (index * 2) / 28;
            var day = 28 - (index * 2) % 28;
            var title = blog.Name + " Post " + index;
            var user = users[random.Next(users.Count)];

            var hour = random.Next(23);
            var minute = random.Next(59);
            var second = random.Next(59);

            var post = new BlogPost
                {
                    Title = title,
                    PublishDate = new DateTime(2011, month, day, hour, minute, second),
                    Author = user,
                    Blog = blog
                };

            for (int i = 0; i < random.Next(10); i++)
            {
                var comment = new Comment
                    {
                        Content = "Comment " + i,
                        PublishDate = new DateTime(2011, month, day, random.Next(23), random.Next(59), random.Next(59))
                    };

                post.AddComment(comment);
            }

            post.Content = post.Title + " content";

            return post;
        }
示例#2
0
 public void AddComment(Comment comment)
 {
     Comments.Add(comment);
     comment.BlogPost = this;
 }
        public void Different_relations_on_same_entity_can_be_loaded_in_separate_queries_using_caching_repository()
        {
            Repository.IsEntityCachingEnabled = true;

            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 1, 1),
                };

            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "Post 2 content",
                    PublishDate = new DateTime(2011, 2, 2),
                };

            var comment1 = new Comment
                {
                    Content = "Comment 1",
                    PublishDate = new DateTime(2011, 1, 5)
                };

            var comment2 = new Comment
                {
                    Content = "Comment 2",
                    PublishDate = new DateTime(2011, 1, 6)
                };

            var comment3 = new Comment
                {
                    Content = "Comment 2",
                    PublishDate = new DateTime(2011, 1, 6)
                };

            var user = new User
                {
                    Username = "******",
                    Password = "******"
                };

            user.AddBlogPost(post1);
            user.AddBlogPost(post2);

            blog1.AddPost(post1);
            blog2.AddPost(post2);

            post1.AddComment(comment1);
            post1.AddComment(comment2);
            post2.AddComment(comment3);

            Repository.Insert(user);
            Repository.Insert(blog1, blog2);
            Repository.Insert(post1, post2);
            Repository.Insert(comment1, comment2, comment3);

            var actualPosts = Repository.Find<BlogPost>()
                                        .OrderBy(x => x.Title)
                                        .OrderBy<Comment>(x => x.Content)
                                        .Join<BlogPost, Comment>(x => x.Comments, x => x.BlogPost)
                                        .ExecuteList();

            var firstActualPost1 = Repository.Find<BlogPost>()
                                             .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                             .Where(x => x.Id == post1.Id)
                                             .OrderBy(x => x.Title)
                                             .Execute();

            var secondActualPost1 = Repository.Find<BlogPost>()
                                              .Join<User, BlogPost>(x => x.BlogPosts, x => x.Author)
                                              .Where(x => x.Id == post1.Id)
                                              .Execute();

            // Set up the original entities according to the expected result
            post2.Author = null;
            post2.Blog = null;

            Assert.AreEqual(2, actualPosts.Count);

            Assert.AreEqual(post1, actualPosts[0]);
            Assert.AreEqual(post2, actualPosts[1]);

            Assert.AreSame(firstActualPost1, actualPosts[0]);
            Assert.AreSame(secondActualPost1, actualPosts[0]);
        }
        public void One_way_child_to_parent_relationship_of_a_joined_table_can_be_loaded_with_another_join()
        {
            Repository.Convention = new BlogConvention();

            var user1 = new User("kalle", "password");
            var user2 = new User("pelle", "password");
            var blog = new Blog("blog 1");

            Repository.Insert(blog);
            Repository.Insert(user1, user2);

            var post1 = new BlogPost("title 1", "content 1") { Blog = blog };
            var post2 = new BlogPost("title 2", "content 2") { Blog = blog };

            Repository.Insert(post1, post2);

            var comment1 = new Comment("comment 1") { User = user1, BlogPost = post1 };
            var comment2 = new Comment("comment 2") { User = user2, BlogPost = post1 };
            var comment3 = new Comment("comment 3") { User = user1, BlogPost = post2 };

            Repository.Insert(comment1, comment2, comment3);

            var actualPost = Repository.Find<BlogPost>()
                                  .Where(x => x.Id == post1.Id)
                                  .OrderBy<Comment>(x => x.Content)
                                  .Join(x => x.Comments, x => x.BlogPost)
                                  .Join<User, Comment>(x => x.User)
                                  .Execute();

            Assert.AreEqual("title 1", actualPost.Title);
            Assert.AreEqual("comment 1", actualPost.Comments[0].Content);
            Assert.AreEqual("comment 2", actualPost.Comments[1].Content);

            Assert.AreEqual(2, actualPost.Comments.Count);
            Assert.IsNotNull(actualPost.Comments[0].User);
            Assert.IsNotNull(actualPost.Comments[1].User);
            Assert.AreEqual("kalle", actualPost.Comments[0].User.Username);
            Assert.AreEqual("pelle", actualPost.Comments[1].User.Username);
        }
        public virtual void Multi_level_relationship_directed_from_middle_level_out_to_parent_and_to_child_can_be_read_with_two_way_relations()
        {
            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 1, 1),
                };

            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "Post 2 content",
                    PublishDate = new DateTime(2011, 1, 2)
                };

            var post3 = new BlogPost
                {
                    Title = "Blog post 3",
                    Content = "Post 3 content",
                    PublishDate = new DateTime(2011, 1, 3)
                };

            var comment1 = new Comment
                {
                    Content = "Comment 1",
                    PublishDate = new DateTime(2011, 1, 5)
                };

            var comment2 = new Comment
                {
                    Content = "Comment 2",
                    PublishDate = new DateTime(2011, 1, 6)
                };

            var comment3 = new Comment
                {
                    Content = "Comment 3",
                    PublishDate = new DateTime(2011, 1, 7)
                };

            var comment4 = new Comment
                {
                    Content = "Comment 4",
                    PublishDate = new DateTime(2011, 1, 8)
                };

            var comment5 = new Comment
                {
                    Content = "Comment 5",
                    PublishDate = new DateTime(2011, 1, 9)
                };

            blog1.AddPost(post1);
            blog1.AddPost(post2);
            blog2.AddPost(post3);

            post1.AddComment(comment1);
            post1.AddComment(comment2);
            post1.AddComment(comment3);
            post2.AddComment(comment4);
            post2.AddComment(comment5);

            Repository.Insert(blog1, blog2);
            Repository.Insert(post1, post2, post3);
            Repository.Insert(comment1, comment2, comment3, comment4, comment5);

            var actualBlogPosts = Repository.Find<BlogPost>().Where(x => x.PublishDate >= new DateTime(2011, 1, 2))
                                            .OrderBy(x => x.PublishDate)
                                            .OrderBy<Comment>(x => x.Content)
                                            .Join<BlogPost, Comment>(x => x.Comments, x => x.BlogPost)
                                            .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                            .ExecuteList();

            Assert.AreEqual(2, actualBlogPosts.Count);

            Assert.AreEqual(post2, actualBlogPosts[0]);
            Assert.AreEqual(post3, actualBlogPosts[1]);

            // Post1 does not match the filter condition of the query, so that won't be in the result
            // Hence, remove that post from the expected blog, so that we can use the Equals method
            // to compare the expected with the actual
            blog1.Posts.Remove(post1);

            Assert.AreEqual(blog1, actualBlogPosts[0].Blog);
            Assert.AreEqual(blog2, actualBlogPosts[1].Blog);

            var actualBlog1 = actualBlogPosts[0].Blog;
            var actualBlog2 = actualBlogPosts[1].Blog;

            Assert.AreEqual(1, actualBlog1.Posts.Count);
            Assert.AreEqual(1, actualBlog2.Posts.Count);

            Assert.AreSame(actualBlogPosts[0], actualBlog1.Posts.First());
            Assert.AreSame(actualBlogPosts[1], actualBlog2.Posts.First());

            actualBlogPosts[0].Comments.ToList().ForEach(x => Assert.AreSame(actualBlogPosts[0], x.BlogPost));
            actualBlogPosts[1].Comments.ToList().ForEach(x => Assert.AreSame(actualBlogPosts[1], x.BlogPost));
        }
        public virtual void Multi_level_relationship_directed_from_child_to_parent_can_be_written_and_read_back_in_single_query()
        {
            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 1, 1),
                };

            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "Post 2 content",
                    PublishDate = new DateTime(2011, 1, 2)
                };

            var post3 = new BlogPost
                {
                    Title = "Blog post 3",
                    Content = "Post 3 content",
                    PublishDate = new DateTime(2011, 1, 3)
                };

            var comment1 = new Comment
                {
                    Content = "Comment 1",
                    PublishDate = new DateTime(2011, 1, 5)
                };

            var comment2 = new Comment
                {
                    Content = "Comment 2",
                    PublishDate = new DateTime(2011, 1, 6)
                };

            var comment3 = new Comment
                {
                    Content = "Comment 3",
                    PublishDate = new DateTime(2011, 1, 7)
                };

            var comment4 = new Comment
                {
                    Content = "Comment 4",
                    PublishDate = new DateTime(2011, 1, 8)
                };

            var comment5 = new Comment
                {
                    Content = "Comment 5",
                    PublishDate = new DateTime(2011, 1, 9)
                };

            blog1.AddPost(post1);
            blog1.AddPost(post2);
            blog2.AddPost(post3);

            post1.AddComment(comment1);
            post1.AddComment(comment2);
            post1.AddComment(comment3);
            post2.AddComment(comment4);
            post2.AddComment(comment5);

            Repository.Insert(blog1, blog2);
            Repository.Insert(post1, post2, post3);
            Repository.Insert(comment1, comment2, comment3, comment4, comment5);

            var actualComments = Repository.Find<Comment>().Where(x => x.PublishDate >= new DateTime(2011, 1, 6))
                                           .OrderBy(x => x.PublishDate)
                                           .Join<BlogPost, Comment>(x => x.Comments, x => x.BlogPost)
                                           .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                           .ExecuteList();

            // Since comment1 has a PublishDate before 2011-01-06 it will not be returned by the query.
            // Because of this the instance of post1 returned will not have comment1 in its Comments collection.
            // So, to make the asserts correct. That comment is removed from the original post1.
            post1.Comments.Remove(comment1);

            Assert.AreEqual(4, actualComments.Count);

            Assert.AreEqual(comment2, actualComments[0]);
            Assert.AreEqual(comment3, actualComments[1]);
            Assert.AreEqual(comment4, actualComments[2]);
            Assert.AreEqual(comment5, actualComments[3]);

            Assert.AreEqual(post1, actualComments[0].BlogPost);
            Assert.AreEqual(post2, actualComments[2].BlogPost);

            Assert.AreEqual(blog1, actualComments[0].BlogPost.Blog);

            Assert.AreSame(actualComments[0].BlogPost, actualComments[1].BlogPost);
            Assert.AreSame(actualComments[2].BlogPost, actualComments[3].BlogPost);

            Assert.AreSame(actualComments[0].BlogPost.Blog, actualComments[1].BlogPost.Blog);
            Assert.AreSame(actualComments[0].BlogPost.Blog, actualComments[2].BlogPost.Blog);
            Assert.AreSame(actualComments[0].BlogPost.Blog, actualComments[3].BlogPost.Blog);
        }
        public virtual void Multi_level_relationship_can_be_written_and_read_back_again_in_single_query_using_join()
        {
            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var blog3 = new Blog
                {
                    Name = "Blog 3",
                };

            var blog4 = new Blog
                {
                    Name = "Blog 4",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 1, 1),
                };

            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "Post 2 content",
                    PublishDate = new DateTime(2011, 1, 2)
                };

            var post3 = new BlogPost
                {
                    Title = "Blog post 3",
                    Content = "Post 3 content",
                    PublishDate = new DateTime(2011, 1, 3)
                };

            var post4 = new BlogPost
                {
                    Title = "Blog post 4",
                    Content = "Post 4 content",
                    PublishDate = new DateTime(2011, 1, 4)
                };

            var comment1 = new Comment
                {
                    Content = "Comment 1",
                    PublishDate = new DateTime(2011, 1, 5)
                };

            var comment2 = new Comment
                {
                    Content = "Comment 2",
                    PublishDate = new DateTime(2011, 1, 6)
                };

            var comment3 = new Comment
                {
                    Content = "Comment 3",
                    PublishDate = new DateTime(2011, 1, 7)
                };

            blog1.AddPost(post1);
            blog1.AddPost(post2);
            blog2.AddPost(post3);
            blog4.AddPost(post4);

            post1.AddComment(comment1);
            post2.AddComment(comment2);
            post2.AddComment(comment3);

            Repository.Insert(blog1, blog2, blog3, blog4);
            Repository.Insert(post1, post2, post3, post4);
            Repository.Insert(comment1, comment2, comment3);

            var actualBlogs = Repository.Find<Blog>().Where(x => x.Name == "Blog 1" || x.Name.EndsWith("3"))
                                        .OrderBy(x => x.Name)
                                        .OrderBy<BlogPost>(x => x.Title)
                                        .OrderBy<Comment>(x => x.Content)
                                        .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                        .Join<BlogPost, Comment>(x => x.Comments, x => x.BlogPost)
                                        .ExecuteList();

            Assert.AreEqual(2, actualBlogs.Count);

            Assert.AreEqual(blog1, actualBlogs[0]);
            Assert.AreEqual(blog3, actualBlogs[1]);
        }