Skip to content

Commit

Permalink
Make mock adapter usable for testing
Browse files Browse the repository at this point in the history
Make adapter configurable and make mock adapter public
  • Loading branch information
Ninigi authored Mar 14, 2018
2 parents 7139760 + 6c6d215 commit d91cb61
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
26 changes: 21 additions & 5 deletions lib/shopify/adapters/mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ defmodule Shopify.Adapters.Mock do

@behaviour Shopify.Adapters.Base

alias Shopify.{Request, Response}
alias Shopify.{
Request,
Response,
Config
}

def get(request) do
request
Expand Down Expand Up @@ -50,12 +54,24 @@ defmodule Shopify.Adapters.Mock do
end

def load_resource(%Request{path: path, body: nil}) do
Path.expand("../../../test/fixtures/#{path}", __DIR__)
Config.fixtures_path <> "/" <> path
|> File.read
end

def load_resource(%Request{body: body}) do
{:ok, body}
case Poison.decode(body) do
{:ok, resource} -> resource |> put_id |> Poison.encode
{:error, _} -> {:error, nil}
end
end

defp put_id(resource) do
key = resource |> Map.keys |> List.first
resource
|> Map.values
|> List.first
|> Map.put_new("id", 1)
|> (fn parsed -> %{key => parsed} end).()
end

def authorize(request) do
Expand All @@ -74,6 +90,6 @@ defmodule Shopify.Adapters.Mock do
end

def oauth_auth(_) do

end
end
end
10 changes: 5 additions & 5 deletions lib/shopify/client.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule Shopify.Client do
@moduledoc false

@adapter Shopify.Config.get(:client_adapter) || Shopify.Adapters.HTTP
alias Shopify.Config

def get(request), do: @adapter.get(request)
def get(request), do: Config.client_adapter.get(request)

def post(request), do: @adapter.post(request)
def post(request), do: Config.client_adapter.post(request)

def put(request), do: @adapter.put(request)
def put(request), do: Config.client_adapter.put(request)

def delete(request), do: @adapter.delete(request)
def delete(request), do: Config.client_adapter.delete(request)
end
10 changes: 8 additions & 2 deletions lib/shopify/config.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
defmodule Shopify.Config do
@moduledoc false

def get(name, default \\ nil) do
Application.get_env(:shopify, name, default)
end

def client_adapter, do: get(:client_adapter, Shopify.Adapters.HTTP)

def shop_name, do: get(:shop_name)

def api_key, do: get(:api_key)

def password, do: get(:password)

def version, do: Mix.Project.config[:version]
end

def fixtures_path do
get(:fixtures_path) || Path.expand("../../test/fixtures/", __DIR__)
end
end
16 changes: 16 additions & 0 deletions test/adapters/mock_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Shopify.Adapters.Mocktest do
use ExUnit.Case, async: true

alias Shopify.{Webhook, Request, Adapters.Mock}

test "it puts an id for created resources" do
body = "{\"webhook\": {\"address\": \"http://www.yoloship.it/webhook\"}}"

{:ok, %{data: resource}} = Shopify.session
|> Request.new(Webhook.all_url(), %{}, Webhook.singular_resource(), body)
|> Mock.post

assert resource.id != nil
end

end

0 comments on commit d91cb61

Please sign in to comment.