couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chewbra...@apache.org
Subject [couchdb] 07/31: Port uuids.js to Elixir
Date Fri, 22 Jun 2018 22:15:36 GMT
This is an automated email from the ASF dual-hosted git repository.

chewbranca pushed a commit to branch elixir-suite
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit fba7e88ab929c53e921d77c76a62de417bf92389
Author: Paul J. Davis <paul.joseph.davis@gmail.com>
AuthorDate: Wed Dec 6 18:29:21 2017 -0600

    Port uuids.js to Elixir
---
 elixir_suite/test/uuids_test.exs | 100 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)

diff --git a/elixir_suite/test/uuids_test.exs b/elixir_suite/test/uuids_test.exs
new file mode 100644
index 0000000..3e18e4a
--- /dev/null
+++ b/elixir_suite/test/uuids_test.exs
@@ -0,0 +1,100 @@
+defmodule UUIDsTest do
+  use CouchTestCase
+
+  @moduletag :config
+
+  @moduledoc """
+  Test CouchDB UUIDs API
+  This is a port of the uuids.js suite
+  """
+
+  test "cache busting headers are set" do
+    resp = Couch.get("/_uuids")
+    assert resp.status_code == 200
+    assert Regex.match?(~r/no-cache/, resp.headers["Cache-Control"])
+    assert resp.headers["Pragma"] == "no-cache"
+    assert String.length(resp.headers["ETag"]) > 0
+  end
+
+  test "can return single uuid" do
+    resp = Couch.get("/_uuids")
+    assert resp.status_code == 200
+    [uuid1] = resp.body["uuids"]
+
+    resp = Couch.get("/_uuids", query: %{:count => 1})
+    assert resp.status_code == 200
+    [uuid2] = resp.body["uuids"]
+
+    assert uuid1 != uuid2
+  end
+
+  test "no duplicates in 1,000 UUIDs" do
+    resp = Couch.get("/_uuids", query: %{:count => 1000})
+    assert resp.status_code == 200
+    uuids = resp.body["uuids"]
+
+    assert length(Enum.uniq(uuids)) == length(uuids)
+  end
+
+  test "Method Not Allowed error on POST" do
+    resp = Couch.post("/_uuids", query: %{:count => 1000})
+    assert resp.status_code == 405
+  end
+
+  test "Bad Request error when exceeding max UUID count" do
+    resp = Couch.get("/_uuids", query: %{:count => 1001})
+    assert resp.status_code == 400
+  end
+
+  @tag config: [
+    {"uuids", "algorithm", "sequential"}
+  ]
+  test "sequential uuids are sequential" do
+    resp = Couch.get("/_uuids", query: %{:count => 1000})
+    assert resp.status_code == 200
+    [uuid | rest_uuids] = resp.body["uuids"]
+
+    assert String.length(uuid) == 32
+    Enum.reduce(rest_uuids, uuid, fn curr, acc ->
+      assert String.length(curr) == 32
+      assert acc < curr
+      curr
+    end)
+  end
+
+  @tag config: [
+    {"uuids", "algorithm", "utc_random"}
+  ]
+  test "utc_random uuids are roughly random" do
+    resp = Couch.get("/_uuids", query: %{:count => 1000})
+    assert resp.status_code == 200
+    uuids = resp.body["uuids"]
+
+    assert String.length(Enum.at(uuids, 1)) == 32
+
+    # Assert no collisions
+    assert length(Enum.uniq(uuids)) == length(uuids)
+
+    # Assert rough ordering of UUIDs
+    u1 = String.slice(Enum.at(uuids, 1), 0..13)
+    u2 = String.slice(Enum.at(uuids, -1), 0..13)
+    assert u1 < u2
+  end
+
+  @tag config: [
+    {"uuids", "algorithm", "utc_id"},
+    {"uuids", "utc_id_suffix", "frog"}
+  ]
+  test "utc_id uuids are correct" do
+    resp = Couch.get("/_uuids", query: %{:count => 10})
+    assert resp.status_code == 200
+    [uuid | rest_uuids] = resp.body["uuids"]
+
+    Enum.reduce(rest_uuids, uuid, fn curr, acc ->
+      assert String.length(curr) == 14 + String.length("frog")
+      assert String.slice(curr, 14..-1) == "frog"
+      assert curr > acc
+      curr
+    end)
+  end
+end


Mime
View raw message