public void UpdateNumberOfReviews() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var book = new Bookstore.Book { Title = nameof(UpdateNumberOfReviews) }; repository.Bookstore.Book.Insert(book); int?getNuberOfReviews() => repository.Bookstore.NumberOfReviews .Query(nor => nor.ID == book.ID) .Select(nor => nor.Count) .SingleOrDefault(); var review1 = new Bookstore.Review { BookID = book.ID, Score = 3 }; var review2 = new Bookstore.Review { BookID = book.ID, Score = 4 }; repository.Bookstore.Review.Insert(review1, review2); Assert.AreEqual(2, getNuberOfReviews()); repository.Bookstore.Review.Delete(review1); Assert.AreEqual(1, getNuberOfReviews()); } }
public void DefaultTextFromScore() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var book = new Bookstore.Book { Title = nameof(DefaultTextFromScore) }; repository.Bookstore.Book.Insert(book); var review1 = new Bookstore.Review { BookID = book.ID, Score = 1 }; var review2 = new Bookstore.Review { BookID = book.ID, Score = 3, Text = "OK" }; var review3 = new Bookstore.Review { BookID = book.ID, Score = 5 }; repository.Bookstore.Review.Insert(review1, review2, review3); var reviews = repository.Bookstore.Review.Load(new[] { review1.ID, review2.ID, review3.ID }); Assert.AreEqual( "1 - I don't like it, 3 - OK, 5 - I like it", TestUtility.DumpSorted(reviews, r => r.Score + " - " + r.Text)); } }
public void AlternativeSource() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var wish1 = new Bookstore.WishList { BookTitle = "New book 1 " + RandomString() }; var wish2 = new Bookstore.WishList { BookTitle = "New book 2 " + RandomString(), HighPriority = true }; repository.Bookstore.WishList.Insert(wish1, wish2); var regularBooks = repository.Bookstore.BookGrid.Query().Select(book => book.Title).ToList(); Assert.IsFalse(regularBooks.Contains(wish1.BookTitle), "Regular books should not contain the items from a with list."); Assert.IsFalse(regularBooks.Contains(wish2.BookTitle), "Regular books should not contain the items from a with list."); // Check that the wanted books returns the books from the alternative source (WishList): var wantedBooks = repository.Bookstore.BookGrid.Query(new Bookstore.WantedBooks()) .Where(book => book.ID == wish1.ID || book.ID == wish2.ID) .ToSimple().ToList(); // NOTE: If the above line fails with an exception, make sure that the Query 'Bookstore.WantedBooks' implementation // returns *all* properties of the Bookstore_BookGrid. For example, if a new property is added to the grid, the EF mapping might fail. Assert.AreEqual($"{wish1.BookTitle}, {wish2.BookTitle}", TestUtility.DumpSorted(wantedBooks, book => book.Title)); // Test the HighPriorityOnly filter parameter: var wantedBooksHighPriority = repository.Bookstore.BookGrid.Query(new Bookstore.WantedBooks { HighPriorityOnly = true }) .Where(book => book.ID == wish1.ID || book.ID == wish2.ID) .ToSimple().ToList(); Assert.AreEqual($"{wish2.BookTitle}", TestUtility.DumpSorted(wantedBooksHighPriority, book => book.Title)); } }
public void UncertainWords() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); // Removing old data to make this test independent. var old = repository.Bookstore.UncertainWord.Load(); repository.Bookstore.UncertainWord.Delete(old); var uncertain = new[] { "sometimes", "maybe", "someone" }; repository.Bookstore.UncertainWord.Insert( uncertain.Select(word => new UncertainWord { Word = word })); var book = new Book { Title = "Some book title" }; repository.Bookstore.Book.Insert(book); var disposal1 = new Disposal { BookID = book.ID, Explanation = "It was damaged" }; repository.Bookstore.Disposal.Insert(disposal1); var disposal2 = new Disposal { BookID = book.ID, Explanation = "Maybe it was damaged" }; TestUtility.ShouldFail <UserException>( () => repository.Bookstore.Disposal.Insert(disposal2), @"The explanation ""Maybe it w..."" should not contain word ""maybe"". Book: Some book title."); } }
public void HighRating() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var book1 = new Book { Title = "Simple book" }; var book2 = new Book { Title = "Super great book" }; repository.Bookstore.Book.Insert(book1, book2); var ratings = repository.Bookstore.BookRating.Query(new[] { book1.ID, book2.ID }) .OrderBy(r => r.Base.Code) .ToSimple().ToList(); Assert.AreEqual("0.00, 150.00", TestUtility.Dump(ratings, r => r.Rating?.ToString("0.00", CultureInfo.InvariantCulture))); var disposal1 = new Disposal { BookID = book1.ID, Explanation = "damaged" }; repository.Bookstore.Disposal.Insert(disposal1); var disposal2 = new Disposal { BookID = book2.ID, Explanation = "damaged" }; TestUtility.ShouldFail <UserException>( () => repository.Bookstore.Disposal.Insert(disposal2), "rating above 100"); } }
public void PhoneNumbersShouldNotContainAlphabetCharactersTest() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var person = new Person { Name = Guid.NewGuid().ToString(), MobilePhone = "07700-a00759" }; TestUtility.ShouldFail <UserException>(() => repository.Bookstore.Person.Insert(person), "Invalid phone number format."); } }
public void CommonMisspellingValidation() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var book = new Book { Title = "x 'curiousity' y" }; TestUtility.ShouldFail <UserException>( () => repository.Bookstore.Book.Insert(book), "It is not allowed to enter misspelled word"); } }
public void SimpleDeactivateOnDeleteTest() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var book = new Book { Title = Guid.NewGuid().ToString() }; repository.Bookstore.Book.Insert(book); repository.Bookstore.Book.Delete(book); var deactivatedBook = repository.Bookstore.Book.Load(x => x.ID == book.ID).FirstOrDefault(); Assert.IsNotNull(deactivatedBook, "The record should not be deleted from the database."); Assert.IsFalse(deactivatedBook.Active.Value, "The active flag should be set to false when deleting the record."); } }
public void ExplanationSpecialCharacters() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var book = new Book { Title = "Some book" }; repository.Bookstore.Book.Insert(book); TestUtility.ShouldFail <UserException>( () => repository.Bookstore.Disposal.Insert( new Disposal { BookID = book.ID, Explanation = "explanation#" }), book.Title, "contains character"); } }
public void ExplanationTooLong() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var book = new Book { Title = "Some book" }; repository.Bookstore.Book.Insert(book); TestUtility.ShouldFail <UserException>( () => repository.Bookstore.Disposal.Insert( new Disposal { BookID = book.ID, Explanation = "explanation" + new string('x', 500) }), "Explanation", "longer then", "500"); } }
public void AutomaticallyUpdateNumberOfComments() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var book = new Book { Title = Guid.NewGuid().ToString() }; repository.Bookstore.Book.Insert(book); int?readNumberOfComments() => repository.Bookstore.BookInfo .Query(bi => bi.ID == book.ID) .Select(bi => bi.NumberOfComments) .Single(); Assert.AreEqual(0, readNumberOfComments()); var c1 = new Comment { BookID = book.ID, Text = "c1" }; var c2 = new Comment { BookID = book.ID, Text = "c2" }; var c3 = new Comment { BookID = book.ID, Text = "c3" }; repository.Bookstore.Comment.Insert(c1); Assert.AreEqual(1, readNumberOfComments()); repository.Bookstore.Comment.Insert(c2, c3); Assert.AreEqual(3, readNumberOfComments()); repository.Bookstore.Comment.Delete(c1); Assert.AreEqual(2, readNumberOfComments()); repository.Bookstore.Comment.Delete(c2, c3); Assert.AreEqual(0, readNumberOfComments()); } }
public void AppendTextIfScoreChanged() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var book = new Bookstore.Book { Title = nameof(AppendTextIfScoreChanged) }; repository.Bookstore.Book.Insert(book); var reviews = new[] { new Bookstore.Review { BookID = book.ID, Score = 2, Text = "A" }, new Bookstore.Review { BookID = book.ID, Score = 3, Text = "B" }, new Bookstore.Review { BookID = book.ID, Score = 4, Text = "C" }, }; repository.Bookstore.Review.Insert(reviews); // Reload before editing. reviews = repository.Bookstore.Review.Load(reviews.Select(r => r.ID)) .OrderBy(r => r.Score).ToArray(); reviews[0].Score = 3; reviews[1].Score = 5; repository.Bookstore.Review.Update(reviews); Assert.AreEqual( "3 - A (changed from 2 to 3), 4 - C, 5 - B (changed from 3 to 5)", TestUtility.DumpSorted( repository.Bookstore.Review.Query(reviews.Select(r => r.ID)), r => r.Score + " - " + r.Text)); } }
public void DenyChangeOfLockedTitle() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var book1 = new Bookstore.Book { Title = "book1" }; var book2 = new Bookstore.Book { Title = "book2 locked" }; repository.Bookstore.Book.Insert(book1, book2); var review1 = new Bookstore.Review { BookID = book1.ID, Score = 3 }; var review2 = new Bookstore.Review { BookID = book2.ID, Score = 4 }; repository.Bookstore.Review.Insert(review1, review2); review1.Text = "updated text 1"; repository.Bookstore.Review.Update(review1); review1.Score += 1; repository.Bookstore.Review.Update(review1); review2.Text = "updated text 2"; repository.Bookstore.Review.Update(review2); review2.Score += 1; TestUtility.ShouldFail <UserException>( () => repository.Bookstore.Review.Update(review2), "It is not allowed to modify score"); } }
public void ImportantBookExplanation() { using (var rhetos = new BookstoreRhetos()) { var repository = rhetos.Resolve <Common.DomRepository>(); var book = new Book { Title = "Very important book" }; repository.Bookstore.Book.Insert(book); var disposal1 = new Disposal { BookID = book.ID, Explanation = "long explanation" + new string('!', 100) }; repository.Bookstore.Disposal.Insert(disposal1); var disposal2 = new Disposal { BookID = book.ID, Explanation = "short explanation" }; TestUtility.ShouldFail <UserException>( () => repository.Bookstore.Disposal.Insert(disposal2), "important", "50 characters"); } }