I think that sorting should be introduced in more details at end of section 3.4.2 or it should not be touched until later in the book.
Rationale
If one feels the urge to add spock tests verifying sort behavior of Post and user.posts, as I did after reading section 3.4.2, they're in for a bumpy ride. Here is what observed:
User.posts is backed by a HashSet which means that calling order of user.addToPosts() has nothing to do with the order the collection is processed by hibernate when actually inserting records to the database;
sorting is applied to the collection when it is fetched from the database; if you write your test so the same hibernate session is used to save and read, the sort will not be applied to user.posts collection;
dateCreated is under the control of hibernate (or GORM ?) and is not the instant in time the actual Post instance was instanciated (in the JVM), but really the instant in time when it got inserted into the database;
Because of that, tirival code to check for sorting behavior does not work and writing tests that work is not easy for the intended audience (from my point of view).
If you think it is worth it to address the subject of sorting at that point, I would suggest adding the following tests to PostIntegrationSpec with a pargraph or two discussing the details :
def "Ensure posts linked to a user are sorted by date descending, earliest first"() {
given: "A user with several posts"
User user = new User(loginId:'joe', password: 'secret')
user.addToPosts(new Post(content:'First post')).save(failOnError: true, flush: true)
user.addToPosts(new Post(content:'Second post')).save(failOnError: true, flush: true)
user.addToPosts(new Post(content:'Third post')).save(failOnError: true, flush: true)
when: "The user is saved"
user.save(failOnError: true)
then: "The posts appear sorted by date created, descending"
User.withNewSession { session ->
User.get(user.id).posts.collect { it.content } == [ 'Third post', 'Second post', 'First post' ]
}
}
def "Ensure listing post collection is using sort order"() {
given: "A user with several posts"
User user = new User(loginId: "joe", password: 'secret')
user.addToPosts(new Post(content: "B")).save(failOnError: true, flush: true)
user.addToPosts(new Post(content: "C")).save(failOnError: true, flush: true)
user.addToPosts(new Post(content: "A")).save(failOnError: true, flush: true)
user.save(failOnError: true, flush: true)
when: "the list of all posts is retrieved"
List<Post> posts = Post.list()
then: "The posts for user created are in date created order"
posts.collect { it.content } == Post.list().sort { it.dateCreated }.reverse().collect { it.content }
}
This is a good point. I need to discuss this with Glen. Chapter 3 is soon to go into pre-production, so we should work out what approach to take pretty soon.
I think that sorting should be introduced in more details at end of section 3.4.2 or it should not be touched until later in the book.
Rationale
If one feels the urge to add spock tests verifying sort behavior of
Post
anduser.posts
, as I did after reading section 3.4.2, they're in for a bumpy ride. Here is what observed:User.posts
is backed by a HashSet which means that calling order ofuser.addToPosts()
has nothing to do with the order the collection is processed by hibernate when actually inserting records to the database;user.posts
collection;Post
instance was instanciated (in the JVM), but really the instant in time when it got inserted into the database;Because of that, tirival code to check for sorting behavior does not work and writing tests that work is not easy for the intended audience (from my point of view).
If you think it is worth it to address the subject of sorting at that point, I would suggest adding the following tests to
PostIntegrationSpec
with a pargraph or two discussing the details :This is with
specified in class
User
, andspecified in class
Post
.