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 Disjuncted_conditions_on_multiple_tables_can_be_specified_in_a_single_join_query() { 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, 4, 1), }; var post2 = new BlogPost { Title = "Blog post 2", Content = "Post 2 content", PublishDate = new DateTime(2011, 2, 2), }; var post3 = new BlogPost { Title = "Blog post 3", Content = "Post 3 content", PublishDate = new DateTime(2011, 3, 3), }; var post4 = new BlogPost { Title = "Blog post 4", Content = "Post 4 content", PublishDate = new DateTime(2011, 4, 4), }; blog1.AddPost(post1); blog2.AddPost(post2); blog2.AddPost(post3); blog2.AddPost(post4); Repository.Insert(blog1, blog2); Repository.Insert(post1, post2, post3, post4); var actualBlogs = Repository.Find<Blog>() .Where(x => x.Name == "Blog 1") .OrWhere<BlogPost>(x => x.Title == "Blog post 2") .OrderBy(x => x.Name) .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog) .ExecuteList(); // Change the original objects to the expected state for the retrieved objects, so that // we can use Equals to compare the actual result with what is expected blog2.Posts.Remove(post3); blog2.Posts.Remove(post4); Assert.AreEqual(2, actualBlogs.Count); Assert.AreEqual(blog1, actualBlogs[0]); Assert.AreEqual(blog2, actualBlogs[1]); }
public void Setting_the_convention_on_one_repository_instance_does_not_change_the_convention_of_another_repository_instance() { var bookRepository = CreateRepository(new BookConvention()); var blogRepository = CreateRepository(new BlogConvention()); var book = new Book { Isbn = "1", AuthorName = "Author Name", Title = "Title 1", PageCount = 100, }; var user = new User { Id = Guid.NewGuid(), Username = "******", Password = "******" }; var blog = new Blog { Name = "Blog" }; var post = new BlogPost { Title = "Blog post 1", Content = "Post 1 content", PublishDate = new DateTime(2011, 1, 1), }; blog.AddPost(post); blogRepository.Insert(blog); blogRepository.Insert(post); bookRepository.Insert(book); Repository.Insert(user); var actualUser = Repository.Find<User>().Where(x => x.Id == user.Id).Execute(); var actualBook = bookRepository.Find<Book>().Where(x => x.Isbn == book.Isbn).Execute(); var actualPost = blogRepository.Find<BlogPost>().Where(x => x.Id == post.Id).Execute(); Assert.AreEqual(book, actualBook); Assert.AreEqual(post.Id, actualPost.Id); // Only check id to avoid equals comparison of Blog Assert.AreEqual(user, actualUser); }
public void Conjuncted_conditions_on_multiple_aliased_tables_can_be_specified_in_a_single_join_query() { 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, 4, 1), }; var post2 = new BlogPost { Title = "Blog post 2", Content = "Post 2 content", PublishDate = new DateTime(2011, 2, 2), }; var post3 = new BlogPost { Title = "Blog post 3", Content = "Post 3 content", PublishDate = new DateTime(2011, 3, 3), }; var post4 = new BlogPost { Title = "Blog post 4", Content = "Post 4 content", PublishDate = new DateTime(2011, 4, 4), }; blog1.AddPost(post1); blog2.AddPost(post2); blog2.AddPost(post3); blog2.AddPost(post4); Repository.Insert(blog1, blog2); Repository.Insert(post1, post2, post3, post4); var actualPosts = Repository.Find<BlogPost>() .Where(x => x.PublishDate > new DateTime(2011, 3, 1)) .AndWhere<Blog>(null, x => x.Name == "Blog 2") .OrderBy(x => x.Title) .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog) .ExecuteList(); Assert.AreEqual(2, actualPosts.Count); Assert.AreEqual(post3, actualPosts[0]); Assert.AreEqual(post4, actualPosts[1]); }
public void Partial_selects_can_be_specified_for_multiple_tables_in_a_join() { Repository.DefaultConvention = new BlogConvention(); var blog1 = new Blog { Name = "Blog 1", }; var post1 = new BlogPost { Title = "Blog post 1", Content = "Post 1 content", PublishDate = new DateTime(2011, 2, 2), }; blog1.AddPost(post1); Repository.Insert(blog1); Repository.Insert(post1); var actualBlogs = Repository.Find<Blog>() .Select(x => x.Id) .Select<BlogPost>(x => x.Id, x => x.Title) .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog) .ExecuteList(); Assert.AreEqual(1, actualBlogs.Count); Assert.AreEqual(blog1.Id, actualBlogs[0].Id); Assert.IsNull(actualBlogs[0].Name); Assert.AreEqual(1, actualBlogs[0].Posts.Count); var actualPost = actualBlogs[0].Posts[0]; Assert.AreEqual(post1.Id, actualPost.Id); Assert.AreEqual("Blog post 1", actualPost.Title); Assert.IsNull(actualPost.Content); Assert.AreEqual(new DateTime(), actualPost.PublishDate); Assert.IsNull(actualPost.Author); Assert.AreSame(actualBlogs[0], actualPost.Blog); CollectionAssert.IsEmpty(actualPost.Comments); }
public void Conditions_on_multiple_tables_are_combined_with_the_expected_operator_precedence_when_specifying_conditions_alternating_between_tables() { Repository.IsEntityCachingEnabled = true; 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 post1 = new BlogPost { Title = "Blog post 1", Content = "Post 1 content", PublishDate = new DateTime(2011, 2, 2), }; var post2 = new BlogPost { Title = "Blog post 2", Content = "Post 2 content", PublishDate = new DateTime(2011, 2, 2), }; var post3 = new BlogPost { Title = "Blog post 3", Content = "Post 3 content", PublishDate = new DateTime(2011, 3, 3), }; var post4 = new BlogPost { Title = "Blog post 4", Content = "Post 4 content", PublishDate = new DateTime(2011, 2, 2), }; var post5 = new BlogPost { Title = "Blog post 5", Content = "Post 5 content", PublishDate = new DateTime(2011, 5, 5), }; blog1.AddPost(post1); blog2.AddPost(post2); blog2.AddPost(post3); blog3.AddPost(post4); blog1.AddPost(post5); Repository.Insert(blog1, blog2, blog3); Repository.Insert(post1, post2, post3, post4); var actualBlogs = Repository.Find<Blog>() .Where(x => x.Name == "Blog 1") .AndWhere<BlogPost>(x => x.PublishDate == new DateTime(2011, 2, 2)) .OrWhere(x => x.Name == "Blog 2") .OrderBy(x => x.Name) .OrderBy<BlogPost>(x => x.Title) .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog) .ExecuteList(); // Change the original objects to the expected state for the retrieved objects, so that // we can use Equals to compare the actual result with what is expected blog1.Posts.Remove(post5); Assert.AreEqual(2, actualBlogs.Count); Assert.AreEqual(blog1, actualBlogs[0]); Assert.AreEqual(blog2, actualBlogs[1]); }
public void Order_by_clauses_can_be_specified_for_multiple_tables_in_a_join() { 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, 2, 3), }; var post2 = new BlogPost { Title = "Blog post 2", Content = "aaa", PublishDate = new DateTime(2011, 2, 2), }; var post3 = new BlogPost { Title = "Blog post 3", Content = "zzz", PublishDate = new DateTime(2011, 2, 2), }; var post4 = new BlogPost { Title = "Blog post 4", Content = "Post 4 content", PublishDate = new DateTime(2011, 2, 1), }; // Add the posts in the expected sort order to enable comparisons to be made correctly during the asserts blog1.AddPost(post2); blog1.AddPost(post3); blog1.AddPost(post1); blog2.AddPost(post4); Repository.Insert(blog1, blog2); Repository.Insert(post1, post2, post3, post4); var actualBlogs = Repository.Find<Blog>() .OrderByDescending(x => x.Name) .OrderBy<BlogPost>(x => x.PublishDate, x => x.Content) .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog) .ExecuteList(); Assert.AreEqual(2, actualBlogs.Count); Assert.AreEqual(blog2, actualBlogs[0]); Assert.AreEqual(blog1, actualBlogs[1]); Assert.AreEqual(1, actualBlogs[0].Posts.Count); Assert.AreEqual(post4, actualBlogs[0].Posts[0]); Assert.AreEqual(3, actualBlogs[1].Posts.Count); Assert.AreEqual(post2, actualBlogs[1].Posts[0]); Assert.AreEqual(post3, actualBlogs[1].Posts[1]); Assert.AreEqual(post1, actualBlogs[1].Posts[2]); }
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]); }
public virtual void Many_to_one_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 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) }; blog1.AddPost(post1); blog1.AddPost(post2); blog2.AddPost(post3); Repository.Insert(blog1, blog2); Repository.Insert(post1, post2, post3); var actualBlog1 = Repository.Find<Blog>().Where(x => x.Name == "Blog 1") .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog) .OrderBy<BlogPost>(x => x.Title) .Execute(); Assert.AreEqual(blog1, actualBlog1); }