Guest User

Untitled

a guest
Sep 20th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. import java.util.List;
  2.  
  3. import models.Comment;
  4. import models.Post;
  5. import models.User;
  6.  
  7. import org.junit.Before;
  8. import org.junit.Test;
  9.  
  10. import play.modules.siena.SienaFixtures;
  11. import play.test.UnitTest;
  12.  
  13. public class BasicTest extends UnitTest {
  14. @Before
  15. public void setup() {
  16. SienaFixtures.deleteAllModels();
  17. }
  18.  
  19. @Test
  20. public void createAndRetrieveUser() {
  21. new User("nobin.mathew@gmail.com", "asdfgh", "Nobin" ).save();
  22.  
  23. User bob = User.all().filter("email", "nobin.mathew@gmail.com").get();
  24.  
  25. assertNotNull(bob);
  26. assertEquals("Nobin", bob.fullname);
  27. }
  28.  
  29. @Test
  30. public void tryConnectAsUser (){
  31. new User("nobin.xyzxyz@gmail.com", "asdfgh", "Nobin").save();
  32. assertNotNull(User.connect("nobin.xyzxyz@gmail.com", "asdfgh"));
  33. assertNull(User.connect("nobin.xyzxyz@gmail.com", "abc"));
  34. assertNull(User.connect("abc@gmail.com", "zxcvbn"));
  35. }
  36.  
  37.  
  38. @Test
  39. public void testPost () {
  40. User bob = new User("nobin.xyzxyz@gmail.com", "asdfgh", "Nobin");
  41. bob.insert();
  42.  
  43. new Post("First Post", bob, "Hello World!").save();
  44. assertEquals(1, Post.count());
  45. List<Post> bobsPosts = Post.all().filter("author", bob).fetch();
  46.  
  47. assertEquals(1, bobsPosts.size());
  48. Post firstPost = bobsPosts.get(0);
  49. assertNotNull(firstPost);
  50. assertEquals(bob, firstPost.author);
  51. assertEquals("First Post", firstPost.title);
  52. assertEquals("Hello World!", firstPost.content);
  53. assertNotNull(firstPost.postedAt);
  54.  
  55. }
  56.  
  57.  
  58. @Test
  59. public void postComments() {
  60. User bob = new User("nobin.xyzxyz@gmail.com", "asdfgh", "Nobin");
  61. bob.insert();
  62.  
  63. Post bobPost = new Post("First Post", bob, "Hello World!");
  64. bobPost.insert();
  65.  
  66. new Comment(bobPost, "mary", "Nice Post").save();
  67. new Comment(bobPost, "Nobin", "I knew that !").save();
  68. List<Comment> bobpostComments = Comment.all().filter("post", bobPost).fetch();
  69.  
  70. assertEquals(2, bobpostComments.size());
  71. }
  72.  
  73.  
  74. @Test
  75. public void useTheCommentsRelation() {
  76. User bob = new User("nobin.xyzxyz@gmail.com", "asdfgh", "Nobin");
  77. bob.insert();
  78.  
  79. Post bobPost = new Post("First Post", bob, "Hello World!");
  80. bobPost.insert();
  81.  
  82. bobPost.addComment("mary", "Nice Post!");
  83. bobPost.addComment("rachel", "You copied Dad!");
  84.  
  85. assertEquals(1, User.count());
  86. assertEquals(1, Post.count());
  87. assertEquals(2, bobPost.comments.asList().size());
  88. Post bobPost1 = Post.all().filter("author", bob).get();
  89. assertNotNull(bobPost1);
  90. //bobPost1 fails; because bobPost is the not the first post
  91. //previous entries are not deleted from table.
  92. assertEquals(2, bobPost.comments.asList().size());
  93. assertEquals("mary", bobPost.comments.asList().get(0).author);
  94. bobPost.delete();
  95.  
  96. assertEquals(1, User.count());
  97. assertEquals(0, Post.count());
  98. assertEquals(0, Comment.count());
  99. }
  100.  
  101.  
  102. @Test
  103. public void fullTest() {
  104. SienaFixtures.loadModels("data.yml");
  105.  
  106. assertEquals(2, User.count());
  107. }
  108.  
  109.  
  110. @Test
  111. public void testTags() {
  112. User bob = new User("nobin.xyzxyz@gmail.com", "asdfgh", "Nobin");
  113. bob.insert();
  114.  
  115. Post bobPost = new Post("My First Post", bob, "Hello World! First Time");
  116. bobPost.insert();
  117. Post AnotherbobPost = new Post("My Second Post", bob, "Hello World! Second Time");
  118. AnotherbobPost.insert();
  119.  
  120. assertEquals(0, Post.findTaggedWith("Red").size());
  121. bobPost.tagItWith("Red").tagItWith("Blue");
  122. bobPost.save();
  123. bobPost.update();
  124. AnotherbobPost.tagItWith("Red").tagItWith("Green");
  125. AnotherbobPost.save();
  126. AnotherbobPost.update();
  127.  
  128. assertEquals(2, Post.findTaggedWith("Red").size());
  129. assertEquals(1, Post.findTaggedWith("Green").size());
  130. assertEquals(1, Post.findTaggedWith("Blue").size());
  131. }
  132. }
Add Comment
Please, Sign In to add comment