From d5f2c581d07ce8ad90f72834cb73ee1b9649efee Mon Sep 17 00:00:00 2001 From: Brian Alexander Date: Fri, 10 May 2024 15:41:12 -0600 Subject: [PATCH] fix tests --- lib/sanity.ex | 2 +- lib/sanity/response.ex | 2 +- test/sanity_test.exs | 126 ++++++++++--------------------------- test/support/mock_finch.ex | 6 -- test/support/mock_req.ex | 5 ++ 5 files changed, 41 insertions(+), 100 deletions(-) delete mode 100644 test/support/mock_finch.ex create mode 100644 test/support/mock_req.ex diff --git a/lib/sanity.ex b/lib/sanity.ex index 00e614d..dbb9415 100644 --- a/lib/sanity.ex +++ b/lib/sanity.ex @@ -232,7 +232,7 @@ defmodule Sanity do [] iex> Sanity.result!(%Sanity.Response{body: %{}, status: 200}) - ** (Sanity.Error) %Sanity.Response{body: %{}, headers: nil, status: 200} + ** (Sanity.Error) %Sanity.Response{body: %{}, headers: %{}, status: 200} """ @spec result!(Response.t()) :: any() def result!(%Response{body: %{"result" => result}}), do: result diff --git a/lib/sanity/response.ex b/lib/sanity/response.ex index 7b0e445..07f0d13 100644 --- a/lib/sanity/response.ex +++ b/lib/sanity/response.ex @@ -1,5 +1,5 @@ defmodule Sanity.Response do @type t :: %Sanity.Response{} - defstruct [:body, :headers, :status] + defstruct body: %{}, headers: %{}, status: nil end diff --git a/test/sanity_test.exs b/test/sanity_test.exs index f4ad2f3..0f04a03 100644 --- a/test/sanity_test.exs +++ b/test/sanity_test.exs @@ -5,14 +5,14 @@ defmodule SanityTest do import Mox setup :verify_on_exit! - alias Sanity.{MockFinch, MockSanity, Request, Response} + alias Sanity.{MockReq, MockSanity, Request, Response} alias NimbleOptions.ValidationError @request_config [ dataset: "myset", - finch_mod: MockFinch, http_options: [receive_timeout: 1], project_id: "projectx", + req_mod: MockReq, token: "supersecret" ] @@ -107,34 +107,33 @@ defmodule SanityTest do describe "request" do test "with query" do - Mox.expect(MockFinch, :request, fn request, Sanity.Finch, [receive_timeout: 1] -> - assert %Finch.Request{ + Mox.expect(MockReq, :request, fn opts -> + assert Enum.sort(opts) == [ body: nil, headers: [{"authorization", "Bearer supersecret"}], - host: "projectx.api.sanity.io", - method: "GET", - path: "/v2021-10-21/data/query/myset", - port: 443, - query: "%24var_2=%22y%22&query=%2A", - scheme: :https - } == request - - {:ok, %Finch.Response{body: "{}", headers: [], status: 200}} + method: :get, + receive_timeout: 1, + url: + "https://projectx.api.sanity.io/v2021-10-21/data/query/myset?%24var_2=%22y%22&query=%2A" + ] + + {:ok, %Req.Response{body: %{}, headers: %{}, status: 200}} end) - assert {:ok, %Response{body: %{}, headers: [], status: 200}} == + assert {:ok, %Response{body: %{}, headers: %{}, status: 200}} == Sanity.query("*", var_2: "y") |> Sanity.request(@request_config) end test "with CDN URL" do - Mox.expect(MockFinch, :request, fn %Finch.Request{host: "projectx.apicdn.sanity.io"}, - Sanity.Finch, - _ -> - {:ok, %Finch.Response{body: "{}", headers: [], status: 200}} + Mox.expect(MockReq, :request, fn opts -> + assert Keyword.fetch!(opts, :url) == + "https://projectx.apicdn.sanity.io/v2021-10-21/data/query/myset?query=%2A" + + {:ok, %Req.Response{body: %{}, headers: %{}, status: 200}} end) - assert {:ok, %Response{body: %{}, headers: [], status: 200}} == + assert {:ok, %Response{body: %{}, headers: %{}, status: 200}} == Sanity.query("*") |> Sanity.request(Keyword.put(@request_config, :cdn, true)) end @@ -170,11 +169,10 @@ defmodule SanityTest do end test "409 response" do - Mox.expect(MockFinch, :request, fn %Finch.Request{}, Sanity.Finch, _ -> + Mox.expect(MockReq, :request, fn _opts -> {:ok, - %Finch.Response{ - body: "{\"error\":{\"description\":\"The mutation(s) failed...\"}}", - headers: [{"content-type", "application/json; charset=utf-8"}], + %Req.Response{ + body: %{"error" => %{"description" => "The mutation(s) failed..."}}, status: 409 }} end) @@ -183,22 +181,18 @@ defmodule SanityTest do :error, %Sanity.Response{ body: %{"error" => %{"description" => "The mutation(s) failed..."}}, - headers: [{"content-type", "application/json; charset=utf-8"}], status: 409 } } end test "414 response" do - Mox.expect(MockFinch, :request, fn %Finch.Request{}, Sanity.Finch, _ -> + Mox.expect(MockReq, :request, fn _ -> {:ok, - %Finch.Response{ + %Req.Response{ status: 414, body: - "\r\n414 Request-URI Too Large\r\n\r\n

414 Request-URI Too Large

\r\n
nginx
\r\n\r\n\r\n", - headers: [ - {"content-type", "text/html; charset=UTF-8"} - ] + "\r\n414 Request-URI Too Large\r\n\r\n

414 Request-URI Too Large

\r\n
nginx
\r\n\r\n\r\n" }} end) @@ -208,32 +202,31 @@ defmodule SanityTest do end assert exception == %Sanity.Error{ - source: %Finch.Response{ + source: %Req.Response{ status: 414, body: - "\r\n414 Request-URI Too Large\r\n\r\n

414 Request-URI Too Large

\r\n
nginx
\r\n\r\n\r\n", - headers: [{"content-type", "text/html; charset=UTF-8"}] + "\r\n414 Request-URI Too Large\r\n\r\n

414 Request-URI Too Large

\r\n
nginx
\r\n\r\n\r\n" } } end test "5xx response" do - Mox.expect(MockFinch, :request, fn %Finch.Request{}, Sanity.Finch, _ -> - {:ok, %Finch.Response{body: "fail!", headers: [], status: 500}} + Mox.expect(MockReq, :request, fn _ -> + {:ok, %Req.Response{body: "fail!", headers: %{}, status: 500}} end) exception = - assert_raise Sanity.Error, ~r'%Finch.Response{', fn -> + assert_raise Sanity.Error, ~r'#Req.Response<', fn -> Sanity.query("*") |> Sanity.request(@request_config) end assert exception == %Sanity.Error{ - source: %Finch.Response{body: "fail!", headers: [], status: 500} + source: %Req.Response{body: "fail!", headers: %{}, status: 500} } end test "timeout error" do - Mox.expect(MockFinch, :request, fn %Finch.Request{}, Sanity.Finch, _ -> + Mox.expect(MockReq, :request, fn _ -> {:error, %Mint.TransportError{reason: :timeout}} end) @@ -243,64 +236,13 @@ defmodule SanityTest do Sanity.query("*") |> Sanity.request(@request_config) end end - - test "retries and succeeds" do - Mox.expect(MockFinch, :request, fn %Finch.Request{}, Sanity.Finch, _ -> - {:error, %Mint.TransportError{reason: :timeout}} - end) - - Mox.expect(MockFinch, :request, fn %Finch.Request{}, Sanity.Finch, _ -> - {:ok, %Finch.Response{body: "fail!", headers: [], status: 500}} - end) - - Mox.expect(MockFinch, :request, fn %Finch.Request{}, Sanity.Finch, _ -> - {:ok, %Finch.Response{body: "{}", headers: [], status: 200}} - end) - - log = - ExUnit.CaptureLog.capture_log([level: :warning], fn -> - assert {:ok, %Sanity.Response{body: %{}, headers: [], status: 200}} = - Sanity.query("*") - |> Sanity.request( - Keyword.merge(@request_config, max_attempts: 3, retry_delay: 10) - ) - end) - - assert log =~ - ~s'retrying failed request in 10ms: %Mint.TransportError{reason: :timeout}' - - assert log =~ - ~s'retrying failed request in 20ms: %Finch.Response{' - end - - test "retries and fails" do - Mox.expect(MockFinch, :request, fn %Finch.Request{}, Sanity.Finch, _ -> - {:ok, %Finch.Response{body: "fail!", headers: [], status: 500}} - end) - - Mox.expect(MockFinch, :request, fn %Finch.Request{}, Sanity.Finch, _ -> - {:error, %Mint.TransportError{reason: :timeout}} - end) - - log = - ExUnit.CaptureLog.capture_log([level: :warning], fn -> - assert_raise Sanity.Error, "%Mint.TransportError{reason: :timeout}", fn -> - Sanity.query("*") - |> Sanity.request(Keyword.merge(@request_config, max_attempts: 2, retry_delay: 5)) - end - end) - - assert log =~ - ~s'retrying failed request in 5ms: %Finch.Response{' - end end test "request!" do - Mox.expect(MockFinch, :request, fn %Finch.Request{}, Sanity.Finch, _ -> + Mox.expect(MockReq, :request, fn _ -> {:ok, - %Finch.Response{ - body: "{\"error\":{\"description\":\"The mutation(s) failed...\"}}", - headers: [{"content-type", "application/json; charset=utf-8"}], + %Req.Response{ + body: %{"error" => %{"description" => "The mutation(s) failed..."}}, status: 409 }} end) diff --git a/test/support/mock_finch.ex b/test/support/mock_finch.ex deleted file mode 100644 index 6a4308b..0000000 --- a/test/support/mock_finch.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Sanity.FinchBehavior do - @callback request(Finch.Request.t(), Finch.name(), keyword()) :: - {:ok, Finch.Response.t()} | {:error, Mint.Types.error()} -end - -Mox.defmock(Sanity.MockFinch, for: Sanity.FinchBehavior) diff --git a/test/support/mock_req.ex b/test/support/mock_req.ex new file mode 100644 index 0000000..fb66c03 --- /dev/null +++ b/test/support/mock_req.ex @@ -0,0 +1,5 @@ +defmodule Sanity.ReqBehavior do + @callback request(keyword()) :: {:ok, Req.Response.t()} | {:error, Exception.t()} +end + +Mox.defmock(Sanity.MockReq, for: Sanity.ReqBehavior)