Skip to content

Commit

Permalink
MONDRIAN: Make the UDF tests less sensitive to architecture-dependent…
Browse files Browse the repository at this point in the history
… floating point differences.

[git-p4: depot-paths = "//open/mondrian/": change = 3819]
  • Loading branch information
julianhyde committed Jul 15, 2005
1 parent 6efc66c commit b823f43
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/main/mondrian/xmla/RowsetDefinition.java
Expand Up @@ -1204,7 +1204,7 @@ private void emitMember(Member member, final Connection connection,
row.set(MemberName.name, member.getName());
row.set(MemberUniqueName.name, member.getUniqueName());
row.set(MemberCaption.name, member.getCaption());
row.set(MemberType.name, member.getType());
row.set(MemberType.name, member.getCategory());
emit(row, saxHandler);
}
}
Expand Down
11 changes: 6 additions & 5 deletions testsrc/main/mondrian/test/BasicQueryTest.java
Expand Up @@ -644,7 +644,8 @@ public void _testEver() {
"select" + nl +
" {[Measures].[Unit Sales], [Measures].[Ever]} on columns," + nl +
" [Gender].members on rows" + nl +
"from Sales", null);
"from Sales",
"xxx");
}

public void _testDairy() {
Expand All @@ -656,7 +657,8 @@ public void _testDairy() {
"select" + nl +
" {[Measures].[Unit Sales], [Measures].[Dairy ever]} on columns," + nl +
" [Customers who never bought dairy] on rows" + nl +
"from Sales\r\n", null);
"from Sales\r\n",
"xxx");
}

