public void Multiple_entities_can_be_read_by_custom_sql_query()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    Title = "Book title 1",
                    AuthorName = "Author Name",
                    PageCount = 123
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    Title = "Book title 2",
                    AuthorName = "Author Name 2",
                    PageCount = 123
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    Title = "Book title 3",
                    AuthorName = "Author Name 2",
                    PageCount = 123
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    Title = "Book title 4",
                    AuthorName = "Author Name",
                    PageCount = 123
                };

            Repository.Insert(book1, book2, book3, book4);

            var sqlCommand = new SqlCommand("select [c_ISBN], [c_TITLE] from [t_Books] where [c_AUTHORNAME] = @AuthorName");
            sqlCommand.Parameters.Add(new SqlParameter("AuthorName", "Author Name 2"));

            var readBooks = Repository.FindBySql<Book>(sqlCommand).ExecuteList()
                .OrderBy(x => x.Title).ToList();

            Assert.AreEqual(2, readBooks.Count);
            Assert.AreEqual("2", readBooks[0].Isbn);
            Assert.AreEqual("Book title 2", readBooks[0].Title);
            Assert.AreEqual("3", readBooks[1].Isbn);
            Assert.AreEqual("Book title 3", readBooks[1].Title);
        }
        public virtual void Result_count_can_be_limited_by_using_Top()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 100,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Another Author Name",
                    Title = "Title 2",
                    PageCount = 200
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Another Author Name",
                    Title = "Title 3",
                    PageCount = 75
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Another Author Name",
                    Title = "Title 4",
                    PageCount = 150
                };

            var book5 = new Book
                {
                    Isbn = "5",
                    AuthorName = "Author Name",
                    Title = "Title 5",
                    PageCount = 75
                };

            var book6 = new Book
                {
                    Isbn = "6",
                    AuthorName = "Author Name",
                    Title = "Title 6",
                    PageCount = 75
                };

            Repository.Insert(book1, book2, book3, book4, book5, book6);

            var actualBooks = Repository.Find<Book>()
                                        .OrderByDescending(x => x.Isbn)
                                        .Top(3)
                                        .ExecuteList();

            Assert.AreEqual(3, actualBooks.Count);
            CollectionAssert.AreEqual(new[] { book6, book5, book4 }, actualBooks);
        }
        public void Query_with_value_comparison_to_variable_that_is_null_generates_an_IS_NULL_condition_in_the_query()
        {
            Repository.Convention = new BookConvention();

            var book1 = new Book { Isbn = "1", Title = "Title1", AuthorName = "Author name 1" };
            var book2 = new Book { Isbn = "2", Title = "Title2", AuthorName = null };
            var book3 = new Book { Isbn = "3", Title = "Title3", AuthorName = "Author name 3" };
            var book4 = new Book { Isbn = "4", Title = "Title4", AuthorName = null };

            Repository.Insert(book1, book2, book3, book4);

            string expectedName = null;
            var actualCompanies = Repository.Find<Book>().Where(x => x.AuthorName == expectedName).ExecuteList();

            CollectionAssert.AreEquivalent(new[] { book2, book4 }, actualCompanies);
        }
        public void Id_column_does_not_have_to_be_included_in_partial_select_for_entity_with_string_id()
        {
            Repository.Convention = new BookConvention();

            var book1 = new Book { Isbn = "1", Title = "Title1", AuthorName = "Author name 1" };
            var book2 = new Book { Isbn = "2", Title = "Title2", AuthorName = "Author name 2" };

            Repository.Insert(book1, book2);

            var expectedBook1 = new Book { Isbn = null, Title = "Title1", AuthorName = "Author name 1" };
            var expectedBook2 = new Book { Isbn = null, Title = "Title2", AuthorName = "Author name 2" };

            var actualBooks = Repository.Find<Book>().Select(x => x.Title).Select(x => x.AuthorName).ExecuteList();

            Assert.AreEqual(2, actualBooks.Count);
            CollectionAssert.AreEquivalent(new[] { expectedBook1, expectedBook2 }, actualBooks);
        }
        public void Single_entity_can_be_read_by_custom_sql_query()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    Title = "Book title 1",
                    AuthorName = "Author Name",
                    PageCount = 123
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    Title = "Book title 2",
                    AuthorName = "Author Name 2",
                    PageCount = 123
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    Title = "Book title 3",
                    AuthorName = "Author Name 2",
                    PageCount = 123
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    Title = "Book title 4",
                    AuthorName = "Author Name",
                    PageCount = 123
                };

            Repository.Insert(book1, book2, book3, book4);

            var sqlCommand = new SqlCommand("select [c_ISBN], [c_TITLE], [c_AUTHORNAME], [c_PAGECOUNT] from [t_Books] where [c_ISBN] = @Isbn");
            sqlCommand.Parameters.Add(new SqlParameter("Isbn", book2.Isbn));

            var readBook2 = Repository.FindBySql<Book>(sqlCommand).Execute();

            Assert.AreEqual(book2, readBook2);
        }
        public void Bool_properties_can_be_used_in_queries_without_explicit_comparison_to_true_or_false()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    Title = "Book title 1",
                    AuthorName = "Author Name",
                    PageCount = 123,
                    IsPublicDomain = true
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    Title = "Book title 2",
                    AuthorName = "Author Name 2",
                    PageCount = 123,
                    IsPublicDomain = false
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    Title = "Book title 3",
                    AuthorName = "Author Name 2",
                    PageCount = 123,
                    IsPublicDomain = true
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    Title = "Book title 4",
                    AuthorName = "Author Name",
                    PageCount = 123,
                    IsPublicDomain = false
                };

            Repository.Insert(book1, book2, book3, book4);

            var actualPublicDomainBooks = Repository.Find<Book>().Where(x => x.IsPublicDomain).ExecuteList();
            var actualNonPublicDomainBooks = Repository.Find<Book>().Where(x => !x.IsPublicDomain).ExecuteList();

            CollectionAssert.AreEquivalent(new[] { book1, book3 }, actualPublicDomainBooks);
            CollectionAssert.AreEquivalent(new[] { book2, book4 }, actualNonPublicDomainBooks);
        }
        public virtual void Update_commands_can_be_issued_to_update_multiple_entities_at_once()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 123,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Author Name 2",
                    Title = "Title 2",
                    PageCount = 123
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Author Name 2",
                    Title = "Title 3",
                    PageCount = 123
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Author Name 2",
                    Title = "Title 4",
                    PageCount = 321
                };

            Repository.Insert(book1);
            Repository.Insert(book2);
            Repository.Insert(book3);
            Repository.Insert(book4);

            Repository.Update<Book>()
                      .Where(x => x.AuthorName == "Author Name 2" && x.PageCount == 123)
                      .Set(x => x.PageCount, 456)
                      .Set(x => x.Title, "new title")
                      .Execute();

            var actualBooks = Repository.Find<Book>()
                                        .Where(x => x.PageCount == 456 && x.Title == "new title")
                                        .ExecuteList();

            Assert.AreEqual(2, actualBooks.Count);

            Assert.AreEqual(1, actualBooks.Count(x => x.Isbn == "2"));
            Assert.AreEqual(1, actualBooks.Count(x => x.Isbn == "3"));
        }
        public virtual void String_StartsWith_and_EndsWith_call_can_be_used_to_execute_like_queries()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 100,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Steven Smith",
                    Title = "Title 2",
                    PageCount = 200
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Smith Stevenson",
                    Title = "Title 3",
                    PageCount = 75
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Another Author Name",
                    Title = "Title 4",
                    PageCount = 150
                };

            var book5 = new Book
                {
                    Isbn = "5",
                    AuthorName = "Author Name",
                    Title = "Title 5",
                    PageCount = 75
                };

            var book6 = new Book
                {
                    Isbn = "6",
                    AuthorName = "Fred-Steve Smith",
                    Title = "Title 6",
                    PageCount = 75
                };

            Repository.Insert(book1, book2, book3, book4, book5, book6);

            var actualBooks = Repository.Find<Book>()
                                        .Where(x => x.AuthorName.StartsWith("Smith") || (x.AuthorName.EndsWith("Smith") && x.Title.EndsWith("6")))
                                        .OrderBy(x => x.Isbn)
                                        .ExecuteList();

            Assert.AreEqual(2, actualBooks.Count);
            CollectionAssert.AreEqual(new[] { book3, book6 }, actualBooks);
        }
        public virtual void Multiple_where_clauses_can_be_combined_using_Or_when_querying_for_objects()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 123,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Author Name",
                    Title = "Title 2",
                    PageCount = 123
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Author Name",
                    Title = "Title 3",
                    PageCount = 123
                };

            Repository.Insert(book1);
            Repository.Insert(book2);
            Repository.Insert(book3);

            var actualBooks = Repository.Find<Book>()
                                        .Where(x => x.Isbn == "1")
                                        .OrWhere(x => x.Title == "Title 2")
                                        .OrderBy(x => x.Isbn)
                                        .ExecuteList();

            Assert.AreEqual(2, actualBooks.Count);
            Assert.AreEqual(book1, actualBooks[0]);
            Assert.AreEqual(book2, actualBooks[1]);
        }
        public virtual void Multiple_where_clauses_can_be_combined_using_both_And_and_Or_when_querying_for_objects()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 123,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Author Name",
                    Title = "Title 2",
                    PageCount = 123
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Author Name",
                    Title = "Title 3",
                    PageCount = 222
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Author Name",
                    Title = "Title 4",
                    PageCount = 222
                };

            var book5 = new Book
                {
                    Isbn = "5",
                    AuthorName = "Author Name",
                    Title = "Title 5",
                    PageCount = 123
                };

            var book6 = new Book
                {
                    Isbn = "6",
                    AuthorName = "Another author Name",
                    Title = "Title 6",
                    PageCount = 222
                };

            Repository.Insert(book1, book2, book3, book4, book5, book6);

            var actualBooks = Repository.Find<Book>()
                                        .Where(x => x.Isbn == "1")
                                        .OrWhere(x => x.PageCount == 222)
                                        .AndWhere(x => x.AuthorName == "Author Name")
                                        .OrderBy(x => x.Isbn)
                                        .ExecuteList();

            Assert.AreEqual(3, actualBooks.Count);
            Assert.AreEqual(book1, actualBooks[0]);
            Assert.AreEqual(book3, actualBooks[1]);
            Assert.AreEqual(book4, actualBooks[2]);
        }
        public virtual void Multiple_where_clauses_can_be_combined_using_And_when_querying_for_objects()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 123,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Author Name",
                    Title = "Title 2",
                    PageCount = 123
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Author Name",
                    Title = "Title 3",
                    PageCount = 123
                };

            Repository.Insert(book1);
            Repository.Insert(book2);
            Repository.Insert(book3);

            Book actualBook = Repository.Find<Book>()
                                        .Where(x => x.AuthorName == "Author Name")
                                        .Where(x => x.Title == "Title 2")
                                        .Where(x => x.PageCount == 123)
                                        .Execute();

            Assert.AreEqual(book2, actualBook);
        }
        public virtual void Multiple_partial_objects_can_be_read_by_explicitly_specifying_which_columns_to_fetch()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 123,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Author Name 2",
                    Title = "Title 2",
                    PageCount = 123
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Author Name 2",
                    Title = "Title 3",
                    PageCount = 123
                };

            Repository.Insert(book1, book2, book3);

            var partialBooks = Repository.Find<Book>()
                                         .Where(x => x.AuthorName == "Author Name 2")
                                         .Select(x => x.Isbn)
                                         .Select(x => x.Title)
                                         .ExecuteList()
                                         .OrderBy(x => x.Isbn).ToList();

            Assert.AreEqual(2, partialBooks.Count);

            Assert.AreEqual("2", partialBooks[0].Isbn);
            Assert.AreEqual("Title 2", partialBooks[0].Title);
            Assert.AreEqual(0, partialBooks[0].PageCount);
            Assert.IsNull(partialBooks[0].AuthorName);

            Assert.AreEqual("3", partialBooks[1].Isbn);
            Assert.AreEqual("Title 3", partialBooks[1].Title);
            Assert.AreEqual(0, partialBooks[1].PageCount);
            Assert.IsNull(partialBooks[1].AuthorName);
        }
        public virtual void Multiple_matching_objects_can_be_read_with_a_single_query()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 123,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Author Name 2",
                    Title = "Title 2",
                    PageCount = 123
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Author Name 2",
                    Title = "Title 3",
                    PageCount = 123
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Author Name 2",
                    Title = "Title 4",
                    PageCount = 321
                };

            Repository.Insert(book1);
            Repository.Insert(book2);
            Repository.Insert(book3);
            Repository.Insert(book4);

            var actualBooks = Repository.Find<Book>()
                                        .Where(x => x.AuthorName == "Author Name 2" && x.PageCount == 123)
                                        .ExecuteList();

            Assert.AreEqual(2, actualBooks.Count);

            CollectionAssert.Contains(actualBooks, book2);
            CollectionAssert.Contains(actualBooks, book3);
        }
        public virtual void Multiple_entities_can_be_inserted_with_one_operation()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 123,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Author Name",
                    Title = "Title 2",
                    PageCount = 123
                };

            Repository.Insert(new[] { book1, book2 });

            var actualBooks = Repository.Find<Book>().ExecuteList();

            Assert.AreEqual(2, actualBooks.Count);
            CollectionAssert.Contains(actualBooks, book1);
            CollectionAssert.Contains(actualBooks, book2);
        }
        public virtual void Linq_contains_call_within_composite_query_expressions_can_be_used_to_run_sql_in_queries()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 100,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Another Author Name",
                    Title = "Title 2",
                    PageCount = 200
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Another Author Name",
                    Title = "Title 3",
                    PageCount = 150
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Another Author Name",
                    Title = "Title 4",
                    PageCount = 75
                };

            var book5 = new Book
                {
                    Isbn = "5",
                    AuthorName = "Author Name",
                    Title = "Title 5",
                    PageCount = 75
                };

            var book6 = new Book
                {
                    Isbn = "6",
                    AuthorName = "Author Name",
                    Title = "Title 6",
                    PageCount = 50
                };

            Repository.Insert(book1, book2, book3, book4, book5, book6);

            var titles = new[] { "Title 4", "Title 5", "Title 6" };

            var actualBooks = Repository.Find<Book>()
                                        .Where(x => titles.Contains(x.Title) && x.AuthorName == "Author Name")
                                        .ExecuteList();

            Assert.AreEqual(2, actualBooks.Count);
            CollectionAssert.Contains(actualBooks, book5);
            CollectionAssert.Contains(actualBooks, book6);
        }
        public virtual void Result_from_find_query_can_be_ordered_by_multiple_columns()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 100,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Another Author Name",
                    Title = "Title 2",
                    PageCount = 200
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Another Author Name",
                    Title = "Title 3",
                    PageCount = 75
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Another Author Name",
                    Title = "Title 4",
                    PageCount = 150
                };

            var book5 = new Book
                {
                    Isbn = "5",
                    AuthorName = "Author Name",
                    Title = "Title 5",
                    PageCount = 75
                };

            var book6 = new Book
                {
                    Isbn = "6",
                    AuthorName = "Author Name",
                    Title = "Title 6",
                    PageCount = 75
                };

            Repository.Insert(book1, book2, book3, book4, book5, book6);

            var actualBooks = Repository.Find<Book>()
                                        .OrderBy(x => x.AuthorName)
                                        .OrderByDescending(x => x.PageCount, x => x.Title)
                                        .ExecuteList();

            Assert.AreEqual(6, actualBooks.Count);
            CollectionAssert.AreEqual(new[] { book2, book4, book3, book1, book6, book5 }, actualBooks);
        }
        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 Multiple_where_clauses_containing_multiple_conditions_can_be_combined_using_both_And_and_Or_when_querying_for_objects()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 123,
                    IsPublicDomain = true
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Author Name",
                    Title = "Title 2",
                    PageCount = 222,
                    IsPublicDomain = true
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Author Name",
                    Title = "Title 3",
                    PageCount = 222,
                    IsPublicDomain = false
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Author Name",
                    Title = "Title 4",
                    PageCount = 222,
                    IsPublicDomain = true
                };

            var book5 = new Book
                {
                    Isbn = "5",
                    AuthorName = "Author Name",
                    Title = "Title 5",
                    PageCount = 123,
                    IsPublicDomain = true
                };

            var book6 = new Book
                {
                    Isbn = "6",
                    AuthorName = "Another author Name",
                    Title = "Title 6",
                    PageCount = 222,
                    IsPublicDomain = true
                };

            var book7 = new Book
                {
                    Isbn = "7",
                    AuthorName = "Author Name 2",
                    Title = "Title 7",
                    PageCount = 222,
                    IsPublicDomain = false
                };

            var book8 = new Book
                {
                    Isbn = "8",
                    AuthorName = "Author Name 2",
                    Title = "Title 8",
                    PageCount = 222,
                    IsPublicDomain = true
                };

            var book9 = new Book
                {
                    Isbn = "9",
                    AuthorName = "Author Name 2",
                    Title = "Title 9",
                    PageCount = 123,
                    IsPublicDomain = false
                };

            var book10 = new Book
                {
                    Isbn = "10",
                    AuthorName = "Another author Name",
                    Title = "Title 10",
                    PageCount = 222,
                    IsPublicDomain = false
                };

            Repository.Insert(book1, book2, book3, book4, book5, book6, book7, book8, book9, book10);

            var actualBooks = Repository.Find<Book>()
                                        .Where(x => x.AuthorName == "Author Name" && x.IsPublicDomain)
                                        .OrWhere(x => x.AuthorName == "Author Name 2" && !x.IsPublicDomain)
                                        .AndWhere(x => x.PageCount == 222)
                                        .OrderBy(x => x.Isbn)
                                        .ExecuteList();

            Assert.AreEqual(3, actualBooks.Count);
            Assert.AreEqual(book2, actualBooks[0]);
            Assert.AreEqual(book4, actualBooks[1]);
            Assert.AreEqual(book7, actualBooks[2]);
        }
        public virtual void The_number_of_items_satisfying_a_series_of_constraints_can_be_read_with_a_count_query()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 123,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Author Name 2",
                    Title = "Title 2",
                    PageCount = 123
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Author Name 2",
                    Title = "Title 3",
                    PageCount = 123
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Author Name 2",
                    Title = "Title 4",
                    PageCount = 321
                };

            Repository.Insert(book1, book2, book3, book4);

            int count = Repository.Count<Book>()
                                  .Where(x => x.AuthorName == "Author Name 2" && x.PageCount == 123)
                                  .Execute();

            Assert.AreEqual(2, count);
        }
        public virtual void Object_with_table_and_columns_using_non_default_conventions_can_be_written_updated_and_read()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    Title = "Book title 1",
                    AuthorName = "Author Name",
                    PageCount = 123
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    Title = "Book title 2",
                    AuthorName = "Author Name 2",
                    PageCount = 123
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    Title = "Book title 3",
                    AuthorName = "Author Name 2",
                    PageCount = 123
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    Title = "Book title 4",
                    AuthorName = "Author Name",
                    PageCount = 123
                };

            Repository.Insert(book1);
            Repository.Insert(book2);
            Repository.Insert(book3);
            Repository.Insert(book4);

            var readBook = Repository.Find<Book>().Where(x => x.Isbn == book1.Isbn).Execute();

            readBook.Title = "Updated book title";
            readBook.AuthorName = "Updated author name";

            Repository.Update(readBook);

            var readUpdatedBook = Repository.Find<Book>()
                                            .Where(x => x.Title == "Updated book title")
                                            .Where(x => x.AuthorName == "Updated author name")
                                            .Execute();

            var updatedRowCount = Repository.Update<Book>().Where(x => x.AuthorName == "Author Name 2")
                                            .Set(x => x.PageCount, 456)
                                            .Execute();

            var updatedBooks = Repository.Find<Book>()
                                         .Where(x => x.PageCount == 456)
                                         .ExecuteList();

            var deletedRowCount = Repository.Delete<Book>()
                                            .Where(x => x.PageCount == 456)
                                            .Execute();

            Repository.Delete(book1);

            var booksAfterDelete = Repository.Find<Book>().ExecuteList();

            Assert.AreEqual(2, updatedRowCount);
            Assert.AreEqual(2, updatedBooks.Count);
            Assert.AreEqual(2, deletedRowCount);
            Assert.AreEqual(1, booksAfterDelete.Count);
            Assert.AreEqual(readBook, readUpdatedBook);
        }
        public virtual void Delete_commands_can_be_issued_to_update_multiple_entities_at_once()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 123,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Author Name 2",
                    Title = "Title 2",
                    PageCount = 123
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Author Name 2",
                    Title = "Title 3",
                    PageCount = 123
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Author Name 2",
                    Title = "Title 4",
                    PageCount = 321
                };

            Repository.Insert(book1);
            Repository.Insert(book2);
            Repository.Insert(book3);
            Repository.Insert(book4);

            Repository.Delete<Book>()
                      .Where(x => x.AuthorName == "Author Name 2" && x.PageCount == 123)
                      .Execute();

            var allBooks = Repository.Find<Book>().ExecuteList();

            // Only the books NOT matching the query shold remain
            Assert.AreEqual(2, allBooks.Count);
            CollectionAssert.Contains(allBooks, book1);
            CollectionAssert.Contains(allBooks, book4);
        }
        public virtual void Paging_query_without_explicit_ordering_orders_by_primary_key()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 100,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Another Author Name",
                    Title = "Title 2",
                    PageCount = 200
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Another Author Name",
                    Title = "Title 3",
                    PageCount = 75
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Another Author Name",
                    Title = "Title 4",
                    PageCount = 150
                };

            Repository.Insert(book1, book2, book3, book4);

            var actualBooks = Repository.Find<Book>()
                                        .Page(1, 2)
                                        .ExecuteList();

            Assert.AreEqual(2, actualBooks.Count);
            CollectionAssert.AreEqual(new[] { book3, book4 }, actualBooks);
        }
        public void Scalar_value_can_be_read_by_custom_sql_query()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    Title = "Book title 1",
                    AuthorName = "Author Name",
                    PageCount = 123
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    Title = "Book title 2",
                    AuthorName = "Author Name 2",
                    PageCount = 321
                };

            Repository.Insert(book1, book2);

            var sqlCommand = new SqlCommand("select [c_PAGECOUNT] from [t_Books] where [c_ISBN] = @Isbn");
            sqlCommand.Parameters.Add(new SqlParameter("Isbn", "2"));

            var actualPageCount = Repository.FindBySql<Book>(sqlCommand).ExecuteScalar<int>();

            Assert.AreEqual(321, actualPageCount);
        }
        public virtual void Partial_object_can_be_read_by_explicitly_specifying_which_columns_to_fetch()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 123,
                };

            Repository.Insert(book1);

            var partialBook = Repository.Find<Book>()
                                        .Where(x => x.Isbn == "1")
                                        .Select(x => x.Isbn)
                                        .Select(x => x.Title)
                                        .Execute();

            Assert.AreEqual("1", partialBook.Isbn);
            Assert.AreEqual("Title 1", partialBook.Title);
            Assert.AreEqual(0, partialBook.PageCount);
            Assert.IsNull(partialBook.AuthorName);
        }
        public virtual void Part_of_the_result_can_be_returned_from_find_query_by_specifying_page_number_and_size()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 100,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Another Author Name",
                    Title = "Title 2",
                    PageCount = 200
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Another Author Name",
                    Title = "Title 3",
                    PageCount = 75
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Another Author Name",
                    Title = "Title 4",
                    PageCount = 150
                };

            var book5 = new Book
                {
                    Isbn = "5",
                    AuthorName = "Author Name",
                    Title = "Title 5",
                    PageCount = 75
                };

            var book6 = new Book
                {
                    Isbn = "6",
                    AuthorName = "Author Name",
                    Title = "Title 6",
                    PageCount = 75
                };

            Repository.Insert(book1, book2, book3, book4, book5, book6);

            var actualBooks = Repository.Find<Book>()
                                        .OrderByDescending(x => x.Isbn)
                                        .Page(1, 2)
                                        .ExecuteList();

            Assert.AreEqual(2, actualBooks.Count);
            CollectionAssert.AreEqual(new[] { book4, book3 }, actualBooks);
        }
        public virtual void Expressions_can_be_used_to_run_queries()
        {
            Repository.DefaultConvention = new BookConvention();

            var book1 = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 100,
                };

            var book2 = new Book
                {
                    Isbn = "2",
                    AuthorName = "Another Author Name",
                    Title = "Title 2",
                    PageCount = 200
                };

            var book3 = new Book
                {
                    Isbn = "3",
                    AuthorName = "Another Author Name",
                    Title = "Title 3",
                    PageCount = 150
                };

            var book4 = new Book
                {
                    Isbn = "4",
                    AuthorName = "Another Author Name",
                    Title = "Title 4",
                    PageCount = 75
                };

            var book5 = new Book
                {
                    Isbn = "5",
                    AuthorName = "Author Name",
                    Title = "Title 5",
                    PageCount = 75
                };

            var book6 = new Book
                {
                    Isbn = "6",
                    AuthorName = "Author Name",
                    Title = "Title 6",
                    PageCount = 50
                };

            Repository.Insert(book1, book2, book3, book4, book5, book6);

            var actualBooks = Repository.Find<Book>()
                                        .Where(x => x.Isbn == "6" || (x.PageCount == 75 && x.AuthorName == "Another Author Name"))
                                        .ExecuteList();

            Assert.AreEqual(2, actualBooks.Count);
            CollectionAssert.Contains(actualBooks, book4);
            CollectionAssert.Contains(actualBooks, book6);
        }