tonysun83 commented on a change in pull request #1038: Allow to use index with 
or
URL: https://github.com/apache/couchdb/pull/1038#discussion_r154813975
 
 

 ##########
 File path: src/mango/src/mango_selector.erl
 ##########
 @@ -704,4 +722,103 @@ has_required_fields_or_test() ->
     Normalized = normalize(Selector),
     ?assertEqual(false, has_required_fields(Normalized, RequiredFields)).
 
--endif.
\ No newline at end of file
+has_required_fields_or_true_test() ->
+    RequiredFields = [<<"A">>, <<"B">>, <<"C">>],
+    Selector = {[{<<"A">>, "foo"},
+          {<<"$or">>,
+              [
+                  {[{<<"B">>, <<"bar">>}]},
+                  {[{<<"B">>, <<"baz">>}]}
+              ]
+          },
+                 {<<"C">>, "qux"}
+       ]},
+    Normalized = normalize(Selector),
+    ?assertEqual(true, has_required_fields(Normalized, RequiredFields)).
+
+has_required_fields_and_nested_or_true_test() ->
+    RequiredFields = [<<"A">>, <<"B">>],
+    Selector1 = {[{<<"$and">>,
+          [
+              {[{<<"A">>, <<"foo">>}]}
+          ]
+    }]},
+    Selector2 = {[{<<"$or">>,
+          [
+              {[{<<"B">>, <<"foo">>}]},
+              {[{<<"B">>, <<"foo">>}]}
+          ]
+    }]},
+    Selector = {[{<<"$and">>,
+          [
+              Selector1,
+              Selector2
+          ]
+    }]},
+    Normalized = normalize(Selector),
+    ?assertEqual(true, has_required_fields(Normalized, RequiredFields)),
+
+    SelectorReverse = {[{<<"$and">>,
+          [
+              Selector2,
+              Selector1
+          ]
+    }]},
+    NormalizedReverse = normalize(SelectorReverse),
+    ?assertEqual(true, has_required_fields(NormalizedReverse, RequiredFields)).
+
+has_required_fields_and_nested_or_false_test() ->
+    RequiredFields = [<<"A">>, <<"B">>],
+    Selector1 = {[{<<"$and">>,
+          [
+              {[{<<"A">>, <<"foo">>}]}
+          ]
+    }]},
+    Selector2 = {[{<<"$or">>,
+          [
+              {[{<<"A">>, <<"foo">>}]},
+              {[{<<"B">>, <<"foo">>}]}
+          ]
+    }]},
+    Selector = {[{<<"$and">>,
+          [
+              Selector1,
+              Selector2
+          ]
+    }]},
+    Normalized = normalize(Selector),
+    ?assertEqual(false, has_required_fields(Normalized, RequiredFields)),
+
+    SelectorReverse = {[{<<"$and">>,
+          [
+              Selector2,
+              Selector1
+          ]
+    }]},
+
+    NormalizedReverse = normalize(SelectorReverse),
+    ?assertEqual(false, has_required_fields(NormalizedReverse, 
RequiredFields)).
+
+has_required_fields_or_nested_and_true_test() ->
+    RequiredFields = [<<"A">>],
+    Selector1 = {[{<<"$and">>,
+          [
+              {[{<<"A">>, <<"foo">>}]}
+          ]
+    }]},
+    Selector2 = {[{<<"$and">>,
+          [
+              {[{<<"A">>, <<"foo">>}]}
+          ]
+    }]},
+    Selector = {[{<<"$or">>,
+          [
+              Selector1,
+              Selector2
+          ]
+    }]},
+    Normalized = normalize(Selector),
+    ?assertEqual(true, has_required_fields(Normalized, RequiredFields)).
+
+
+-endif.
 
 Review comment:
   I know the remainder mechanism was added for $and and it should work with 
$or's peers as well, but should we  have a redundant nested $or with $or test 
just to feel safe?

----------------------------------------------------------------
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