|
| 1 | +defmodule LocalDocsTest do |
| 2 | + use CouchTestCase |
| 3 | + |
| 4 | + @moduletag :local_docs |
| 5 | + |
| 6 | + @moduledoc """ |
| 7 | + Test CouchDB _local_docs |
| 8 | + """ |
| 9 | + |
| 10 | + setup_all do |
| 11 | + db_name = random_db_name() |
| 12 | + {:ok, _} = create_db(db_name) |
| 13 | + on_exit(fn -> delete_db(db_name) end) |
| 14 | + |
| 15 | + resp1 = Couch.put( |
| 16 | + "/#{db_name}/_local/foo", |
| 17 | + body: %{ |
| 18 | + _id: "foo", |
| 19 | + bar: "baz" |
| 20 | + } |
| 21 | + ) |
| 22 | + assert resp1.status_code == 201 |
| 23 | + |
| 24 | + resp2 = Couch.put( |
| 25 | + "/#{db_name}/_local/foo2", |
| 26 | + body: %{ |
| 27 | + _id: "foo", |
| 28 | + bar: "baz2" |
| 29 | + } |
| 30 | + ) |
| 31 | + assert resp2.status_code == 201 |
| 32 | + |
| 33 | + {:ok, [db_name: db_name]} |
| 34 | + end |
| 35 | + |
| 36 | + test "GET with no parameters", context do |
| 37 | + resp = Couch.get( |
| 38 | + "/#{context[:db_name]}/_local_docs" |
| 39 | + ) |
| 40 | + |
| 41 | + assert resp.status_code == 200 |
| 42 | + assert length(Map.get(resp, :body)["rows"]) == 2 |
| 43 | + end |
| 44 | + |
| 45 | + test "GET with multiple keys", context do |
| 46 | + resp = Couch.get( |
| 47 | + "/#{context[:db_name]}/_local_docs", |
| 48 | + query: %{ |
| 49 | + :keys => "[\"_local/foo\", \"_local/foo2\"]", |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + assert resp.status_code == 200 |
| 54 | + assert length(Map.get(resp, :body)["rows"]) == 2 |
| 55 | + end |
| 56 | + |
| 57 | + test "POST with empty body", context do |
| 58 | + resp = Couch.post( |
| 59 | + "/#{context[:db_name]}/_local_docs", |
| 60 | + body: %{} |
| 61 | + ) |
| 62 | + |
| 63 | + assert resp.status_code == 200 |
| 64 | + assert length(Map.get(resp, :body)["rows"]) == 2 |
| 65 | + end |
| 66 | + |
| 67 | + test "POST with keys and limit", context do |
| 68 | + resp = Couch.post( |
| 69 | + "/#{context[:db_name]}/_local_docs", |
| 70 | + body: %{ |
| 71 | + :keys => ["_local/foo", "_local/foo2"], |
| 72 | + :limit => 1 |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + assert resp.status_code == 200 |
| 77 | + assert length(Map.get(resp, :body)["rows"]) == 1 |
| 78 | + end |
| 79 | + |
| 80 | + test "POST with query parameter and JSON body", context do |
| 81 | + resp = Couch.post( |
| 82 | + "/#{context[:db_name]}/_local_docs", |
| 83 | + query: %{ |
| 84 | + :limit => 1 |
| 85 | + }, |
| 86 | + body: %{ |
| 87 | + :keys => ["_local/foo", "_local/foo2"] |
| 88 | + } |
| 89 | + ) |
| 90 | + |
| 91 | + assert resp.status_code == 200 |
| 92 | + assert length(Map.get(resp, :body)["rows"]) == 1 |
| 93 | + end |
| 94 | + |
| 95 | + test "POST edge case with colliding parameters - query takes precedence", context do |
| 96 | + resp = Couch.post( |
| 97 | + "/#{context[:db_name]}/_local_docs", |
| 98 | + query: %{ |
| 99 | + :limit => 0 |
| 100 | + }, |
| 101 | + body: %{ |
| 102 | + :keys => ["_local/foo", "_local/foo2"], |
| 103 | + :limit => 2 |
| 104 | + } |
| 105 | + ) |
| 106 | + |
| 107 | + assert resp.status_code == 200 |
| 108 | + assert Enum.empty?(Map.get(resp, :body)["rows"]) |
| 109 | + end |
| 110 | +end |
0 commit comments