<commit_before>package org.codehaus.groovy.grails.orm.hibernate.binding

import org.codehaus.groovy.grails.orm.hibernate.AbstractGrailsHibernateTests
import org.springframework.mock.web.MockHttpServletRequest

/**
 * @author <NAME>
 * @since 1.0
 * 
 * Created: Dec 8, 2008
 */

public class AssociationDataBindingTests extends AbstractGrailsHibernateTests{

    protected void onSetUp() {
        gcl.parseClass('''

class AssociationBindingReviewer {
    Long id
    Long version

    String name
    Integer age = 25
}
class AssociationBindingPage {
    Long id
    Long version

    Integer number
}
class AssociationBindingBook {
    Long id
    Long version

    String title
    AssociationBindingAuthor author
    List pages
    Map  reviewers
    static belongsTo = [author: AssociationBindingAuthor]
    static hasMany = [pages:AssociationBindingPage, reviewers:AssociationBindingReviewer]
}

class AssociationBindingBook2 {
    Long id
    Long version

    String title
}

class AssociationBindingAuthor {
    Long id
    Long version

    String name
    Set books
    Set moreBooks
    static hasMany = [books: AssociationBindingBook, moreBooks:AssociationBindingBook2]
}
''')

    }



    void testManyToOneBinding() {
        def Book = ga.getDomainClass("AssociationBindingBook").clazz
        def Author = ga.getDomainClass("AssociationBindingAuthor").clazz

        assert Author.newInstance(name:"<NAME>").save(flush:true)

        def book = Book.newInstance()

        def params = ['author.id':1, title:'The Shining']

        book.properties = params

        assertNotNull "The author should have been bound", book.author
        assertEquals "The Shining", book.title
    }

