You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2023/01/20 17:04:41 UTC
[couchdb] 01/02: Show mango_selector:match/2 call using test
This is an automated email from the ASF dual-hosted git repository.
jan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 5f2d5b135b5ebf3a3e19e2200afdacf9fe0cb1fa
Author: Mike Rhodes <mi...@dx13.co.uk>
AuthorDate: Fri Jan 13 14:51:22 2023 +0000
Show mango_selector:match/2 call using test
I needed to understand the format of arguments to `match/2` when writing
the code to support projecting fields on the shard, so I wrote some code
to figure it out as a test. I figure this may be useful for future
work in this area, so push as commit.
---
src/mango/src/mango_selector.erl | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/src/mango/src/mango_selector.erl b/src/mango/src/mango_selector.erl
index 584b2dffb..7de16bd51 100644
--- a/src/mango/src/mango_selector.erl
+++ b/src/mango/src/mango_selector.erl
@@ -982,4 +982,29 @@ has_required_fields_or_nested_or_false_test() ->
Normalized = normalize(Selector),
?assertEqual(false, has_required_fields(Normalized, RequiredFields)).
+%% This test shows the shape match/2 expects for its arguments.
+match_demo_test_() ->
+ Doc =
+ {[
+ {<<"_id">>, <<"foo">>},
+ {<<"_rev">>, <<"bar">>},
+ {<<"user_id">>, 11}
+ ]},
+ Check = fun(Selector) ->
+ % Call match_int/2 to avoid ERROR for missing metric; this is confusing
+ % in the middle of test output.
+ match_int(mango_selector:normalize(Selector), Doc)
+ end,
+ [
+ % matching
+ ?_assertEqual(true, Check({[{<<"user_id">>, 11}]})),
+ ?_assertEqual(true, Check({[{<<"_id">>, <<"foo">>}]})),
+ ?_assertEqual(true, Check({[{<<"_id">>, <<"foo">>}, {<<"_rev">>, <<"bar">>}]})),
+ % non-matching
+ ?_assertEqual(false, Check({[{<<"user_id">>, 1234}]})),
+ % string 11 doesn't match number 11
+ ?_assertEqual(false, Check({[{<<"user_id">>, <<"11">>}]})),
+ ?_assertEqual(false, Check({[{<<"_id">>, <<"foo">>}, {<<"_rev">>, <<"quux">>}]}))
+ ].
+
-endif.