Skip to content

Commit afe19dc

Browse files
committed
Port text limit tests to Elixir
1 parent 290314a commit afe19dc

4 files changed

Lines changed: 176 additions & 3 deletions

File tree

test/elixir/test/config/search.elixir

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@
3737
"elem match non object"
3838
],
3939
"LimitTests": [
40-
"limit field"
40+
"limit field",
41+
"limit field 2",
42+
"limit field 3",
43+
"limit field 4",
44+
"limit field 5",
45+
"limit skip field 1",
46+
"limit skip field 2",
47+
"limit skip field 3",
48+
"limit skip field 4",
49+
"limit skip field 5",
50+
"limit skip field 6",
51+
"limit bookmark",
52+
"limit bookmark with sort",
4153
]
4254
}

test/elixir/test/mango/08_text_limit_test.exs

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,164 @@ defmodule LimitTests do
2121

2222
test "limit field" do
2323
q = %{"$or" => [%{"user_id" => %{"$lt" => 10}}, %{"filtered_array.[]" => 1}]}
24-
{:ok, docs} = MangoDatabase.find(@db_name, q, limit: 10)
24+
{:ok, docs} = MangoDatabase.find(@db_name, q, limit: 10, sort: ["user_id"])
2525

2626
assert length(docs) == 8
2727
Enum.each(docs, fn d -> assert d["user_id"] < 10 end)
2828
end
29+
30+
test "limit field 2" do
31+
q = %{"$or" => [%{"user_id" => %{"$lt" => 20}}, %{"filtered_array.[]" => 1}]}
32+
{:ok, docs} = MangoDatabase.find(@db_name, q, limit: 10, sort: ["user_id"])
33+
assert length(docs) == 10
34+
Enum.each(docs, fn d -> assert d["user_id"] < 20 end)
35+
end
36+
37+
test "limit field 3" do
38+
q = %{"$or" => [%{"user_id" => %{"$lt" => 100}}, %{"filtered_array.[]" => 1}]}
39+
{:ok, docs} = MangoDatabase.find(@db_name, q, limit: 1, sort: ["user_id"])
40+
assert length(docs) == 1
41+
Enum.each(docs, fn d -> assert d["user_id"] < 100 end)
42+
end
43+
44+
test "limit field 4" do
45+
q = %{"$or" => [%{"user_id" => %{"$lt" => 0}}, %{"filtered_array.[]" => 1}]}
46+
{:ok, docs} = MangoDatabase.find(@db_name, q, limit: 35)
47+
assert Enum.empty?(docs)
48+
end
49+
50+
# We reach our cap here of 50
51+
test "limit field 5" do
52+
q = %{"age" => %{"$exists" => true}}
53+
{:ok, docs} = MangoDatabase.find(@db_name, q, limit: 250)
54+
assert length(docs) == LimitDocs.get_docs_length()
55+
Enum.each(docs, fn d -> assert d["age"] < 100 end)
56+
end
57+
58+
test "limit skip field 1" do
59+
q = %{"$or" => [%{"user_id" => %{"$lt" => 100}}, %{"filtered_array.[]" => 1}]}
60+
{:ok, docs} = MangoDatabase.find(@db_name, q, limit: 10, skip: 20, sort: ["user_id"])
61+
assert length(docs) == 10
62+
Enum.each(docs, fn d -> assert d["user_id"] > 20 end)
63+
end
64+
65+
test "limit skip field 2" do
66+
q = %{"$or" => [%{"user_id" => %{"$lt" => 100}}, %{"filtered_array.[]" => 1}]}
67+
{:ok, docs} = MangoDatabase.find(@db_name, q, limit: 100, skip: 100)
68+
assert Enum.empty?(docs)
69+
end
70+
71+
test "limit skip field 3" do
72+
q = %{"$or" => [%{"user_id" => %{"$lt" => 20}}, %{"filtered_array.[]" => 1}]}
73+
{:ok, docs} = MangoDatabase.find(@db_name, q, limit: 1, skip: 30)
74+
assert Enum.empty?(docs)
75+
end
76+
77+
test "limit skip field 4" do
78+
q = %{"$or" => [%{"user_id" => %{"$lt" => 100}}, %{"filtered_array.[]" => 1}]}
79+
{:ok, docs} = MangoDatabase.find(@db_name, q, limit: 0, skip: 0)
80+
assert Enum.empty?(docs)
81+
end
82+
83+
test "limit skip field 5" do
84+
q = %{"$or" => [%{"user_id" => %{"$lt" => 100}}, %{"filtered_array.[]" => 1}]}
85+
{:error, resp} = MangoDatabase.find(@db_name, q, limit: -1)
86+
assert resp.status_code == 400
87+
end
88+
89+
test "limit skip field 6" do
90+
q = %{"$or" => [%{"user_id" => %{"$lt" => 100}}, %{"filtered_array.[]" => 1}]}
91+
{:error, resp} = MangoDatabase.find(@db_name, q, skip: -1)
92+
assert resp.status_code == 400
93+
end
94+
95+
# Basic test to ensure we can iterate through documents with a bookmark
96+
test "limit bookmark" do
97+
Enum.each(
98+
Enum.to_list(1..LimitDocs.get_docs_length()//5), #[1]
99+
fn size ->
100+
seen_docs = run_bookmark_check(size, "")
101+
102+
assert MapSet.size(seen_docs) == LimitDocs.get_docs_length()
103+
end
104+
)
105+
end
106+
107+
defp run_bookmark_check(size, bookmark) do
108+
q = %{"age" => %{"$gt" => 0}}
109+
seen_docs = MapSet.new([])
110+
111+
seen_docs = get_docs(q, size, bookmark, seen_docs)
112+
113+
assert MapSet.size(seen_docs) == LimitDocs.get_docs_length()
114+
115+
seen_docs
116+
end
117+
118+
defp get_docs(q, size, bookmark, seen_docs) do
119+
{:ok, json} = MangoDatabase.find(@db_name, q, limit: size, bookmark: bookmark, return_raw: true)
120+
121+
seen_docs = Enum.reduce(json["docs"], seen_docs,
122+
fn doc, seen_docs ->
123+
assert not Enum.member?(seen_docs, doc["_id"])
124+
MapSet.put(seen_docs, doc["_id"])
125+
end
126+
)
127+
128+
if length(json["docs"]) != 0 do
129+
assert bookmark != json["bookmark"]
130+
get_docs(q, size, json["bookmark"], seen_docs)
131+
else
132+
seen_docs
133+
end
134+
end
135+
136+
test "limit bookmark with sort" do
137+
Enum.each(
138+
Enum.to_list(1..LimitDocs.get_docs_length()//5), #[1]
139+
fn size ->
140+
seen_docs = run_bookmark_check_with_sort(size, "", 0)
141+
142+
assert MapSet.size(seen_docs) == LimitDocs.get_docs_length()
143+
end
144+
)
145+
end
146+
147+
defp run_bookmark_check_with_sort(size, bookmark, age) do
148+
q = %{"age" => %{"$gt" => 0}}
149+
seen_docs = MapSet.new([])
150+
151+
seen_docs = get_docs_with_sort(q, size, bookmark, age, seen_docs)
152+
153+
assert MapSet.size(seen_docs) == LimitDocs.get_docs_length()
154+
155+
seen_docs
156+
end
157+
158+
defp get_docs_with_sort(q, size, bookmark, age, seen_docs) do
159+
{:ok, json} = MangoDatabase.find(
160+
@db_name,
161+
q,
162+
limit: size,
163+
bookmark: bookmark,
164+
sort: ["age"],
165+
return_raw: true
166+
)
167+
168+
{seen_docs, age} = Enum.reduce(json["docs"], {seen_docs, age},
169+
fn doc, {seen_docs, age} ->
170+
assert not Enum.member?(seen_docs, doc["_id"])
171+
assert doc["age"] >= age
172+
173+
{MapSet.put(seen_docs, doc["_id"]), doc["age"]}
174+
end
175+
)
176+
177+
if length(json["docs"]) != 0 do
178+
assert bookmark != json["bookmark"]
179+
get_docs_with_sort(q, size, json["bookmark"], age, seen_docs)
180+
else
181+
seen_docs
182+
end
183+
end
29184
end

test/elixir/test/support/limit_docs.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,8 @@ defmodule LimitDocs do
109109
defp add_text_indexes(db) do
110110
MangoDatabase.create_text_index(db)
111111
end
112+
113+
def get_docs_length() do
114+
length(@docs)
115+
end
112116
end

test/elixir/test/support/mango_database.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ defmodule MangoDatabase do
165165
r: 1,
166166
conflicts: false,
167167
explain: false,
168-
return_raw: false
168+
return_raw: false,
169+
bookmark: nil,
169170
]
170171
options = Keyword.merge(defaults, opts)
171172

@@ -187,6 +188,7 @@ defmodule MangoDatabase do
187188
|> put_if_set("fields", options, :fields)
188189
|> put_if_set("execution_stats", options, :executionStats)
189190
|> put_if_set("allow_fallback", options, :allow_fallback)
191+
|> put_if_set("bookmark", options, :bookmark)
190192
)
191193

192194
case {(options[:explain] or options[:return_raw]), resp.status_code} do

0 commit comments

Comments
 (0)