You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ho...@apache.org on 2011/05/14 00:21:41 UTC

svn commit: r1102910 - in /lucene/dev/branches/branch_3x: ./ lucene/ lucene/backwards/ solr/ solr/src/test-framework/org/apache/solr/ solr/src/test/org/apache/solr/search/function/distance/

Author: hossman
Date: Fri May 13 22:21:41 2011
New Revision: 1102910

URL: http://svn.apache.org/viewvc?rev=1102910&view=rev
Log:
SOLR-2451: backport r1102907 from trunk

Modified:
    lucene/dev/branches/branch_3x/   (props changed)
    lucene/dev/branches/branch_3x/lucene/   (props changed)
    lucene/dev/branches/branch_3x/lucene/backwards/   (props changed)
    lucene/dev/branches/branch_3x/solr/   (props changed)
    lucene/dev/branches/branch_3x/solr/CHANGES.txt
    lucene/dev/branches/branch_3x/solr/src/test-framework/org/apache/solr/JSONTestUtil.java
    lucene/dev/branches/branch_3x/solr/src/test-framework/org/apache/solr/SolrTestCaseJ4.java
    lucene/dev/branches/branch_3x/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.java

Modified: lucene/dev/branches/branch_3x/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/CHANGES.txt?rev=1102910&r1=1102909&r2=1102910&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/CHANGES.txt (original)
+++ lucene/dev/branches/branch_3x/solr/CHANGES.txt Fri May 13 22:21:41 2011
@@ -115,7 +115,11 @@ Other Changes
 	
 * SOLR-2485: Deprecate BaseResponseWriter, GenericBinaryResponseWriter, and 
   GenericTextResponseWriter.  These classes will be removed in 4.0.  (ryan)
-	
+
+* 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)
 
 Build
 ----------------------

Modified: lucene/dev/branches/branch_3x/solr/src/test-framework/org/apache/solr/JSONTestUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/src/test-framework/org/apache/solr/JSONTestUtil.java?rev=1102910&r1=1102909&r2=1102910&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/src/test-framework/org/apache/solr/JSONTestUtil.java (original)
+++ lucene/dev/branches/branch_3x/solr/src/test-framework/org/apache/solr/JSONTestUtil.java Fri May 13 22:21:41 2011
@@ -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();
@@ -139,7 +184,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 + "'");

Modified: lucene/dev/branches/branch_3x/solr/src/test-framework/org/apache/solr/SolrTestCaseJ4.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/src/test-framework/org/apache/solr/SolrTestCaseJ4.java?rev=1102910&r1=1102909&r2=1102910&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/src/test-framework/org/apache/solr/SolrTestCaseJ4.java (original)
+++ lucene/dev/branches/branch_3x/solr/src/test-framework/org/apache/solr/SolrTestCaseJ4.java Fri May 13 22:21:41 2011
@@ -29,6 +29,12 @@ import org.apache.solr.common.util.XML;
 import org.apache.solr.core.SolrConfig;
 import org.apache.solr.request.LocalSolrQueryRequest;
 import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.request.SolrRequestHandler;
+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.util.TestHarness;
 import org.junit.AfterClass;
@@ -367,15 +373,29 @@ public abstract class SolrTestCaseJ4 ext
     }
   }
 
-  /** 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();
@@ -402,7 +422,7 @@ public abstract class SolrTestCaseJ4 ext
 
         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 +

Modified: lucene/dev/branches/branch_3x/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.java?rev=1102910&r1=1102909&r2=1102910&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.java (original)
+++ lucene/dev/branches/branch_3x/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.java Fri May 13 22:21:41 2011
@@ -75,39 +75,74 @@ public class DistanceFunctionTest extend
     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"
+             );
 
   }