Skip to content

Commit 13501dc

Browse files
jcoglanjanl
authored andcommitted
Port the friend_docs fixture and one dependent test as an example from Python to Elixir
1 parent ea16a55 commit 13501dc

4 files changed

Lines changed: 333 additions & 2 deletions

File tree

test/elixir/test/config/search.elixir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@
3232
"facet counts, empty",
3333
"facet ranges, empty",
3434
"facet ranges, non-empty"
35+
],
36+
"ElemMatchTests": [
37+
"elem match non object"
3538
]
3639
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 ElemMatchTests do
14+
use CouchTestCase
15+
16+
@db_name "basic-text-elem-match"
17+
18+
setup do
19+
FriendDocs.setup(@db_name, "text")
20+
end
21+
22+
test "elem match non object" do
23+
q = %{"bestfriends" => %{"$elemMatch" => %{"$eq" => "Wolverine", "$eq" => "Cyclops"}}}
24+
docs = MangoDatabase.find(@db_name, q)
25+
assert length(docs) == 1
26+
assert Enum.at(docs, 0)["bestfriends"] == ["Wolverine", "Cyclops"]
27+
28+
q = %{"results" => %{"$elemMatch" => %{"$gte" => 80, "$lt" => 85}}}
29+
docs = MangoDatabase.find(@db_name, q)
30+
assert length(docs) == 1
31+
assert Enum.at(docs, 0)["results"] == [82, 85, 88]
32+
end
33+
end
Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
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 FriendDocs do
14+
@moduledoc """
15+
Generated with http://www.json-generator.com/
16+
17+
With this pattern:
18+
19+
[
20+
'{{repeat(15)}}',
21+
{
22+
_id: '{{index()}}',
23+
name: {
24+
first: '{{firstName()}}',
25+
last: '{{surname()}}'
26+
},
27+
friends: [
28+
'{{repeat(3)}}',
29+
{
30+
id: '{{index()}}',
31+
name: {
32+
first: '{{firstName()}}',
33+
last: '{{surname()}}'
34+
},
35+
type: '{{random("personal", "work")}}'
36+
}
37+
]
38+
}
39+
]
40+
"""
41+
42+
@docs [
43+
%{
44+
"_id" => "54a43171d37ae5e81bff5ae0",
45+
"user_id" => 0,
46+
"name" => %{"first" => "Ochoa", "last" => "Fox"},
47+
"friends" => [
48+
%{
49+
"id" => 0,
50+
"name" => %{"first" => "Sherman", "last" => "Davidson"},
51+
"type" => "personal",
52+
},
53+
%{
54+
"id" => 1,
55+
"name" => %{"first" => "Vargas", "last" => "Mendez"},
56+
"type" => "personal",
57+
},
58+
%{"id" => 2, "name" => %{"first" => "Sheppard", "last" => "Cotton"}, "type" => "work"},
59+
],
60+
},
61+
%{
62+
"_id" => "54a43171958485dc32917c50",
63+
"user_id" => 1,
64+
"name" => %{"first" => "Sheppard", "last" => "Cotton"},
65+
"friends" => [
66+
%{"id" => 0, "name" => %{"first" => "Ochoa", "last" => "Fox"}, "type" => "work"},
67+
%{
68+
"id" => 1,
69+
"name" => %{"first" => "Vargas", "last" => "Mendez"},
70+
"type" => "personal",
71+
},
72+
%{"id" => 2, "name" => %{"first" => "Kendra", "last" => "Burns"}, "type" => "work"},
73+
],
74+
},
75+
%{
76+
"_id" => "54a431711cf025ba74bea899",
77+
"user_id" => 2,
78+
"name" => %{"first" => "Hunter", "last" => "Wells"},
79+
"friends" => [
80+
%{"id" => 0, "name" => %{"first" => "Estes", "last" => "Fischer"}, "type" => "work"},
81+
%{
82+
"id" => 1,
83+
"name" => %{"first" => "Farrell", "last" => "Maddox"},
84+
"type" => "personal",
85+
},
86+
%{"id" => 2, "name" => %{"first" => "Kendra", "last" => "Burns"}, "type" => "work"},
87+
],
88+
},
89+
%{
90+
"_id" => "54a4317151a70a9881ac28a4",
91+
"user_id" => 3,
92+
"name" => %{"first" => "Millicent", "last" => "Guy"},
93+
"friends" => [
94+
%{"id" => 0, "name" => %{"first" => "Luella", "last" => "Mendoza"}, "type" => "work"},
95+
%{
96+
"id" => 1,
97+
"name" => %{"first" => "Melanie", "last" => "Foster"},
98+
"type" => "personal",
99+
},
100+
%{"id" => 2, "name" => %{"first" => "Hopkins", "last" => "Scott"}, "type" => "work"},
101+
],
102+
},
103+
%{
104+
"_id" => "54a43171d946b78703a0e076",
105+
"user_id" => 4,
106+
"name" => %{"first" => "Elisabeth", "last" => "Brady"},
107+
"friends" => [
108+
%{"id" => 0, "name" => %{"first" => "Sofia", "last" => "Workman"}, "type" => "work"},
109+
%{"id" => 1, "name" => %{"first" => "Alisha", "last" => "Reilly"}, "type" => "work"},
110+
%{"id" => 2, "name" => %{"first" => "Ochoa", "last" => "Burch"}, "type" => "personal"},
111+
],
112+
},
113+
%{
114+
"_id" => "54a4317118abd7f1992464ee",
115+
"user_id" => 5,
116+
"name" => %{"first" => "Pollard", "last" => "French"},
117+
"friends" => [
118+
%{
119+
"id" => 0,
120+
"name" => %{"first" => "Hollie", "last" => "Juarez"},
121+
"type" => "personal",
122+
},
123+
%{"id" => 1, "name" => %{"first" => "Nelda", "last" => "Newton"}, "type" => "personal"},
124+
%{"id" => 2, "name" => %{"first" => "Yang", "last" => "Pace"}, "type" => "personal"},
125+
],
126+
},
127+
%{
128+
"_id" => "54a43171f139e63d6579121e",
129+
"user_id" => 6,
130+
"name" => %{"first" => "Acevedo", "last" => "Morales"},
131+
"friends" => [
132+
%{"id" => 0, "name" => %{"first" => "Payne", "last" => "Berry"}, "type" => "personal"},
133+
%{
134+
"id" => 1,
135+
"name" => %{"first" => "Rene", "last" => "Valenzuela"},
136+
"type" => "personal",
137+
},
138+
%{"id" => 2, "name" => %{"first" => "Dora", "last" => "Gallegos"}, "type" => "work"},
139+
],
140+
},
141+
%{
142+
"_id" => "54a431719783cef80876dde8",
143+
"user_id" => 7,
144+
"name" => %{"first" => "Cervantes", "last" => "Marquez"},
145+
"friends" => [
146+
%{
147+
"id" => 0,
148+
"name" => %{"first" => "Maxwell", "last" => "Norman"},
149+
"type" => "personal",
150+
},
151+
%{"id" => 1, "name" => %{"first" => "Shields", "last" => "Bass"}, "type" => "personal"},
152+
%{"id" => 2, "name" => %{"first" => "Luz", "last" => "Jacobson"}, "type" => "work"},
153+
],
154+
},
155+
%{
156+
"_id" => "54a43171ecc7540d1f7aceae",
157+
"user_id" => 8,
158+
"name" => %{"first" => "West", "last" => "Morrow"},
159+
"friends" => [
160+
%{
161+
"id" => 0,
162+
"name" => %{"first" => "Townsend", "last" => "Dixon"},
163+
"type" => "personal",
164+
},
165+
%{
166+
"id" => 1,
167+
"name" => %{"first" => "Callahan", "last" => "Buck"},
168+
"type" => "personal",
169+
},
170+
%{
171+
"id" => 2,
172+
"name" => %{"first" => "Rachel", "last" => "Fletcher"},
173+
"type" => "personal",
174+
},
175+
],
176+
},
177+
%{
178+
"_id" => "54a4317113e831f4af041a0a",
179+
"user_id" => 9,
180+
"name" => %{"first" => "Cotton", "last" => "House"},
181+
"friends" => [
182+
%{
183+
"id" => 0,
184+
"name" => %{"first" => "Mckenzie", "last" => "Medina"},
185+
"type" => "personal",
186+
},
187+
%{"id" => 1, "name" => %{"first" => "Cecilia", "last" => "Miles"}, "type" => "work"},
188+
%{"id" => 2, "name" => %{"first" => "Guerra", "last" => "Cervantes"}, "type" => "work"},
189+
],
190+
},
191+
%{
192+
"_id" => "54a43171686eb1f48ebcbe01",
193+
"user_id" => 10,
194+
"name" => %{"first" => "Wright", "last" => "Rivas"},
195+
"friends" => [
196+
%{
197+
"id" => 0,
198+
"name" => %{"first" => "Campos", "last" => "Freeman"},
199+
"type" => "personal",
200+
},
201+
%{
202+
"id" => 1,
203+
"name" => %{"first" => "Christian", "last" => "Ferguson"},
204+
"type" => "personal",
205+
},
206+
%{"id" => 2, "name" => %{"first" => "Doreen", "last" => "Wilder"}, "type" => "work"},
207+
],
208+
},
209+
%{
210+
"_id" => "54a43171a4f3d5638c162f4f",
211+
"user_id" => 11,
212+
"name" => %{"first" => "Lorene", "last" => "Dorsey"},
213+
"friends" => [
214+
%{
215+
"id" => 0,
216+
"name" => %{"first" => "Gibbs", "last" => "Mccarty"},
217+
"type" => "personal",
218+
},
219+
%{"id" => 1, "name" => %{"first" => "Neal", "last" => "Franklin"}, "type" => "work"},
220+
%{"id" => 2, "name" => %{"first" => "Kristy", "last" => "Head"}, "type" => "personal"},
221+
],
222+
"bestfriends" => ["Wolverine", "Cyclops"],
223+
},
224+
%{
225+
"_id" => "54a431719faa420a5b4fbeb0",
226+
"user_id" => 12,
227+
"name" => %{"first" => "Juanita", "last" => "Cook"},
228+
"friends" => [
229+
%{"id" => 0, "name" => %{"first" => "Wilkins", "last" => "Chang"}, "type" => "work"},
230+
%{"id" => 1, "name" => %{"first" => "Haney", "last" => "Rivera"}, "type" => "work"},
231+
%{"id" => 2, "name" => %{"first" => "Lauren", "last" => "Manning"}, "type" => "work"},
232+
],
233+
},
234+
%{
235+
"_id" => "54a43171e65d35f9ee8c53c0",
236+
"user_id" => 13,
237+
"name" => %{"first" => "Levy", "last" => "Osborn"},
238+
"friends" => [
239+
%{"id" => 0, "name" => %{"first" => "Vinson", "last" => "Vargas"}, "type" => "work"},
240+
%{"id" => 1, "name" => %{"first" => "Felicia", "last" => "Beach"}, "type" => "work"},
241+
%{"id" => 2, "name" => %{"first" => "Nadine", "last" => "Kemp"}, "type" => "work"},
242+
],
243+
"results" => [82, 85, 88],
244+
},
245+
%{
246+
"_id" => "54a4317132f2c81561833259",
247+
"user_id" => 14,
248+
"name" => %{"first" => "Christina", "last" => "Raymond"},
249+
"friends" => [
250+
%{"id" => 0, "name" => %{"first" => "Herrera", "last" => "Walton"}, "type" => "work"},
251+
%{"id" => 1, "name" => %{"first" => "Hahn", "last" => "Rutledge"}, "type" => "work"},
252+
%{"id" => 2, "name" => %{"first" => "Stacie", "last" => "Harding"}, "type" => "work"},
253+
],
254+
},
255+
%{
256+
"_id" => "589f32af493145f890e1b051",
257+
"user_id" => 15,
258+
"name" => %{"first" => "Tanisha", "last" => "Bowers"},
259+
"friends" => [
260+
%{"id" => 0, "name" => %{"first" => "Ochoa", "last" => "Pratt"}, "type" => "personal"},
261+
%{"id" => 1, "name" => %{"first" => "Ochoa", "last" => "Romero"}, "type" => "personal"},
262+
%{"id" => 2, "name" => %{"first" => "Ochoa", "last" => "Bowman"}, "type" => "work"},
263+
],
264+
},
265+
]
266+
267+
def setup(db, index_type \\ "view") do
268+
MangoDatabase.recreate(db)
269+
MangoDatabase.save_docs(db, @docs)
270+
271+
case index_type do
272+
"view" -> add_view_indexes(db)
273+
"text" -> add_text_indexes(db)
274+
end
275+
276+
:ok
277+
end
278+
279+
defp add_view_indexes(db) do
280+
# TODO: this function is not defined in the Python version of this module?
281+
end
282+
283+
defp add_text_indexes(db) do
284+
MangoDatabase.create_text_index(db)
285+
end
286+
end

test/elixir/test/support/mango_database.ex

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
# the License.
1212

1313
defmodule MangoDatabase do
14+
def has_text_service() do
15+
resp = Couch.get("/")
16+
"search" in resp.body["features"]
17+
end
18+
1419
def recreate(db, opts \\ []) do
1520
resp = Couch.get("/#{db}")
1621
if resp.status_code == 200 do
@@ -39,7 +44,7 @@ defmodule MangoDatabase do
3944
end
4045

4146
def create_index(db, fields, name) do
42-
resp = Couch.post("/#{db}/_index", body: %{
47+
Couch.post("/#{db}/_index", body: %{
4348
"index" => %{"fields" => fields},
4449
"name" => name,
4550
"ddoc" => name,
@@ -49,7 +54,11 @@ defmodule MangoDatabase do
4954
end
5055

5156
def create_text_index(db) do
52-
# TODO
57+
Couch.post("/#{db}/_index", body: %{
58+
"index" => %{},
59+
"type" => "text",
60+
"w" => 3
61+
})
5362
end
5463

5564
# TODO: port more options from src/mango/test/mango.py `def find(...)`

0 commit comments

Comments
 (0)