Skip to content

Commit

Permalink
[#20] testing httpoison_adapter.post method, fixing repo.insert, unit…
Browse files Browse the repository at this point in the history
… tests for repo.insert including error cases
  • Loading branch information
flaviogranero committed May 9, 2016
1 parent 2fa9293 commit 407f48b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/dayron/adapters/httpoison_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule Dayron.HTTPoisonAdapter do

def process_request_body(body), do: Poison.encode!(body)

def process_response_body(_body = ""), do: nil
def process_response_body(""), do: nil
def process_response_body("ok"), do: %{}

def process_response_body(body) do
body |> Poison.decode! |> process_decoded_body
Expand Down
9 changes: 4 additions & 5 deletions lib/dayron/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,10 @@ defmodule Dayron.Repo do
def insert(adapter, model, data, opts, config) do
{url, response} = post_response(adapter, model, data, opts, config)
case response do
%HTTPoison.Response{status_code: 200, body: body} ->
{:ok, Model.from_json(model, data)}
%HTTPoison.Response{status_code: 422} ->
# TODO: return a changeset with validation errors
{:error, model}
%HTTPoison.Response{status_code: 201, body: body} ->
{:ok, Model.from_json(model, body)}
%HTTPoison.Response{status_code: 422, body: body} ->
{:error, body}
%HTTPoison.Response{status_code: code, body: body} when code >= 500 ->
raise Dayron.ServerError, method: "GET", url: url, body: body
%HTTPoison.Error{reason: reason} ->
Expand Down
14 changes: 13 additions & 1 deletion test/lib/dayron/adapters/httpoison_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,22 @@ defmodule Dayron.HTTPoisonAdapterTest do
assert second[:name] == "Second Resource"
end

test "returns a decoded body for a valid post request", %{bypass: bypass, api_url: api_url} do
Bypass.expect bypass, fn conn ->
assert "/resources" == conn.request_path
assert [{"accept", "application/json"}, {"content-type", "application/json"} | _] = conn.req_headers
assert "POST" == conn.method
Plug.Conn.resp(conn, 201, ~s<{"name": "Full Name", "age": 30}>)
end
response = HTTPoisonAdapter.post("#{api_url}/resources", %{name: "Full Name", age: 30})
assert {:ok, %HTTPoison.Response{status_code: 201, body: body}} = response
assert body[:name] == "Full Name"
assert body[:age] == 30
end

test "accepts custom headers", %{bypass: bypass, api_url: api_url} do
Bypass.expect bypass, fn conn ->
assert "/resources/id" == conn.request_path
IO.inspect {:req_headers, conn.req_headers}
assert [{"accept", "application/json"}, {"content-type", "application/json"} | _] = conn.req_headers
assert [_a, _b, _c, {"accesstoken", "token"} | _] = conn.req_headers
assert "GET" == conn.method
Expand Down
20 changes: 14 additions & 6 deletions test/lib/dayron/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Dayron.RepoTest do
defmodule MyModel do
use Dayron.Model, resource: "resources"

defstruct name: "", age: 0
defstruct id: nil, name: "", age: 0
end

# ================ GET ===========================
Expand Down Expand Up @@ -131,14 +131,22 @@ defmodule Dayron.RepoTest do
end

test "`insert` fails when creating a resource from an invalid model" do

data = %{name: nil, age: 30}
{:error, errors} = TestRepo.insert(MyModel, data)
assert errors[:error] == "name is required"
end

test "`insert` creates a valid resource from a changeset" do

test "`insert` raises an exception on request error" do
msg = ~r/Internal Exception/
assert_raise Dayron.ServerError, msg, fn ->
TestRepo.insert(MyModel, %{error: "server-error"})
end
end

test "`insert` fails when creating a resource from an invalid changeset" do

test "`insert` raises an exception on connection error" do
msg = ~r/econnrefused/
assert_raise Dayron.ClientError, msg, fn ->
TestRepo.insert(MyModel, %{error: "connection-error"})
end
end
end
15 changes: 14 additions & 1 deletion test/support/test_repo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,21 @@ defmodule Dayron.TestAdapter do
{:error, %HTTPoison.Error{id: nil, reason: :connect_timeout}}
end

def post("http://localhost/resources", %{error: "server-error"}, [], []) do
{:ok, %HTTPoison.Response{status_code: 500, body: "Internal Exception..."}}
end

def post("http://localhost/resources", %{error: "connection-error"}, [], []) do
{:error, %HTTPoison.Error{id: nil, reason: :econnrefused}}
end

def post("http://localhost/resources", %{name: nil}, [], []) do
{:ok, %HTTPoison.Response{status_code: 422, body: %{error: "name is required"}}}
end

def post("http://localhost/resources", model, [], []) do
{:ok, %HTTPoison.Response{status_code: 200, body: Poison.encode!(model)}}
model = Map.put(model, :id, "new-model-id")
{:ok, %HTTPoison.Response{status_code: 201, body: model}}
end

%HTTPoison.Error{reason: :connect_timeout}
Expand Down

0 comments on commit 407f48b

Please sign in to comment.