Why is the grails.gorm.autoFlush parameter not working?

Consider the following code:

class User {
    static constraints = {
        email email: true, unique: true
        token nullable: true
    }

    String email
    String password
    String token
}

@TestFor(User)
@TestMixin(DomainClassUnitTestMixin)
class UserSpec extends Specification {
    def "email should be unique"() {
        when: "twice same email"
        def user = new User(email: "test@test.com", password: "test")
        def user2 = new User(email: "test@test.com", password: "test")

        then: "saving should fail"
        user.save()
        !user2.save(failOnError: false)
    }
}

With the following configuration (part of it), application.yml:

grails:
    gorm:
        failOnError: true
        autoFlush: true

Why user2.save(failOnError: false)doesn’t it return falsebecause it is not stored in the database?

The result of work grails test-app *UserSpec::

businesssoftware.UserSpec> email must be unique. Faulty org.spockframework.runtime.ConditionNotSatisfiedError in UserSpec.groovy: 40

When I replace user.save()with user.save(flush: true), it works.

However, the documentation at https://grails.imtqy.com/grails-doc/latest/guide/conf.html , section 4.1.3 states that:

grails.gorm.autoFlush - true, , , , save (flush: true).

grails --version:

| Grails : 3.0.2
  | Groovy : 2.4.3
  | JVM: 1.8.0_40

, ?

+4
1

, autoFlush .

grails.gorm.autoFlush - true, , , flush save (flush: true).

save() doSave() GormInstanceApi. doSave() , , flush true:

if (params?.flush) {
    session.flush()
}

, flush , , save().

, , , autoFlush , Hibernate , , .

, GORM, Spock JUnit, GORM, autoFlush.

. , .

@TestFor(User)
@TestMixin(DomainClassUnitTestMixin)
class UserSpec extends Specification {
    def "email should be unique"() {
        when: "twice same email"
        def user = new User(email: "test@test.com", password: "test")
        def user2 = new User(email: "test@test.com", password: "test")
        simpleDatastore.currentSession.flush()

        then: "saving should fail"
        user.save()
        !user2.save(failOnError: false)
    }
}

simpleDataStore DomainClassUnitTestMixin.

+1

All Articles