Skip to content

Commit 650af02

Browse files
committed
Port users db find tests into Elixir
1 parent 030d45e commit 650af02

3 files changed

Lines changed: 126 additions & 3 deletions

File tree

test/elixir/test/config/suite.elixir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,5 +860,18 @@
860860
"index pagination two keys",
861861
"index pagination reverse",
862862
"index pagination same emitted key"
863+
],
864+
"UsersDbFindTests": [
865+
"simple find",
866+
"multi cond and",
867+
"multi cond or",
868+
"sort",
869+
"fields",
870+
"empty"
871+
],
872+
"UsersDbIndexFindTests": [
873+
"multi cond and",
874+
"multi cond or",
875+
"sort"
863876
]
864877
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 UsersDbFindTests do
14+
use CouchTestCase
15+
16+
@db_name "_users"
17+
18+
setup do
19+
UserDocs.setup_users(@db_name)
20+
end
21+
22+
test "simple find" do
23+
{:ok, docs} = MangoDatabase.find(@db_name, %{"name" => %{"$eq" => "demo02"}})
24+
assert length(docs) == 1
25+
assert Enum.at(docs, 0)["_id"] == "org.couchdb.user:demo02"
26+
end
27+
28+
def multi_cond_and_test() do
29+
MangoDatabase.create_index(@db_name, ["type", "roles"])
30+
selector = %{"type" => "user", "roles" => %{"$eq" => ["reader"]}}
31+
{:ok, docs} = MangoDatabase.find(@db_name, selector)
32+
assert length(docs) == 1
33+
assert Enum.at(docs, 0)["_id"] == "org.couchdb.user:demo02"
34+
end
35+
36+
test "multi cond and" do
37+
multi_cond_and_test()
38+
end
39+
40+
def multi_cond_or_test() do
41+
selector = %{"$and" => [%{"type" => "user"}, %{"$or" => [%{"order" => 1}, %{"order" => 3}]}]}
42+
{:ok, docs} = MangoDatabase.find(@db_name, selector)
43+
assert length(docs) == 2
44+
assert Enum.at(docs, 0)["_id"] == "org.couchdb.user:demo01"
45+
assert Enum.at(docs, 1)["_id"] == "org.couchdb.user:demo03"
46+
end
47+
48+
test "multi cond or" do
49+
multi_cond_or_test()
50+
end
51+
52+
def sort_test() do
53+
MangoDatabase.create_index(@db_name, ["order", "name"])
54+
selector = %{"name" => %{"$gt" => "demo01"}}
55+
{:ok, docs1} = MangoDatabase.find(@db_name, selector, sort: [%{"order" => "asc"}])
56+
docs2 = Enum.sort_by(docs1, & &1["order"])
57+
assert docs1 == docs2
58+
59+
{:ok, docs1} = MangoDatabase.find(@db_name, selector, sort: [%{"order" => "desc"}])
60+
docs2 =
61+
docs1
62+
|> Enum.sort_by(& &1["order"])
63+
|> Enum.reverse()
64+
assert docs1 == docs2
65+
end
66+
67+
test "sort" do
68+
sort_test()
69+
end
70+
71+
test "fields" do
72+
selector = %{"name" => %{"$eq" => "demo02"}}
73+
{:ok, docs} = MangoDatabase.find(@db_name, selector, fields: ["name", "order"])
74+
assert length(docs) == 1
75+
assert Map.keys(Enum.at(docs, 0)) == ["name", "order"]
76+
end
77+
78+
test "empty" do
79+
{:ok, docs} = MangoDatabase.find(@db_name, %{})
80+
assert length(docs) == 3
81+
end
82+
end
83+
84+
defmodule UsersDbIndexFindTests do
85+
use CouchTestCase
86+
87+
@db_name "_users"
88+
setup do
89+
MangoDatabase.create_index(@db_name, ["name"])
90+
:ok
91+
end
92+
93+
test "multi cond and" do
94+
MangoDatabase.create_index(@db_name, ["type", "roles"])
95+
:ok
96+
UsersDbFindTests.multi_cond_and_test()
97+
end
98+
99+
test "multi cond or" do
100+
MangoDatabase.create_index(@db_name, ["type", "order"])
101+
UsersDbFindTests.multi_cond_or_test()
102+
end
103+
104+
test "sort" do
105+
MangoDatabase.create_index(@db_name, ["order", "name"])
106+
UsersDbFindTests.sort_test()
107+
end
108+
end

test/elixir/test/support/user_docs.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ defmodule UserDocs do
308308

309309
@users_docs [
310310
%{
311-
"_id" => "org.couchdb.user =>demo01",
311+
"_id" => "org.couchdb.user:demo01",
312312
"name" => "demo01",
313313
"username" => "demo01",
314314
"password" => "apple01",
@@ -317,7 +317,7 @@ defmodule UserDocs do
317317
"type" => "user",
318318
},
319319
%{
320-
"_id" => "org.couchdb.user =>demo02",
320+
"_id" => "org.couchdb.user:demo02",
321321
"name" => "demo02",
322322
"username" => "demo02",
323323
"password" => "apple02",
@@ -326,7 +326,7 @@ defmodule UserDocs do
326326
"type" => "user",
327327
},
328328
%{
329-
"_id" => "org.couchdb.user =>demo03",
329+
"_id" => "org.couchdb.user:demo03",
330330
"name" => "demo03",
331331
"username" => "demo03",
332332
"password" => "apple03",
@@ -339,6 +339,8 @@ defmodule UserDocs do
339339
def setup_users(db) do
340340
MangoDatabase.recreate(db)
341341
MangoDatabase.save_docs(db, @users_docs)
342+
343+
:ok
342344
end
343345

344346
def setup(db, index_type \\ "view", partitioned \\ false) do

0 commit comments

Comments
 (0)