jaydoane commented on a change in pull request #1760: test: port coffee.js to 
Elixir test suite
URL: https://github.com/apache/couchdb/pull/1760#discussion_r236024908
 
 

 ##########
 File path: test/elixir/test/coffee_test.exs
 ##########
 @@ -0,0 +1,82 @@
+defmodule CoffeeTest do
+  use CouchTestCase
+
+  @moduletag :coffee
+  @headers ["X-Couch-Full-Commit": "false"]
+
+  @moduledoc """
+  Test basic coffeescript functionality.
+  This is a port of the coffee.js test suite.
+  """
+
+  @tag :with_db
+  test "CoffeeScript basic functionality", context do
+    db_name = context[:db_name]
+
+    design_doc = %{
+      :_id => "_design/coffee",
+      :language => "coffeescript",
+      :views => %{
+        :myview => %{
+          :map => "(doc) -> if doc.foo\n  emit(doc.foo, 1)",
+          :reduce =>
+            "(keys, values, rereduce) ->\n  sum = 0\n  for x in values\n    
sum = sum + x\n  sum"
+        }
+      },
+      :shows => %{
+        :myshow => "(doc) ->\n  \"Foo #\{doc.foo}\""
+      },
+      :lists => %{
+        :mylist =>
+          "(head, req) ->\n  while row = getRow()\n    send(toJSON({\"resp\": 
\"Foo #\{row.value}\"}))\n  return"
+      },
+      :filters => %{
+        :filter => "(doc) ->\n  doc.foo"
+      }
+    }
+
+    design_resp = Couch.put("/#{db_name}/_design/coffee", body: design_doc, 
headers: @headers)
+    assert design_resp.status_code === 201
+
+    docs = [
+      %{:_id => "a", :foo => 100},
+      %{:foo => 1},
+      %{:foo => 1},
+      %{:foo => 2},
+      %{:foo => 2},
+      %{:bar => 1},
+      %{:bar => 1},
+      %{:bar => 2},
+      %{:bar => 2}
+    ]
+
+    resp =
+      Couch.post(
+        "/#{db_name}/_bulk_docs",
+        body: %{docs: docs},
+        headers: @headers
+      )
+
+    assert resp.status_code === 201 and length(resp.body) === length(docs)
+
+    # Giving time to the database to complete sync, otherwise values
+    # would be messed up and the test would be so much flaky
+    :timer.sleep(100)
 
 Review comment:
   Using a fixed sleep time is still very fragile (since CI machines can be 
much slower than your dev env), and I would recommend using a more robust 
synchronization technique. You could do something like:
   ```elixir
     retry_until(fn ->
       %{"rows" => values} =
         Couch.get("/#{db_name}/_design/coffee/_view/myview",
           headers: @headers
         ).body
        assert 5 === hd(values)["value"]  
     end)
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to