@Test public void useTheCommentsRelation() { // Create a new user and save it User bob = new User("*****@*****.**", "secret", "Bob").save(); // Create a new post Post bobPost = new Post(bob, "My first post", "Hello world").save(); // Post a first comment bobPost.addComment("Jeff", "Nice post"); bobPost.addComment("Tom", "I knew that !"); // Count things assertEquals(1, User.count()); assertEquals(1, Post.count()); assertEquals(2, Comment.count()); // Retrieve Bob's post bobPost = Post.find("byAuthor", bob).first(); assertNotNull(bobPost); // Navigate to comments assertEquals(2, bobPost.comments.size()); assertEquals("Jeff", bobPost.comments.get(0).author); // Delete the post bobPost.delete(); // Check that all comments have been deleted assertEquals(1, User.count()); assertEquals(0, Post.count()); assertEquals(0, Post.count()); assertEquals(0, Comment.count()); }
@Test public void testDeletePost() { Post post = getPostWithTags(null); Post.create(post); Post retrievedPost = Post.find.byId(post.id); assertNotNull("You should have got an post back", retrievedPost); Post.delete(post.id); retrievedPost = Post.find.byId(post.id); assertNull("You should not have got an post back", retrievedPost); }
@Test public void testDeletePostWithTags() { Post post = getPostWithTags(Arrays.asList(tag1, tag2)); Post.create(post); Post retrievedPost = Post.find.byId(post.id); assertNotNull("You should have got an post back", retrievedPost); List<PostTag> postTags = PostTag.findTagsByPostId(post.id); assertEquals("You should have got 2 tags back", 2, postTags.size()); Post.delete(post.id); retrievedPost = Post.find.byId(post.id); assertNull("You should not have got an post back", retrievedPost); postTags = PostTag.findTagsByPostId(post.id); assertEquals("You should have got 2 tags back", 0, postTags.size()); }