tonysun83 commented on a change in pull request #962: Mango: generate a warning 
instead of an error when an user-specified index cannot be used
URL: https://github.com/apache/couchdb/pull/962#discussion_r153702449
 
 

 ##########
 File path: src/mango/test/05-index-selection-test.py
 ##########
 @@ -108,41 +103,53 @@ def 
test_reject_use_index_ddoc_and_name_invalid_fields(self):
         selector = {
             "company": "Pharmex"
         }
-        try:
-            self.db.find(selector, use_index=[ddocid,name])
-        except Exception as e:
-            self.assertEqual(e.response.status_code, 400)
-        else:
-            raise AssertionError("did not reject bad use_index")
+        
+        resp = self.db.find(selector, use_index=[ddocid,name], return_raw=True)
+        self.assertEqual(resp["warning"], "{0}, {1} was not used because it is 
not a valid index for this query.".format(ddocid, name))
 
     def test_reject_use_index_sort_order(self):
         # index on ["company","manager"] which should not be valid
+        # and there is no valid fallback (i.e. an index on ["company"])
         ddocid = "_design/a0c425a60cf3c3c09e3c537c9ef20059dcef9198"
         selector = {
-            "company": {"$gt": None},
-            "manager": {"$gt": None}
+            "company": {"$gt": None}
         }
         try:
-            self.db.find(selector, use_index=ddocid, sort=[{"manager":"desc"}])
+            self.db.find(selector, use_index=ddocid, sort=[{"company":"desc"}])
         except Exception as e:
             self.assertEqual(e.response.status_code, 400)
         else:
             raise AssertionError("did not reject bad use_index")
 
-    def test_reject_use_index_ddoc_and_name_sort_order(self):
-        # index on ["company","manager"] which should not be valid
-        ddocid = "_design/a0c425a60cf3c3c09e3c537c9ef20059dcef9198"
-        name = "a0c425a60cf3c3c09e3c537c9ef20059dcef9198"
+    def test_use_index_fallback_if_valid_sort(self):
+        ddocid_valid = "_design/fallbackfoo"
+        ddocid_invalid = "_design/fallbackfoobar"
+        self.db.create_index(fields=["foo"], ddoc=ddocid_invalid)
+        self.db.create_index(fields=["foo", "bar"], ddoc=ddocid_valid)
         selector = {
-            "company": {"$gt": None},
-            "manager": {"$gt": None}
+            "foo": {"$gt": None}
         }
-        try:
-            self.db.find(selector, use_index=[ddocid,name], 
sort=[{"manager":"desc"}])
-        except Exception as e:
-            self.assertEqual(e.response.status_code, 400)
-        else:
-            raise AssertionError("did not reject bad use_index")
+
+        resp_explain = self.db.find(selector, sort=["foo", "bar"], 
use_index=ddocid_invalid, explain=True)
+        self.assertEqual(resp_explain["index"]["ddoc"], ddocid_valid)    
+
+        resp = self.db.find(selector, sort=["foo", "bar"], 
use_index=ddocid_invalid, return_raw=True)
+        self.assertEqual(resp["warning"], '{0} was not used because it does 
not contain a valid index for this query.'.format(ddocid_invalid))    
 
 Review comment:
   same as above, we should get a value back here right? It's going to fall 
back on the ddoc_valid and return a result, so just confirm a result?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to