summaryrefslogtreecommitdiff
path: root/test/elixir/test/rev_stemming_test.exs
blob: 1fb745eadf796ddfd37b3b593a928b0a64ed9657 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
defmodule RevStemmingTest do
  use CouchTestCase

  @moduletag :revs
  @moduletag kind: :single_node

  @moduledoc """
  This is a port of the rev_stemming.js suite
  """

  @new_limit 5

  @tag :with_db
  test "revs limit update", context do
    db_name = context[:db_name]

    resp = Couch.get("/#{db_name}/_revs_limit")
    assert resp.body == 1000

    create_rev_doc(db_name, "foo", @new_limit + 1)
    resp = Couch.get("/#{db_name}/foo?revs=true")
    assert length(resp.body["_revisions"]["ids"]) == @new_limit + 1

    resp =
      Couch.put("/#{db_name}/_revs_limit",
        body: "#{@new_limit}",
        headers: ["Content-type": "application/json"]
      )

    assert resp.status_code == 200

    create_rev_doc(db_name, "foo", @new_limit + 1)
    resp = Couch.get("/#{db_name}/foo?revs=true")
    assert length(resp.body["_revisions"]["ids"]) == @new_limit
  end

  @tag :with_db
  test "revs limit produces replication conflict ", context do
    db_name = context[:db_name]

    db_name_b = "#{db_name}_b"
    create_db(db_name_b)
    delete_db_on_exit([db_name_b])

    resp =
      Couch.put("/#{db_name}/_revs_limit",
        body: "#{@new_limit}",
        headers: ["Content-type": "application/json"]
      )

    assert resp.status_code == 200

    create_rev_doc(db_name, "foo", @new_limit + 1)
    resp = Couch.get("/#{db_name}/foo?revs=true")
    assert length(resp.body["_revisions"]["ids"]) == @new_limit

    # If you replicate after you make more edits than the limit, you'll
    # cause a spurious edit conflict.
    replicate(db_name, db_name_b)
    resp = Couch.get("/#{db_name_b}/foo?conflicts=true")
    assert not Map.has_key?(resp.body, "_conflicts")

    create_rev_doc(db_name, "foo", @new_limit - 1)

    # one less edit than limit, no conflict
    replicate(db_name, db_name_b)
    resp = Couch.get("/#{db_name_b}/foo?conflicts=true")
    assert not Map.has_key?(resp.body, "_conflicts")
    prev_conflicted_rev = resp.body["_rev"]

    # now we hit the limit
    create_rev_doc(db_name, "foo", @new_limit + 1)

    replicate(db_name, db_name_b)
    resp = Couch.get("/#{db_name_b}/foo?conflicts=true")
    assert Map.has_key?(resp.body, "_conflicts")

    conflicted_rev =
      resp.body["_conflicts"]
      |> Enum.at(0)

    # we have a conflict, but the previous replicated rev is always the losing
    # conflict
    assert conflicted_rev == prev_conflicted_rev
  end

  @tag :with_db
  test "revs limit is kept after compaction", context do
    db_name = context[:db_name]

    create_rev_doc(db_name, "bar", @new_limit + 1)
    resp = Couch.get("/#{db_name}/bar?revs=true")
    assert length(resp.body["_revisions"]["ids"]) == @new_limit + 1

    resp =
      Couch.put("/#{db_name}/_revs_limit",
        body: "#{@new_limit}",
        headers: ["Content-type": "application/json"]
      )

    assert resp.status_code == 200

    # We having already updated bar before setting the limit, so it's still got
    # a long rev history. compact to stem the revs.
    resp = Couch.get("/#{db_name}/bar?revs=true")
    assert length(resp.body["_revisions"]["ids"]) == @new_limit

    compact(db_name)

    # force reload because ETags don't honour compaction
    resp =
      Couch.get("/#{db_name}/bar?revs=true",
        headers: ["if-none-match": "pommes"]
      )

    assert length(resp.body["_revisions"]["ids"]) == @new_limit
  end

  # function to create a doc with multiple revisions
  defp create_rev_doc(db_name, id, num_revs) do
    resp = Couch.get("/#{db_name}/#{id}")

    doc =
      if resp.status_code == 200 do
        resp.body
      else
        %{_id: id, count: 0}
      end

    {:ok, resp} = create_doc(db_name, doc)
    create_rev_doc(db_name, id, num_revs, [Map.put(doc, :_rev, resp.body["rev"])])
  end

  defp create_rev_doc(db_name, id, num_revs, revs) do
    if length(revs) < num_revs do
      doc = %{_id: id, _rev: Enum.at(revs, -1)[:_rev], count: length(revs)}
      {:ok, resp} = create_doc(db_name, doc)

      create_rev_doc(
        db_name,
        id,
        num_revs,
        revs ++ [Map.put(doc, :_rev, resp.body["rev"])]
      )
    else
      revs
    end
  end

  def delete_db_on_exit(db_names) when is_list(db_names) do
    on_exit(fn ->
      Enum.each(db_names, fn name ->
        delete_db(name)
      end)
    end)
  end

end