Skip to content

Commit

Permalink
test: add macro for asserting on links (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
gordoneliel authored Sep 13, 2023
1 parent fcf78a4 commit 5f5c695
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 178 deletions.
11 changes: 11 additions & 0 deletions lib/ash_json_api/test/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ defmodule AshJsonApi.Test do
end
end

defmacro assert_equal_links(conn, expected_links) do
quote bind_quoted: [expected_links: expected_links, conn: conn] do
%{"links" => resp_links} = conn.resp_body

sorted_links = Enum.sort_by(resp_links, fn {key, _value} -> key end, :desc)
sorted_expected_links = Enum.sort_by(expected_links, fn {key, _value} -> key end, :desc)

assert sorted_links == sorted_expected_links
end
end

defp set_content_type_request_header(conn, opts) do
cond do
opts[:exclude_req_content_type_header] ->
Expand Down
28 changes: 15 additions & 13 deletions test/acceptance/links_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule Test.Acceptance.Links do
]

json_api do
router(AshJsonApiTest.FetchingData.Pagination.Keyset.Router)
router(Test.Acceptance.Links.Router)
end

resources do
Expand All @@ -71,7 +71,7 @@ defmodule Test.Acceptance.Links do
end

defmodule TestPhoenixEndpoint do
def url do
def url() do
"https://test-endpoint.com"
end
end
Expand All @@ -91,21 +91,23 @@ defmodule Test.Acceptance.Links do

describe "link generation" do
test "generates links when Phoenix Endpoint is present" do
page_size = 5
conn = get(Api, "/posts", phoenix_endpoint: TestPhoenixEndpoint, status: 200)

assert %{"links" => links} = conn.resp_body
{:ok, %Ash.Page.Keyset{} = keyset} =
Api.read(Post,
page: [limit: page_size]
)

sorted_links =
links
|> Map.to_list()
|> Enum.sort()
after_cursor = List.last(keyset.results).__metadata__.keyset

assert sorted_links == [
{"first", "#{TestPhoenixEndpoint.url()}?page[limit]=5"},
{"next", nil},
{"prev", nil},
{"self", "#{TestPhoenixEndpoint.url()}?page[limit]=5"}
]
assert_equal_links(conn, %{
"first" => "#{TestPhoenixEndpoint.url()}?page[limit]=#{page_size}",
"next" =>
"#{TestPhoenixEndpoint.url()}?page[after]=#{after_cursor}&page[limit]=#{page_size}",
"prev" => nil,
"self" => "#{TestPhoenixEndpoint.url()}?page[limit]=#{page_size}"
})
end
end
end
141 changes: 60 additions & 81 deletions test/spec_compliance/fetching_data/pagination/keyset_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,15 @@ defmodule AshJsonApiTest.FetchingData.Pagination.Keyset do
after_cursor = List.last(keyset.results).__metadata__.keyset
before_cursor = List.first(keyset.results).__metadata__.keyset

assert %{"links" => links} = conn.resp_body

assert links == %{
"first" =>
"http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: cursor_at_post_10})}&sort=-inserted_at",
"next" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"prev" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: before_cursor})}&sort=-inserted_at"
}
assert_equal_links(conn, %{
"first" => "http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: cursor_at_post_10})}&sort=-inserted_at",
"next" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"prev" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: before_cursor})}&sort=-inserted_at"
})
end
end

Expand Down Expand Up @@ -242,17 +239,13 @@ defmodule AshJsonApiTest.FetchingData.Pagination.Keyset do

after_cursor = List.last(keyset.results).__metadata__.keyset

assert %{"links" => links} = conn.resp_body

assert links == %{
"first" =>
"http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"next" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"prev" => nil
}
assert_equal_links(conn, %{
"first" => "http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"self" => "http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"next" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"prev" => nil
})
end

test "[Before] when there are no more results, prev is nil" do
Expand Down Expand Up @@ -288,17 +281,14 @@ defmodule AshJsonApiTest.FetchingData.Pagination.Keyset do

after_cursor = List.last(keyset.results).__metadata__.keyset

assert %{"links" => links} = conn.resp_body

