/*
// $Id: //open/mondrian-release/3.0/testsrc/main/mondrian/olap/fun/FunctionTest.java#4 $
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2003-2008 Julian Hyde and others
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/
package mondrian.olap.fun;
import junit.framework.Assert;
import junit.framework.ComparisonFailure;
import mondrian.olap.*;
import mondrian.test.*;
import mondrian.resource.MondrianResource;
import mondrian.udf.CurrentDateMemberExactUdf;
import mondrian.udf.CurrentDateMemberUdf;
import mondrian.udf.CurrentDateStringUdf;
import mondrian.util.Bug;
import org.eigenbase.xom.StringEscaper;
import java.io.*;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
/**
* <code>FunctionTest</code> tests the functions defined in
* {@link BuiltinFunTable}.
*
* @author gjohnson
* @version $Id: //open/mondrian-release/3.0/testsrc/main/mondrian/olap/fun/FunctionTest.java#4 $
*/
public class FunctionTest extends FoodMartTestCase {
private static final String months =
fold("[Time].[1997].[Q1].[1]\n" +
"[Time].[1997].[Q1].[2]\n" +
"[Time].[1997].[Q1].[3]\n" +
"[Time].[1997].[Q2].[4]\n" +
"[Time].[1997].[Q2].[5]\n" +
"[Time].[1997].[Q2].[6]\n" +
"[Time].[1997].[Q3].[7]\n" +
"[Time].[1997].[Q3].[8]\n" +
"[Time].[1997].[Q3].[9]\n" +
"[Time].[1997].[Q4].[10]\n" +
"[Time].[1997].[Q4].[11]\n" +
"[Time].[1997].[Q4].[12]");
private static final String quarters =
fold("[Time].[1997].[Q1]\n" +
"[Time].[1997].[Q2]\n" +
"[Time].[1997].[Q3]\n" +
"[Time].[1997].[Q4]");
private static final String year1997 = "[Time].[1997]";
private static final String hierarchized1997 =
fold(year1997 + "\n" +
"[Time].[1997].[Q1]\n" +
"[Time].[1997].[Q1].[1]\n" +
"[Time].[1997].[Q1].[2]\n" +
"[Time].[1997].[Q1].[3]\n" +
"[Time].[1997].[Q2]\n" +
"[Time].[1997].[Q2].[4]\n" +
"[Time].[1997].[Q2].[5]\n" +
"[Time].[1997].[Q2].[6]\n" +
"[Time].[1997].[Q3]\n" +
"[Time].[1997].[Q3].[7]\n" +
"[Time].[1997].[Q3].[8]\n" +
"[Time].[1997].[Q3].[9]\n" +
"[Time].[1997].[Q4]\n" +
"[Time].[1997].[Q4].[10]\n" +
"[Time].[1997].[Q4].[11]\n" +
"[Time].[1997].[Q4].[12]");
private static final String NullNumericExpr = (" ([Measures].[Unit Sales]," +
" [Customers].[All Customers].[USA].[CA].[Bellflower], " +
" [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Beer].[Good].[Good Imported Beer])");
/**
* Tests that Integeer.MIN_VALUE(-2147483648) does not cause NPE.
*/
public void testParallelPeriodMinValue() {
String query =
"with " +
"member [measures].[foo] as " +
"'([Measures].[unit sales],ParallelPeriod([Time].[Quarter], -2147483648))' " +
"select " +
"[measures].[foo] on columns, " +
"[time].[1997].children on rows " +
"from [sales]";
executeQuery(query);
}
/**
* Tests that Integeer.MIN_VALUE(-2147483648) in Lag is handled correctly.
*/
public void testLagMinValue() {
String query =
"with " +
"member [measures].[foo] as " +
"'([Measures].[unit sales], [Time].[1997].[Q1].Lag(-2147483648))' " +
"select " +
"[measures].[foo] on columns, " +
"[time].[1997].children on rows " +
"from [sales]";
executeQuery(query);
}
/*
* Tests that ParallelPeriod with Aggregate function works
*/
public void testParallelPeriodWithSlicer() {
String query =
"With " +
"Set [*NATIVE_CJ_SET] as 'NonEmptyCrossJoin([*BASE_MEMBERS_Time],[*BASE_MEMBERS_Product])' " +
"Set [*BASE_MEMBERS_Measures] as '{[Measures].[*FORMATTED_MEASURE_0], [Measures].[*FORMATTED_MEASURE_1]}' " +
"Set [*BASE_MEMBERS_Time] as '{[Time].[1997].[Q2].[6]}' " +
"Set [*NATIVE_MEMBERS_Time] as 'Generate([*NATIVE_CJ_SET], {[Time].CurrentMember})' " +
"Set [*BASE_MEMBERS_Product] as '{[Product].[All Products].[Drink],[Product].[All Products].[Food]}' " +
"Set [*NATIVE_MEMBERS_Product] as 'Generate([*NATIVE_CJ_SET], {[Product].CurrentMember})' " +
"Member [Measures].[*FORMATTED_MEASURE_0] as '[Measures].[Customer Count]', FORMAT_STRING = '#,##0', SOLVE_ORDER=400 " +
"Member [Measures].[*FORMATTED_MEASURE_1] as " +
"'([Measures].[Customer Count], ParallelPeriod([Time].[Quarter], 1, [Time].currentMember))', FORMAT_STRING = '#,##0', SOLVE_ORDER=-200 " +
"Member [Product].[*FILTER_MEMBER] as 'Aggregate ([*NATIVE_MEMBERS_Product])', SOLVE_ORDER=-300 " +
"Select " +
"[*BASE_MEMBERS_Measures] on columns, Non Empty Generate([*NATIVE_CJ_SET], {([Time].CurrentMember)}) on rows " +
"From [Sales] " +
"Where ([Product].[*FILTER_MEMBER])";
String result =
"Axis #0:\n" +
"{[Product].[*FILTER_MEMBER]}\n" +
"Axis #1:\n" +
"{[Measures].[*FORMATTED_MEASURE_0]}\n" +
"{[Measures].[*FORMATTED_MEASURE_1]}\n" +
"Axis #2:\n" +
"{[Time].[1997].[Q2].[6]}\n" +
"Row #0: 1,314\n" +
"Row #0: 1,447\n";
assertQueryReturns(query, fold(result));
}
public void testNumericLiteral() {
assertExprReturns("2", "2");
if (false) {
// The test is currently broken because the value 2.5 is formatted
// as "2". TODO: better default format string
assertExprReturns("2.5", "2.5");
}
assertExprReturns("-10.0", "-10");
getTestContext().assertExprDependsOn("1.5", "{}");
}
public FunctionTest() {
}
public FunctionTest(String s) {
super(s);
}
public void testStringLiteral() {
// single-quoted string
if (false) {
// TODO: enhance parser so that you can include a quoted string
// inside a WITH MEMBER clause
assertExprReturns("'foobar'", "foobar");
}
// double-quoted string
assertExprReturns("\"foobar\"", "foobar");
// literals don't depend on any dimensions
getTestContext().assertExprDependsOn("\"foobar\"", "{}");
}
public void testDimensionHierarchy() {
assertExprReturns("[Time].Dimension.Name", "Time");
}
public void testLevelDimension() {
assertExprReturns("[Time].[Year].Dimension.UniqueName", "[Time]");
}
public void testMemberDimension() {
assertExprReturns("[Time].[1997].[Q2].Dimension.UniqueName", "[Time]");
}
public void testDimensionsNumeric() {
getTestContext().assertExprDependsOn("Dimensions(2).Name", "{}");
getTestContext().assertMemberExprDependsOn("Dimensions(3).CurrentMember",
TestContext.allDims());
assertExprReturns("Dimensions(2).Name", "Store Size in SQFT");
// bug 1426134 -- Dimensions(0) throws 'Index '0' out of bounds'
assertExprReturns("Dimensions(0).Name", "Measures");
assertExprThrows("Dimensions(-1).Name", "Index '-1' out of bounds");
assertExprThrows("Dimensions(100).Name", "Index '100' out of bounds");
}
public void testDimensionsString() {
getTestContext().assertExprDependsOn("Dimensions(\"foo\").UniqueName",
"{}");
getTestContext().assertMemberExprDependsO
评论0