From c5af1e3240fcb587ec3a2aa0ea8f5fd7e3529179 Mon Sep 17 00:00:00 2001 From: jiangph Date: Mon, 14 Jan 2019 15:14:26 +0800 Subject: Add Elixir tests for database partitions size --- test/elixir/test/partition_helpers.exs | 88 ++++++++++++++++++++++++++++++++ test/elixir/test/partition_size_test.exs | 12 +++++ 2 files changed, 100 insertions(+) diff --git a/test/elixir/test/partition_helpers.exs b/test/elixir/test/partition_helpers.exs index 6eac2b1a4..4920c1198 100644 --- a/test/elixir/test/partition_helpers.exs +++ b/test/elixir/test/partition_helpers.exs @@ -1,5 +1,6 @@ defmodule PartitionHelpers do use ExUnit.Case + use CouchTestCase def create_partition_docs(db_name, pk1 \\ "foo", pk2 \\ "bar") do docs = @@ -54,6 +55,81 @@ defmodule PartitionHelpers do assert Map.has_key?(resp.body, "ok") == true end + def crud_partition_doc(db_name, pk \\ "foo") do + resp = Couch.post("/#{db_name}", body: %{:_id => "#{pk}:id1", :a => 1, :b => 1}).body + assert resp["ok"] + + url = "/#{db_name}/_partition/#{pk}" + resp = Couch.get(url) + + assert resp.status_code == 200 + %{:body => body} = resp + assert body["doc_count"] == 1 + assert body["doc_del_count"] == 0 + assert body["partition"] == "#{pk}" + exernal_size1 = body["sizes"]["external"] + assert exernal_size1 > 0 + + resp = Couch.post("/#{db_name}", body: %{:_id => "#{pk}:id2", :a => 2, :b => 22}).body + rev = resp["rev"] + assert resp["ok"] + + url = "/#{db_name}/_partition/#{pk}" + resp = Couch.get(url) + + assert resp.status_code == 200 + %{:body => body} = resp + assert body["doc_count"] == 2 + assert body["doc_del_count"] == 0 + assert body["partition"] == "#{pk}" + exernal_size2 = body["sizes"]["external"] + assert exernal_size2 > exernal_size1 + + resp = Couch.delete("/#{db_name}/#{pk}:id2?rev=#{rev}").body + assert resp["ok"] + :timer.sleep(300) + + url = "/#{db_name}/_partition/#{pk}" + resp = Couch.get(url) + + assert resp.status_code == 200 + %{:body => body} = resp + assert body["doc_count"] == 1 + assert body["doc_del_count"] == 1 + assert body["partition"] == "#{pk}" + exernal_size3 = body["sizes"]["external"] + assert exernal_size3 <= exernal_size2 + + compact(db_name) + + url = "/#{db_name}/_partition/#{pk}" + resp = Couch.get(url) + %{:body => body} = resp + exernal_size4 = body["sizes"]["external"] + assert exernal_size4 < exernal_size3 + + map = ~s""" + function (doc) { + emit(doc.integer, doc.integer); + emit(doc.integer, doc.integer); + }; + """ + red_doc = %{:views => %{:bar => %{:map => map}}} + + assert Couch.put("/#{db_name}/_design/#{pk}_foo", body: red_doc).body["ok"] + + url = "/#{db_name}/_partition/#{pk}" + resp = Couch.get(url) + + assert resp.status_code == 200 + %{:body => body} = resp + assert body["doc_count"] == 1 + assert body["doc_del_count"] == 1 + assert body["partition"] == "#{pk}" + exernal_size5 = body["sizes"]["external"] + assert exernal_size5 == exernal_size4 + end + def get_ids(resp) do %{:body => %{"rows" => rows}} = resp Enum.map(rows, fn row -> row["id"] end) @@ -73,4 +149,16 @@ defmodule PartitionHelpers do partition == correct_partition end) end + + def compact(db) do + assert Couch.post("/#{db}/_compact").status_code == 202 + + retry_until( + fn -> + Couch.get("/#{db}").body["compact_running"] == false + end, + 200, + 20_000 + ) + end end diff --git a/test/elixir/test/partition_size_test.exs b/test/elixir/test/partition_size_test.exs index 289d1e113..3cd30e8ce 100644 --- a/test/elixir/test/partition_size_test.exs +++ b/test/elixir/test/partition_size_test.exs @@ -1,5 +1,6 @@ defmodule PartitionSizeTest do use CouchTestCase + import PartitionHelpers @moduledoc """ Test Partition size functionality @@ -85,6 +86,17 @@ defmodule PartitionSizeTest do assert info["sizes"]["active"] > 0 end + test "get partition size for curd", context do + db_name = context[:db_name] + crud_partition_doc(db_name, "pk1") + end + + test "get partition size for multipe curd", context do + db_name = context[:db_name] + crud_partition_doc(db_name, "pk1") + crud_partition_doc(db_name, "pk2") + end + test "adding docs increases partition sizes", context do db_name = context[:db_name] save_doc(db_name, %{_id: "foo:bar", val: 42}) -- cgit v1.2.1