assert links == %{
"first" =>
"http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: cursor_at_post_5})}&sort=-inserted_at",
"next" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"prev" => nil
}
assert_equal_links(conn, %{
"first" => "http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: cursor_at_post_5})}&sort=-inserted_at",
"next" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"prev" => nil
})
end

test "[Before] when there are results, prev and next are set" do
Expand Down Expand Up @@ -342,18 +332,15 @@ defmodule AshJsonApiTest.FetchingData.Pagination.Keyset do
after_cursor = List.last(keyset.results).__metadata__.keyset
before_cursor = List.first(keyset.results).__metadata__.keyset

assert %{"links" => links} = conn.resp_body

assert links == %{
"first" =>
"http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: cursor_at_post_10})}&sort=-inserted_at",
"next" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"prev" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: before_cursor})}&sort=-inserted_at"
}
assert_equal_links(conn, %{
"first" => "http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: cursor_at_post_10})}&sort=-inserted_at",
"next" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"prev" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: before_cursor})}&sort=-inserted_at"
})
end

# FIX: Not sure why, but getting invalid keyset here sometimes...
Expand Down Expand Up @@ -396,18 +383,15 @@ defmodule AshJsonApiTest.FetchingData.Pagination.Keyset do
after_cursor = List.last(keyset_at_10.results).__metadata__.keyset
before_cursor = List.first(keyset_at_10.results).__metadata__.keyset

assert %{"links" => links} = conn.resp_body

assert links == %{
"first" =>
"http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor_at_5})}&page[limit]=#{page_size}&sort=-inserted_at",
"next" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"prev" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: before_cursor})}&sort=-inserted_at"
}
assert_equal_links(conn, %{
"first" => "http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor_at_5})}&page[limit]=#{page_size}&sort=-inserted_at",
"next" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"prev" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: before_cursor})}&sort=-inserted_at"
})
end

test "[After] when there are no more results, next is nil" do
Expand Down Expand Up @@ -449,17 +433,14 @@ defmodule AshJsonApiTest.FetchingData.Pagination.Keyset do

before_cursor = List.first(keyset_at_15.results).__metadata__.keyset

assert %{"links" => links} = conn.resp_body

assert links == %{
"first" =>
"http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor_at_10})}&page[limit]=#{page_size}&sort=-inserted_at",
"next" => nil,
"prev" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: before_cursor})}&sort=-inserted_at"
}
assert_equal_links(conn, %{
"first" => "http://www.example.com/posts?page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?#{encode_page_query(%{after: after_cursor_at_10})}&page[limit]=#{page_size}&sort=-inserted_at",
"next" => nil,
"prev" =>
"http://www.example.com/posts?page[limit]=#{page_size}&#{encode_page_query(%{before: before_cursor})}&sort=-inserted_at"
})
end

test "when count is true page[count] is present in links" do
Expand Down Expand Up @@ -498,18 +479,16 @@ defmodule AshJsonApiTest.FetchingData.Pagination.Keyset do
before_cursor = List.first(keyset_at_10.results).__metadata__.keyset
after_cursor = List.last(keyset_at_10.results).__metadata__.keyset

assert %{"links" => links} = conn.resp_body

assert links == %{
"first" =>
"http://www.example.com/posts?page[count]=true&page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?page[count]=true&#{encode_page_query(%{after: initial_after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"next" =>
"http://www.example.com/posts?page[count]=true&#{encode_page_query(%{after: after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"prev" =>
"http://www.example.com/posts?page[count]=true&page[limit]=#{page_size}&#{encode_page_query(%{before: before_cursor})}&sort=-inserted_at"
}
assert_equal_links(conn, %{
"first" =>
"http://www.example.com/posts?page[count]=true&page[limit]=#{page_size}&sort=-inserted_at",
"self" =>
"http://www.example.com/posts?page[count]=true&#{encode_page_query(%{after: initial_after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"next" =>
"http://www.example.com/posts?page[count]=true&#{encode_page_query(%{after: after_cursor})}&page[limit]=#{page_size}&sort=-inserted_at",
"prev" =>
"http://www.example.com/posts?page[count]=true&page[limit]=#{page_size}&#{encode_page_query(%{before: before_cursor})}&sort=-inserted_at"
})
end
end

Expand Down
Loading

0 comments on commit 5f5c695

Please sign in to comment.