-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Merge pull request #913 from jdaugherty/feature/9.0.x/1155-skip-validate
fix #1155 - skipValidate logic in GormValidateable causes missed validations and is inconsistent
Showing
2 changed files
with
112 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...re-gorm-hibernate5/src/test/groovy/grails/gorm/tests/validation/SkipValidationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package grails.gorm.tests.validation | ||
|
||
import grails.gorm.annotation.Entity | ||
import grails.gorm.transactions.Rollback | ||
import grails.validation.ValidationException | ||
import org.grails.orm.hibernate.HibernateDatastore | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
class SkipValidationSpec extends Specification { | ||
@Shared @AutoCleanup HibernateDatastore hibernateDatastore = new HibernateDatastore(Author) | ||
|
||
// For whatever reason it may be valid to flush & save without validation (database would obviously fail if the field is too long, but maybe the object is expected to only have an invalid validator?) so continue to support this scenario | ||
@Rollback | ||
void "calling save with flush with validate false should skip validation"() { | ||
when: | ||
new Author(name: 'false').save(failOnError: true, validate: false, flush: true) | ||
|
||
then: | ||
noExceptionThrown() | ||
} | ||
|
||
@Rollback | ||
void "calling save with flush and invalid attribute"() { | ||
when: | ||
new Author(name: 'ThisNameIsTooLong').save(failOnError: true, flush: true) | ||
|
||
then: | ||
thrown(ValidationException) | ||
} | ||
|
||
@Rollback | ||
void "calling validate with property list after save should validate again"() { | ||
// Save but don't flush, this causes the new author to have skipValidate = true | ||
Author author = new Author(name: 'Aaron').save(failOnError: true) | ||
|
||
when: "validate is called again with a property list" | ||
author.name = "ThisNameIsTooLong" | ||
def isValid = author.validate(['name']) | ||
|
||
then: "it should be invalid but it skips validation instead" | ||
!isValid | ||
} | ||
|
||
@Rollback | ||
void "calling validate with property list after save with flush should validate again"() { | ||
// Save but don't flush, this causes the new author to have skipValidate = true | ||
Author author = new Author(name: 'Aaron').save(failOnError: true, flush: true) | ||
|
||
when: "validate is called again with a property list" | ||
author.name = "ThisNameIsTooLong" | ||
def isValid = author.validate(['name']) | ||
|
||
then: "it should be invalid but it skips validation instead" | ||
!isValid | ||
} | ||
|
||
@Rollback | ||
void "calling validate with property list after save should validate again on explicit flush"() { | ||
// Save but don't flush, this causes the new author to have skipValidate = true | ||
Author author = new Author(name: 'Aaron').save(failOnError: true) | ||
|
||
when: "validate is called again with a property list" | ||
author.name = "ThisNameIsTooLong" | ||
Author.withSession { session -> | ||
session.flush() | ||
} | ||
|
||
then: | ||
author.hasErrors() | ||
} | ||
|
||
@Rollback | ||
void "calling validate with no list after save should validate again"() { | ||
// Save but don't flush, this causes the new author to have skipValidate = true | ||
Author author = new Author(name: 'Aaron').save(failOnError: true) | ||
|
||
when: "validate is called again without any parameters" | ||
author.name = "ThisNameIsTooLong" | ||
def isValid = author.validate() | ||
|
||
then: "this works since validate without params doesn't honor skipValidate for some reason" | ||
!isValid | ||
} | ||
} | ||
|
||
@Entity | ||
class Author { | ||
String name | ||
|
||
static constraints = { | ||
name(nullable: false, maxSize: 8, validator: { val, obj -> | ||
if(val == "false") { | ||
return "name.invalid" | ||
} | ||
|
||
println "Validate called" | ||
true | ||
}) | ||
} | ||
} |