public virtual void An_object_can_be_inserted_into_the_database_and_read_back_via_a_query_on_the_given_id()
        {
            var user = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };

            Repository.Insert(user);
            var actualUser = Repository.Find<User>().Where(x => x.Id == user.Id).Execute();

            Assert.AreEqual(user, actualUser);
        }
Example #2
0
        public void Join_example_with_transaction_insert()
        {
            Repository.DefaultConnectionString = @"Data source=.\SQLEXPRESS;Initial Catalog=WeenyMapper;Trusted_Connection=true";
            Repository.DefaultConvention = new BlogConvention();

            var myBlog = new Blog("My blog");

            var steve = new User("Steve", "password");

            var post1 = new BlogPost("Title 1", "Content 1 goes here") { Blog = myBlog, Author = steve };
            var post2 = new BlogPost("Title 2", "Content 2 goes here") { Blog = myBlog, Author = steve };
            var post3 = new BlogPost("Title 3", "Content 3 goes here") { Blog = myBlog, Author = steve };
            var post4 = new BlogPost("Title 4", "Content 4 goes here") { Blog = myBlog, Author = steve };

            using (var repository = new Repository())
            {
                using (var transaction = repository.BeginTransaction())
                {
                    repository.Insert(myBlog);
                    repository.Insert(steve);
                    repository.Insert(post1, post2, post3, post4);

                    transaction.Commit();
                }

                repository.Update<BlogPost>()
                          .Set(x => x.Title, "Updated title 2")
                          .Where(x => x.Id == post2.Id)
                          .Execute();

                repository.Delete<BlogPost>()
                          .Where(x => x.Id == post3.Id || x.Title == "Title 4")
                          .Execute();

                var actualBlog = repository.Find<Blog>().Where(x => x.Id == myBlog.Id)
                                           .Join(x => x.Posts, x => x.Blog)
                                           .OrderBy<BlogPost>(x => x.Title)
                                           .Execute();

                Assert.AreEqual("My blog", actualBlog.Name);
                Assert.AreEqual(2, actualBlog.Posts.Count);
                Assert.AreEqual("Title 1", actualBlog.Posts[0].Title);
                Assert.AreEqual("Updated title 2", actualBlog.Posts[1].Title);
            }
        }
        public void Alias_can_be_given_to_the_starting_table_of_a_query()
        {
            Repository.Convention = new BlogConvention();

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

            Repository.Insert(blog1);
            Repository.Insert(user1);

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

            Repository.Insert(post1, post2);

            var actualBlogs = Repository.Find<Blog>("blog_alias")
                                        .OrderBy<BlogPost>("blogpost_alias", x => x.Title)
                                        .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog, parentAlias: "blog_alias", childAlias: "blogpost_alias")
                                        .ExecuteList();

            Assert.AreEqual(1, actualBlogs.Count);
            Assert.AreEqual(2, actualBlogs[0].Posts.Count);
        }
Example #4
0
        private void CreateTestData()
        {
            var codeBlog = new Blog { Name = "Code" };
            var bookBlog = new Blog { Name = "Books" };
            var screencastBlog = new Blog { Name = "Screencasts" };
            var podcastBlog = new Blog { Name = "Podcasts" };

            var steve = new User { Username = "******", Password = "******", Id = Guid.NewGuid() };
            var john = new User { Username = "******", Password = "******", Id = Guid.NewGuid() };

            var users = new List<User> { steve, john };
            var posts = new List<BlogPost>();

            var random = new Random();

            for (int i = 0; i < 50; i++)
            {
                var post = CreatePost(i, codeBlog, users, random);
                posts.Add(post);
            }

            for (int i = 0; i < 21; i++)
            {
                var post = CreatePost(i, bookBlog, users, random);
                posts.Add(post);
            }

            for (int i = 0; i < 12; i++)
            {
                var post = CreatePost(i, screencastBlog, users, random);
                posts.Add(post);
            }

            for (int i = 0; i < 3; i++)
            {
                var post = CreatePost(i, podcastBlog, users, random);
                posts.Add(post);
            }

            var comments = posts.SelectMany(x => x.Comments);

            _repository.InsertCollection(users);
            _repository.Insert(codeBlog, bookBlog, screencastBlog, podcastBlog);
            _repository.InsertCollection(posts);
            _repository.InsertCollection(comments);
        }
