-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3d490d
commit 33c7347
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defprotocol AsMap do | ||
@moduledoc """ | ||
""" | ||
def as_map(json) | ||
end | ||
|
||
defimpl AsMap, for: BitString do | ||
|
||
Poison.decode("") | ||
def as_map(json) do | ||
case Poison.decode(json) do | ||
{:ok, decoded} -> decoded | ||
{:error, :invalid} -> | ||
raise RuntimeError, message: | ||
""" | ||
Document returned by CouchDB is invalid | ||
json: #{json} | ||
""" | ||
{:error, reason} -> | ||
raise RuntimeError, message: "Document returned by CouchDB is not parseable\nreason: #{elem(reason, 0)}\nchar: #{elem(reason, 1)}\njson: #{json}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule Couchdb.Connector.AsMapTest do | ||
use ExUnit.Case | ||
|
||
import AsMap | ||
|
||
test "as_map/2 with invalid json string should raise RuntimeError" do | ||
invalid = "{\"_id\":\"foo\",\"_rev\":\"1-0f97561a543ed2e9c98a24dea818ec10\",test_key\":\"test_value\"}\n" | ||
|
||
assert_raise(RuntimeError, """ | ||
Document returned by CouchDB is not parseable | ||
reason: invalid | ||
char: t | ||
json: {"_id":"foo","_rev":"1-0f97561a543ed2e9c98a24dea818ec10",test_key":"test_value"} | ||
""", fn -> as_map(invalid) end) | ||
end | ||
|
||
test "as_map/2 with empty string should raise RuntimeError" do | ||
empty = "" | ||
|
||
assert_raise(RuntimeError, """ | ||
Document returned by CouchDB is invalid | ||
json: | ||
""", | ||
fn -> as_map(empty) end) | ||
end | ||
|
||
# test "as_map/2 with valid json string should return decoded Map" do | ||
# valid = "{\"_id\":\"foo\",\"_rev\":\"1-0f97561a543ed2e9c98a24dea818ec10\",test_key\":\"test_value\"}\n" | ||
# | ||
# end | ||
end |