Created
March 10, 2013 17:33
-
-
Save avgerin0s/5129572 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.junit.*; | |
| import java.util.*; | |
| import play.test.*; | |
| import models.*; | |
| public class BasicTest extends UnitTest { | |
| @Before | |
| public void setup() { | |
| Fixtures.deleteDatabase(); | |
| } | |
| @Test | |
| public void createAndRetrieveUser() { | |
| new User("evaggelos.avgerinos@gmail.com", "secret", "angel").save(); | |
| User angel = User.find("byEmail", "evaggelos.avgerinos@gmail.com").first(); | |
| assertNotNull(angel); | |
| assertEquals("angel", angel.fullName); | |
| } | |
| @Test | |
| public void tryConnectAsUser() { | |
| new User("evaggelos.avgerinos@gmail.com", "secret", "angel").save(); | |
| assertNotNull(User.connect("evaggelos.avgerinos@gmail.com", "secret")); | |
| assertNull(User.connect("evaggelos.avgerinos@gmail.com", "wat")); | |
| assertNull(User.connect("wat@wat.gr", "secret")); | |
| } | |
| @Test | |
| public void createPost() { | |
| User angel = new User("a@a.a", "secret", "angel").save(); | |
| new Post(angel, "My first post", "Hello world!").save(); | |
| assertEquals(1, Post.count()); | |
| List<Post> angelPosts = Post.find("byAuthor", angel).fetch(); | |
| assertEquals(1, angelPosts.size()); | |
| Post firstPost = angelPosts.get(0); | |
| assertEquals(angel, firstPost.author); | |
| assertEquals("My first post", firstPost.title); | |
| assertEquals("Hello world!", firstPost.content); | |
| assertNotNull(firstPost.postedAt); | |
| } | |
| @Test | |
| public void postComments() { | |
| User angel = new User("a@a.a", "secret", "angel").save(); | |
| Post angelPost = new Post(angel, "My first post", "Hello world!").save(); | |
| new Comment(angelPost, "Wat", "Wat wat wat").save(); | |
| new Comment(angelPost, "John", "Y0 mate! Sup?").save(); | |
| List<Comment> angelPostComments = Comment.find("byPost", angelPost).fetch(); | |
| Comment firstComment = angelPostComments.get(0); | |
| assertNotNull(firstComment); | |
| assertEquals("Wat", firstComment.author); | |
| assertEquals("Wat wat wat", firstComment.content); | |
| assertNotNull(firstComment.postedAt); | |
| Comment secondComment = angelPostComments.get(1); | |
| assertNotNull(secondComment); | |
| assertEquals("John", secondComment.author); | |
| assertEquals("Y0 mate! Sup?", secondComment.content); | |
| assertNotNull(secondComment.postedAt); | |
| } | |
| @Test | |
| public void useTheCommentRelation() { | |
| User angel = new User("a@a.a", "secret", "angel").save(); | |
| Post angelPost = new Post(angel, "My first post", "Hello world!").save(); | |
| angelPost.addComment("Wat", "asdas"); | |
| angelPost.addComment("Wataaaa", "asdas"); | |
| assertEquals(1, User.count()); | |
| assertEquals(1, Post.count()); | |
| assertEquals(2, Comment.count()); | |
| angelPost = Post.find("byAuthor", angel).first(); | |
| assertNotNull(angelPost); | |
| assertEquals(2, angelPost.comments.size()); | |
| assertEquals("Wat", angelPost.comments.get(0).author); | |
| angelPost.delete(); | |
| assertEquals(1, User.count()); | |
| assertEquals(0, Post.count()); | |
| assertEquals(0, Comment.count()); | |
| } | |
| @Test | |
| public void fullTest() { | |
| Fixtures.loadModels("data.yml"); | |
| assertEquals(2, User.count()); | |
| assertEquals(3, Post.count()); | |
| assertEquals(3, Comment.count()); | |
| assertNotNull(User.connect("evaggelos.avgerinos@gmail.com", "secret")); | |
| assertNotNull(User.connect("jeff@gmail.com", "secret")); | |
| assertNull(User.connect("jeff@gmail.com", "badpassword")); | |
| assertNull(User.connect("tom@gmail.com", "secret")); | |
| List<Post> angelPosts = Post.find("author.email", "evaggelos.avgerinos@gmail.com").fetch(); | |
| assertEquals(2, angelPosts.size()); | |
| List<Comment> angelComments = Comment.find("post.author.email", "evaggelos.avgerinos@gmail.com").fetch(); | |
| assertEquals(2, angelComments.size()); | |
| Post frontPost = Post.find("order by postedAt desc").first(); | |
| assertNotNull(frontPost); | |
| assertEquals("About the model layer", frontPost.title); | |
| assertEquals(2, frontPost.comments.size()); | |
| frontPost.addComment("Jim", "Hello guys"); | |
| assertEquals(3, frontPost.comments.size()); | |
| assertEquals(4, Comment.count()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment