summaryrefslogtreecommitdiff
path: root/test/elixir/test/design_docs_test.exs
blob: 85d2dd175a5c799376e8494033809cc9dc5c87ae (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
defmodule DesignDocsTest do
  use CouchTestCase

  @moduletag :design_docs

  @design_doc %{
    _id: "_design/test",
    language: "javascript",
    autoupdate: false,
    whatever: %{
      stringzone: "exports.string = 'plankton';",
      commonjs: %{
        whynot: """
        exports.test = require('../stringzone');
        exports.foo = require('whatever/stringzone');
        """,
        upper: """
        exports.testing = require('./whynot').test.string.toUpperCase()+
        module.id+require('./whynot').foo.string
        """,
        circular_one: "require('./circular_two'); exports.name = 'One';",
        circular_two: "require('./circular_one'); exports.name = 'Two';"
      },
      # paths relative to parent
      idtest1: %{
        a: %{
          b: %{d: "module.exports = require('../c/e').id;"},
          c: %{e: "exports.id = module.id;"}
        }
      },
      # multiple paths relative to parent
      idtest2: %{
        a: %{
          b: %{d: "module.exports = require('../../a/c/e').id;"},
          c: %{e: "exports.id = module.id;"}
        }
      },
      # paths relative to module
      idtest3: %{
        a: %{
          b: "module.exports = require('./c/d').id;",
          c: %{
            d: "module.exports = require('./e');",
            e: "exports.id = module.id;"
          }
        }
      },
      # paths relative to module and parent
      idtest4: %{
        a: %{
          b: "module.exports = require('../a/./c/d').id;",
          c: %{
            d: "module.exports = require('./e');",
            e: "exports.id = module.id;"
          }
        }
      },
      # paths relative to root
      idtest5: %{
        a: "module.exports = require('whatever/idtest5/b').id;",
        b: "exports.id = module.id;"
      }
    },
    views: %{
      all_docs_twice: %{
        map: """
        function(doc) {
          emit(doc.integer, null);
          emit(doc.integer, null);
        }
        """
      },
      no_docs: %{
        map: """
        function(doc) {}
        """
      },
      single_doc: %{
        map: """
        function(doc) {
          if (doc._id === "1") {
            emit(1, null);
          }
        }
        """
      },
      summate: %{
        map: """
        function(doc) {
          emit(doc.integer, doc.integer);
        }
        """,
        reduce: """
        function(keys, values) {
          return sum(values);
        }
        """
      },
      summate2: %{
        map: """
        function(doc) {
          emit(doc.integer, doc.integer);
        }
        """,
        reduce: """
        function(keys, values) {
          return sum(values);
        }
        """
      },
      huge_src_and_results: %{
        map: """
         function(doc) {
           if (doc._id === "1") {
             emit("#{String.duplicate("a", 16)}", null);
           }
         }
        """,
        reduce: """
        function(keys, values) {
          return "#{String.duplicate("a", 16)}";
        }
        """
      },
      lib: %{
        baz: "exports.baz = 'bam';",
        foo: %{
          foo: "exports.foo = 'bar';",
          boom: "exports.boom = 'ok';",
          zoom: "exports.zoom = 'yeah';"
        }
      },
      commonjs: %{
        map: """
        function(doc) {
          emit(null, require('views/lib/foo/boom').boom);
        }
        """
      }
    },
    shows: %{
      simple: """
      function() {
        return 'ok';
      }
      """,
      requirey: """
      function() {
        var lib = require('whatever/commonjs/upper');
        return lib.testing;
      }
      """,
      circular: """
      function() {
        var lib = require('whatever/commonjs/upper');
        return JSON.stringify(this);
      }
      """,
      circular_require: """
      function() {
        return require('whatever/commonjs/circular_one').name;
      }
      """,
      idtest1: """
      function() {
        return require('whatever/idtest1/a/b/d');
      }
      """,
      idtest2: """
      function() {
        return require('whatever/idtest2/a/b/d');
      }
      """,
      idtest3: """
      function() {
        return require('whatever/idtest3/a/b');
      }
      """,
      idtest4: """
      function() {
        return require('whatever/idtest4/a/b');
      }
      """,
      idtest5: """
      function() {
        return require('whatever/idtest5/a');
      }
      """
    }
  }

  setup_all do
    db_name = random_db_name()
    {:ok, _} = create_db(db_name)
    on_exit(fn -> delete_db(db_name) end)

    {:ok, _} = create_doc(db_name, @design_doc)
    {:ok, _} = create_doc(db_name, %{})
    {:ok, [db_name: db_name]}
  end

  test "consistent _rev for design docs", context do
    resp = Couch.get("/#{context[:db_name]}/_design/test")
    assert resp.status_code == 200
    first_db_rev = resp.body["_rev"]

    second_db_name = random_db_name()
    create_db(second_db_name)
    {:ok, resp2} = create_doc(second_db_name, @design_doc)
    assert first_db_rev == resp2.body["rev"]
  end

  test "commonjs require", context do
    db_name = context[:db_name]
    resp = Couch.get("/#{db_name}/_design/test/_show/requirey")
    assert resp.status_code == 200
    assert resp.body == "PLANKTONwhatever/commonjs/upperplankton"

    resp = Couch.get("/#{db_name}/_design/test/_show/circular")
    assert resp.status_code == 200

    result =
      resp.body
      |> IO.iodata_to_binary()
      |> :jiffy.decode([:return_maps])

    assert result["language"] == "javascript"
  end

  test "circular commonjs dependencies", context do
    db_name = context[:db_name]
    resp = Couch.get("/#{db_name}/_design/test/_show/circular_require")
    assert resp.status_code == 200
    assert resp.body == "One"
  end

  test "module id values are as expected", context do
    db_name = context[:db_name]

    check_id_value(db_name, "idtest1", "whatever/idtest1/a/c/e")
    check_id_value(db_name, "idtest2", "whatever/idtest2/a/c/e")
    check_id_value(db_name, "idtest3", "whatever/idtest3/a/c/e")
    check_id_value(db_name, "idtest4", "whatever/idtest4/a/c/e")
    check_id_value(db_name, "idtest5", "whatever/idtest5/b")
  end

  defp check_id_value(db_name, id, expected) do
    resp = Couch.get("/#{db_name}/_design/test/_show/#{id}")
    assert resp.status_code == 200
    assert resp.body == expected
  end

  @tag :with_db
  test "that we get correct design doc info back", context do
    db_name = context[:db_name]
    {:ok, _} = create_doc(db_name, @design_doc)

    resp = Couch.get("/#{db_name}/_design/test/_info")
    prev_view_sig = resp.body["view_index"]["signature"]
    prev_view_size = resp.body["view_index"]["sizes"]["file"]

    num_docs = 500
    bulk_save(db_name, make_docs(1..(num_docs + 1)))

    Couch.get("/#{db_name}/_design/test/_view/summate", query: [stale: "ok"])

    for _x <- 0..1 do
      resp = Couch.get("/#{db_name}/_design/test/_info")
      assert resp.body["name"] == "test"
      assert resp.body["view_index"]["sizes"]["file"] == prev_view_size
      assert resp.body["view_index"]["compact_running"] == false
      assert resp.body["view_index"]["signature"] == prev_view_sig
    end
  end

  test "commonjs in map functions", context do
    db_name = context[:db_name]

    resp = Couch.get("/#{db_name}/_design/test/_view/commonjs", query: [limit: 1])
    assert resp.status_code == 200
    assert Enum.at(resp.body["rows"], 0)["value"] == "ok"
  end

  test "_all_docs view returns correctly with keys", context do
    db_name = context[:db_name]

    resp =
      Couch.get("/#{db_name}/_all_docs",
        query: [startkey: :jiffy.encode("_design"), endkey: :jiffy.encode("_design0")]
      )

    assert length(resp.body["rows"]) == 1
  end

  @tag :with_db
  test "all_docs_twice", context do
    db_name = context[:db_name]
    {:ok, _} = create_doc(db_name, @design_doc)

    num_docs = 500
    bulk_save(db_name, make_docs(1..(2 * num_docs)))

    for _x <- 0..1 do
      test_all_docs_twice(db_name, num_docs)
    end
  end

  defp test_all_docs_twice(db_name, num_docs) do
    resp = Couch.get("/#{db_name}/_design/test/_view/all_docs_twice")
    assert resp.status_code == 200
    rows = resp.body["rows"]

    for x <- 0..num_docs do
      assert Map.get(Enum.at(rows, 2 * x), "key") == x + 1
      assert Map.get(Enum.at(rows, 2 * x + 1), "key") == x + 1
    end

    resp = Couch.get("/#{db_name}/_design/test/_view/no_docs")
    assert resp.body["total_rows"] == 0

    resp = Couch.get("/#{db_name}/_design/test/_view/single_doc")
    assert resp.body["total_rows"] == 1
  end

  @tag :with_db
  test "language not specified, Javascript is implied", context do
    db_name = context[:db_name]
    bulk_save(db_name, make_docs(1..2))

    design_doc_2 = %{
      _id: "_design/test2",
      views: %{
        single_doc: %{
          map: """
          function(doc) {
            if (doc._id === "1") {
              emit(1, null);
            }
          }
          """
        }
      }
    }

    {:ok, _} = create_doc(db_name, design_doc_2)

    resp = Couch.get("/#{db_name}/_design/test2/_view/single_doc")
    assert resp.status_code == 200
    assert length(resp.body["rows"]) == 1
  end

  @tag :with_db
  test "startkey and endkey", context do
    db_name = context[:db_name]
    {:ok, _} = create_doc(db_name, @design_doc)

    num_docs = 500
    bulk_save(db_name, make_docs(1..(2 * num_docs)))

    resp = Couch.get("/#{db_name}/_design/test/_view/summate")
    assert Enum.at(resp.body["rows"], 0)["value"] == summate(num_docs * 2)

    resp =
      Couch.get("/#{db_name}/_design/test/_view/summate",
        query: [startkey: 4, endkey: 4]
      )

    assert Enum.at(resp.body["rows"], 0)["value"] == 4

    resp =
      Couch.get("/#{db_name}/_design/test/_view/summate",
        query: [startkey: 4, endkey: 5]
      )

    assert Enum.at(resp.body["rows"], 0)["value"] == 9

    resp =
      Couch.get("/#{db_name}/_design/test/_view/summate",
        query: [startkey: 4, endkey: 6]
      )

    assert Enum.at(resp.body["rows"], 0)["value"] == 15

    # test start_key and end_key aliases
    resp =
      Couch.get("/#{db_name}/_design/test/_view/summate",
        query: [start_key: 4, end_key: 6]
      )

    assert Enum.at(resp.body["rows"], 0)["value"] == 15

    # Verify that a shared index (view def is an exact copy of "summate")
    # does not confuse the reduce stage
    resp =
      Couch.get("/#{db_name}/_design/test/_view/summate2",
        query: [startkey: 4, endkey: 6]
      )

    assert Enum.at(resp.body["rows"], 0)["value"] == 15

    for x <- 0..Integer.floor_div(num_docs, 60) do
      resp =
        Couch.get("/#{db_name}/_design/test/_view/summate",
          query: [startkey: x * 30, endkey: num_docs - x * 30]
        )

      assert Enum.at(resp.body["rows"], 0)["value"] ==
               summate(num_docs - x * 30) - summate(x * 30 - 1)
    end
  end

  defp summate(n) do
    (n + 1) * (n / 2)
  end

  @tag :with_db
  test "design doc deletion", context do
    db_name = context[:db_name]
    {:ok, resp} = create_doc(db_name, @design_doc)

    del_resp =
      Couch.delete("/#{db_name}/#{resp.body["id"]}", query: [rev: resp.body["rev"]])

    assert del_resp.status_code == 200

    resp = Couch.get("/#{db_name}/#{resp.body["id"]}")
    assert resp.status_code == 404

    resp = Couch.get("/#{db_name}/_design/test/_view/no_docs")
    assert resp.status_code == 404
  end

  @tag :with_db
  test "validate doc update", context do
    db_name = context[:db_name]

    # COUCHDB-1227 - if a design document is deleted, by adding a "_deleted"
    # field with the boolean value true, its validate_doc_update functions
    # should no longer have effect.

    ddoc = %{
      _id: "_design/test",
      language: "javascript",
      validate_doc_update: """
        function(newDoc, oldDoc, userCtx, secObj) {
         if (newDoc.value % 2 == 0) {
            throw({forbidden: "dont like even numbers"});
         }
         return true;
      }
      """
    }

    {:ok, resp_ddoc} = create_doc(db_name, ddoc)

    resp =
      Couch.post("/#{db_name}",
        body: %{_id: "doc1", value: 4}
      )

    assert resp.status_code == 403
    assert resp.body["reason"] == "dont like even numbers"

    ddoc_resp = Couch.get("/#{db_name}/#{resp_ddoc.body["id"]}")

    ddoc =
      ddoc_resp.body
      |> Map.put("_deleted", true)

    del_resp =
      Couch.post("/#{db_name}",
        body: ddoc
      )

    assert del_resp.status_code in [201, 202]

    {:ok, _} = create_doc(db_name, %{_id: "doc1", value: 4})
  end
end