Created
March 28, 2012 12:56
-
-
Save nmathew/2225904 to your computer and use it in GitHub Desktop.
Aggreator Issue
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 java.util.List; | |
| import models.Comment; | |
| import models.Post; | |
| import models.User; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import play.modules.siena.SienaFixtures; | |
| import play.test.UnitTest; | |
| public class BasicTest extends UnitTest { | |
| @Before | |
| public void setup() { | |
| SienaFixtures.deleteAllModels(); | |
| } | |
| @Test | |
| public void createAndRetrieveUser() { | |
| new User("[email protected]", "asdfgh", "Nobin" ).save(); | |
| User bob = User.all().filter("email", "[email protected]").get(); | |
| assertNotNull(bob); | |
| assertEquals("Nobin", bob.fullname); | |
| } | |
| @Test | |
| public void tryConnectAsUser (){ | |
| new User("[email protected]", "asdfgh", "Nobin").save(); | |
| assertNotNull(User.connect("[email protected]", "asdfgh")); | |
| assertNull(User.connect("[email protected]", "abc")); | |
| assertNull(User.connect("[email protected]", "zxcvbn")); | |
| } | |
| @Test | |
| public void testPost () { | |
| User bob = new User("[email protected]", "asdfgh", "Nobin"); | |
| bob.insert(); | |
| new Post("First Post", bob, "Hello World!").save(); | |
| assertEquals(1, Post.count()); | |
| List<Post> bobsPosts = Post.all().filter("author", bob).fetch(); | |
| assertEquals(1, bobsPosts.size()); | |
| Post firstPost = bobsPosts.get(0); | |
| assertNotNull(firstPost); | |
| assertEquals(bob, firstPost.author); | |
| assertEquals("First Post", firstPost.title); | |
| assertEquals("Hello World!", firstPost.content); | |
| assertNotNull(firstPost.postedAt); | |
| } | |
| @Test | |
| public void postComments() { | |
| User bob = new User("[email protected]", "asdfgh", "Nobin"); | |
| bob.insert(); | |
| Post bobPost = new Post("First Post", bob, "Hello World!"); | |
| bobPost.insert(); | |
| new Comment(bobPost, "mary", "Nice Post").save(); | |
| new Comment(bobPost, "Nobin", "I knew that !").save(); | |
| List<Comment> bobpostComments = Comment.all().filter("post", bobPost).fetch(); | |
| assertEquals(2, bobpostComments.size()); | |
| } | |
| @Test | |
| public void useTheCommentsRelation() { | |
| User bob = new User("[email protected]", "asdfgh", "Nobin"); | |
| bob.insert(); | |
| Post bobPost = new Post("First Post", bob, "Hello World!"); | |
| bobPost.insert(); | |
| bobPost.addComment("mary", "Nice Post!"); | |
| bobPost.addComment("rachel", "You copied Dad!"); | |
| assertEquals(1, User.count()); | |
| assertEquals(1, Post.count()); | |
| assertEquals(2, bobPost.comments.asList().size()); | |
| Post bobPost1 = Post.all().filter("author", bob).get(); | |
| assertNotNull(bobPost1); | |
| //bobPost1 fails; because bobPost is the not the first post | |
| //previous entries are not deleted from table. | |
| assertEquals(2, bobPost.comments.asList().size()); | |
| assertEquals("mary", bobPost.comments.asList().get(0).author); | |
| bobPost.delete(); | |
| assertEquals(1, User.count()); | |
| assertEquals(0, Post.count()); | |
| assertEquals(0, Comment.count()); | |
| } | |
| @Test | |
| public void fullTest() { | |
| SienaFixtures.loadModels("data.yml"); | |
| assertEquals(2, User.count()); | |
| } | |
| @Test | |
| public void testTags() { | |
| User bob = new User("[email protected]", "asdfgh", "Nobin"); | |
| bob.insert(); | |
| Post bobPost = new Post("My First Post", bob, "Hello World! First Time"); | |
| bobPost.insert(); | |
| Post AnotherbobPost = new Post("My Second Post", bob, "Hello World! Second Time"); | |
| AnotherbobPost.insert(); | |
| assertEquals(0, Post.findTaggedWith("Red").size()); | |
| bobPost.tagItWith("Red").tagItWith("Blue"); | |
| bobPost.save(); | |
| bobPost.update(); | |
| AnotherbobPost.tagItWith("Red").tagItWith("Green"); | |
| AnotherbobPost.save(); | |
| AnotherbobPost.update(); | |
| assertEquals(2, Post.findTaggedWith("Red").size()); | |
| assertEquals(1, Post.findTaggedWith("Green").size()); | |
| assertEquals(1, Post.findTaggedWith("Blue").size()); | |
| } | |
| } |
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
| package models; | |
| import java.util.ArrayList; | |
| import java.util.Date; | |
| import java.util.Iterator; | |
| import java.util.List; | |
| import play.*; | |
| import play.data.validation.Required; | |
| import siena.DateTime; | |
| import siena.Id; | |
| import siena.Index; | |
| import siena.Model; | |
| import siena.Query; | |
| import siena.Table; | |
| import siena.core.Aggregated; | |
| import siena.core.Many; | |
| import siena.core.Owned; | |
| import siena.Text; | |
| public class Post extends Model { | |
| @Id | |
| public Long id; | |
| @Required | |
| public String title; | |
| @Required | |
| @DateTime | |
| public Date postedAt; | |
| @Text | |
| @Required | |
| public String content; | |
| @Required | |
| @Index("author_index") | |
| public User author; | |
| @Owned(mappedBy="post") | |
| public Many<Comment> comments; | |
| @Aggregated | |
| public Many<Tag> tags; | |
| public Post(String title, User author, String content) { | |
| this.author = author; | |
| this.title = title; | |
| this.content = content; | |
| this.postedAt = new Date(); | |
| } | |
| public Post addComment(String author, String content) { | |
| Comment newComment = new Comment(this, author, content); | |
| this.comments.asList().add(newComment); | |
| this.save(); | |
| this.update(); | |
| return this; | |
| } | |
| public static Query<Post> all() { | |
| return Model.all(Post.class); | |
| } | |
| public Post previous () { | |
| return all().order("-postedAt").filter("postedAt<", postedAt).get(); | |
| } | |
| public Post next() { | |
| return all().order("postedAt").filter("postedAt>", postedAt).get(); | |
| } | |
| public Post tagItWith(String name) { | |
| tags.asList().add(Tag.findOrCreateByName(name)); | |
| return this; | |
| } | |
| public static List<Post> findTaggedWith(String tag) { | |
| List <Post> posts = all().fetch(); | |
| List <Post> result = new ArrayList(); | |
| Iterator<Post> postIterator = posts.iterator(); | |
| while(postIterator.hasNext()) | |
| { | |
| if(postIterator.next().tags.asQuery().filter("name", tag).count() >= 1) | |
| result.add(postIterator.next()); | |
| } | |
| return result; | |
| } | |
| public String toString() { | |
| return this.title; | |
| } | |
| public static Post findById(Long id) { | |
| return all().filter("id", id).get(); | |
| } | |
| public static int count() { | |
| return all().fetch().size(); | |
| } | |
| public List<Comment> findComments(){ | |
| return this.comments.asList(); | |
| } | |
| public List<Tag> findTags() { | |
| return this.tags.asList(); | |
| } | |
| public Long getId() { | |
| return id; | |
| } | |
| } |
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
| package models; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Map; | |
| import play.data.validation.Required; | |
| import siena.Id; | |
| import siena.Model; | |
| import siena.Query; | |
| import siena.Table; | |
| public class Tag extends Model implements Comparable<Tag> { | |
| @Id | |
| public Long id; | |
| @Required | |
| public String name; | |
| private Tag(String name) { | |
| this.name = name; | |
| } | |
| public String toString() { | |
| return name; | |
| } | |
| public int compareTo(Tag otherTag) { | |
| return name.compareTo(otherTag.name); | |
| } | |
| public static Query<Tag> all() { | |
| return Model.all(Tag.class); | |
| } | |
| public static Tag findOrCreateByName(String name) { | |
| Tag tag = all().filter("name", name).get(); | |
| if(tag == null) { | |
| tag = new Tag(name); | |
| } | |
| return tag; | |
| } | |
| public static List<Map> getCloud() { | |
| List<Map> result = new ArrayList(all().fetch()); | |
| return result; | |
| } | |
| public static int count() { | |
| return all().fetch().size(); | |
| } | |
| public Long getId() { | |
| return id; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment