summaryrefslogtreecommitdiff
path: root/test/elixir/test/utf8_test.exs
blob: 6afaee6067fd43a6d4fd306996fd75efeaf4cf18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
defmodule UTF8Test do
  use CouchTestCase

  @moduletag :utf8
  @moduletag kind: :single_node

  @moduledoc """
  Test CouchDB UTF8 support
  This is a port of the utf8.js test suite
  """

  @tag :with_db
  test "UTF8 support", context do
    db_name = context[:db_name]
    texts = [
      "1. Ascii: hello",
      "2. Russian: На берегу пустынных волн",
      "3. Math: ∮ E⋅da = Q,  n → ∞, ∑ f(i) = ∏ g(i),",
      "4. Geek: STARGΛ̊TE SG-1",
      "5. Braille: ⡌⠁⠧⠑ ⠼⠁⠒  ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌",
      "6. null \u0000 byte",
    ]
    
    texts
    |> Enum.with_index()
    |> Enum.each(fn {string, index} ->
      status = Couch.post("/#{db_name}", query: [w: 3], body: %{"_id" => Integer.to_string(index), "text" => string}).status_code
      assert status in [201, 202]
    end)

    texts
    |> Enum.with_index()
    |> Enum.each(fn {_, index} ->
      resp = Couch.get("/#{db_name}/#{index}")
      %{"_id" => id, "text" => text} = resp.body
      assert resp.status_code == 200
      assert Enum.at(texts, String.to_integer(id)) === text
    end)

    design_doc = %{
      :_id => "_design/temp_utf8_support",
      :language => "javascript",
      :views => %{
        :view => %{
          :map => "function(doc) { emit(null, doc.text) }"
        }
      }
    }

    design_resp =
      Couch.put(
        "/#{db_name}/_design/temp_utf8_support",
        body: design_doc,
        query: %{w: 3}
      )

    assert design_resp.status_code in [201, 202]

    %{"rows" => values} = Couch.get("/#{db_name}/_design/temp_utf8_support/_view/view").body
    values
    |> Enum.with_index()
    |> Enum.each(fn {%{"value" => value}, index} ->
      assert Enum.at(texts, index) === value
    end)
  end
end