<commit_before>package grails.gorm.tests.validation

import grails.gorm.annotation.Entity
import grails.gorm.tests.GormDatastoreSpec
import grails.gorm.transactions.Rollback
import org.springframework.dao.DataIntegrityViolationException
import spock.lang.Issue

/**
 * Created by francoiskha on 19/04/18.
 */
class DeepValidationSpec extends GormDatastoreSpec {

    @Override
    List getDomainClasses() {
        return [City, Market, Address]
    }

    @Rollback
    @Issue('https://github.com/grails/grails-data-mapping/issues/1033')
    void "performs deep validation correctly"() {

        when: "save market with failing custom validator on child"
        new Market(name: "Main", address: new Address(streetName: "Main St.", landmark: "The Golder Gate Bridge", postalCode: "11")).save(deepValidate: false)

        then: "market is saved, no validation error"
        Market.count() == 1

        when: "save market with nullable on child"
        new Market(name: "NIT", address: new Address(landmark: "1B, Main St.", postalCode: "121001")).save(deepValidate: false)

        then:
        thrown(DataIntegrityViolationException)

        when: "nested validation fails"
        new City(name: "Faridabad").addToMarkets(name: "NIT 1", address: new Address(streetName: "1B, Main St.", landmark: "V2", postalCode: "11")).save(deepValidate: false)

        then: "market is saved, no validation error"
        City.count() == 1
        Market.count() == 2
        Address.count() == 2

        when: "invalid embedded object"
        new City(name: "St. Louis", country: new Country()).save(deepValidate: false)

        then: "should save the city"
        City.count() == 2
        Country.count() == 0
    }
}

@Entity
class City {

    String name
    Country country

    static hasMany = [markets: Market]
    static embedded = ['country']
    static constraints = {
        country nullable: true
    }

}
<commit_msg>grails/grails-data-mapping#1033 Setting deepValidate to false will not skip validation before flush so updating tests<commit_after>package grails.gorm.tests.validation

import grails.gorm.annotation.Entity
import grails.gorm.tests.GormDatastoreSpec
import grails.gorm.transactions.Rollback
import org.springframework.dao.DataIntegrityViolationException
import spock.lang.Issue

/**
 * Created by francoiskha on 19/04/18.
 */
class DeepValidationSpec extends GormDatastoreSpec {

    @Override
    List getDomainClasses() {
        return [City, Market, Address]
    }

    @Rollback
    @Issue('https://github.com/grails/grails-data-mapping/issues/1033')
    void "performs deep validation correctly"() {

        when: "save market with failing custom validator on child"
        Address address = new Address(streetName: "Main St.", landmark: "The Golder Gate Bridge", postalCode: "11").save(validate: false)
        new Market(name: "Main", address: address).save(deepValidate: false)

        then: "market is saved, no validation error"
        Market.count() == 1

        when: "save market with nullable on child"
        address = new Address(landmark: "1B, Main St.", postalCode: "121001").save(validate: false)
        new Market(name: "NIT", address: address).save(deepValidate: false)

        then:
        thrown(DataIntegrityViolationException)

        when: "nested validation fails"
        address = new Address(streetName: "1B, Main St.", landmark: "V2", postalCode: "11").save(validate: false)
        new City(name: "Faridabad").addToMarkets(name: "NIT 1", address: address).save(deepValidate: false)

        then: "market is saved, no validation error"
        City.count() == 1
        Market.count() == 2
        Address.count() == 2

        when: "invalid embedded object"
        new City(name: "St. Louis", country: new Country()).save(deepValidate: false)

        then: "should save the city"
        City.count() == 2
        Country.count() == 0
    }
}

@Entity
class City {

    String name
    Country country

    static hasMany = [markets: Market]
    static embedded = ['country']
    static constraints = {
        country nullable: true
    }

}

{'fraction_non_alphanumeric': 0.11475012130033964, 'fraction_numerical': 0.015526443474041727, 'mean_word_length': 3.143718592964824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4829287', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1188, 'n_words': 349}