source: trunk/test/unit/HqlBuilderTests.groovy @ 641

Last change on this file since 641 was 641, checked in by gav, 14 years ago

HqlBuilder? and tests, draftA.

File size: 4.2 KB
Line 
1/* Copyright 2010 the original author or authors.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import org.apache.commons.logging.LogFactory
17
18/**
19 * Unit tests for HqlBuilder class.
20 * GroovyTestCase is used so that class does not depend on Grails as it may be useful outside of Grails.
21 *
22 * @author Gavin Kromhout
23 * @version DraftA
24 *
25 */
26public class HqlBuilderTests extends GroovyTestCase {
27
28    def n = '\n'
29    def savedMetaClass
30
31    protected void setUp() {
32        super.setUp()
33        savedMetaClass = HqlBuilder.metaClass
34        def emc = new ExpandoMetaClass(HqlBuilder, true, true)
35        //emc.log = LogFactory.getLog(getClass())
36        emc.initialize()
37        GroovySystem.metaClassRegistry.setMetaClass(HqlBuilder, emc)
38    }
39
40    protected void tearDown() {
41        GroovySystem.metaClassRegistry.removeMetaClass(HqlBuilder)
42        GroovySystem.metaClassRegistry.setMetaClass(HqlBuilder, savedMetaClass)
43        super.tearDown()
44    }
45
46    void testSelectBasic() {
47
48        def q = new HqlBuilder().query {
49            select 'count(distinct book)'
50            from 'Book as book'
51            where 'book.id > 100'
52        }
53
54        assert q.query == 'select count(distinct book) from Book as book where book.id > 100'
55
56        q.select = 'distinct book'
57        assert q.query == 'select distinct book from Book as book where book.id > 100'
58        assert q.printFormattedQuery == 'select distinct book \nfrom Book as book \nwhere book.id > 100'
59
60    } // testSelectBasic()
61
62    void testSelectAdditional() {
63
64        def q = new HqlBuilder().query {
65            select 'distinct book'
66            from 'Book as book'
67            left 'join book.group as group',
68                    'left join group.type as type'
69            where 'book.id > 100',
70                        'and group = :group'
71        }
72
73        assert q.query == 'select distinct book from Book as book left join book.group as group left join group.type as type where book.id > 100 and group = :group'
74
75    } // testSelectAdditional()
76
77    void testSelectAlternate() {
78
79        def q = new HqlBuilder()
80
81        q {
82            select 'distinct book'
83            from 'Book as book'
84            where(/book.name like '%Ned%'/) // Slashy string literals have to be protected when calling a function.
85            where 'and book.description like "Head"'
86        }
87
88        assert q.query == /select distinct book from Book as book where book.name like '%Ned%' and book.description like "Head"/
89        assert q.printFormattedQuery == /select distinct book ${n}from Book as book ${n}where book.name like '%Ned%' ${n}and book.description like "Head"/
90
91    } // testSelectAlternate()
92
93    void testSelectWithPlaceHolder() {
94
95        def q = new HqlBuilder().query {
96            select 'distinct book'
97            from 'Book as book'
98            where '' // Place holder.
99            order 'by book.name asc'
100        }
101
102        // Insert to place holder which is in the middle of query string.
103        q.where = /book.name like '%Ned%'/  // Slashy string literals don't need protecting when assigning.
104
105        assert q.query == /select distinct book from Book as book where book.name like '%Ned%' order by book.name asc/
106
107    } // testSelectWithPlaceHolder()
108
109    void testSelectWithClauseRemoval() {
110
111        def q = new HqlBuilder().query {
112            select 'count(distinct book)'
113            from 'Book as book'
114            where = /book.name like '%Ned%'/  // Slashy string literals don't need protecting when assigning.
115            order 'by book.name asc'
116        }
117
118        q.order = null // Remove clause, since order by makes no sense when selecting a count ;-)
119        assert q.query == /select count(distinct book) from Book as book where book.name like '%Ned%'/
120
121    } // testSelectWithClauseRemoval()
122
123} // end class
Note: See TracBrowser for help on using the repository browser.