pep
pep

Reputation: 107

Grails - is table ID default not nullable or yes?

I have a problem with testing domain validation. Here is my unit test:

@Unroll
    void "Validate domain object SecUser when id:#id, username:#username, 
password:#password"() {
        when:
        SecUser user = new SecUser(id: id, username: username, password: 
password, enabled: "true", accountExpired: "false", accountLocked: 
"false", passwordExpired: "false")

    def userSave = user.validate()

    then:
    assert userSave == expected

    where:
    id      |         username          |   password    ||      expected
    527     |     "[email protected]"     |   "test"      ||        true
    null    |     "[email protected]"     |   "test"      ||        false
    528     |           null            |   "test"      ||        false
    529     |     "[email protected]"     |   null        ||        true
    ''      |     "[email protected]"     |   "test"      ||        false
    530     |           ''              |   "test"      ||        false
    555     |     "[email protected]"     |     ''        ||        true
    557     | "[email protected], [email protected]" |   "test"      ||        false

}

The problem is that the validation result is true even when id is null or ''. I didn't expect that. This is SecUser domain:

class SecUser {

Long id
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
String sifpartnera
Long dodadresaid
String sifosobe
Long coid
String langid


static constraints = {
    id(blank: false)
    username blank: false, unique: true, email: true
    password nullable: true
    enabled()
    accountExpired()
    accountLocked()
    passwordExpired()
    sifpartnera nullable: true
    dodadresaid nullable: true
    sifosobe nullable: true
    coid nullable: true
    langid nullable: true
}

static mapping = {
    version false
    table name: "sec_user", schema: "public"
    password column: '`password`'
    id generator: 'sequence', column: 'id'
}
}

I don't understand what's the problem. Am I doing something wrong? Thank you.

Upvotes: 1

Views: 40

Answers (1)

injecteer
injecteer

Reputation: 20709

The id is not checked by default in validate() method. The reason behind it is quite simple: at the very moment of instantiation the id is null, and is get set only after calling save().

Upvotes: 4

Related Questions