Skip to content

Commit

Permalink
feat: add tests for caching on error cases and increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaAK committed Aug 31, 2023
1 parent 90f1f38 commit 2905a02
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 14 deletions.
2 changes: 1 addition & 1 deletion coveralls.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"skip_files": ["lib/application.ex"],
"skip_files": ["lib/request_cache/application.ex"],
"custom_stop_words": ["defdelegate"],
"terminal_options": {"file_column_width": 80},
"coverage_options": {"treat_no_relevant_lines_as_covered": true, "minimum_coverage": 85}
Expand Down
6 changes: 5 additions & 1 deletion lib/request_cache/resolver_middleware.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ if absinthe_loaded? do

@impl Absinthe.Middleware
def call(%Absinthe.Resolution{} = resolution, opts) do
enable_cache_for_resolution(resolution, opts)
if RequestCache.Config.enabled?() do
enable_cache_for_resolution(resolution, opts)
else
resolution
end
end

defp enable_cache_for_resolution(resolution, opts) do
Expand Down
12 changes: 6 additions & 6 deletions lib/request_cache/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ defmodule RequestCache.Util do

@moduledoc false

def parse_gql_name(query_string) do
case Regex.run(~r/^(?:query) ([^\({]+(?=\(|{))/, query_string, capture: :all_but_first) do
[query_name] -> String.trim(query_name)
_ -> nil
end
end
# def parse_gql_name(query_string) do
# case Regex.run(~r/^(?:query) ([^\({]+(?=\(|{))/, query_string, capture: :all_but_first) do
# [query_name] -> String.trim(query_name)
# _ -> nil
# end
# end

def create_key(url_path, query_string) do
"#{url_path}:#{hash_string(query_string)}"
Expand Down
10 changes: 10 additions & 0 deletions test/request_cache/con_cache_store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ defmodule RequestCache.ConCacheStoreTest do
assert {:ok, nil} = ConCacheStore.get(pid, key)
end
end

describe "&child_spec/1" do
test "starts up properly" do
pid_name = :"test_#{Enum.random(1..100_000_000)}"

start_link_supervised!(RequestCache.ConCacheStore.child_spec(name: :pid_name))

assert pid_name |> Process.whereis |> Process.alive?
end
end
end
41 changes: 38 additions & 3 deletions test/request_cache_absinthe_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,44 @@ defmodule RequestCacheAbsintheTest do
end
end

@tag capture_log: true
test "does not errors when error caching not enabled", %{call_pid: pid} do
assert %Plug.Conn{} = :get
|> conn(graphql_url(@uncached_query))
|> RequestCache.Support.Utils.ensure_default_opts()
|> Absinthe.Plug.put_options(context: %{call_pid: pid})
|> Router.call([])

assert_raise Plug.Conn.WrapperError, fn ->
conn = :get
|> conn(graphql_url(@uncached_query))
|> RequestCache.Support.Utils.ensure_default_opts()
|> Absinthe.Plug.put_options(context: %{call_pid: pid})
|> Router.call([])

assert [] === get_resp_header(conn, RequestCache.Plug.request_cache_header())
end
end

@tag capture_log: true
test "caches errors when error caching enabled", %{call_pid: pid} do
assert %Plug.Conn{} = :get
|> conn(graphql_url(@uncached_query))
|> RequestCache.Support.Utils.ensure_default_opts()
|> Absinthe.Plug.put_options(context: %{call_pid: pid})
|> Router.call([])

assert_raise Plug.Conn.WrapperError, fn ->
conn = :get
|> conn(graphql_url(@uncached_query))
|> RequestCache.Support.Utils.ensure_default_opts()
|> Absinthe.Plug.put_options(context: %{call_pid: pid})
|> Router.call([])

assert [] === get_resp_header(conn, RequestCache.Plug.request_cache_header())
end
end

@tag capture_log: true
test "allows you to use middleware before a resolver to cache the results of the request", %{call_pid: pid} do
conn = :get
Expand Down Expand Up @@ -161,9 +199,6 @@ defmodule RequestCacheAbsintheTest do
end) =~ "RequestCache requested"
end

test "allows you to use `cache` key inside opts to override specific cache for a request" do
end

defp graphql_url(query) do
"/graphql?#{URI.encode_query(%{query: query})}"
end
Expand Down
90 changes: 89 additions & 1 deletion test/request_cache_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ defmodule RequestCachePlugTest do
|> RequestCache.store(:timer.seconds(20))
|> send_resp(200, Jason.encode!(%{test: Enum.random(1..100_000_000)}))
end

match "/error-route" do
conn
|> RequestCache.store(:timer.seconds(20))
|> send_resp(404, Jason.encode!(%{code: :not_found, message: "NOT WORKING #{Enum.random(1..100_000_000)}"}))
end
end

defmodule EnsureCalledOnlyOncePlug do
Expand Down Expand Up @@ -221,6 +227,88 @@ defmodule RequestCachePlugTest do
]
end

test "allows you to use `cache` key inside opts to override specific cache for a request" do
test "doesn't cache errors if error caching not enabled", %{caller_pid: pid} do
route = "/error-route"

assert %Plug.Conn{resp_headers: uncached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(cached_errors: [])
|> put_private(:call_pid, pid)
|> Router.call([])

assert uncached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"}
]

assert %Plug.Conn{resp_headers: expected_uncached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> put_resp_content_type("text/html")
|> Router.call([])

assert expected_uncached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"}
]
end

test "caches errors if error cached error code enabled", %{caller_pid: pid} do
route = "/error-route"

assert %Plug.Conn{resp_headers: uncached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(cached_errors: [:not_found])
|> put_private(:call_pid, pid)
|> Router.call([])

assert uncached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"}
]

assert %Plug.Conn{resp_headers: expected_cached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> put_resp_content_type("text/html")
|> Router.call([])

assert expected_cached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"},
{"content-type", "text/html; charset=utf-8"},
{"rc-cache-status", "HIT"}
]
end

test "caches errors if error caching enabled", %{caller_pid: pid} do
route = "/error-route"

assert %Plug.Conn{resp_headers: uncached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(cached_errors: :all)
|> put_private(:call_pid, pid)
|> Router.call([])

assert uncached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"}
]

assert %Plug.Conn{resp_headers: expected_cached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> put_resp_content_type("text/html")
|> Router.call([])

assert expected_cached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"},
{"content-type", "text/html; charset=utf-8"},
{"rc-cache-status", "HIT"}
]
end
end
8 changes: 6 additions & 2 deletions test/support/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ defmodule RequestCache.Support.Utils do

alias Plug.Conn

def ensure_default_opts(conn) do
Conn.put_private(conn, RequestCache.Config.conn_private_key(), request: [])
def ensure_default_opts(conn, extra_opts \\ []) do
Conn.put_private(
conn,
RequestCache.Config.conn_private_key(),
Keyword.merge([request: []], extra_opts)
)
end

def graphql_conn, do: "GET" |> build_conn("/graphql") |> ensure_default_opts
Expand Down

0 comments on commit 2905a02

Please sign in to comment.