##### Coverage Report - BasicTest

Classes in this File | Line Coverage | Branch Coverage | Complexity
BasicTest$1 |
100 % | 3/3
N/A | N/A
| 1.0;1

1 | 12 |
     import org.junit.*;
2 |  |
     import java.util.*;
3 |  |
     import play.test.*;
4 |  |
     import models.*;
5 |  |
6 |  |
     public class BasicTest extends UnitTest {
7 |  |
8 |  |
         @Before
9 |  |
         public void setup() {
10 |  |
             Fixtures.deleteAll();
11 |  |
         }
12 |  |
13 |  |
         @Test
14 |  |
         public void createAndRetrieveUser() {
15 |  |
             // Create a new user and save it
16 |  |
             new User("bob@gmail.com", "secret", "Bob").save();
17 |  |
18 |  |
             // Retrieve the user with bob username
19 |  |
             User bob = User.find("byEmail", "bob@gmail.com").first();
20 |  |
21 |  |
             // Test
22 |  |
             assertNotNull(bob);
23 |  |
             assertEquals("Bob", bob.fullname);
24 |  |
         }
25 |  |
26 |  |
         @Test
27 |  |
         public void tryConnectAsUser() {
28 |  |
             // Create a new user and save it
29 |  |
             new User("bob@gmail.com", "secret", "Bob").save();
30 |  |
31 |  |
             // Test
32 |  |
             assertNotNull(User.connect("bob@gmail.com", "secret"));
33 |  |
             assertNull(User.connect("bob@gmail.com", "badpassword"));
34 |  |
             assertNull(User.connect("tom@gmail.com", "secret"));
35 |  |
         }
36 |  |
37 |  |
         @Test
38 |  |
         public void createPost() {
39 |  |
             // Create a new user and save it
40 |  |
             User bob = new User("bob@gmail.com", "secret", "Bob").save();
41 |  |
42 |  |
             // Create a new post
43 |  |
             new Post(bob, "My first post", "Hello world").save();
44 |  |
45 |  |
             // Test that the post has been created
46 |  |
             assertEquals(1, Post.count());
47 |  |
48 |  |
             // Retrieve all post created by bob
49 |  |
             List<Post> bobPosts = Post.find("byAuthor", bob).fetch();
50 |  |
51 |  |
             // Tests
52 |  |
             assertEquals(1, bobPosts.size());
53 |  |
             Post firstPost = bobPosts.get(0);
54 |  |
             assertNotNull(firstPost);
55 |  |
             assertEquals(bob, firstPost.author);
56 |  |
             assertEquals("My first post", firstPost.title);
57 |  |
             assertEquals("Hello world", firstPost.content);
58 |  |
             assertNotNull(firstPost.postedAt);
59 |  |
         }
60 |  |
61 |  |
         @Test
62 |  |
         public void postComments() {
63 |  |
             // Create a new user and save it
64 |  |
             User bob = new User("bob@gmail.com", "secret", "Bob").save();
65 |  |
66 |  |
             // Create a new post
67 |  |
             Post bobPost = new Post(bob, "My first post", "Hello world").save();
68 |  |
69 |  |
             // Post a first comment
70 |  |
             new Comment(bobPost, "Jeff", "Nice post").save();
71 |  |
             new Comment(bobPost, "Tom", "I knew that !").save();
72 |  |
73 |  |
             // Retrieve all comments
74 |  |
             List<Comment> bobPostComments = Comment.find("byPost", bobPost).fetch();
75 |  |
76 |  |
             // Tests
77 |  |
             assertEquals(2, bobPostComments.size());
78 |  |
79 |  |
             Comment firstComment = bobPostComments.get(0);
80 |  |
             assertNotNull(firstComment);
81 |  |
             assertEquals("Jeff", firstComment.author);
82 |  |
             assertEquals("Nice post", firstComment.content);
83 |  |
             assertNotNull(firstComment.postedAt);
84 |  |
85 |  |
             Comment secondComment = bobPostComments.get(1);
86 |  |
             assertNotNull(secondComment);
87 |  |
             assertEquals("Tom", secondComment.author);
88 |  |
             assertEquals("I knew that !", secondComment.content);
89 |  |
             assertNotNull(secondComment.postedAt);
90 |  |
         }
91 |  |
92 |  |
         @Test
93 |  |
         public void useTheCommentsRelation() {
94 |  |
             // Create a new user and save it
95 |  |
             User bob = new User("bob@gmail.com", "secret", "Bob").save();
96 |  |
97 |  |
             // Create a new post
98 |  |
             Post bobPost = new Post(bob, "My first post", "Hello world").save();
99 |  |
100 |  |
             // Post a first comment
101 |  |
             bobPost.addComment("Jeff", "Nice post");
102 |  |
             bobPost.addComment("Tom", "I knew that !");
103 |  |
104 |  |
             // Count things
105 |  |
             assertEquals(1, User.count());
106 |  |
             assertEquals(1, Post.count());
107 |  |
             assertEquals(2, Comment.count());
108 |  |
109 |  |
             // Retrieve the bob post
110 |  |
             bobPost = Post.find("byAuthor", bob).first();
111 |  |
             assertNotNull(bobPost);
112 |  |
113 |  |
             // Navigate to comments
114 |  |
             assertEquals(2, bobPost.comments.size());
115 |  |
             assertEquals("Jeff", bobPost.comments.get(0).author);
116 |  |
117 |  |
             // Delete the post
118 |  |
             bobPost.delete();
119 |  |
120 |  |
             // Chech the all comments have been deleted
121 |  |
             assertEquals(1, User.count());
122 |  |
             assertEquals(0, Post.count());
123 |  |
             assertEquals(0, Comment.count());
124 |  |
         }
125 |  |
126 |  |
         @Test
127 |  |
         public void fullTest() {
128 |  |
             Fixtures.load("data.yml");
129 |  |
130 |  |
             // Count things
131 |  |
             assertEquals(2, User.count());
132 |  |
             assertEquals(3, Post.count());
133 |  |
             assertEquals(3, Comment.count());
134 |  |
135 |  |
             // Try to connect as users
136 |  |
             assertNotNull(User.connect("bob@gmail.com", "secret"));
137 |  |
             assertNotNull(User.connect("jeff@gmail.com", "secret"));
138 |  |
             assertNull(User.connect("jeff@gmail.com", "badpassword"));
139 |  |
             assertNull(User.connect("tom@gmail.com", "secret"));
140 |  |
141 |  |
             // Find all bob posts
142 |  |
             List<Post> bobPosts = Post.find("author.email", "bob@gmail.com").fetch();
143 |  |
             assertEquals(2, bobPosts.size());
144 |  |
145 |  |
             // Find all comments related to bob posts
146 |  |
             List<Comment> bobComments = Comment.find("post.author.email", "bob@gmail.com").fetch();
147 |  |
             assertEquals(3, bobComments.size());
148 |  |
149 |  |
             // Find the most recent post
150 |  |
             Post frontPost = Post.find("order by postedAt desc").first();
151 |  |
             assertNotNull(frontPost);
152 |  |
             assertEquals("About the model layer", frontPost.title);
153 |  |
154 |  |
             // Check that this post has two comments
155 |  |
             assertEquals(2, frontPost.comments.size());
156 |  |
157 |  |
             // Post a new comment
158 |  |
             frontPost.addComment("Jim", "Hello guys");
159 |  |
             assertEquals(3, frontPost.comments.size());
160 |  |
             assertEquals(4, Comment.count());
161 |  |
         }
162 |  |
163 |  |
         @Test
164 |  |
         public void testTags() {
165 |  |
             // Create a new user and save it
166 |  |
             User bob = new User("bob@gmail.com", "secret", "Bob").save();
167 |  |
168 |  |
             // Create a new post
169 |  |
             Post bobPost = new Post(bob, "My first post", "Hello world").save();
170 |  |
             Post anotherBobPost = new Post(bob, "My second post post", "Hello world").save();
171 |  |
172 |  |
             // Well
173 |  |
             assertEquals(0, Post.findTaggedWith("Red").size());
174 |  |
175 |  |
             // Tag it now
176 |  |
             bobPost.tagItWith("Red").tagItWith("Blue").save();
177 |  |
             anotherBobPost.tagItWith("Red").tagItWith("Green").save();
178 |  |
179 |  |
             // Check
180 |  |
             assertEquals(2, Post.findTaggedWith("Red").size());
181 |  |
             assertEquals(1, Post.findTaggedWith("Blue").size());
182 |  |
             assertEquals(1, Post.findTaggedWith("Green").size());
183 |  |
184 |  |
             assertEquals(1, Post.findTaggedWith("Red", "Blue").size());
185 |  |
             assertEquals(1, Post.findTaggedWith("Red", "Green").size());
186 |  |
             assertEquals(0, Post.findTaggedWith("Red", "Green", "Blue").size());
187 |  |
             assertEquals(0, Post.findTaggedWith("Green", "Blue").size());
188 |  |
189 |  |
             List<Map> cloud = Tag.getCloud();
190 | 3 |
             Collections.sort(cloud, new Comparator<Map>() {
191 |  |
                 public int compare(Map m1, Map m2) {
192 | 9 |
                     return m1.get("tag").toString().compareTo(m2.get("tag").toString());
193 |  |
                 }
194 |  |
             });
195 |  |
             assertEquals("[{tag=Blue, pound=1}, {tag=Green, pound=1}, {tag=Red, pound=2}]", cloud.toString());
196 |  |
197 |  |
         }
198 |  |
199 |  |
     }