Skip to content

Commit 71bdee2

Browse files
authored
Merge pull request #5765 from neighbourhoodie/port-python-04-key-tests
Port Python test to Elixir: `04-key-tests`
2 parents 0a005a8 + bc68bca commit 71bdee2

3 files changed

Lines changed: 186 additions & 159 deletions

File tree

src/mango/test/04-key-tests.py

Lines changed: 0 additions & 159 deletions
This file was deleted.

test/elixir/test/config/search.elixir

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939
"LimitTests": [
4040
"limit field"
4141
],
42+
"KeyTest": [
43+
"dot key",
44+
"peso key",
45+
"unicode in fieldname",
46+
"unicode in selector field",
47+
"internal field tests",
48+
"escape period",
49+
"object period"
50+
],
4251
"TextIndexSelectionTest": [
4352
"with text",
4453
"no view index",
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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 KeyTest do
14+
use CouchTestCase
15+
16+
@db_name "key-test"
17+
18+
@test_docs [
19+
%{"_id" => "100", "type" => "complex_key", "title" => "normal key"},
20+
%{
21+
"_id" => "200",
22+
"type" => "complex_key",
23+
"title" => "key with dot",
24+
"dot.key" => "dot's value",
25+
"none" => %{"dot" => "none dot's value"},
26+
"name.first" => "Kvothe",
27+
},
28+
%{
29+
"_id" => "300",
30+
"type" => "complex_key",
31+
"title" => "key with peso",
32+
"$key" => "peso",
33+
"deep" => %{"$key" => "deep peso"},
34+
"name" => %{"first" => "Master Elodin"},
35+
},
36+
%{"_id" => "400", "type" => "complex_key", "title" => "unicode key", "" => "apple"},
37+
%{
38+
"_id" => "500",
39+
"title" => "internal_fields_format",
40+
"utf8-1[]:string" => "string",
41+
"utf8-2[]:boolean[]" => true,
42+
"utf8-3[]:number" => 9,
43+
"utf8-3[]:null" => nil,
44+
},
45+
]
46+
47+
setup do
48+
MangoDatabase.recreate(@db_name)
49+
MangoDatabase.save_docs(@db_name, @test_docs, w: 3)
50+
MangoDatabase.create_index(@db_name, ["type"], ddoc: "view")
51+
MangoDatabase.create_text_index(@db_name, ddoc: "text")
52+
:ok
53+
end
54+
55+
defp run_check(query, check, opts) do
56+
fields = Keyword.get(opts, :fields, [])
57+
indexes = Keyword.get(opts, :indexes, ["view", "text"])
58+
Enum.each(indexes, fn idx ->
59+
{:ok, docs} = MangoDatabase.find(@db_name, query, fields: fields, use_index: idx)
60+
check.(docs)
61+
end)
62+
end
63+
64+
test "dot key" do
65+
query = %{"type" => "complex_key"}
66+
fields = ["title", "dot\\.key", "none.dot"]
67+
68+
check = fn docs ->
69+
assert length(docs) == 4
70+
doc = Enum.at(docs, 1)
71+
assert Map.has_key?(doc, "dot.key")
72+
assert doc["dot.key"] == "dot's value"
73+
assert Map.has_key?(doc, "none")
74+
assert doc["none"]["dot"] == "none dot's value"
75+
end
76+
77+
run_check(query, check, fields: fields)
78+
end
79+
80+
test "peso key" do
81+
query = %{"type" => "complex_key"}
82+
fields = ["title", "$key", "deep.$key"]
83+
84+
check = fn docs ->
85+
assert length(docs) == 4
86+
doc = Enum.at(docs, 2)
87+
assert Map.has_key?(doc, "$key")
88+
assert doc["$key"] == "peso"
89+
assert Map.has_key?(doc, "deep")
90+
assert doc["deep"]["$key"] == "deep peso"
91+
end
92+
93+
run_check(query, check, fields: fields)
94+
end
95+
96+
test "unicode in fieldname" do
97+
query = %{"type" => "complex_key"}
98+
fields = ["title", ""]
99+
100+
check = fn docs ->
101+
assert length(docs) == 4
102+
doc = Enum.at(docs, 3)
103+
# note:  == \uf8ff
104+
assert Map.has_key?(doc, "\uf8ff" )
105+
assert doc["\uf8ff"] == "apple"
106+
end
107+
108+
run_check(query, check, fields: fields)
109+
end
110+
111+
# The rest of these tests are only run against the text
112+
# indexes because view indexes don't have to worry about
113+
# field *name* escaping in the index.
114+
115+
test "unicode in selector field" do
116+
query = %{"" => "apple"}
117+
118+
check = fn docs ->
119+
assert length(docs) == 1
120+
doc = Enum.at(docs, 0)
121+
assert doc["\uf8ff"] == "apple"
122+
end
123+
124+
run_check(query, check, indexes: ["text"])
125+
end
126+
127+
test "internal field tests" do
128+
queries = [
129+
%{"utf8-1[]:string" => "string"},
130+
%{"utf8-2[]:boolean[]" => true},
131+
%{"utf8-3[]:number" => 9},
132+
%{"utf8-3[]:null" => nil},
133+
]
134+
135+
check = fn docs ->
136+
assert length(docs) == 1
137+
doc = Enum.at(docs, 0)
138+
assert doc["title"] == "internal_fields_format"
139+
end
140+
141+
Enum.each(queries, fn query ->
142+
run_check(query, check, indexes: ["text"])
143+
end)
144+
end
145+
146+
test "escape period" do
147+
query = %{"name\\.first" => "Kvothe"}
148+
check = fn docs ->
149+
assert length(docs) == 1
150+
doc = Enum.at(docs, 0)
151+
assert doc["name.first"] == "Kvothe"
152+
end
153+
run_check(query, check, indexes: ["text"])
154+
155+
query = %{"name.first" => "Kvothe"}
156+
check_empty = fn docs ->
157+
assert Enum.empty?(docs)
158+
end
159+
run_check(query, check_empty, indexes: ["text"])
160+
end
161+
162+
test "object period" do
163+
query = %{"name.first" => "Master Elodin"}
164+
check = fn docs ->
165+
assert length(docs) == 1
166+
doc = Enum.at(docs, 0)
167+
assert doc["title"] == "key with peso"
168+
end
169+
run_check(query, check, indexes: ["text"])
170+
171+
query = %{"name\\.first" => "Master Elodin"}
172+
check_empty = fn docs ->
173+
assert Enum.empty?(docs)
174+
end
175+
run_check(query, check_empty, indexes: ["text"])
176+
end
177+
end

0 commit comments

Comments
 (0)