public void ShouldCreateNewAuthor()
        {
            var model = new Author()
                {
                    Name = "Author",
                    Biography = "Biography",
                    PictureUrl = "myPicture.jpg"
                };
            RedirectToRouteResult actionResult = null;
            UsingSession((session) =>
                {
                    var controller = new AuthorsController(new Repository(session));

                    actionResult = (RedirectToRouteResult) (controller.Create(model));
                    Assert.AreEqual("Authors", actionResult.RouteValues["controller"]);
                    Assert.AreEqual("Details", actionResult.RouteValues["action"]);
                });

            UsingSession((session) =>
                {
                    var author = WaitForTheLastWrite<Author>(session);
                    Assert.AreEqual(author.Id, actionResult.RouteValues["id"]);
                    AuthorsContollerTestHelper.AssertEqual(model, author);
                });
        }
        public void DetailsShouldAuthorsBooks()
        {
            var author1 = new Author()
            {
                Name = "Author1",
                Biography = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt = DateTime.UtcNow
            };

            var book1 = new Book
            {
                Title = "Oliver Orphan",
                Author = author1.Name,
                AgeRange = "0~2",
                CreatedAt = DateTime.UtcNow
            };

            var book2 = new Book
            {
                Title = "Oliver Orphan2",
                Author = author1.Name,
                AgeRange = "0~2",
                CreatedAt = DateTime.UtcNow.AddDays(-1)
            };

            UsingSession((session) =>
            {
                var repository = new Repository(session);
                var controller = new AuthorsController(repository);
                controller.Create(author1);

                repository.Create(book1);
                repository.Create(book2);
            });

            using (var session = _documentStore.OpenSession())
            {
                var author = WaitForTheLastWrite<Author>(session);
                var controller = new AuthorsController(new Repository(session));
                var result = (ViewResult) controller.Details(author.Id);
                var authorViewModel = (AuthorViewModel) result.Model;

                AuthorsContollerTestHelper.AssertEqual(authorViewModel.Author, author1);
                var books = authorViewModel.Books;
                Assert.AreEqual(2, books.Count());
                Assert.IsFalse(authorViewModel.HasMoreBooks);

                Assert.AreEqual(book1.Title, books.First().Title);
                Assert.AreEqual(book2.Title, books.Last().Title);
            }
        }
        public ActionResult Create(Author author)
        {
            if (!ModelState.IsValid)
            {
                return View(author);
            }

            if (IsAuthorExist(author.Name))
            {
                TempData["flashError"] = "An author with this name already exists";
                return View(author);
            }

            var createdAuthor = Repository.Create(author);
            return RedirectToAction("Details", "Authors", new {id = createdAuthor.Id});
        }
        public void DetailsShouldHaveAuthorInfoAndListAuthorBooks()
        {
            var author = new Author()
                {
                    Id = 3,
                    Name = "Author",
                    Biography = "Biography",
                    PictureUrl = "Pic.jpg"
                };
            var books = new List<Book> {new Book(), new Book(), new Book(), new Book()};
            var mockedRepo = new Mock<Repository>();
            mockedRepo.Setup(repo => repo.Get<Author>(3)).Returns(author);
            mockedRepo.Setup(r => r.Search(It.IsAny<Expression<Func<Book, bool>>>())).Returns(books);
            var controller = new AuthorsController(mockedRepo.Object);

            var result = controller.Details(3);
            var model = (AuthorViewModel)result.Model;
            AuthorsContollerTestHelper.AssertEqual(author, model.Author);
            Assert.AreEqual(4, model.Books.Count());
        }
 public ActionResult Edit(Author author)
 {
     if (!ModelState.IsValid )
     {
         return View(author);
     }
     if (IsAuthorNameUsedByAnotherAuthor(author))
     {
         TempData["flashError"] = "An author with this name already exists";
         return View(author);
     }
     Repository.Edit(author);
     return RedirectToAction("Details", "Authors", new {id = author.Id});
 }
 private bool IsAuthorNameUsedByAnotherAuthor(Author author)
 {
     return Repository.Search<Author>(a => a.Id != author.Id && a.Name == author.Name).Any();
 }
 public static void AssertEqual(Author expected, Author actual)
 {
     Assert.AreEqual(expected.Name, actual.Name);
     Assert.AreEqual(expected.PictureUrl, actual.PictureUrl);
     Assert.AreEqual(expected.Biography, actual.Biography);
 }
