Grails
  1. Grails
  2. GRAILS-8826

Named Query isNull in test context does not work on foreign key relations

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: 2.0.1
    • Fix Version/s: 2.0.4
    • Component/s: Persistence
    • Labels:
    • Environment:
      Grails v2.0.1 on IntelliJ IDEA v11

      Description

      We have the following model:

      A.java
      class A {
      
        String name
        B b
      
        static namedQueries = {
          withoutB{
            isNull('b')
          }
        }
      
      }
      
      B.java
      class B {}
      

      Furthermore, the following test is in place:

      ATests.java
      import grails.test.mixin.*
      import static org.junit.Assert.assertEquals
      
      @TestFor(A)
      class ATests {
      
        void testIsNull() {
          new A(name: "alpha").save(flush: true, validate: false)
          def list = A.list()
          assertEquals(1, list.size())
          assertEquals("alpha", list.get(0).name)
          assertEquals(null, list.get(0).b)
          def results = A.withoutB
          assertEquals 1, results.count()
        }
      }
      

      What you see here is that an instance of type A is created, and that its field 'b' is null. This is proven by the assert statements in the testIsNull method, where the record is fetched and checked for a null value in field 'b'.

      Now the named query 'withoutB' is called, which must return all objects where 'b' is null. Note that this named query is defined in the body of class A.

      The result now is that no results are returned, instead of the expected 1 record.

      If isNull('b' is changed to isNotNull('b'), 1 result is returned.

      Remarkable is that isNull seems to work fine when the application is run under the full Grails context.

      It has all appearances that isNull in a test context has a different implementation from the real context and that the test context implementation has a bug.

        Activity

        Robert Bor created issue -
        Graeme Rocher made changes -
        Field Original Value New Value
        Fix Version/s 2.0.2 [ 13064 ]
        Priority Major [ 3 ] Critical [ 2 ]
        Graeme Rocher made changes -
        Fix Version/s 2.0.3 [ 13094 ]
        Fix Version/s 2.0.2 [ 13064 ]
        Graeme Rocher made changes -
        Fix Version/s 2.0.4 [ 13099 ]
        Fix Version/s 2.0.3 [ 13094 ]
        Graeme Rocher made changes -
        Assignee Graeme Rocher [ graemerocher ]
        Graeme Rocher made changes -
        Status Open [ 1 ] In Progress [ 3 ]
        Show
        Graeme Rocher added a comment - fixed by https://github.com/SpringSource/grails-data-mapping/commit/7e6f6b3c0ef40209941cf4d5d0762d99c8bbb743
        Graeme Rocher made changes -
        Status In Progress [ 3 ] Closed [ 6 ]
        Resolution Fixed [ 1 ]

          People

          • Assignee:
            Graeme Rocher
            Reporter:
            Robert Bor
          • Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development