integration test and GORM associations

View: New views
9 Messages — Rating Filter:   Alert me  

integration test and GORM associations

by BrockHeinz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello All,

I'm struggling to understand why I can't use an association property
reliably in an integration test.  I understand that each integration
test is wrapped in a transaction, and then rolled back at
completion... but it still seem like the following should work:

BookTests.groovy:
class BookTests extends GroovyTestCase {
    void testSomething() {
      assert 0 == Author.count()
      assert 0 == Book.count()
      Author author = new Author(name: "Jonny Author")
      author.save(flush: true)
      assert 1 == Author.count()

      Book book = new Book(author: author, name: "Some Title")
      book.save(flush: true)
      assert 1 == Book.count()

      Author savedAuth = Author.get(author.id)
      assert savedAuth
      //the following will fail - 'books' is null!
      assert 1 == savedAuth.books.size()
    }
}

Author.groovy:
class Author {
  static hasMany = [books: Book]
  String name
}

Book.groovy:
class Book {
  static belongsTo = [author: Author]
  String name
}


Can anyone fill me in on why 'savedAuth.books' is null?

Thanks,
Brock

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: integration test and GORM associations

by BrockHeinz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

bump...

Anyone know why in an integration test why Book.count() returns what
I'd expect, but author.books.size() does not?

(see below for a more thorough explanation / example)

Thanks,
Brock



On Mon, Jul 21, 2008 at 9:59 AM, Brock Heinz <brock.heinz@...> wrote:

> Hello All,
>
> I'm struggling to understand why I can't use an association property
> reliably in an integration test.  I understand that each integration
> test is wrapped in a transaction, and then rolled back at
> completion... but it still seem like the following should work:
>
> BookTests.groovy:
> class BookTests extends GroovyTestCase {
>    void testSomething() {
>      assert 0 == Author.count()
>      assert 0 == Book.count()
>      Author author = new Author(name: "Jonny Author")
>      author.save(flush: true)
>      assert 1 == Author.count()
>
>      Book book = new Book(author: author, name: "Some Title")
>      book.save(flush: true)
>      assert 1 == Book.count()
>
>      Author savedAuth = Author.get(author.id)
>      assert savedAuth
>      //the following will fail - 'books' is null!
>      assert 1 == savedAuth.books.size()
>    }
> }
>
> Author.groovy:
> class Author {
>  static hasMany = [books: Book]
>  String name
> }
>
> Book.groovy:
> class Book {
>  static belongsTo = [author: Author]
>  String name
> }
>
>
> Can anyone fill me in on why 'savedAuth.books' is null?
>
> Thanks,
> Brock
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re: integration test and GORM associations

by Fred Janon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Brock,

I think it's because "author.id" is null: the "id" is created by the Hibernate or db when the instance is saved but the instance in BookTests has not been refreshed, the "id" has still not been assigned. Add println "author.id" and see what it is.

Fred


On Tue, Jul 22, 2008 at 07:50, Brock Heinz <brock.heinz@...> wrote:
bump...

Anyone know why in an integration test why Book.count() returns what
I'd expect, but author.books.size() does not?

(see below for a more thorough explanation / example)

Thanks,
Brock



On Mon, Jul 21, 2008 at 9:59 AM, Brock Heinz <brock.heinz@...> wrote:
> Hello All,
>
> I'm struggling to understand why I can't use an association property
> reliably in an integration test.  I understand that each integration
> test is wrapped in a transaction, and then rolled back at
> completion... but it still seem like the following should work:
>
> BookTests.groovy:
> class BookTests extends GroovyTestCase {
>    void testSomething() {
>      assert 0 == Author.count()
>      assert 0 == Book.count()
>      Author author = new Author(name: "Jonny Author")
>      author.save(flush: true)
>      assert 1 == Author.count()
>
>      Book book = new Book(author: author, name: "Some Title")
>      book.save(flush: true)
>      assert 1 == Book.count()
>
>      Author savedAuth = Author.get(author.id)
>      assert savedAuth
>      //the following will fail - 'books' is null!
>      assert 1 == savedAuth.books.size()
>    }
> }
>
> Author.groovy:
> class Author {
>  static hasMany = [books: Book]
>  String name
> }
>
> Book.groovy:
> class Book {
>  static belongsTo = [author: Author]
>  String name
> }
>
>
> Can anyone fill me in on why 'savedAuth.books' is null?
>
> Thanks,
> Brock
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Re: Re: integration test and GORM associations

by BrockHeinz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey there Jason,

Thanks for taking a look.  In the original post, I post the entire
method.  As a sanity check, I assert that I can actually find the
author with the provided id (I call flush: true on the save()).

