Permalink
Browse files

Added spock tests to sample blog app

Added lots of spock tests to the sample blog app, removed a couple of
logging statements sitting around
  • Loading branch information...
1 parent 519f1f2 commit f533d41d033c92fc789092b41555f21c8fc4ea66 @clarkdave committed Nov 25, 2011
@@ -92,7 +92,7 @@ public static void run(String test) throws Exception {
Logger.error(e, "Cannot ouput XML unit output");
}
response.contentType = "text/html";
- Logger.info(response.status.toString())
+ //Logger.info(response.status.toString())
renderText(result);
}
if (test.endsWith(".test.html.suite")) {
@@ -10,7 +10,7 @@ class Bootstrap extends Job {
public void doJob() {
// load initial data if db is empty
if (User.count() == 0) {
- println('Count: ' + User.count())
+ //println('Count: ' + User.count())
Fixtures.load('initial-data.yml')
}
}
@@ -136,8 +136,14 @@ class Tag extends Model implements Comparable<Tag> {
Tag.find('byName', name).first() ?: new Tag(name)
}
+ static List<Map> getCloud() {
+ Tag.find(
+ 'select new map(t.name as tag, count(p.id) as pound) from Post p join p.tags as t group by t.name'
+ ).fetch()
+ }
+
String toString() {
- name
+ "Tag(${name})"
}
int compareTo(Tag tag) {
@@ -0,0 +1,17 @@
+import org.junit.*
+import play.test.*
+import play.mvc.*
+import play.mvc.Http.*
+import models.*
+
+class ApplicationTest extends FunctionalTest {
+
+ @Test
+ public void testThatIndexPageWorks() {
+ Response response = GET('/')
+ assertIsOk(response)
+ assertContentType('text/html', response)
+ assertCharset(play.Play.defaultWebEncoding, response)
+ }
+
+}
@@ -1,17 +0,0 @@
-import org.junit.*;
-import play.test.*;
-import play.mvc.*;
-import play.mvc.Http.*;
-import models.*;
-
-public class ApplicationTest extends FunctionalTest {
-
- @Test
- public void testThatIndexPageWorks() {
- Response response = GET("/");
- assertIsOk(response);
- assertContentType("text/html", response);
- assertCharset(play.Play.defaultWebEncoding, response);
- }
-
-}
@@ -47,4 +47,150 @@ class BasicTest extends SpockTest {
firstPost.content == 'Hello world'
firstPost.postedAt != null
}
+
+
+ def 'post comments'() {
+ setup: 'create a user and a post'
+ User bob = new User(email: 'bob@gmail.com', password: 'secret', fullname: 'Bob').save()
+ Post bobPost = new Post(bob, 'My first post', 'Hello world').save()
+
+ and: 'create some comments'
+ new Comment(bobPost, 'Jeff', 'Nice post').save()
+ new Comment(bobPost, 'Tom', 'I knew that!').save()
+
+ and: 'retreive comments for the post'
+ def bobPostComments = Comment.find('byPost', bobPost).fetch()
+ assert bobPostComments.size() == 2
+
+ when:
+ def firstComment = bobPostComments.get(0)
+
+ then:
+ firstComment != null
+ firstComment.author == 'Jeff'
+ firstComment.content == 'Nice post'
+ firstComment.postedAt != null
+
+ when:
+ def secondComment = bobPostComments.get(1)
+
+ then:
+ secondComment != null
+ secondComment.author == 'Tom'
+ secondComment.content == 'I knew that!'
+ secondComment.postedAt != null
+ }
+
+
+ def 'comment to post relationship'() {
+ setup: 'create a user and post'
+ User bob = new User(email: 'bob@gmail.com', password: 'secret', fullname: 'Bob').save()
+ Post bobPost = new Post(bob, 'My first post', 'Hello world').save()
+
+ when: 'post some comments'
+ bobPost.addComment('Jeff', 'Nice post')
+ bobPost.addComment('Tom', 'I knew that !')
+
+ then: 'confirm counts'
+ User.count() == 1
+ Post.count() == 1
+ Comment.count() == 2
+
+ when: 'retrieve bob post'
+ bobPost = Post.find('byAuthor', bob).first()
+
+ then:
+ bobPost != null
+ bobPost.comments.size() == 2
+ bobPost.comments.get(0).author == 'Jeff'
+
+ when: 'delete the post'
+ bobPost.delete()
+
+ then: 'confirm all comments are deleted'
+ User.count() == 1
+ Post.count() == 0
+ Comment.count() == 0
+ }
+
+
+ def 'full test'() {
+ given: 'loading test fixtures'
+ Fixtures.load('data.yml')
+
+ expect:
+ User.count() == 2
+ Post.count() == 3
+ Comment.count() == 3
+
+ // user authentication tests
+ User.connect('bob@gmail.com', 'secret') != null
+ User.connect('jeff@gmail.com', 'secret') != null
+ User.connect('jeff@gmail.com', 'badpassword') == null
+ User.connect('tom@gmail.com', 'baduser') == null
+
+ when: 'get bob posts'
+ def bobPosts = Post.find('author.email', 'bob@gmail.com').fetch()
+
+ then:
+ bobPosts.size() == 2
+
+ when: 'get comments on bob posts'
+ def bobComments = Comment.find('post.author.email', 'bob@gmail.com').fetch()
+
+ then:
+ bobComments.size() == 3
+
+ when: 'get most recent post'
+ Post frontPost = Post.find('order by postedAt desc').first()
+
+ then:
+ frontPost != null
+ frontPost.title == 'About the model layer'
+ frontPost.comments.size() == 2
+
+ when: 'adding a new comment'
+ frontPost.addComment('Jim', 'Hello guys')
+
+ then:
+ frontPost.comments.size() == 3
+ Comment.count() == 4
+ }
+
+
+ def 'test tags'() {
+ given:
+ User bob = new User(email: 'bob@gmail.com', password: 'secret', fullname: 'Bob').save()
+ Post bobPost = new Post(bob, "My first post", "Hello world").save()
+ Post anotherBobPost = new Post(bob, "My second post post", "Hello world").save()
+
+ expect: 'no posts tagged with [Red]'
+ Post.findTaggedWith('Red').size() == 0
+
+ when: 'tagging the posts'
+ bobPost.tagItWith('Red').tagItWith('Blue').save()
+ anotherBobPost.tagItWith('Red').tagItWith('Green').save()
+
+ then:
+ Post.findTaggedWith('Red').size() == 2
+ Post.findTaggedWith('Blue').size() == 1
+ Post.findTaggedWith('Green').size() == 1
+ Post.findTaggedWith('Red', 'Blue').size() == 1
+ Post.findTaggedWith('Red', 'Green').size() == 1
+ Post.findTaggedWith('Red', 'Green', 'Blue').size() == 0
+ Post.findTaggedWith('Green', 'Blue').size() == 0
+
+ when: 'get tag cloud and sort it'
+ def cloud = Tag.getCloud()
+ cloud.sort { a, b ->
+ a.tag.toString().compareTo(b.tag.toString())
+ }
+
+ then:
+ cloud == [
+ [tag: 'Blue', pound: 1],
+ [tag: 'Green', pound: 1],
+ [tag: 'Red', pound: 2]
+ ]
+ }
}
View
@@ -1,7 +1,68 @@
-# you describe your data using the YAML notation here
-# and then load them using Fixtures.load("data.yml")
+# Test data
-# User(bob):
-# email: bob@gmail.com
-# password: secret
-# fullname: Bob
+User(bob):
+ email: bob@gmail.com
+ password: secret
+ fullname: Bob
+ isAdmin: true
+
+User(jeff):
+ email: jeff@gmail.com
+ password: secret
+ fullname: Jeff
+
+Post(firstBobPost):
+ title: About the model layer
+ postedAt: 2009-06-14
+ author: bob
+ content: >
+ The model has a central position in a Play! application. It is the domain-specific
+ representation of the information on which the application operates.
+
+ Martin fowler defines it as :
+
+ Responsible for representing concepts of the business, information about the
+ business situation, and business rules. State that reflects the business situation
+ is controlled and used here, even though the technical details of storing it are
+ delegated to the infrastructure. This layer is the heart of business software.
+
+Post(secondBobPost):
+ title: Just a test of YABE
+ postedAt: 2009-03-25
+ author: bob
+ content: >
+ Well, it's just a test.
+
+Post(jeffPost):
+ title: The MVC application
+ postedAt: 2009-06-06
+ author: jeff
+ content: >
+ A Play! application follows the MVC architectural pattern as applied to the
+ architecture of the Web.
+
+ This pattern splits the application into separate layers: the Presentation
+ layer and the Model layer. The Presentation layer is further split into a
+ View and a Controller layer.
+
+Comment(c1):
+ author: Guest
+ content: >
+ You are right !
+ postedAt: 2009-06-14
+ post: firstBobPost
+
+Comment(c2):
+ author: Mike
+ content: >
+ I knew that ...
+ postedAt: 2009-06-15
+ post: firstBobPost
+
+Comment(c3):
+ author: Tom
+ content: >
+ This post is useless ?
+ postedAt: 2009-04-05
+ post: secondBobPost
+

0 comments on commit f533d41

Please sign in to comment.