Apache Lucene open-source search software
SOLR-2451: Enhance assertJQ to allow individual tests to specify the tolerance delta used in numeric equalities.
Chris M. Hostetterc503b44b8fbe
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 42555a66b8a..52c3c0fcbc8 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -241,6 +241,11 @@ Other Changes* SOLR-2461: QuerySenderListener and AbstractSolrEventListener arenow public (hossman)+* SOLR-2451: Enhance assertJQ to allow individual tests to specify the+ tolerance delta used in numeric equalities. This allows for slight+ variance in asserting score comparisons in unit tests.+ (David Smiley, Chris Hostetter)+Documentation----------------------diff --git a/solr/src/test-framework/org/apache/solr/JSONTestUtil.java b/solr/src/test-framework/org/apache/solr/JSONTestUtil.javaindex 8bd5a79c8f3..deb681d99f7 100644--- a/solr/src/test-framework/org/apache/solr/JSONTestUtil.java+++ b/solr/src/test-framework/org/apache/solr/JSONTestUtil.java@@ -25,29 +25,69 @@ import java.util.*;public class JSONTestUtil {+ /**+ * Default delta used in numeric equality comparisons for floats and doubles.+ */+ public final static double DEFAULT_DELTA = 1e-5;++ /**+ * comparison using default delta+ * @see #DEFAULT_DELTA+ * @see #match(String,String,double)+ */public static String match(String input, String pathAndExpected) throws Exception {+ return match(input, pathAndExpected, DEFAULT_DELTA);+ }++ /**+ * comparison using default delta+ * @see #DEFAULT_DELTA+ * @see #match(String,String,String,double)+ */+ public static String match(String path, String input, String expected) throws Exception {+ return match(path, input, expected, DEFAULT_DELTA);+ }++ /**+ * comparison using default delta+ * @see #DEFAULT_DELTA+ * @see #matchObj(String,Object,Object,double)+ */+ public static String matchObj(String path, Object input, Object expected) throws Exception {+ return matchObj(path,input,expected, DEFAULT_DELTA);+ }++ /**+ * @param input JSON Structure to parse and test against+ * @param pathAndExpected JSON path expression + '==' + expected value+ * @param delta tollerance allowed in comparing float/double values+ */+ public static String match(String input, String pathAndExpected, double delta) throws Exception {int pos = pathAndExpected.indexOf("==");String path = pos>=0 ? pathAndExpected.substring(0,pos) : null;String expected = pos>=0 ? pathAndExpected.substring(pos+2) : pathAndExpected;- return match(path, input, expected);+ return match(path, input, expected, delta);}- public static String match(String path, String input, String expected) throws Exception {+ /**+ * @param path JSON path expression+ * @param input JSON Structure to parse and test against+ * @param expected expected value of path+ * @param delta tollerance allowed in comparing float/double values+ */+ public static String match(String path, String input, String expected, double delta) throws Exception {Object inputObj = ObjectBuilder.fromJSON(input);Object expectObj = ObjectBuilder.fromJSON(expected);return matchObj(path, inputObj, expectObj);}-- /**- public static Object fromJSON(String json) {- try {- Object out = ObjectBuilder.fromJSON(json);- } finally {-- }- **/- public static String matchObj(String path, Object input, Object expected) throws Exception {+ /**+ * @param path JSON path expression+ * @param input JSON Structure+ * @param expected expected JSON Object+ * @param delta tollerance allowed in comparing float/double values+ */+ public static String matchObj(String path, Object input, Object expected, double delta) throws Exception {CollectionTester tester = new CollectionTester(input);boolean reversed = path.startsWith("!");String positivePath = reversed ? path.substring(1) : path;@@ -68,14 +108,19 @@ class CollectionTester {public Object val;public Object expectedRoot;public Object expected;+ public double delta;public List<Object> path;public String err;- public CollectionTester(Object val) {+ public CollectionTester(Object val, double delta) {this.val = val;this.valRoot = val;+ this.delta = delta;path = new ArrayList<Object>();}+ public CollectionTester(Object val) {+ this(val, JSONTestUtil.DEFAULT_DELTA);+ }public String getPath() {StringBuilder sb = new StringBuilder();@@ -143,7 +188,7 @@ class CollectionTester {double a = ((Number)expected).doubleValue();double b = ((Number)val).doubleValue();if (Double.compare(a,b) == 0) return true;- if (Math.abs(a-b) < 1e-5) return true;+ if (Math.abs(a-b) < delta) return true;return false;} else {setErr("mismatch: '" + expected + "'!='" + val + "'");diff --git a/solr/src/test-framework/org/apache/solr/SolrTestCaseJ4.java b/solr/src/test-framework/org/apache/solr/SolrTestCaseJ4.javaindex 113329286ae..f053c3c1f8d 100755--- a/solr/src/test-framework/org/apache/solr/SolrTestCaseJ4.java+++ b/solr/src/test-framework/org/apache/solr/SolrTestCaseJ4.java@@ -36,8 +36,12 @@ import org.apache.solr.handler.JsonUpdateRequestHandler;import org.apache.solr.request.LocalSolrQueryRequest;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrRequestHandler;+import org.apache.solr.response.ResultContext;+import org.apache.solr.response.SolrQueryResponse;import org.apache.solr.schema.IndexSchema;import org.apache.solr.schema.SchemaField;+import org.apache.solr.search.DocIterator;+import org.apache.solr.search.DocList;import org.apache.solr.search.SolrIndexSearcher;import org.apache.solr.servlet.DirectSolrConnection;import org.apache.solr.util.TestHarness;@@ -374,15 +378,29 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {}}- /** Validates a query matches some JSON test expressions and closes the query.- * The text expression is of the form path:JSON. To facilitate easy embedding- * in Java strings, the JSON can have double quotes replaced with single quotes.- *- * Please use this with care: this makes it easy to match complete structures, but doing so- * can result in fragile tests if you are matching more than what you want to test.- *- **/+ /**+ * Validates a query matches some JSON test expressions using the default double delta tollerance.+ * @see JSONTestUtil#DEFAULT_DELTA+ * @see #assertJQ(SolrQueryRequest,double,String...)+ */public static void assertJQ(SolrQueryRequest req, String... tests) throws Exception {+ assertJQ(req, JSONTestUtil.DEFAULT_DELTA, tests);+ }+ /**+ * Validates a query matches some JSON test expressions and closes the+ * query. The text expression is of the form path:JSON. To facilitate+ * easy embedding in Java strings, the JSON can have double quotes+ * replaced with single quotes.+ * <p>+ * Please use this with care: this makes it easy to match complete+ * structures, but doing so can result in fragile tests if you are+ * matching more than what you want to test.+ * </p>+ * @param req Solr request to execute+ * @param delta tollerance allowed in comparing float/double values+ * @param tests JSON path expression + '==' + expected value+ */+ public static void assertJQ(SolrQueryRequest req, double delta, String... tests) throws Exception {SolrParams params = null;try {params = req.getParams();@@ -409,7 +427,7 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {try {failed = true;- String err = JSONTestUtil.match(response, testJSON);+ String err = JSONTestUtil.match(response, testJSON, delta);failed = false;if (err != null) {log.error("query failed JSON validation. error=" + err +diff --git a/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.java b/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.javaindex f2324c32d09..2ba9bb2a84a 100644--- a/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.java+++ b/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.java@@ -76,39 +76,74 @@ public class DistanceFunctionTest extends SolrTestCaseJ4 {assertU(adoc("id", "100", "store", "1,2"));assertU(commit());- assertJQ(req("defType","func", "q","geodist(1,2,3,4)","fq","id:100","fl","id,score")- ,"/response/docs/[0]/score==314.40338"- );+ assertJQ(req("defType","func",+ "q","geodist(1,2,3,4)",+ "fq","id:100",+ "fl","id,score")+ , 1e-5+ , "/response/docs/[0]/score==314.40338"+ );// throw in some decimal points- assertJQ(req("defType","func", "q","geodist(1.0,2,3,4.0)","fq","id:100","fl","id,score")- ,"/response/docs/[0]/score==314.40338"- );+ assertJQ(req("defType","func",+ "q","geodist(1.0,2,3,4.0)",+ "fq","id:100",+ "fl","id,score")+ , 1e-5+ , "/response/docs/[0]/score==314.40338"+ );// default to reading pt- assertJQ(req("defType","func", "q","geodist(1,2)","pt","3,4", "fq","id:100","fl","id,score")- ,"/response/docs/[0]/score==314.40338"- );+ assertJQ(req("defType","func",+ "q","geodist(1,2)",+ "pt","3,4",+ "fq","id:100",+ "fl","id,score")+ , 1e-5+ , "/response/docs/[0]/score==314.40338"+ );// default to reading pt first- assertJQ(req("defType","func", "q","geodist(1,2)","pt","3,4", "sfield","store", "fq","id:100","fl","id,score")- ,"/response/docs/[0]/score==314.40338"- );+ assertJQ(req("defType","func",+ "q","geodist(1,2)",+ "pt","3,4",+ "sfield","store",+ "fq","id:100",+ "fl","id,score")+ , 1e-5+ , "/response/docs/[0]/score==314.40338"+ );// if pt missing, use sfield- assertJQ(req("defType","func", "q","geodist(3,4)","sfield","store", "fq","id:100","fl","id,score")- ,"/response/docs/[0]/score==314.40338"- );-+ assertJQ(req("defType","func",+ "q","geodist(3,4)",+ "sfield","store",+ "fq","id:100",+ "fl","id,score")+ , 1e-5+ ,"/response/docs/[0]/score==314.40338"+ );+// read both pt and sfield- assertJQ(req("defType","func", "q","geodist()","pt","3,4","sfield","store", "fq","id:100","fl","id,score")- ,"/response/docs/[0]/score==314.40338"- );+ assertJQ(req("defType","func",+ "q","geodist()","pt","3,4",+ "sfield","store",+ "fq","id:100",+ "fl","id,score")+ , 1e-5+ ,"/response/docs/[0]/score==314.40338"+ );// param substitution- assertJQ(req("defType","func", "q","geodist($a,$b)","a","3,4","b","store", "fq","id:100","fl","id,score")- ,"/response/docs/[0]/score==314.40338"- );+ assertJQ(req("defType","func",+ "q","geodist($a,$b)",+ "a","3,4",+ "b","store",+ "fq","id:100",+ "fl","id,score")+ , 1e-5+ ,"/response/docs/[0]/score==314.40338"+ );}