Вопрос

One of the reasons we are moving from Grails 1.3.7 to 2.2 is for the ability to unit test our (many) criteria.

And the first thing we have run into with criteria testing is this:

Property [nightly.id] is not a valid property of class [com.litle.bldvwr.Result] at grails.gorm.CriteriaBuilder.validatePropertyName(CriteriaBuilder.java:968)

What we have is a simple many to one relationship between result and nightly. Each Result has 1 nightly. There is no direct relationship between Nightly and Result.

Updated

The specific code is: Nightly.groovy:

package examples

import java.util.Date;

class Nightly {
    String name 
    String status

    static constraints = {
        status nullable:true
        name unique: true, nullable: false, blank: false
    }

    static mapping = {
    }
    String toString () {
        name +' ' + status
    }


}

Result.groovy:

package examples

import java.util.Date;

class Result {
String status
String name
String type
Nightly nightly



static mapping = {
}

Result() {
}

static def gimmeCountByNightlyAndStatusAndRerunIsNull(def nightly, def status) {
    def count = Result.createCriteria().count {
        and {
            eq('nightly.id', nightly.id)
            eq('status', status)
            isNull('rerun')
        }
    }
    return count
}

}

ResultTests.groovy

package examples

import grails.test.mixin.*
import org.junit.*

@TestFor(Result)
@Mock(Nightly)
class ResultTests {

void testCriteriaMess ()    {

            //this test will fail due to: Property [nightly.id] is not a valid property of class [examples.Result]

            Nightly night = new Nightly( name:'nightly1', status:'Success')
            night.save(validate:false)


            Result res = new Result(status: 'SUCCESS', type: 'INTEGRATION')
            res.nightly = night
            res.save(validate:false, flush:true)

            def count = Result.gimmeCountByNightlyAndStatusAndRerunIsNull(night, 'SUCCESS')
            assertTrue count==0
        }
}

The above code is from a brand new Grails 2.2 project as created with GGTS 3.1 Except for the code above, everything else in the project is complete boiler plate has generated.

I would expect that, as it does against the actual database, 'nightly.id' would be translated into the nightly_id column of the Result table.

Is this an issue with how GORM is being mocked in 2.2, or are we missing something?

Это было полезно?

Решение

You should change the query to

def count = Result.createCriteria().count {
        and {
            eq('nightly', nightly)
            eq('status', status)
            isNull('rerun')
        }
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top