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 | |
|
16 | |
new User("bob@gmail.com", "secret", "Bob").save(); |
17 | |
|
18 | |
|
19 | |
User bob = User.find("byEmail", "bob@gmail.com").first(); |
20 | |
|
21 | |
|
22 | |
assertNotNull(bob); |
23 | |
assertEquals("Bob", bob.fullname); |
24 | |
} |
25 | |
|
26 | |
@Test |
27 | |
public void tryConnectAsUser() { |
28 | |
|
29 | |
new User("bob@gmail.com", "secret", "Bob").save(); |
30 | |
|
31 | |
|
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 | |
|
40 | |
User bob = new User("bob@gmail.com", "secret", "Bob").save(); |
41 | |
|
42 | |
|
43 | |
new Post(bob, "My first post", "Hello world").save(); |
44 | |
|
45 | |
|
46 | |
assertEquals(1, Post.count()); |
47 | |
|
48 | |
|
49 | |
List<Post> bobPosts = Post.find("byAuthor", bob).fetch(); |
50 | |
|
51 | |
|
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 | |
|
64 | |
User bob = new User("bob@gmail.com", "secret", "Bob").save(); |
65 | |
|
66 | |
|
67 | |
Post bobPost = new Post(bob, "My first post", "Hello world").save(); |
68 | |
|
69 | |
|
70 | |
new Comment(bobPost, "Jeff", "Nice post").save(); |
71 | |
new Comment(bobPost, "Tom", "I knew that !").save(); |
72 | |
|
73 | |
|
74 | |
List<Comment> bobPostComments = Comment.find("byPost", bobPost).fetch(); |
75 | |
|
76 | |
|
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 | |
|
95 | |
User bob = new User("bob@gmail.com", "secret", "Bob").save(); |
96 | |
|
97 | |
|
98 | |
Post bobPost = new Post(bob, "My first post", "Hello world").save(); |
99 | |
|
100 | |
|
101 | |
bobPost.addComment("Jeff", "Nice post"); |
102 | |
bobPost.addComment("Tom", "I knew that !"); |
103 | |
|
104 | |
|
105 | |
assertEquals(1, User.count()); |
106 | |
assertEquals(1, Post.count()); |
107 | |
assertEquals(2, Comment.count()); |
108 | |
|
109 | |
|
110 | |
bobPost = Post.find("byAuthor", bob).first(); |
111 | |
assertNotNull(bobPost); |
112 | |
|
113 | |
|
114 | |
assertEquals(2, bobPost.comments.size()); |
115 | |
assertEquals("Jeff", bobPost.comments.get(0).author); |
116 | |
|
117 | |
|
118 | |
bobPost.delete(); |
119 | |
|
120 | |
|
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 | |
|
131 | |
assertEquals(2, User.count()); |
132 | |
assertEquals(3, Post.count()); |
133 | |
assertEquals(3, Comment.count()); |
134 | |
|
135 | |
|
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 | |
|
142 | |
List<Post> bobPosts = Post.find("author.email", "bob@gmail.com").fetch(); |
143 | |
assertEquals(2, bobPosts.size()); |
144 | |
|
145 | |
|
146 | |
List<Comment> bobComments = Comment.find("post.author.email", "bob@gmail.com").fetch(); |
147 | |
assertEquals(3, bobComments.size()); |
148 | |
|
149 | |
|
150 | |
Post frontPost = Post.find("order by postedAt desc").first(); |
151 | |
assertNotNull(frontPost); |
152 | |
assertEquals("About the model layer", frontPost.title); |
153 | |
|
154 | |
|
155 | |
assertEquals(2, frontPost.comments.size()); |
156 | |
|
157 | |
|
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 | |
|
166 | |
User bob = new User("bob@gmail.com", "secret", "Bob").save(); |
167 | |
|
168 | |
|
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 | |
|
173 | |
assertEquals(0, Post.findTaggedWith("Red").size()); |
174 | |
|
175 | |
|
176 | |
bobPost.tagItWith("Red").tagItWith("Blue").save(); |
177 | |
anotherBobPost.tagItWith("Red").tagItWith("Green").save(); |
178 | |
|
179 | |
|
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 | |
} |