Here's that original method:
class BookTests extends GroovyTestCase {
    void testSomething() {
      assert 0 == Author.count()
      assert 0 == Book.count()
      Author author = new Author(name: "Jonny Author")
      author.save(flush: true)
      assert 1 == Author.count()

      Book book = new Book(author: author, name: "Some Title")
      book.save(flush: true)
      assert 1 == Book.count()

      Author savedAuth = Author.get(author.id)
      assert savedAuth
      //the following will fail - 'books' is null!
      assert 1 == savedAuth.books.size()
    }


Any other ideas?

Thanks,
Brock

On Mon, Jul 21, 2008 at 9:03 PM, Fred Janon <fjanon@...> wrote:

> Hi Brock,
>
> I think it's because "author.id" is null: the "id" is created by the
> Hibernate or db when the instance is saved but the instance in BookTests has
> not been refreshed, the "id" has still not been assigned. Add println
> "author.id" and see what it is.
>
> Fred
>
>
> On Tue, Jul 22, 2008 at 07:50, Brock Heinz <brock.heinz@...> wrote:
>>
>> bump...
>>
>> Anyone know why in an integration test why Book.count() returns what
>> I'd expect, but author.books.size() does not?
>>
>> (see below for a more thorough explanation / example)
>>
>> Thanks,
>> Brock
>>
>>
>>
>> On Mon, Jul 21, 2008 at 9:59 AM, Brock Heinz <brock.heinz@...>
>> wrote:
>> > Hello All,
>> >
>> > I'm struggling to understand why I can't use an association property
>> > reliably in an integration test.  I understand that each integration
>> > test is wrapped in a transaction, and then rolled back at
>> > completion... but it still seem like the following should work:
>> >
>> > BookTests.groovy:
>> > class BookTests extends GroovyTestCase {
>> >    void testSomething() {
>> >      assert 0 == Author.count()
>> >      assert 0 == Book.count()
>> >      Author author = new Author(name: "Jonny Author")
>> >      author.save(flush: true)
>> >      assert 1 == Author.count()
>> >
>> >      Book book = new Book(author: author, name: "Some Title")
>> >      book.save(flush: true)
>> >      assert 1 == Book.count()
>> >
>> >      Author savedAuth = Author.get(author.id)
>> >      assert savedAuth
>> >      //the following will fail - 'books' is null!
>> >      assert 1 == savedAuth.books.size()
>> >    }
>> > }
>> >
>> > Author.groovy:
>> > class Author {
>> >  static hasMany = [books: Book]
>> >  String name
>> > }
>> >
>> > Book.groovy:
>> > class Book {
>> >  static belongsTo = [author: Author]
>> >  String name
>> > }
>> >
>> >
>> > Can anyone fill me in on why 'savedAuth.books' is null?
>> >
>> > Thanks,
>> > Brock
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re: integration test and GORM associations

by Fred Janon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Brock,

I still don't see a check on "author.id" but only a "count" on the entire collection.

author.save(flush: true)
     assert 1 == Author.count() // <-- did you mean assert 1 == author.id ???

try this:

author.save(flush: true)
println "author.id ${author.id}"

and check your test logs.

Fred

On Tue, Jul 22, 2008 at 10:39, Brock Heinz <brock.heinz@...> wrote:
Hey there Jason,

Thanks for taking a look.  In the original post, I post the entire
method.  As a sanity check, I assert that I can actually find the
author with the provided id (I call flush: true on the save()).

Here's that original method:
class BookTests extends GroovyTestCase {
   void testSomething() {
     assert 0 == Author.count()
     assert 0 == Book.count()
     Author author = new Author(name: "Jonny Author")
     author.save(flush: true)
     assert 1 == Author.count()

     Book book = new Book(author: author, name: "Some Title")
     book.save(flush: true)
     assert 1 == Book.count()

     Author savedAuth = Author.get(author.id)
     assert savedAuth
     //the following will fail - 'books' is null!
     assert 1 == savedAuth.books.size()
   }


Any other ideas?

Thanks,
Brock

On Mon, Jul 21, 2008 at 9:03 PM, Fred Janon <fjanon@...> wrote:
> Hi Brock,
>
> I think it's because "author.id" is null: the "id" is created by the
> Hibernate or db when the instance is saved but the instance in BookTests has
> not been refreshed, the "id" has still not been assigned. Add println
> "author.id" and see what it is.
>
> Fred
>
>
> On Tue, Jul 22, 2008 at 07:50, Brock Heinz <brock.heinz@...> wrote:
>>
>> bump...
>>
>> Anyone know why in an integration test why Book.count() returns what
>> I'd expect, but author.books.size() does not?
>>
>> (see below for a more thorough explanation / example)
>>
>> Thanks,
>> Brock
>>
>>
>>
>> On Mon, Jul 21, 2008 at 9:59 AM, Brock Heinz <brock.heinz@...>
>> wrote:
>> > Hello All,
>> >
>> > I'm struggling to understand why I can't use an association property
>> > reliably in an integration test.  I understand that each integration
>> > test is wrapped in a transaction, and then rolled back at
>> > completion... but it still seem like the following should work:
>> >
>> > BookTests.groovy:
>> > class BookTests extends GroovyTestCase {
>> >    void testSomething() {
>> >      assert 0 == Author.count()
>> >      assert 0 == Book.count()
>> >      Author author = new Author(name: "Jonny Author")
>> >      author.save(flush: true)
>> >      assert 1 == Author.count()
>> >
>> >      Book book = new Book(author: author, name: "Some Title")
>> >      book.save(flush: true)
>> >      assert 1 == Book.count()
>> >
>> >      Author savedAuth = Author.get(author.id)
>> >      assert savedAuth
>> >      //the following will fail - 'books' is null!
>> >      assert 1 == savedAuth.books.size()
>> >    }
>> > }
>> >
>> > Author.groovy:
>> > class Author {
>> >  static hasMany = [books: Book]
>> >  String name
>> > }
>> >
>> > Book.groovy:
>> > class Book {
>> >  static belongsTo = [author: Author]
>> >  String name
>> > }
>> >
>> >
>> > Can anyone fill me in on why 'savedAuth.books' is null?
>> >
>> > Thanks,
>> > Brock
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Re: Re: integration test and GORM associations

by BrockHeinz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Fred,

author.id is implicitly checked with the code here:

> Author savedAuth = Author.get(author.id)
> assert savedAuth

The 'get()' method on the domain object would not return if author.id was null.

I'm going to turn on SQL logging to see what's happening.

Thanks,
Brock



On Mon, Jul 21, 2008 at 9:59 PM, Fred Janon <fjanon@...> wrote:

> Hi Brock,
>
> I still don't see a check on "author.id" but only a "count" on the entire
> collection.
>
> author.save(flush: true)
>      assert 1 == Author.count() // <-- did you mean assert 1 == author.id
> ???
>
> try this:
>
> author.save(flush: true)
> println "author.id ${author.id}"
>
> and check your test logs.
>
> Fred
>
> On Tue, Jul 22, 2008 at 10:39, Brock Heinz <brock.heinz@...> wrote:
>>
>> Hey there Jason,
>>
>> Thanks for taking a look.  In the original post, I post the entire
>> method.  As a sanity check, I assert that I can actually find the
>> author with the provided id (I call flush: true on the save()).
>>
>> Here's that original method:
>> class BookTests extends GroovyTestCase {
>>    void testSomething() {
>>      assert 0 == Author.count()
>>      assert 0 == Book.count()
>>      Author author = new Author(name: "Jonny Author")
>>      author.save(flush: true)
>>      assert 1 == Author.count()
>>
>>      Book book = new Book(author: author, name: "Some Title")
>>      book.save(flush: true)
>>      assert 1 == Book.count()
>>
>>      Author savedAuth = Author.get(author.id)
>>      assert savedAuth
>>      //the following will fail - 'books' is null!
>>      assert 1 == savedAuth.books.size()
>>    }
>>
>>
>> Any other ideas?
>>
>> Thanks,
>> Brock
>>
>> On Mon, Jul 21, 2008 at 9:03 PM, Fred Janon <fjanon@...> wrote:
>> > Hi Brock,
>> >
>> > I think it's because "author.id" is null: the "id" is created by the
>> > Hibernate or db when the instance is saved but the instance in BookTests
>> > has
>> > not been refreshed, the "id" has still not been assigned. Add println
>> > "author.id" and see what it is.
>> >
>> > Fred
>> >
>> >
>> > On Tue, Jul 22, 2008 at 07:50, Brock Heinz <brock.heinz@...>
>> > wrote:
>> >>
>> >> bump...
>> >>
>> >> Anyone know why in an integration test why Book.count() returns what
>> >> I'd expect, but author.books.size() does not?
>> >>
>> >> (see below for a more thorough explanation / example)
>> >>
>> >> Thanks,
>> >> Brock
>> >>
>> >>
>> >>
>> >> On Mon, Jul 21, 2008 at 9:59 AM, Brock Heinz <brock.heinz@...>
>> >> wrote:
>> >> > Hello All,
>> >> >
>> >> > I'm struggling to understand why I can't use an association property
>> >> > reliably in an integration test.  I understand that each integration
>> >> > test is wrapped in a transaction, and then rolled back at
>> >> > completion... but it still seem like the following should work:
>> >> >
>> >> > BookTests.groovy:
>> >> > class BookTests extends GroovyTestCase {
>> >> >    void testSomething() {
>> >> >      assert 0 == Author.count()
>> >> >      assert 0 == Book.count()
>> >> >      Author author = new Author(name: "Jonny Author")
>> >> >      author.save(flush: true)
>> >> >      assert 1 == Author.count()
>> >> >
>> >> >      Book book = new Book(author: author, name: "Some Title")
>> >> >      book.save(flush: true)
>> >> >      assert 1 == Book.count()
>> >> >
>> >> >      Author savedAuth = Author.get(author.id)
>> >> >      assert savedAuth
>> >> >      //the following will fail - 'books' is null!
>> >> >      assert 1 == savedAuth.books.size()
>> >> >    }
>> >> > }
>> >> >
>> >> > Author.groovy:
>> >> > class Author {
>> >> >  static hasMany = [books: Book]
>> >> >  String name
>> >> > }
>> >> >
>> >> > Book.groovy:
>> >> > class Book {
>> >> >  static belongsTo = [author: Author]
>> >> >  String name
>> >> > }
>> >> >
>> >> >
>> >> > Can anyone fill me in on why 'savedAuth.books' is null?
>> >> >
>> >> > Thanks,
>> >> > Brock
>> >> >
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe from this list, please visit:
>> >>
>> >>    http://xircles.codehaus.org/manage_email
>> >>
>> >>
>> >
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re: integration test and GORM associations

by BrockHeinz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Here's the SQL logging:

Running test BookTests...
                    testSomething...[5015] hibernate.SQL select
count(*) as y0_ from author this_
[5028] hibernate.SQL select count(*) as y0_ from book this_
[5103] hibernate.SQL insert into author (id, version, name) values (null, ?, ?)
[5104] hibernate.SQL call identity()
[5111] hibernate.SQL select count(*) as y0_ from author this_
[5150] hibernate.SQL insert into book (id, version, author_id, name)
values (null, ?, ?, ?)
[5151] hibernate.SQL call identity()
[5158] hibernate.SQL select count(*) as y0_ from book this_
FAILURE

So, I took a look at $GRAILS_HOME/scripts/TestApp.groovy, and I think
I may know what is happening.  Each test method is called around life
cycle methods of PersistenceContextInterceptor.  Within that
lifecycle, 'flush' is never called.

I even tried adding the 'static transactional = false' variable to the
test class and I got the same result.

I suppose I could have the test harness dependency inject the
PersistenceContextInterceptor bean into my test and I can call flush()
myself :-\

To me, this just seems like a bug though.  I don't want to have to MOP
(yes, used that as a verb again :) my command object so that my
**integration** test will work.

Brock



On Tue, Jul 22, 2008 at 6:40 AM, Brock Heinz <brock.heinz@...> wrote:

> Hello Fred,
>
> author.id is implicitly checked with the code here:
>
>> Author savedAuth = Author.get(author.id)
>> assert savedAuth
>
> The 'get()' method on the domain object would not return if author.id was null.
>
> I'm going to turn on SQL logging to see what's happening.
>
> Thanks,
> Brock
>
>
>
> On Mon, Jul 21, 2008 at 9:59 PM, Fred Janon <fjanon@...> wrote:
>> Hi Brock,
>>
>> I still don't see a check on "author.id" but only a "count" on the entire
>> collection.
>>
>> author.save(flush: true)
>>      assert 1 == Author.count() // <-- did you mean assert 1 == author.id
>> ???
>>
>> try this:
>>
>> author.save(flush: true)
>> println "author.id ${author.id}"
>>
>> and check your test logs.
>>
>> Fred
>>
>> On Tue, Jul 22, 2008 at 10:39, Brock Heinz <brock.heinz@...> wrote:
>>>
>>> Hey there Jason,
>>>
>>> Thanks for taking a look.  In the original post, I post the entire
>>> method.  As a sanity check, I assert that I can actually find the
>>> author with the provided id (I call flush: true on the save()).
>>>
>>> Here's that original method:
>>> class BookTests extends GroovyTestCase {
>>>    void testSomething() {
>>>      assert 0 == Author.count()
>>>      assert 0 == Book.count()
>>>      Author author = new Author(name: "Jonny Author")
>>>      author.save(flush: true)
>>>      assert 1 == Author.count()
>>>
>>>      Book book = new Book(author: author, name: "Some Title")
>>>      book.save(flush: true)
>>>      assert 1 == Book.count()
>>>
>>>      Author savedAuth = Author.get(author.id)
>>>      assert savedAuth
>>>      //the following will fail - 'books' is null!
>>>      assert 1 == savedAuth.books.size()
>>>    }
>>>
>>>
>>> Any other ideas?
>>>
>>> Thanks,
>>> Brock
>>>
>>> On Mon, Jul 21, 2008 at 9:03 PM, Fred Janon <fjanon@...> wrote:
>>> > Hi Brock,
>>> >
>>> > I think it's because "author.id" is null: the "id" is created by the
>>> > Hibernate or db when the instance is saved but the instance in BookTests
>>> > has
>>> > not been refreshed, the "id" has still not been assigned. Add println
>>> > "author.id" and see what it is.
>>> >
>>> > Fred
>>> >
>>> >
>>> > On Tue, Jul 22, 2008 at 07:50, Brock Heinz <brock.heinz@...>
>>> > wrote:
>>> >>
>>> >> bump...
>>> >>
>>> >> Anyone know why in an integration test why Book.count() returns what
>>> >> I'd expect, but author.books.size() does not?
>>> >>
>>> >> (see below for a more thorough explanation / example)
>>> >>
>>> >> Thanks,
>>> >> Brock
>>> >>
>>> >>
>>> >>
>>> >> On Mon, Jul 21, 2008 at 9:59 AM, Brock Heinz <brock.heinz@...>
>>> >> wrote:
>>> >> > Hello All,
>>> >> >
>>> >> > I'm struggling to understand why I can't use an association property
>>> >> > reliably in an integration test.  I understand that each integration
>>> >> > test is wrapped in a transaction, and then rolled back at
>>> >> > completion... but it still seem like the following should work:
>>> >> >
>>> >> > BookTests.groovy:
>>> >> > class BookTests extends GroovyTestCase {
>>> >> >    void testSomething() {
>>> >> >      assert 0 == Author.count()
>>> >> >      assert 0 == Book.count()
>>> >> >      Author author = new Author(name: "Jonny Author")
>>> >> >      author.save(flush: true)
>>> >> >      assert 1 == Author.count()
>>> >> >
>>> >> >      Book book = new Book(author: author, name: "Some Title")
>>> >> >      book.save(flush: true)
>>> >> >      assert 1 == Book.count()
>>> >> >
>>> >> >      Author savedAuth = Author.get(author.id)
>>> >> >      assert savedAuth
>>> >> >      //the following will fail - 'books' is null!
>>> >> >      assert 1 == savedAuth.books.size()
>>> >> >    }
>>> >> > }
>>> >> >
>>> >> > Author.groovy:
>>> >> > class Author {
>>> >> >  static hasMany = [books: Book]
>>> >> >  String name
>>> >> > }
>>> >> >
>>> >> > Book.groovy:
>>> >> > class Book {
>>> >> >  static belongsTo = [author: Author]
>>> >> >  String name
>>> >> > }
>>> >> >
>>> >> >
>>> >> > Can anyone fill me in on why 'savedAuth.books' is null?
>>> >> >
>>> >> > Thanks,
>>> >> > Brock
>>> >> >
>>> >>
>>> >> ---------------------------------------------------------------------
>>> >> To unsubscribe from this list, please visit:
>>> >>
>>> >>    http://xircles.codehaus.org/manage_email
>>> >>
>>> >>
>>> >
>>> >
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>    http://xircles.codehaus.org/manage_email
>>>
>>>
>>
>>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re: integration test and GORM associations

by Graeme Rocher-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Raise an issue and we can take a look (by adding flush:true to your
saves it should be persisting the data)

Cheers

On Tue, Jul 22, 2008 at 12:59 PM, Brock Heinz <brock.heinz@...> wrote:

> Here's the SQL logging:
>
> Running test BookTests...
>                    testSomething...[5015] hibernate.SQL select
> count(*) as y0_ from author this_
> [5028] hibernate.SQL select count(*) as y0_ from book this_
> [5103] hibernate.SQL insert into author (id, version, name) values (null, ?, ?)
> [5104] hibernate.SQL call identity()
> [5111] hibernate.SQL select count(*) as y0_ from author this_
> [5150] hibernate.SQL insert into book (id, version, author_id, name)
> values (null, ?, ?, ?)
> [5151] hibernate.SQL call identity()
> [5158] hibernate.SQL select count(*) as y0_ from book this_
> FAILURE
>
> So, I took a look at $GRAILS_HOME/scripts/TestApp.groovy, and I think
> I may know what is happening.  Each test method is called around life
> cycle methods of PersistenceContextInterceptor.  Within that
> lifecycle, 'flush' is never called.
>
> I even tried adding the 'static transactional = false' variable to the
> test class and I got the same result.
>
> I suppose I could have the test harness dependency inject the
> PersistenceContextInterceptor bean into my test and I can call flush()
> myself :-\
>
> To me, this just seems like a bug though.  I don't want to have to MOP
> (yes, used that as a verb again :) my command object so that my
> **integration** test will work.
>
> Brock
>
>
>
> On Tue, Jul 22, 2008 at 6:40 AM, Brock Heinz <brock.heinz@...> wrote:
>> Hello Fred,
>>
>> author.id is implicitly checked with the code here:
>>
>>> Author savedAuth = Author.get(author.id)
>>> assert savedAuth
>>
>> The 'get()' method on the domain object would not return if author.id was null.
>>
>> I'm going to turn on SQL logging to see what's happening.
>>
>> Thanks,
>> Brock
>>
>>
>>
>> On Mon, Jul 21, 2008 at 9:59 PM, Fred Janon <fjanon@...> wrote:
>>> Hi Brock,
>>>
>>> I still don't see a check on "author.id" but only a "count" on the entire
>>> collection.
>>>
>>> author.save(flush: true)
>>>      assert 1 == Author.count() // <-- did you mean assert 1 == author.id
>>> ???
>>>
>>> try this:
>>>
>>> author.save(flush: true)
>>> println "author.id ${author.id}"
>>>
>>> and check your test logs.
>>>
>>> Fred
>>>
>>> On Tue, Jul 22, 2008 at 10:39, Brock Heinz <brock.heinz@...> wrote:
>>>>
>>>> Hey there Jason,
>>>>
>>>> Thanks for taking a look.  In the original post, I post the entire
>>>> method.  As a sanity check, I assert that I can actually find the
>>>> author with the provided id (I call flush: true on the save()).
>>>>
>>>> Here's that original method:
>>>> class BookTests extends GroovyTestCase {
>>>>    void testSomething() {
>>>>      assert 0 == Author.count()
>>>>      assert 0 == Book.count()
>>>>      Author author = new Author(name: "Jonny Author")
>>>>      author.save(flush: true)
>>>>      assert 1 == Author.count()
>>>>
>>>>      Book book = new Book(author: author, name: "Some Title")
>>>>      book.save(flush: true)
>>>>      assert 1 == Book.count()
>>>>
>>>>      Author savedAuth = Author.get(author.id)
>>>>      assert savedAuth
>>>>      //the following will fail - 'books' is null!
>>>>      assert 1 == savedAuth.books.size()
>>>>    }
>>>>
>>>>
>>>> Any other ideas?
>>>>
>>>> Thanks,
>>>> Brock
>>>>
>>>> On Mon, Jul 21, 2008 at 9:03 PM, Fred Janon <fjanon@...> wrote:
>>>> > Hi Brock,
>>>> >
>>>> > I think it's because "author.id" is null: the "id" is created by the
>>>> > Hibernate or db when the instance is saved but the instance in BookTests
>>>> > has
>>>> > not been refreshed, the "id" has still not been assigned. Add println
>>>> > "author.id" and see what it is.
>>>> >
>>>> > Fred
>>>> >
>>>> >
>>>> > On Tue, Jul 22, 2008 at 07:50, Brock Heinz <brock.heinz@...>
>>>> > wrote:
>>>> >>
>>>> >> bump...
>>>> >>
>>>> >> Anyone know why in an integration test why Book.count() returns what
>>>> >> I'd expect, but author.books.size() does not?
>>>> >>
>>>> >> (see below for a more thorough explanation / example)
>>>> >>
>>>> >> Thanks,
>>>> >> Brock
>>>> >>
>>>> >>
>>>> >>
>>>> >> On Mon, Jul 21, 2008 at 9:59 AM, Brock Heinz <brock.heinz@...>
>>>> >> wrote:
>>>> >> > Hello All,
>>>> >> >
>>>> >> > I'm struggling to understand why I can't use an association property
>>>> >> > reliably in an integration test.  I understand that each integration
>>>> >> > test is wrapped in a transaction, and then rolled back at
>>>> >> > completion... but it still seem like the following should work:
>>>> >> >
>>>> >> > BookTests.groovy:
>>>> >> > class BookTests extends GroovyTestCase {
>>>> >> >    void testSomething() {
>>>> >> >      assert 0 == Author.count()
>>>> >> >      assert 0 == Book.count()
>>>> >> >      Author author = new Author(name: "Jonny Author")
>>>> >> >      author.save(flush: true)
>>>> >> >      assert 1 == Author.count()
>>>> >> >
>>>> >> >      Book book = new Book(author: author, name: "Some Title")
>>>> >> >      book.save(flush: true)
>>>> >> >      assert 1 == Book.count()
>>>> >> >
>>>> >> >      Author savedAuth = Author.get(author.id)
>>>> >> >      assert savedAuth
>>>> >> >      //the following will fail - 'books' is null!
>>>> >> >      assert 1 == savedAuth.books.size()
>>>> >> >    }
>>>> >> > }
>>>> >> >
>>>> >> > Author.groovy:
>>>> >> > class Author {
>>>> >> >  static hasMany = [books: Book]
>>>> >> >  String name
>>>> >> > }
>>>> >> >
>>>> >> > Book.groovy:
>>>> >> > class Book {
>>>> >> >  static belongsTo = [author: Author]
>>>> >> >  String name
>>>> >> > }
>>>> >> >
>>>> >> >
>>>> >> > Can anyone fill me in on why 'savedAuth.books' is null?
>>>> >> >
>>>> >> > Thanks,
>>>> >> > Brock
>>>> >> >
>>>> >>
>>>> >> ---------------------------------------------------------------------
>>>> >> To unsubscribe from this list, please visit:
>>>> >>
>>>> >>    http://xircles.codehaus.org/manage_email
>>>> >>
>>>> >>
>>>> >
>>>> >
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>    http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>
>>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re: integration test and GORM associations

by BrockHeinz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

http://jira.codehaus.org/browse/GRAILS-3267

On Tue, Jul 22, 2008 at 7:30 AM, Graeme Rocher <graeme@...> wrote:

> Raise an issue and we can take a look (by adding flush:true to your
> saves it should be persisting the data)
>
> Cheers
>
> On Tue, Jul 22, 2008 at 12:59 PM, Brock Heinz <brock.heinz@...> wrote:
>> Here's the SQL logging:
>>
>> Running test BookTests...
>>                    testSomething...[5015] hibernate.SQL select
>> count(*) as y0_ from author this_
>> [5028] hibernate.SQL select count(*) as y0_ from book this_
>> [5103] hibernate.SQL insert into author (id, version, name) values (null, ?, ?)
>> [5104] hibernate.SQL call identity()
>> [5111] hibernate.SQL select count(*) as y0_ from author this_
>> [5150] hibernate.SQL insert into book (id, version, author_id, name)
>> values (null, ?, ?, ?)
>> [5151] hibernate.SQL call identity()
>> [5158] hibernate.SQL select count(*) as y0_ from book this_
>> FAILURE
>>
>> So, I took a look at $GRAILS_HOME/scripts/TestApp.groovy, and I think
>> I may know what is happening.  Each test method is called around life
>> cycle methods of PersistenceContextInterceptor.  Within that
>> lifecycle, 'flush' is never called.
>>
>> I even tried adding the 'static transactional = false' variable to the
>> test class and I got the same result.
>>
>> I suppose I could have the test harness dependency inject the
>> PersistenceContextInterceptor bean into my test and I can call flush()
>> myself :-\
>>
>> To me, this just seems like a bug though.  I don't want to have to MOP
>> (yes, used that as a verb again :) my command object so that my
>> **integration** test will work.
>>
>> Brock
>>
>>
>>
>> On Tue, Jul 22, 2008 at 6:40 AM, Brock Heinz <brock.heinz@...> wrote:
>>> Hello Fred,
>>>
>>> author.id is implicitly checked with the code here:
>>>
>>>> Author savedAuth = Author.get(author.id)
>>>> assert savedAuth
>>>
>>> The 'get()' method on the domain object would not return if author.id was null.
>>>
>>> I'm going to turn on SQL logging to see what's happening.
>>>
>>> Thanks,
>>> Brock
>>>
>>>
>>>
>>> On Mon, Jul 21, 2008 at 9:59 PM, Fred Janon <fjanon@...> wrote:
>>>> Hi Brock,
>>>>
>>>> I still don't see a check on "author.id" but only a "count" on the entire
>>>> collection.
>>>>
>>>> author.save(flush: true)
>>>>      assert 1 == Author.count() // <-- did you mean assert 1 == author.id
>>>> ???
>>>>
>>>> try this:
>>>>
>>>> author.save(flush: true)
>>>> println "author.id ${author.id}"
>>>>
>>>> and check your test logs.
>>>>
>>>> Fred
>>>>
>>>> On Tue, Jul 22, 2008 at 10:39, Brock Heinz <brock.heinz@...> wrote:
>>>>>
>>>>> Hey there Jason,
>>>>>
>>>>> Thanks for taking a look.  In the original post, I post the entire
>>>>> method.  As a sanity check, I assert that I can actually find the
>>>>> author with the provided id (I call flush: true on the save()).
>>>>>
>>>>> Here's that original method:
>>>>> class BookTests extends GroovyTestCase {
>>>>>    void testSomething() {
>>>>>      assert 0 == Author.count()
>>>>>      assert 0 == Book.count()
>>>>>      Author author = new Author(name: "Jonny Author")
>>>>>      author.save(flush: true)
>>>>>      assert 1 == Author.count()
>>>>>
>>>>>      Book book = new Book(author: author, name: "Some Title")
>>>>>      book.save(flush: true)
>>>>>      assert 1 == Book.count()
>>>>>
>>>>>      Author savedAuth = Author.get(author.id)
>>>>>      assert savedAuth
>>>>>      //the following will fail - 'books' is null!
>>>>>      assert 1 == savedAuth.books.size()
>>>>>    }
>>>>>
>>>>>
>>>>> Any other ideas?
>>>>>
>>>>> Thanks,
>>>>> Brock
>>>>>
>>>>> On Mon, Jul 21, 2008 at 9:03 PM, Fred Janon <fjanon@...> wrote:
>>>>> > Hi Brock,
>>>>> >
>>>>> > I think it's because "author.id" is null: the "id" is created by the
>>>>> > Hibernate or db when the instance is saved but the instance in BookTests
>>>>> > has
>>>>> > not been refreshed, the "id" has still not been assigned. Add println
>>>>> > "author.id" and see what it is.
>>>>> >
>>>>> > Fred
>>>>> >
>>>>> >
>>>>> > On Tue, Jul 22, 2008 at 07:50, Brock Heinz <brock.heinz@...>
>>>>> > wrote:
>>>>> >>
>>>>> >> bump...
>>>>> >>
>>>>> >> Anyone know why in an integration test why Book.count() returns what
>>>>> >> I'd expect, but author.books.size() does not?
>>>>> >>
>>>>> >> (see below for a more thorough explanation / example)
>>>>> >>
>>>>> >> Thanks,
>>>>> >> Brock
>>>>> >>
>>>>> >>
>>>>> >>
>>>>> >> On Mon, Jul 21, 2008 at 9:59 AM, Brock Heinz <brock.heinz@...>
>>>>> >> wrote:
>>>>> >> > Hello All,
>>>>> >> >
>>>>> >> > I'm struggling to understand why I can't use an association property
>>>>> >> > reliably in an integration test.  I understand that each integration
>>>>> >> > test is wrapped in a transaction, and then rolled back at
>>>>> >> > completion... but it still seem like the following should work:
>>>>> >> >
>>>>> >> > BookTests.groovy:
>>>>> >> > class BookTests extends GroovyTestCase {
>>>>> >> >    void testSomething() {
>>>>> >> >      assert 0 == Author.count()
>>>>> >> >      assert 0 == Book.count()
>>>>> >> >      Author author = new Author(name: "Jonny Author")
>>>>> >> >      author.save(flush: true)
>>>>> >> >      assert 1 == Author.count()
>>>>> >> >
>>>>> >> >      Book book = new Book(author: author, name: "Some Title")
>>>>> >> >      book.save(flush: true)
>>>>> >> >      assert 1 == Book.count()
>>>>> >> >
>>>>> >> >      Author savedAuth = Author.get(author.id)
>>>>> >> >      assert savedAuth
>>>>> >> >      //the following will fail - 'books' is null!
>>>>> >> >      assert 1 == savedAuth.books.size()
>>>>> >> >    }
>>>>> >> > }
>>>>> >> >
>>>>> >> > Author.groovy:
>>>>> >> > class Author {
>>>>> >> >  static hasMany = [books: Book]
>>>>> >> >  String name
>>>>> >> > }
>>>>> >> >
>>>>> >> > Book.groovy:
>>>>> >> > class Book {
>>>>> >> >  static belongsTo = [author: Author]
>>>>> >> >  String name
>>>>> >> > }
>>>>> >> >
>>>>> >> >
>>>>> >> > Can anyone fill me in on why 'savedAuth.books' is null?
>>>>> >> >
>>>>> >> > Thanks,
>>>>> >> > Brock
>>>>> >> >
>>>>> >>
>>>>> >> ---------------------------------------------------------------------
>>>>> >> To unsubscribe from this list, please visit:
>>>>> >>
>>>>> >>    http://xircles.codehaus.org/manage_email
>>>>> >>
>>>>> >>
>>>>> >
>>>>> >
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe from this list, please visit:
>>>>>
>>>>>    http://xircles.codehaus.org/manage_email
>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
>
> --
> Graeme Rocher
> Grails Project Lead
> G2One, Inc. Chief Technology Officer
> http://www.g2one.com
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price