view test/BasicTest.java @ 0:03e9be886e82 draft

add project
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Thu, 13 Sep 2012 00:50:16 +0900
parents
children 76e3d7881a20
line wrap: on
line source

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("bob@gmail.com","secret","Bob").save();
    	
    	User bob = User.find("byEmail","bob@gmail.com").first();

    	//Test
    	assertNotNull(bob);
    	assertEquals("Bob",bob.fullname);
    }

    @Test
    public void tryConnectAsUser() {
    	// Create a new user and save it
    	new User("bob@gmail.com","secret","Bob").save();
    	
    	// Test
    	assertNotNull(User.connect("bob@gmail.com","secret"));
    	assertNull(User.connect("bob@gmail.com","badpassword"));
    	assertNull(User.connect("tom@gmail.com", "secret"));
    }
    
    @Test
    public void createPost() {
    	User bob = new User("bob@gmail.com","secret","Bob").save();
    	
    	new Post(bob, "My first post","Hello world").save();
    	
    	// Test that the post has been created
    	assertEquals(1,Post.count());
    	
    	// Retrieve all posts created by Bob
    	List<Post> bobPosts = Post.find("byAuthor",bob).fetch();
    	Post firstPost = bobPosts.get(0);
    	assertNotNull(firstPost);
    	assertEquals(bob, firstPost.author);
    	assertEquals("My first post",firstPost.title);
    	assertEquals("Hello world",firstPost.content);
    	assertNotNull(firstPost.postedAt);
    }
    
    @Test
    public void postComments() {
    	User bob = new User("bob@gmail.com","secret","Bob").save();
    	
    	Post bobPost = new Post(bob, "My first post","Hello world").save();
    	
    	new Comment(bobPost,"Jeff","Nice post").save();
    	new Comment(bobPost,"Tom","I knew that !").save();
    	
    	// Retrieve all comments
    	List<Comment> bobPostComments = Comment.find("byPost", bobPost).fetch();
    	
    	// Tests
    	assertEquals(2,bobPostComments.size());
    	
    	Comment firstComment = bobPostComments.get(0);
    	assertNotNull(firstComment);
    	assertEquals("Jeff",firstComment.author);
    	assertEquals("Nice post",firstComment.content);
    	assertNotNull(firstComment.postedAt);
    	
    	Comment secondComment = bobPostComments.get(1);
    	assertNotNull(secondComment);
    	assertEquals("Tom",secondComment.author);
    	assertEquals("I knew that !",secondComment.content);
    	assertNotNull(secondComment.postedAt);
    }

    @Test 
    public void useTheCommentsRelation() {
    	User bob = new User("bob@gmail.com", "secret", "Bob").save();
    	
    	Post bobPost = new Post(bob, "My first post","Hello world").save();
    	
    	bobPost.addComment("Jeff","Nice post");
    	bobPost.addComment("Tom","I knew that !");
    	
    	assertEquals(1,User.count());
    	assertEquals(1,Post.count());
    	assertEquals(2,Comment.count());
    	
    	bobPost = Post.find("byAuthor", bob).first();
    	assertNotNull(bobPost);
    	
    	assertEquals(2,bobPost.comments.size());
    	assertEquals("Jeff",bobPost.comments.get(0).author);
    	
    	bobPost.delete();
    	
    	assertEquals(1,User.count());
    	assertEquals(0,Post.count());
    	assertEquals(0,Comment.count());
        	
    }
    
    
    
    
    

}