|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 2 | +# use this file except in compliance with the License. You may obtain a copy of |
| 3 | +# the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations under |
| 11 | +# the License. |
| 12 | + |
| 13 | +defmodule BeginsWithOperator do |
| 14 | + use CouchTestCase |
| 15 | + |
| 16 | + @db_name "begins-with-operator" |
| 17 | + |
| 18 | + @docs [ |
| 19 | + %{"_id" => "aaa", "name" => "Jimi", "location" => "AUS", "age" => 27}, |
| 20 | + %{"_id" => "abc", "name" => "Eddie", "location" => "AND", "age" => 65}, |
| 21 | + %{"_id" => "bbb", "name" => "Harry", "location" => "CAN", "age" => 21}, |
| 22 | + %{"_id" => "ccc", "name" => "Eddie", "location" => "DEN", "age" => 37}, |
| 23 | + %{"_id" => "ddd", "name" => "Jones", "location" => "ETH", "age" => 49}, |
| 24 | +] |
| 25 | + |
| 26 | + setup do |
| 27 | + MangoDatabase.recreate(@db_name) |
| 28 | + MangoDatabase.save_docs(@db_name, @docs) |
| 29 | + MangoDatabase.create_index(@db_name, ["location"]) |
| 30 | + MangoDatabase.create_index(@db_name, ["name", "location"]) |
| 31 | + :ok |
| 32 | + end |
| 33 | + |
| 34 | + # split into Unicode graphemes |
| 35 | + defp split_unicode(binary) do |
| 36 | + binary |
| 37 | + |> String.graphemes() |
| 38 | + end |
| 39 | + |
| 40 | + defp get_mrargs(selector, opts \\ []) do |
| 41 | + sort = Keyword.get(opts, :sort, []) |
| 42 | + {:ok, explain} = MangoDatabase.find(@db_name, selector, sort: sort, explain: true) |
| 43 | + explain["mrargs"] |
| 44 | + end |
| 45 | + |
| 46 | + defp assert_doc_ids(user_ids, docs) do |
| 47 | + user_ids_returned = |
| 48 | + docs |
| 49 | + |> Enum.map(fn d -> d["_id"] end) |
| 50 | + |> Enum.sort() |
| 51 | + assert Enum.sort(user_ids) == user_ids_returned |
| 52 | + end |
| 53 | + |
| 54 | + test "basic" do |
| 55 | + {:ok, docs} = MangoDatabase.find(@db_name, %{"location" => %{"$beginsWith" => "A"}}) |
| 56 | + assert length(docs) == 2 |
| 57 | + assert_doc_ids(["aaa", "abc"], docs) |
| 58 | + end |
| 59 | + |
| 60 | + test "json range" do |
| 61 | + mrargs = get_mrargs(%{"location" => %{"$beginsWith" => "A"}}) |
| 62 | + assert mrargs["start_key"] == ["A"] |
| 63 | + assert mrargs["end_key"] == [<<"A\xef\xbf\xbf">>, <<"<MAX>">>] |
| 64 | + end |
| 65 | + |
| 66 | + test "compound key" do |
| 67 | + selector = %{"name" => "Eddie", "location" => %{"$beginsWith" => "A"}} |
| 68 | + mrargs = get_mrargs(selector) |
| 69 | + assert mrargs["start_key"] == ["Eddie", "A"] |
| 70 | + assert mrargs["end_key"] == [<<"Eddie">>, <<"A\xef\xbf\xbf">>, <<"<MAX>">>] |
| 71 | + |
| 72 | + {:ok, docs} = MangoDatabase.find(@db_name, selector) |
| 73 | + assert length(docs) == 1 |
| 74 | + assert_doc_ids(["abc"], docs) |
| 75 | + end |
| 76 | + |
| 77 | + test "sort" do |
| 78 | + selector = %{"location" => %{"$beginsWith" => "A"}} |
| 79 | + cases = [ |
| 80 | + %{ |
| 81 | + "sort" => ["location"], |
| 82 | + "start_key" => [<<"A">>], |
| 83 | + "end_key" => [<<"A\xef\xbf\xbf">>, <<"<MAX>">>], |
| 84 | + "direction" => "fwd", |
| 85 | + }, |
| 86 | + %{ |
| 87 | + "sort" => [%{"location" => "desc"}], |
| 88 | + "start_key" => [<<"A\xef\xbf\xbf">>, <<"<MAX>">>], |
| 89 | + "end_key" => [<<"A">>], |
| 90 | + "direction" => "rev", |
| 91 | + }, |
| 92 | + ] |
| 93 | + for case <- cases do |
| 94 | + mrargs = get_mrargs(selector, sort: case["sort"]) |
| 95 | + |
| 96 | + assert mrargs["start_key"] == case["start_key"] |
| 97 | + assert mrargs["end_key"] == case["end_key"] |
| 98 | + assert mrargs["direction"] == case["direction"] |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + test "all docs range" do |
| 103 | + mrargs = get_mrargs(%{"_id" => %{"$beginsWith" => "a"}}) |
| 104 | + |
| 105 | + assert mrargs["start_key"] == "a" |
| 106 | + end_key_bytes = split_unicode(mrargs["end_key"]) |
| 107 | + assert end_key_bytes == [<<"a">>, <<"\xef\xbf\xbf">>] |
| 108 | + end |
| 109 | + |
| 110 | + test "no index" do |
| 111 | + selector = %{"foo" => %{"$beginsWith" => "a"}} |
| 112 | + {:ok, resp_explain} = MangoDatabase.find(@db_name, selector, explain: true) |
| 113 | + mrargs = resp_explain["mrargs"] |
| 114 | + |
| 115 | + assert resp_explain["index"]["type"] == "special" |
| 116 | + assert mrargs["start_key"] == nil |
| 117 | + assert mrargs["end_key"] == "<MAX>" |
| 118 | + end |
| 119 | + |
| 120 | + test "invalid operand" do |
| 121 | + {:error, resp} = MangoDatabase.find(@db_name, %{"_id" => %{"$beginsWith" => true}}) |
| 122 | + assert resp.status_code == 400 |
| 123 | + assert resp.body["error"] == "invalid_operator" |
| 124 | + end |
| 125 | + |
| 126 | + test "does not match non string value" do |
| 127 | + {:ok, docs} = MangoDatabase.find(@db_name, %{"age" => %{"$beginsWith" => "a"}}) |
| 128 | + assert Enum.empty?(docs) |
| 129 | + end |
| 130 | + |
| 131 | + test "no matches" do |
| 132 | + {:ok, docs} = MangoDatabase.find(@db_name, %{"name" => %{"$beginsWith" => "Z"}}) |
| 133 | + assert Enum.empty?(docs) |
| 134 | + end |
| 135 | + |
| 136 | + test "case sensitivity" do |
| 137 | + {:ok, docs} = MangoDatabase.find(@db_name, %{"name" => %{"$beginsWith" => "j"}}) |
| 138 | + assert Enum.empty?(docs) |
| 139 | + |
| 140 | + {:ok, docs} = MangoDatabase.find(@db_name, %{"name" => %{"$beginsWith" => "J"}}) |
| 141 | + assert length(docs) == 2 |
| 142 | + end |
| 143 | +end |
0 commit comments