Example #5
0
        private static void CreateBlogTestData()
        {
            Repository.DefaultConvention = new BlogConvention();

            var myBlog = new Blog("My blog");

            var steve = new User("Steve", "password");

            var post1 = new BlogPost("Title 1", "Content 1 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2011, 1, 1) };
            var post2 = new BlogPost("Title 2", "Content 2 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2012, 1, 5) };
            var post3 = new BlogPost("Title 3", "Content 3 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2012, 4, 1) };
            var post4 = new BlogPost("Title 4", "Content 4 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2013, 1, 1) };

            using (var repository = new Repository())
            {
                using (var transaction = repository.BeginTransaction())
                {
                    repository.Insert(myBlog);
                    repository.Insert(steve);
                    repository.Insert(post1, post2, post3, post4);

                    transaction.Commit();
                }
            }
        }
        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 virtual void Deleting_an_entity_deletes_the_corresponding_row_and_no_other_row()
        {
            var user1 = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };
            var user2 = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };

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

            Repository.Delete(user2);

            var actualUsers = Repository.Find<User>().ExecuteList();

            Assert.AreEqual(1, actualUsers.Count);
            Assert.AreEqual(user1, actualUsers[0]);
        }
        public void Less_than_or_equal_and_greater_than_or_equal_can_be_used_for_filtering()
        {
            Repository.Convention = new BlogConvention();

            var myBlog = new Blog("My blog");

            var steve = new User("Steve", "password");

            var post1 = new BlogPost("Title 1", "Content 1 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2011, 1, 1) };
            var post2 = new BlogPost("Title 2", "Content 2 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2012, 1, 5) };
            var post3 = new BlogPost("Title 3", "Content 3 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2012, 4, 1) };
            var post4 = new BlogPost("Title 4", "Content 4 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2013, 1, 1) };
            var post5 = new BlogPost("Title 5", "Content 5 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2013, 1, 2) };

            Repository.Insert(myBlog);
            Repository.Insert(steve);
            Repository.Insert(post1, post2, post3, post4, post5);

            var actualPosts = Repository.Find<BlogPost>()
                                        .Where(x => x.PublishDate >= new DateTime(2012, 1, 5) && x.PublishDate <= new DateTime(2013, 1, 1))
                                        .OrderBy(x => x.Title)
                                        .ExecuteList();

            Assert.AreEqual(3, actualPosts.Count);
            Assert.AreEqual("Title 2", actualPosts[0].Title);
            Assert.AreEqual("Title 3", actualPosts[1].Title);
            Assert.AreEqual("Title 4", actualPosts[2].Title);
        }
        public virtual void Subset_of_the_columns_of_a_table_can_be_read_by_specifying_a_target_type_which_contains_properties_matching_the_subset()
        {
            Repository.DefaultConvention = new UserConvention();

            var user = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };

            Repository.Insert(user);
            var actualUser = Repository.Find<PartialUser>().Where(x => x.Id == user.Id).Execute();

            Assert.AreEqual(user.Id, actualUser.Id);
            Assert.AreEqual("a username", actualUser.Username);
        }
        public virtual void Scalar_values_can_be_returned_for_find_query_matching_multiple_entities()
        {
            var user1 = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };

            var user2 = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };

            var user3 = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };

            Repository.Insert(user1, user2, user3);

            var usernames = Repository.Find<User>()
                                      .Where(x => x.Password == "a password")
                                      .Select(x => x.Username)
                                      .ExecuteScalarList<string>();

            Assert.AreEqual(2, usernames.Count);
            CollectionAssert.Contains(usernames, "username1");
            CollectionAssert.Contains(usernames, "username3");
        }
        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 virtual void Query_for_single_entity_without_any_match_returns_null()
        {
            var user = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };

            Repository.Insert(user);

            var nonExistingId = Guid.NewGuid();
            var actualUser = Repository.Find<User>().Where(x => x.Id == nonExistingId).Execute();

            Assert.IsNull(actualUser);
        }
        public void Properties_which_already_have_values_are_not_set_to_null_if_another_query_is_run_with_caching_enabled_where_those_properties_are_not_loaded()
        {
            Repository.Convention = new BlogConvention();
            Repository.IsEntityCachingEnabled = true;

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

            Repository.Insert(blog1, blog2);
            Repository.Insert(user1, user2);

            var post1 = new BlogPost("title 1", "content 1") { Blog = blog1, Author = user1 };
            var post2 = new BlogPost("title 2", "content 2") { Blog = blog2, Author = user2 };

            Repository.Insert(post1, post2);

            var actualPosts = Repository.Find<BlogPost>()
                                        .OrderBy<BlogPost>(x => x.Title)
                                        .Join(x => x.Blog)
                                        .ExecuteList();

            Repository.Find<BlogPost>()
                      .Select(x => x.Id)
                      .Select<User>(x => x.Id, x => x.Username)
                      .Join(x => x.Author)
                      .ExecuteList();

            Assert.AreEqual(2, actualPosts.Count);
            Assert.AreEqual("title 1", actualPosts[0].Title);
            Assert.AreEqual("title 2", actualPosts[1].Title);
            Assert.IsNotNull(actualPosts[0].Blog);
            Assert.IsNotNull(actualPosts[1].Blog);
            Assert.IsNotNull(actualPosts[0].Author);
            Assert.IsNotNull(actualPosts[1].Author);
            Assert.AreEqual("kalle", actualPosts[0].Author.Username);
            Assert.AreEqual("pelle", actualPosts[1].Author.Username);
            Assert.AreEqual("blog 1", actualPosts[0].Blog.Name);
            Assert.AreEqual("blog 2", actualPosts[1].Blog.Name);
        }
        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 void Single_equality_comparison_with_object_property_invocation_is_parsed_into_property_name_and_value()
        {
            var user = new User { Username = "******" };

            var expression = _parser.Parse<User>(x => x.Username == user.Username);

            var expectedExpression = new EqualsExpression(new PropertyExpression("Username", typeof(string)),
                                                          new ValueExpression("a username"));

            Assert.AreEqual(expectedExpression, expression);
        }
        public virtual void Updating_an_object_updates_the_database_entry_with_the_corresponding_id()
        {
            var user1 = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };
            var user2 = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };

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

            var updatedUser2 = new User
                {
                    Id = user2.Id,
                    Username = "******",
                    Password = "******"
                };

            Repository.Update(updatedUser2);

            var actualUser = Repository.Find<User>().Where(x => x.Id == updatedUser2.Id).Execute();

            Assert.AreEqual(updatedUser2, actualUser);
        }
        public void Single_equality_comparison_with_property_acces_on_lambda_parameter_is_parsed_into_property_name_and_value()
        {
            var user = new User { Username = "******" };

            Func<User, QueryExpression> func = u => _parser.Parse<User>(x => x.Username == u.Username);
            var expression = func(user);

            var expectedExpression = new EqualsExpression(new PropertyExpression("Username", typeof(string)),
                                                          new ValueExpression("a username"));

            Assert.AreEqual(expectedExpression, expression);
        }
        public virtual void Find_query_can_be_evaluated_to_a_single_scalar_value()
        {
            var user1 = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };

            var user2 = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };

            Repository.Insert(user1, user2);

            var actualUsername = Repository.Find<User>()
                                           .Where(x => x.Id == user2.Id)
                                           .Select(x => x.Username)
                                           .ExecuteScalar<string>();

            Assert.AreEqual("username2", actualUsername);
        }