Beispiel #8
0
 public AuthorViewModel(Author author, List<Book> books, bool hasMoreBooks)
 {
     Author = author;
     Books = books;
     HasMoreBooks = hasMoreBooks;
 }
        public void ShouldRetrieveTheAuthorToEdit()
        {
            var author1 = new Author()
            {
                Name = "Author1",
                Biography = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt = DateTime.UtcNow
            };

            UsingSession((session) =>
            {
                var controller = new AuthorsController(new Repository(session));
                controller.Create(author1);
            });

            UsingSession((session) =>
            {
                var author = WaitForTheLastWrite<Author>(session);
                var controller = new AuthorsController(new Repository(session));
                var viewResult = controller.Edit(author.Id);
                var authorInView = (Author)(viewResult.Model);

                AuthorsContollerTestHelper.AssertEqual(author1, authorInView);
            });
        }
        public void ShouldOnlyListAuthorFirst4BooksInAuthorDetailPage()
        {
            var author1 = new Author()
            {
                Name = "Author1",
                Biography = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt = DateTime.UtcNow
            };

            UsingSession((session) =>
            {
                var repository = new Repository(session);
                var controller = new AuthorsController(repository);
                controller.Create(author1);
                Enumerable.Range(1, 9)
                          .ToList()
                          .ForEach(i => repository.Create(new Book() { Title = "Book " + i, Author = author1.Name, CreatedAt = DateTime.UtcNow.AddDays(-i)}));
            });

            UsingSession((session) =>
            {
                var author = WaitForTheLastWrite<Author>(session);
                var controller = new AuthorsController(new Repository(session));
                var viewResult = controller.Details(author.Id);
                var authorViewModel = (AuthorViewModel) (viewResult.Model);
                Assert.AreEqual(4, authorViewModel.Books.Count);
                Assert.IsTrue(authorViewModel.HasMoreBooks);
            });
        }
        public void ShouldNotEditAuthorNameToAnExsitingOne()
        {
            var author1 = new Author()
            {
                Name = "Author1",
                Biography = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt = DateTime.UtcNow
            };

            var author2 = new Author()
            {
                Name = "Author2",
                Biography = "Biography2",
                PictureUrl = "myPicture2.jpg",
                CreatedAt = DateTime.UtcNow
            };

            UsingSession((session) =>
            {
                var controller = new AuthorsController(new Repository(session));
                controller.Create(author1);
                controller.Create(author2);
            });

            UsingSession((session) =>
            {
                WaitForTheLastWrite<Author>(session);

                var author = session.Query<Author>().First(a => a.Name == author2.Name);
                author.Name = author1.Name;

                var controller = new AuthorsController(new Repository(session));
                var viewResult = (System.Web.Mvc.ViewResult)(controller.Edit(author));

                Assert.AreEqual("An author with this name already exists", controller.TempData["flashError"]);

                Assert.AreEqual("", viewResult.MasterName);
            });
        }
        public void ShouldNotCreateExistingAuthor()
        {
            var model = new Author()
                {
                    Name = "Author",
                    Biography = "Biography",
                    PictureUrl = "myPicture.jpg"
                };

            UsingSession((session) =>
                {
                    var controller = new AuthorsController(new Repository(session));
                    controller.Create(model);
                });

            UsingSession((session) =>
                {
                    WaitForTheLastWrite<Author>(session);
                    var controller = new AuthorsController(new Repository(session));
                    var viewResult = (System.Web.Mvc.ViewResult) (controller.Create(model));

                    Assert.AreEqual("An author with this name already exists", controller.TempData["flashError"]);

                    Assert.AreEqual("", viewResult.MasterName);
                });
        }
        public void ShouldListAuthors()
        {
            var author1 = new Author()
                {
                    Name = "Author1",
                    Biography = "Biography1",
                    PictureUrl = "myPicture1.jpg",
                    CreatedAt = DateTime.UtcNow
                };

            var author2 = new Author()
                {
                    Name = "Author2",
                    Biography = "Biography2",
                    PictureUrl = "myPicture2.jpg",
                    CreatedAt = DateTime.UtcNow.AddDays(-1)
                };

            UsingSession((session) =>
                {
                    var controller = new AuthorsController(new Repository(session));
                    controller.Create(author1);
                    controller.Create(author2);
                });

            UsingSession((session) =>
                {
                    WaitForTheLastWrite<Author>(session);
                    var controller = new AuthorsController(new Repository(session));
                    var viewResult = controller.List();
                    var authors = (IPagedList<Author>)(viewResult.Model);

                    Assert.AreEqual("", viewResult.MasterName);
                    Assert.AreEqual("Authors", viewResult.ViewBag.Title);

                    Assert.AreEqual(2, authors.Count);
                    AuthorsContollerTestHelper.AssertEqual(author1, authors.ElementAt(0));
                    AuthorsContollerTestHelper.AssertEqual(author2, authors.ElementAt(1));
                });
        }
        public void ShouldListAuthorBooksWithPagination()
        {
            var author1 = new Author()
            {
                Name = "Author1",
                Biography = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt = DateTime.UtcNow
            };

            UsingSession((session) =>
            {
                var repository = new Repository(session);
                var controller = new AuthorsController(repository);
                controller.Create(author1);
                Enumerable.Range(1, 22)
                          .ToList()
                          .ForEach(i => repository.Create(new Book() { Title = "Book " + i, Author = author1.Name, CreatedAt = DateTime.UtcNow.AddDays(-i) }));
            });

            UsingSession((session) =>
            {
                var author = WaitForTheLastWrite<Author>(session);
                var controller = new AuthorsController(new Repository(session));
                var viewResult = controller.Books(author.Id);
                var books = (IPagedList<Book>)(viewResult.Model);
                Assert.AreEqual(9, books.Count);
                Assert.AreEqual(3, books.PageCount);

                viewResult = controller.Books(author.Id, 3, 9);
                books = (IPagedList<Book>)(viewResult.Model);
                Assert.AreEqual(4, books.Count);
                Assert.AreEqual(3, books.PageCount);

                Assert.AreEqual(author.Name, ((Author)(viewResult.ViewBag.Author)).Name);
            });
        }
        public void ShouldEditAuthor()
        {
            var author1 = new Author()
            {
                Name = "Author1",
                Biography = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt = DateTime.UtcNow
            };

            UsingSession((session) =>
            {
                var controller = new AuthorsController(new Repository(session));
                controller.Create(author1);
            });

            Author updatedAuthorInfo = null;
            UsingSession((session) =>
            {
                var author = WaitForTheLastWrite<Author>(session);

                updatedAuthorInfo = new Author()
                {
                    Id = author.Id,
                    Name = "Author updated",
                    Biography = "Biography updated",
                    PictureUrl = "myPictureupdated.jpg",
                };
                var controller = new AuthorsController(new Repository(session));
                var actionResult = (RedirectToRouteResult)(controller.Edit(updatedAuthorInfo));
                Assert.AreEqual("Authors", actionResult.RouteValues["controller"]);
                Assert.AreEqual("Details", actionResult.RouteValues["action"]);
                Assert.AreEqual(author.Id, actionResult.RouteValues["id"]);
            });

            UsingSession((session) =>
            {
                var updatedAuthor = WaitForTheLastWrite<Author>(session);
                AuthorsContollerTestHelper.AssertEqual(updatedAuthorInfo, updatedAuthor);
            });
        }