public void testSolveOrder() {
Expand Down Expand Up @@ -4383,7 +4385,7 @@ public void testSameDimOnTwoAxesFails() {
"Dimension '[Measures]' appears in more than one independent axis");

// within aggregate is OK
runQueryCheckResult(
runQuery(
"with member [Measures].[West Coast Total] as " +
" ' Aggregate({[Store].[USA].[CA], [Store].[USA].[OR], [Store].[USA].[WA]}) ' " + nl +
"select " +
Expand All @@ -4392,8 +4394,7 @@ public void testSameDimOnTwoAxesFails() {
" CrossJoin(" + nl +
" {[Product].children}," + nl +
" {[Store].children}) on Rows" + nl +
"from [Sales]",
null);
"from [Sales]");
}

public void _testSetArgToTupleFails() {
Expand Down
8 changes: 8 additions & 0 deletions testsrc/main/mondrian/test/FoodMartTestCase.java
Expand Up @@ -16,6 +16,7 @@
import mondrian.olap.*;

import java.util.ArrayList;
import java.util.regex.Pattern;

/**
* <code>FoodMartTestCase</code> is a unit test which runs against the FoodMart
Expand Down Expand Up @@ -151,6 +152,13 @@ public void runQueryCheckResult(String query, String desiredResult) {
getTestContext().runQueryCheckResult(query, desiredResult);
}

/**
* Runs a query and checks that the result matches a given pattern.
*/
public void runQueryCheckResult(String query, Pattern desiredResult) {
getTestContext().runQueryCheckResult(query, desiredResult);
}

/**
* Runs a query.
*/
Expand Down
56 changes: 54 additions & 2 deletions testsrc/main/mondrian/test/TestContext.java
Expand Up @@ -75,9 +75,9 @@ protected TestContext() {
* FoodMart database.
*
* The algorithm is as follows:<ul>
* <li>Starts with {@link MondrianProperties#getTestConnectString}, if it is
* <li>Starts with {@link MondrianProperties#TestConnectString}, if it is
* set.</li>
* <li>If {@link MondrianProperties#getFoodmartJdbcURL} is set, this
* <li>If {@link MondrianProperties#FoodmartJdbcURL} is set, this
* overrides the <code>Jdbc</code> property.</li>
* <li>If the <code>catalog</code> URL is unset or invalid, it assumes that
* we are at the root of the source tree, and references
Expand Down Expand Up @@ -293,6 +293,21 @@ public void runQueryCheckResult(String query, String desiredResult) {
}
}

/**
* Runs a query and checks that the result matches a given pattern.
*/
public void runQueryCheckResult(String query, Pattern desiredResult) {
Result result = runQuery(query);
String resultString = toString(result);
assertMatchesVerbose(desiredResult, resultString);
}

/**
* Checks that an actual string matches an expected string.
* If they do not, throws a {@link ComparisonFailure} and prints the
* difference, including the actual string as an easily pasted Java string
* literal.
*/
public static void assertEqualsVerbose(
String expected,
String actual)
Expand Down Expand Up @@ -325,6 +340,42 @@ public static void assertEqualsVerbose(
throw new ComparisonFailure(message, expected, actual);
}

/**
* Checks that an actual string matches an expected pattern.
* If they do not, throws a {@link ComparisonFailure} and prints the
* difference, including the actual string as an easily pasted Java string
* literal.
*/
public void assertMatchesVerbose(
Pattern expected,
String actual)
{
Util.assertPrecondition(expected != null, "expected != null");
if (expected.matcher(actual).matches()) {
return;
}
String s = actual;

// Convert [string with "quotes" split
// across lines]
// into ["string with \"quotes\" split" + nl +
// "across lines
//
s = Util.replace(s, "\"", "\\\"");
s = LineBreakPattern.matcher(s).replaceAll(lineBreak);
s = TabPattern.matcher(s).replaceAll("\\\\t");
s = "\"" + s + "\"";
final String spurious = " + " + FoodMartTestCase.nl + "\"\"";
if (s.endsWith(spurious)) {
s = s.substring(0, s.length() - spurious.length());
}
String message =
"Expected pattern:" + FoodMartTestCase.nl + expected + FoodMartTestCase.nl +
"Actual: " + FoodMartTestCase.nl + actual + FoodMartTestCase.nl +
"Actual java: " + FoodMartTestCase.nl + s + FoodMartTestCase.nl;
throw new ComparisonFailure(message, expected.pattern(), actual);
}

/**
* Converts a {@link Throwable} to a stack trace.
*/
Expand All @@ -346,6 +397,7 @@ public static String toString(Result result) {
pw.flush();
return sw.toString();
}

}

// End TestContext.java
56 changes: 28 additions & 28 deletions testsrc/main/mondrian/test/UdfTest.java
Expand Up @@ -17,6 +17,8 @@
import mondrian.olap.type.Type;
import mondrian.spi.UserDefinedFunction;

import java.util.regex.Pattern;

/**
* Unit-test for {@link UserDefinedFunction user-defined functions}.
*
Expand Down Expand Up @@ -171,7 +173,7 @@ public synchronized Connection getFoodMartConnection(boolean fresh) {

public void testComplexFun() {
runQueryCheckResult(
"WITH MEMBER [Measures].[InverseNormal] AS 'InverseNormal([Measures].[Grocery Sqft] / [Measures].[Store Sqft])', FORMAT_STRING = \"0.000000\"" + nl +
"WITH MEMBER [Measures].[InverseNormal] AS 'InverseNormal([Measures].[Grocery Sqft] / [Measures].[Store Sqft])', FORMAT_STRING = \"0.0000\"" + nl +
"SELECT {[Measures].[InverseNormal]} ON COLUMNS, " + nl +
" {[Store Type].children} ON ROWS " + nl +
"FROM [Store]",
Expand All @@ -187,38 +189,36 @@ public void testComplexFun() {
"{[Store Type].[All Store Types].[Mid-Size Grocery]}" + nl +
"{[Store Type].[All Store Types].[Small Grocery]}" + nl +
"{[Store Type].[All Store Types].[Supermarket]}" + nl +
"Row #0: 0.467304" + nl +
"Row #1: 0.462815" + nl +
"Row #0: 0.4673" + nl +
"Row #1: 0.4628" + nl +
"Row #2: (null)" + nl +
"Row #3: 0.625130" + nl +
"Row #4: 0.520862" + nl +
"Row #5: 0.503598" + nl);
"Row #3: 0.6251" + nl +
"Row #4: 0.5208" + nl +
"Row #5: 0.5036" + nl);
}

public void testException() {
runQueryCheckResult(
"WITH MEMBER [Measures].[InverseNormal] AS 'InverseNormal([Measures].[Store Sqft] / [Measures].[Grocery Sqft])', FORMAT_STRING = \"0.000000\"" + nl +
Result result = runQuery(
"WITH MEMBER [Measures].[InverseNormal] " +
" AS 'InverseNormal([Measures].[Store Sqft] / [Measures].[Grocery Sqft])'," +
" FORMAT_STRING = \"0.000000\"" + nl +
"SELECT {[Measures].[InverseNormal]} ON COLUMNS, " + nl +
" {[Store Type].children} ON ROWS " + nl +
"FROM [Store]",

"Axis #0:" + nl +
"{}" + nl +
"Axis #1:" + nl +
"{[Measures].[InverseNormal]}" + nl +
"Axis #2:" + nl +
"{[Store Type].[All Store Types].[Deluxe Supermarket]}" + nl +
"{[Store Type].[All Store Types].[Gourmet Supermarket]}" + nl +
"{[Store Type].[All Store Types].[HeadQuarters]}" + nl +
"{[Store Type].[All Store Types].[Mid-Size Grocery]}" + nl +
"{[Store Type].[All Store Types].[Small Grocery]}" + nl +
"{[Store Type].[All Store Types].[Supermarket]}" + nl +
"Row #0: #ERR: mondrian.olap.fun.MondrianEvaluationException: Invalid value for inverse normal distribution: 1.4708933427334072" + nl +
"Row #1: #ERR: mondrian.olap.fun.MondrianEvaluationException: Invalid value for inverse normal distribution: 1.4743792921288958" + nl +
"Row #2: (null)" + nl +
"Row #3: #ERR: mondrian.olap.fun.MondrianEvaluationException: Invalid value for inverse normal distribution: 1.3622919366091897" + nl +
"Row #4: #ERR: mondrian.olap.fun.MondrianEvaluationException: Invalid value for inverse normal distribution: 1.4310888905786632" + nl +
"Row #5: #ERR: mondrian.olap.fun.MondrianEvaluationException: Invalid value for inverse normal distribution: 1.4435681830051705" + nl);
"FROM [Store]");
Axis rowAxis = result.getAxes()[0];
assertTrue(rowAxis.positions.length == 1);
Axis colAxis = result.getAxes()[1];
assertTrue(colAxis.positions.length == 6);
Cell cell = result.getCell(new int[] {0, 0});
assertTrue(cell.isError());
getTestContext().assertMatchesVerbose(
Pattern.compile(".*Invalid value for inverse normal distribution: 1.4708.*"),
cell.getValue().toString());
cell = result.getCell(new int[] {0, 5});
assertTrue(cell.isError());
getTestContext().assertMatchesVerbose(
Pattern.compile(".*Invalid value for inverse normal distribution: 1.4435.*"),
cell.getValue().toString());
}

/**
Expand All @@ -234,7 +234,7 @@ protected String filterSchema(String s) {
PlusOneUdf.class.getName() + "\"/>" + nl +
"<UserDefinedFunction name=\"LastNonEmpty\" className=\"" +
LastNonEmptyUdf.class.getName() + "\"/>" + nl +
"<UserDefinedFunction name=\"InverseNormal\" className=\"" +
"<UserDefinedFunction name=\"InverseNormal\" className=\"" +
InverseNormalFunDef.class.getName() + "\"/>" + nl +
"</Schema>");
}
Expand Down

0 comments on commit b823f43

Please sign in to comment.