summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlessio Biancalana <dottorblaster@gmail.com>2018-11-26 15:17:39 +0100
committergarren smith <garren.smith@gmail.com>2018-11-26 16:17:39 +0200
commitd1c8a833055dd855bb57c7639f22f42216dde919 (patch)
treeba625e7936ac18314bf39931db6647d002d74b07
parent2e22aa6ed2dd935f4f5eb67ac83c908ae6472b1b (diff)
downloadcouchdb-d1c8a833055dd855bb57c7639f22f42216dde919.tar.gz
test: port lots_of_docs.js to Elixir test suite (#1738)
-rw-r--r--test/elixir/README.md2
-rw-r--r--test/elixir/test/lots_of_docs_test.exs95
2 files changed, 96 insertions, 1 deletions
diff --git a/test/elixir/README.md b/test/elixir/README.md
index b1b745af3..48f936fcb 100644
--- a/test/elixir/README.md
+++ b/test/elixir/README.md
@@ -48,7 +48,7 @@ X means done, - means partially
- [ ] Port list_views.js
- [ ] Port lorem_b64.txt
- [ ] Port lorem.txt
- - [ ] Port lots_of_docs.js
+ - [X] Port lots_of_docs.js
- [ ] Port method_override.js
- [ ] Port multiple_rows.js
- [ ] Port proxyauth.js
diff --git a/test/elixir/test/lots_of_docs_test.exs b/test/elixir/test/lots_of_docs_test.exs
new file mode 100644
index 000000000..4115f1c78
--- /dev/null
+++ b/test/elixir/test/lots_of_docs_test.exs
@@ -0,0 +1,95 @@
+defmodule LotsOfDocsTest do
+ use CouchTestCase
+
+ @moduletag :lots_of_docs
+ @docs_range 0..499
+
+ @moduledoc """
+ Test saving a semi-large quanitity of documents and do some view queries.
+ This is a port of the lots_of_docs.js suite
+ """
+
+ @tag :with_db
+ test "lots of docs with _all_docs", context do
+ db_name = context[:db_name]
+ @docs_range
+ |> create_docs()
+ |> Enum.chunk_every(100)
+ |> Enum.each(fn(docs) -> bulk_post(docs, db_name) end)
+
+ %{"rows" => rows, "total_rows" => total_rows} = Couch.get("/#{db_name}/_all_docs").body
+ assert total_rows === Enum.count(@docs_range)
+ assert total_rows === Enum.count(rows)
+ @docs_range
+ |> Enum.map(fn(i) -> Integer.to_string(i) end)
+ |> Enum.sort()
+ |> Enum.with_index()
+ |> Enum.each(fn({value, index}) ->
+ assert Map.fetch!(Enum.at(rows, index), "key") === value
+ end)
+
+ %{"rows" => desc_rows, "total_rows" => desc_total_rows} = Couch.get(
+ "/#{db_name}/_all_docs",
+ query: %{:descending => true}
+ ).body
+ assert desc_total_rows === Enum.count(@docs_range)
+ assert desc_total_rows === Enum.count(desc_rows)
+ @docs_range
+ |> Enum.map(fn(i) -> Integer.to_string(i) end)
+ |> Enum.sort()
+ |> Enum.reverse()
+ |> Enum.with_index()
+ |> Enum.each(fn({value, index}) ->
+ assert Map.fetch!(Enum.at(desc_rows, index), "key") === value
+ end)
+ end
+
+ @tag :with_db
+ test "lots of docs with a regular view", context do
+ db_name = context[:db_name]
+ @docs_range
+ |> create_docs()
+ |> Enum.chunk_every(100)
+ |> Enum.each(fn(docs) -> bulk_post(docs, db_name) end)
+
+ %{"rows" => rows, "total_rows" => total_rows} = query_view(db_name)
+ assert total_rows === Enum.count(rows)
+ assert total_rows === Enum.count(@docs_range)
+ Enum.each(@docs_range, fn(i) ->
+ assert Map.fetch!(Enum.at(rows, i), "key") === i
+ end)
+
+ %{"rows" => desc_rows, "total_rows" => desc_total_rows} = query_view(db_name, "descending")
+ assert desc_total_rows === Enum.count(desc_rows)
+ assert desc_total_rows === Enum.count(@docs_range)
+ @docs_range
+ |> Enum.reverse()
+ |> Enum.with_index()
+ |> Enum.each(fn({value, index}) ->
+ assert Map.fetch!(Enum.at(desc_rows, index), "key") === value
+ end)
+ end
+
+ defp query_view(db_name, sorting \\ "ascending") do
+ descending = if(sorting === "descending", do: true, else: false)
+ map_fun = "function(doc) { emit(doc.integer, null); }"
+ map_doc = %{:views => %{:view => %{:map => map_fun}}}
+ %{"rev" => rev} = Couch.put("/#{db_name}/_design/tempddoc", body: map_doc).body
+ response = Couch.get(
+ "/#{db_name}/_design/tempddoc/_view/view",
+ query: %{:descending => descending}
+ ).body
+ Couch.delete("/#{db_name}/_design/tempddoc?rev=#{rev}")
+ response
+ end
+
+ defp bulk_post(docs, db) do
+ resp = Couch.post("/#{db}/_bulk_docs", [body: %{docs: docs}])
+ assert resp.status_code == 201 and length(resp.body) == length(docs),
+ """
+ Expected 201 and the same number of response rows as in request, but got
+ #{pretty_inspect resp}
+ """
+ resp
+ end
+end \ No newline at end of file