    void testOneToManyBindingWithSubscriptOperatorAndExistingInstance() {
        def Book = ga.getDomainClass("AssociationBindingBook").clazz
        def Author = ga.getDomainClass("AssociationBindingAuthor").clazz
        def Reviewer = ga.getDomainClass("AssociationBindingReviewer").clazz
        def Page = ga.getDomainClass("AssociationBindingPage").clazz

        def author = Author.newInstance(name: "<NAME>").save(flush: true)
        assert author
        def book = Book.newInstance(title: "The Shining", author: author).save(flush:true)
        assert book


        assert Reviewer.newInstance(name:"<NAME>", age:41).save(flush:true)
        assert Page.newInstance(number:11).save(flush:true)

        session.clear()

        author = Author.get(1)<commit_msg>GRAILS-5986: added extra test coverage for indexed collection binding<commit_after>package org.codehaus.groovy.grails.orm.hibernate.binding

import org.codehaus.groovy.grails.orm.hibernate.AbstractGrailsHibernateTests
import org.springframework.mock.web.MockHttpServletRequest

/**
 * @author <NAME>
 * @since 1.0
 * 
 * Created: Dec 8, 2008
 */

public class AssociationDataBindingTests extends AbstractGrailsHibernateTests{

    protected void onSetUp() {
        gcl.parseClass('''

class AssociationBindingReviewer {
    Long id
    Long version

    String name
    Integer age = 25
}
class AssociationBindingPage {
    Long id
    Long version

    Integer number
}
class AssociationBindingBook {
    Long id
    Long version

    String title
    AssociationBindingAuthor author
    List pages
    Map  reviewers
    static belongsTo = [author: AssociationBindingAuthor]
    static hasMany = [pages:AssociationBindingPage, reviewers:AssociationBindingReviewer]
}

class AssociationBindingBook2 {
    Long id
    Long version

    String title
}

class AssociationBindingAuthor {
    Long id
    Long version

    String name
    Set books
    Set moreBooks
    static hasMany = [books: AssociationBindingBook, moreBooks:AssociationBindingBook2]
}
''')

    }



    void testManyToOneBinding() {
        def Book = ga.getDomainClass("AssociationBindingBook").clazz
        def Author = ga.getDomainClass("AssociationBindingAuthor").clazz

        assert Author.newInstance(name:"<NAME>").save(flush:true)

        def book = Book.newInstance()

        def params = ['author.id':1, title:'The Shining']

        book.properties = params

        assertNotNull "The author should have been bound", book.author
        assertEquals "The Shining", book.title
    }

    void testManyToOneUnBinding() {
        def Book = ga.getDomainClass("AssociationBindingBook").clazz
        def Author = ga.getDomainClass("AssociationBindingAuthor").clazz

        def author = Author.newInstance(name:"<NAME>").save(flush:true, failOnError: true)
        def book = Book.newInstance(title: "The Shining", author: author).save(flush:true, failOnError: true)

        def params = ['author.id': "null"]

        book.properties = params

        assertNull "The author should have been unbound", book.author
    }

    void testOneToManyListBindingWithSubscriptOperator() {
        def Book = ga.getDomainClass("AssociationBindingBook").clazz
        def Author = ga.getDomainClass("AssociationBindingAuthor").clazz
        def Page = ga.getDomainClass("AssociationBindingPage").clazz

        def author = Author.newInstance(name: "<NAME>").save(flush: true, failOnError: true)
        def page1 = Page.newInstance(number: 1).save(flush: true, failOnError: true)
        def page2 = Page.newInstance(number: 2).save(flush: true, failOnError: true)
        def book = Book.newInstance(title: "Pattern Recognition", author: author).save(flush: true, failOnError: true)
        session.clear()
        book = book.refresh()

        def params = ["pages[0].id": "$page1.id", "pages[1].id": "$page2.id"]

        book.properties = params

        assertEquals 2, book.pages.size()
        assertEquals 1, book.pages[0].number
        assertEquals 2, book.pages[1].number
    }

    void testOneToManyListUnBindingWithSubscriptOperator() {
        def Book = ga.getDomainClass("AssociationBindingBook").clazz
        def Author = ga.getDomainClass("AssociationBindingAuthor").clazz
        def Page = ga.getDomainClass("AssociationBindingPage").clazz

        def author = Author.newInstance(name: "<NAME>").save(flush: true, failOnError: true)
        def page = Page.newInstance(number: 1).save(flush: true, failOnError: true)
        def book = Book.newInstance(title: "Pattern Recognition", author: author, pages: [page]).save(flush: true, failOnError: true)
        session.clear()
        book = book.refresh()

        def params = ["pages[0].id": "null"]

        book.properties = params

        assertNull "Should have removed pages[0] but it is ${book.pages[0]}", book.pages[0]
    }

    void testOneToManyMapUnBindingWithSubscriptOperator() {
        def Book = ga.getDomainClass("AssociationBindingBook").clazz
        def Author = ga.getDomainClass("AssociationBindingAuthor").clazz
        def Reviewer = ga.getDomainClass("AssociationBindingReviewer").clazz

        def author = Author.newInstance(name: "<NAME>").save(flush: true, failOnError: true)
        def reviewer = Reviewer.newInstance(name: "<NAME>").save(flush: true, failOnError: true)
        def book = Book.newInstance(title: "Grails In Action", author: author, reviewers: [rob: reviewer]).save(flush: true, failOnError: true)
        session.clear()
        book = book.refresh()

        def params = ["reviewers[rob].id": "null"]

        book.properties = params

        assertNull "Should have removed reviewers[rob] but it is ${book.reviewers['rob']}", book.reviewers["rob"]
    }

    void testOneToManyBindingWithSubscriptOperatorAndExistingInstance() {
        def Book = ga.getDomainClass("AssociationBindingBook").clazz
        def Author = ga.getDomainClass("AssociationBindingAuthor").clazz
        def Reviewer = ga.getDomainClass("AssociationBindingReviewer").clazz
        def Page = ga.getDomainClass("AssociationBindingPage").clazz

        def author = Author.newInstance(name: "<NAME>").save(flush: true)
        assert author
        def book = Book.newInstance(title: "The Shining", author: author).save(flush:true)
        assert book


        assert Reviewer.newInstance(name:"<NAME>", age:41).save(flush:true)
        assert Page.newInstance(number:11).save(flush:true)

        session.clear()

        author = Author.get(1)
{'fraction_non_alphanumeric': 0.10253596683735675, 'fraction_numerical': 0.006827603023652768, 'mean_word_length': 3.410215053763441, 'pattern_counts': {'":': 4, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, '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': '3877794', 'n_tokens_mistral': 2356, 'n_tokens_neox': 2194, 'n_words': 537}