Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MEAP v9, chapter 3, section 3.4.2 : sorting gotchas #28

Open
marcpa00 opened this issue Jul 1, 2013 · 1 comment
Open

MEAP v9, chapter 3, section 3.4.2 : sorting gotchas #28

marcpa00 opened this issue Jul 1, 2013 · 1 comment

Comments

@marcpa00
Copy link
Contributor

marcpa00 commented Jul 1, 2013

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 with

   static mapping = {
       posts sort: 'dateCreated', order: 'desc'
    }

specified in class User, and

    static mapping = {
        sort dateCreated: 'desc'
    }

specified in class Post.

@pledbrook
Copy link
Contributor

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.

Thanks so much for the comprehensive report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants