001: /*
002: * $Id: TestFunctions.java,v 1.11 2005/03/09 02:13:02 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.functional;
042:
043: import java.sql.SQLException;
044: import java.sql.Timestamp;
045:
046: import junit.framework.Test;
047: import junit.framework.TestSuite;
048:
049: /**
050: * @version $Revision: 1.11 $ $Date: 2005/03/09 02:13:02 $
051: * @author Rodney Waldhoff
052: */
053: public class TestFunctions extends AbstractFunctionalTest {
054:
055: //------------------------------------------------------------ Conventional
056:
057: public TestFunctions(String testName) {
058: super (testName);
059: }
060:
061: public static Test suite() {
062: return new TestSuite(TestFunctions.class);
063: }
064:
065: //--------------------------------------------------------------- Lifecycle
066:
067: //------------------------------------------------------------------- Tests
068:
069: public void testAggregateFunctions() throws Exception {
070: _stmt.execute("create table foo ( val int )");
071:
072: assertResult(new Object[] { null }, "select avg(val) from foo");
073: assertResult(new Object[] { null }, "select max(val) from foo");
074: assertResult(new Object[] { null }, "select min(val) from foo");
075: assertResult(new Object[] { null }, "select sum(val) from foo");
076: assertResult(0, "select count(val) from foo");
077:
078: _stmt.execute("insert into foo values ( 3 )");
079: assertResult(3, "select avg(val) from foo");
080:
081: _stmt.execute("insert into foo values ( 2 )");
082: _stmt.execute("insert into foo values ( 4 )");
083: assertResult(3, "select avg(val) from foo");
084: _stmt.execute("insert into foo values ( 1 )");
085: _stmt.execute("insert into foo values ( 5 )");
086: assertResult(3, "select avg(val) from foo");
087: _stmt.execute("insert into foo values ( 1 )");
088: _stmt.execute("insert into foo values ( 1 )");
089: _stmt.execute("insert into foo values ( 1 )");
090: _stmt.execute("insert into foo values ( 1 )");
091: _stmt.execute("insert into foo values ( 1 )");
092: _stmt.execute("insert into foo values ( 1 )");
093: _stmt.execute("insert into foo values ( 1 )");
094: _stmt.execute("insert into foo values ( 1 )");
095: _stmt.execute("insert into foo values ( 2 )");
096: _stmt.execute("insert into foo values ( 1 )");
097: assertResult(15, "select count(val) from foo");
098: assertResult(5, "select count(distinct val) from foo");
099:
100: try {
101: assertResult(5, "select count(distinct *) from foo");
102: fail("Expected Exception");
103: } catch (Exception e) {
104: // expected
105: }
106:
107: assertResult(2, "select avg(val) from foo");
108: assertResult(3, "select avg(distinct val) from foo");
109:
110: assertResult(1, "select min(val) from foo");
111: assertResult(5, "select max(val) from foo");
112: }
113:
114: public void testExists() throws Exception {
115: _stmt.execute("create table foo ( val int )");
116: _stmt.execute("insert into foo values ( 3 )");
117: assertResult(1, "select 1 where exists (select * from foo)");
118: assertResult(1,
119: "select 1 where exists (select val from foo where val = 3)");
120: assertNoRows("select 1 where exists (select val from foo where val = 2)");
121: }
122:
123: public void testNotExists() throws Exception {
124: _stmt.execute("create table foo ( val int )");
125: _stmt.execute("insert into foo values ( 3 )");
126: assertNoRows("select 1 where not exists (select * from foo)");
127: assertNoRows("select 1 where not exists (select val from foo where val = 3)");
128: assertResult(1,
129: "select 1 where not exists (select val from foo where val = 2)");
130: }
131:
132: public void testCoalesce() throws Exception {
133: assertResult(1, "select coalesce(null,null,1,null)");
134: assertResult(3, "select coalesce(null,3,2,null)");
135: assertResult(1, "select coalesce(1)");
136: assertNullResult("select coalesce(null)");
137: assertNullResult("select coalesce(null,null)");
138: }
139:
140: public void testDateAdd() throws Exception {
141: long time = 1083609680013000L;
142: assertObjectResult(new Timestamp(time),
143: "select dateadd(millisecond,0," + time + ")");
144: assertObjectResult(new Timestamp(time + 2),
145: "select dateadd(millisecond,2," + time + ")");
146: assertObjectResult(new Timestamp(time + 2 * 1000),
147: "select dateadd(second,2," + time + ")");
148: assertObjectResult(new Timestamp(time + 2 * 60 * 1000),
149: "select dateadd(minute,2," + time + ")");
150: assertObjectResult(new Timestamp(time + 2 * 60 * 60 * 1000),
151: "select dateadd(hour,2," + time + ")");
152: assertObjectResult(
153: new Timestamp(time + 2 * 24 * 60 * 60 * 1000),
154: "select dateadd(day,2," + time + ")");
155: assertObjectResult(new Timestamp(time + 2 * 7 * 24 * 60 * 60
156: * 1000), "select dateadd(week,2," + time + ")");
157: }
158:
159: public void testDateDiff() throws Exception {
160: long time1 = 1083609680013000L;
161: long time2 = time1 + 2 * 7 * 24 * 60 * 60 * 1000;
162: assertObjectResult(new Long(2 * 7 * 24 * 60 * 60 * 1000),
163: "select datediff(millisecond," + time1 + "," + time2
164: + ")");
165: assertObjectResult(new Long(2 * 7 * 24 * 60 * 60),
166: "select datediff(second," + time1 + "," + time2 + ")");
167: assertObjectResult(new Long(2 * 7 * 24 * 60),
168: "select datediff(minute," + time1 + "," + time2 + ")");
169: assertObjectResult(new Long(2 * 7 * 24),
170: "select datediff(hour," + time1 + "," + time2 + ")");
171: assertObjectResult(new Long(2 * 7), "select datediff(day,"
172: + time1 + "," + time2 + ")");
173: assertObjectResult(new Long(2), "select datediff(week," + time1
174: + "," + time2 + ")");
175: }
176:
177: public void testDivisionByZero() throws Exception {
178: try {
179: assertResult(0, "Select 20/0");
180: fail("Expected Exception");
181: } catch (SQLException e) {
182: // expected
183: }
184:
185: try {
186: assertResult(0, "Select 20/0.0");
187: fail("Expected Exception");
188: } catch (SQLException e) {
189: // expected
190: }
191:
192: assertResult(10000, "Select 1/0.0001");
193:
194: }
195: }
|