diff --git a/lib/jsonapi/paginator.ex b/lib/jsonapi/paginator.ex deleted file mode 100644 index 83bba4d9..00000000 --- a/lib/jsonapi/paginator.ex +++ /dev/null @@ -1,22 +0,0 @@ -defmodule JSONAPI.Paginator do - @moduledoc """ - Pagination strategy behaviour - """ - - alias Plug.Conn - - @type options :: Keyword.t() - - @type page :: map() - - @type params :: %{String.t() => String.t()} - - @type links :: %{ - first: String.t() | nil, - last: String.t() | nil, - next: String.t() | nil, - prev: String.t() | nil - } - - @callback paginate(data :: term, view :: atom, conn :: Conn.t(), page, options) :: links -end diff --git a/lib/jsonapi/serializer.ex b/lib/jsonapi/serializer.ex index 0ea777e8..f1aae768 100644 --- a/lib/jsonapi/serializer.ex +++ b/lib/jsonapi/serializer.ex @@ -43,7 +43,7 @@ defmodule JSONAPI.Serializer do encoded_data end - merge_links(encoded_data, data, view, conn, query_page, remove_links?(), options) + custom_merge_links(encoded_data, view.links(data, conn, query_page)) end def encode_data(_view, nil, _conn, _query_includes, _options), do: {[], nil} @@ -65,12 +65,10 @@ defmodule JSONAPI.Serializer do relationships: %{} } - doc = merge_links(encoded_data, data, view, conn, nil, remove_links?(), options) - doc = case view.meta(data, conn) do - nil -> doc - meta -> Map.put(doc, :meta, meta) + nil -> encoded_data + meta -> Map.put(encoded_data, :meta, meta) end encode_relationships(conn, doc, {view, data, query_includes, valid_includes}, options) @@ -89,7 +87,7 @@ defmodule JSONAPI.Serializer do tuple() def build_relationships( conn, - {parent_view, parent_data, query_includes, valid_includes}, + {_parent_view, _parent_data, query_includes, valid_includes}, relationship_name, rel_data, rel_view, @@ -98,8 +96,8 @@ defmodule JSONAPI.Serializer do ) do # Build the relationship url rel_key = transform_fields(relationship_name) - rel_url = parent_view.url_for_rel(parent_data, rel_key, conn) - + # rel_url = parent_view.url_for_rel(parent_data, rel_key, conn) + rel_url = nil # Build the relationship acc = put_in( @@ -199,48 +197,18 @@ defmodule JSONAPI.Serializer do end @spec encode_relation(tuple()) :: map() - def encode_relation({rel_view, rel_data, _rel_url, _conn} = info) do - data = %{ + def encode_relation({rel_view, rel_data, _rel_url, _conn}) do + %{ data: encode_rel_data(rel_view, rel_data) } - - merge_related_links(data, info, remove_links?()) end - defp merge_base_links(%{links: links} = doc, data, view, conn) do - view_links = Map.merge(view.links(data, conn), links) - Map.merge(doc, %{links: view_links}) - end - - defp merge_links(doc, data, view, conn, page, false, options) when is_list(data) do - links = - Map.merge(view.pagination_links(data, conn, page, options), %{ - self: view.url_for_pagination(data, conn, page) - }) + def custom_merge_links(encoded_data, nil), do: encoded_data - doc - |> Map.merge(%{links: links}) - |> merge_base_links(data, view, conn) + def custom_merge_links(encoded_data, links) do + Map.merge(encoded_data, %{links: links}) end - defp merge_links(doc, data, view, conn, _page, false, _options) do - doc - |> Map.merge(%{links: %{self: view.url_for(data, conn)}}) - |> merge_base_links(data, view, conn) - end - - defp merge_links(doc, _data, _view, _conn, _page, _remove_links, _options), do: doc - - defp merge_related_links( - encoded_data, - {rel_view, rel_data, rel_url, conn}, - false = _remove_links - ) do - Map.merge(encoded_data, %{links: %{self: rel_url, related: rel_view.url_for(rel_data, conn)}}) - end - - defp merge_related_links(encoded_rel_data, _info, _remove_links), do: encoded_rel_data - @spec encode_rel_data(module(), map() | list()) :: map() | nil def encode_rel_data(_view, nil), do: nil @@ -296,8 +264,6 @@ defmodule JSONAPI.Serializer do |> List.flatten() end - defp remove_links?, do: Application.get_env(:jsonapi, :remove_links, false) - defp transform_fields(fields) do case Utils.String.field_transformation() do :camelize -> Utils.String.expand_fields(fields, &Utils.String.camelize/1) diff --git a/lib/jsonapi/view.ex b/lib/jsonapi/view.ex index 5198ca2a..8d46e7d9 100644 --- a/lib/jsonapi/view.ex +++ b/lib/jsonapi/view.ex @@ -33,7 +33,7 @@ defmodule JSONAPI.View do end defmodule DogView do - use JSONAPI.View, namespace: "/pupperz-api" + use JSONAPI.View end You can now call `UserView.show(user, conn, conn.params)` and it will render @@ -112,22 +112,8 @@ defmodule JSONAPI.View do If you always want to include a relationship. First make sure its always preloaded and then use the `[user: {UserView, :include}]` syntax in your `includes` function. This tells the serializer to *always* include if its loaded. - - ## Options - * `:host` (binary) - Allows the `host` to be overridden for generated URLs. Defaults to `host` of the supplied `conn`. - - * `:scheme` (atom) - Enables configuration of the HTTP scheme for generated URLS. Defaults to `scheme` from the provided `conn`. - - * `:namespace` (binary) - Allows the namespace of a given resource. This may be - configured globally or overridden on the View itself. Note that if you have - a globally defined namespace and need to *remove* the namespace for a - resource, set the namespace to a blank String. - - The default behaviour for `host` and `scheme` is to derive it from the `conn` provided, while the - default style for presentation in names is to be underscored and not dashed. """ - alias JSONAPI.{Paginator, Utils} alias Plug.Conn @type t :: module() @@ -144,28 +130,18 @@ defmodule JSONAPI.View do @callback fields() :: [field()] @callback get_field(field(), data(), Conn.t()) :: any() @callback hidden(data()) :: [field()] - @callback links(data(), Conn.t()) :: links() + @callback links(data(), Conn.t(), map() | nil) :: links() | nil @callback meta(data(), Conn.t()) :: meta() | nil - @callback namespace() :: String.t() - @callback pagination_links(data(), Conn.t(), Paginator.page(), Paginator.options()) :: - Paginator.links() - @callback path() :: String.t() | nil @callback relationships() :: [ {atom(), t() | {t(), :include} | {atom(), t()} | {atom(), t(), :include}} ] @callback type() :: resource_type() - @callback url_for(data(), Conn.t() | nil) :: String.t() - @callback url_for_pagination(data(), Conn.t(), Paginator.params()) :: String.t() - @callback url_for_rel(term(), String.t(), Conn.t() | nil) :: String.t() @callback visible_fields(data(), Conn.t() | nil) :: list(atom) @optional_callbacks [get_field: 3] defmacro __using__(opts \\ []) do - {type, opts} = Keyword.pop(opts, :type) - {namespace, opts} = Keyword.pop(opts, :namespace) - {path, opts} = Keyword.pop(opts, :path) - {paginator, _opts} = Keyword.pop(opts, :paginator) + {type, _opts} = Keyword.pop(opts, :type) quote do alias JSONAPI.{Serializer, View} @@ -173,9 +149,6 @@ defmodule JSONAPI.View do @behaviour View @resource_type unquote(type) - @namespace unquote(namespace) - @path unquote(path) - @paginator unquote(paginator) @impl View def id(nil), do: nil @@ -210,32 +183,11 @@ defmodule JSONAPI.View do def hidden(_data), do: [] @impl View - def links(_data, _conn), do: %{} + def links(_data, _conn, _query_page), do: nil @impl View def meta(_data, _conn), do: nil - @impl View - if @namespace do - def namespace, do: @namespace - else - def namespace, do: Application.get_env(:jsonapi, :namespace, "") - end - - @impl View - def pagination_links(data, conn, page, options) do - paginator = Application.get_env(:jsonapi, :paginator, @paginator) - - if Code.ensure_loaded?(paginator) && function_exported?(paginator, :paginate, 5) do - paginator.paginate(data, __MODULE__, conn, page, options) - else - %{} - end - end - - @impl View - def path, do: @path - @impl View def relationships, do: [] @@ -246,18 +198,6 @@ defmodule JSONAPI.View do def type, do: raise("Need to implement type/0") end - @impl View - def url_for(data, conn), - do: View.url_for(__MODULE__, data, conn) - - @impl View - def url_for_pagination(data, conn, pagination_params), - do: View.url_for_pagination(__MODULE__, data, conn, pagination_params) - - @impl View - def url_for_rel(data, rel_type, conn), - do: View.url_for_rel(__MODULE__, data, rel_type, conn) - @impl View def visible_fields(data, conn), do: View.visible_fields(__MODULE__, data, conn) @@ -296,60 +236,6 @@ defmodule JSONAPI.View do end end - @spec url_for(t(), term(), Conn.t() | nil) :: String.t() - def url_for(view, data, nil = _conn) when is_nil(data) or is_list(data), - do: URI.to_string(%URI{path: Enum.join([view.namespace(), path_for(view)], "/")}) - - def url_for(view, data, nil = _conn) do - URI.to_string(%URI{ - path: Enum.join([view.namespace(), path_for(view), view.id(data)], "/") - }) - end - - def url_for(view, data, %Plug.Conn{} = conn) when is_nil(data) or is_list(data) do - URI.to_string(%URI{ - scheme: scheme(conn), - host: host(conn), - port: port(conn), - path: Enum.join([view.namespace(), path_for(view)], "/") - }) - end - - def url_for(view, data, %Plug.Conn{} = conn) do - URI.to_string(%URI{ - scheme: scheme(conn), - host: host(conn), - port: port(conn), - path: Enum.join([view.namespace(), path_for(view), view.id(data)], "/") - }) - end - - @spec url_for_rel(t(), data(), resource_type(), Conn.t() | nil) :: String.t() - def url_for_rel(view, data, rel_type, conn) do - "#{url_for(view, data, conn)}/relationships/#{rel_type}" - end - - @spec url_for_rel(t(), data(), Conn.query_params(), Paginator.params()) :: String.t() - def url_for_pagination( - view, - data, - %{query_params: query_params} = conn, - nil = _pagination_params - ) do - query = - query_params - |> Utils.List.to_list_of_query_string_components() - |> URI.encode_query() - - prepare_url(view, query, data, conn) - end - - def url_for_pagination(view, data, %{query_params: query_params} = conn, pagination_params) do - query_params = Map.put(query_params, "page", pagination_params) - - url_for_pagination(view, data, %{conn | query_params: query_params}, nil) - end - @spec visible_fields(t(), data(), Conn.t() | nil) :: list(atom) def visible_fields(view, data, conn) do all_fields = @@ -372,33 +258,9 @@ defmodule JSONAPI.View do |> MapSet.to_list() end - defp prepare_url(view, "", data, conn), do: url_for(view, data, conn) - - defp prepare_url(view, query, data, conn) do - view - |> url_for(data, conn) - |> URI.parse() - |> struct(query: query) - |> URI.to_string() - end - defp requested_fields_for_type(view, %Conn{assigns: %{jsonapi_query: %{fields: fields}}}) do fields[view.type()] end defp requested_fields_for_type(_view, _conn), do: nil - - defp host(%Conn{host: host}), - do: Application.get_env(:jsonapi, :host, host) - - defp port(%Conn{port: 0} = conn), - do: port(%{conn | port: URI.default_port(scheme(conn))}) - - defp port(%Conn{port: port}), - do: Application.get_env(:jsonapi, :port, port) - - defp scheme(%Conn{scheme: scheme}), - do: Application.get_env(:jsonapi, :scheme, to_string(scheme)) - - defp path_for(view), do: view.path() || view.type() end diff --git a/test/jsonapi/serializer_test.exs b/test/jsonapi/serializer_test.exs index 14cb113c..1db71ddb 100644 --- a/test/jsonapi/serializer_test.exs +++ b/test/jsonapi/serializer_test.exs @@ -1,7 +1,7 @@ defmodule JSONAPI.SerializerTest do use ExUnit.Case, async: false - alias JSONAPI.{Config, QueryParser, Serializer} + alias JSONAPI.{Config, Serializer} defmodule PostView do use JSONAPI.View @@ -18,60 +18,6 @@ defmodule JSONAPI.SerializerTest do end end - defmodule PageBasedPaginator do - @moduledoc """ - Page based pagination strategy - """ - - @behaviour JSONAPI.Paginator - - @impl true - def paginate(data, view, conn, page, options) do - number = - page - |> Map.get("page", "0") - |> String.to_integer() - - size = - page - |> Map.get("size", "0") - |> String.to_integer() - - total_pages = - options - |> Keyword.get(:total_pages, 0) - - %{ - first: view.url_for_pagination(data, conn, %{page | "page" => "1"}), - last: view.url_for_pagination(data, conn, %{page | "page" => total_pages}), - next: next_link(data, view, conn, number, size, total_pages), - prev: previous_link(data, view, conn, number, size), - self: view.url_for_pagination(data, conn, %{size: size, page: number}) - } - end - - defp next_link(data, view, conn, page, size, total_pages) - when page < total_pages, - do: view.url_for_pagination(data, conn, %{size: size, page: page + 1}) - - defp next_link(_data, _view, _conn, _page, _size, _total_pages), - do: nil - - defp previous_link(data, view, conn, page, size) - when page > 1, - do: view.url_for_pagination(data, conn, %{size: size, page: page - 1}) - - defp previous_link(_data, _view, _conn, _page, _size), - do: nil - end - - defmodule PaginatedPostView do - use JSONAPI.View, paginator: PageBasedPaginator - - def fields, do: [:text, :body, :full_description, :inserted_at] - def type, do: "mytype" - end - defmodule UserView do use JSONAPI.View @@ -169,9 +115,9 @@ defmodule JSONAPI.SerializerTest do def type, do: "expensive-resource" - def links(nil, _conn), do: %{} + def links(nil, _conn, _page_params), do: nil - def links(data, _conn) do + def links(data, _conn, _page_params) do %{ queue: "/expensive-resource/queue/#{data.id}", promotions: %{ @@ -224,12 +170,10 @@ defmodule JSONAPI.SerializerTest do } encoded = Serializer.serialize(PostView, data, nil) - assert encoded[:links][:self] == PostView.url_for(data, nil) encoded_data = encoded[:data] assert encoded_data[:id] == PostView.id(data) assert encoded_data[:type] == PostView.type() - assert encoded_data[:links][:self] == PostView.url_for(data, nil) assert %{meta_text: "meta_Hello"} = encoded_data[:meta] @@ -237,7 +181,6 @@ defmodule JSONAPI.SerializerTest do assert attributes[:text] == data[:text] assert attributes[:body] == data[:body] - assert encoded_data[:links][:self] == PostView.url_for(data, nil) assert map_size(encoded_data[:relationships]) == 2 assert Enum.count(encoded[:included]) == 4 @@ -271,7 +214,6 @@ defmodule JSONAPI.SerializerTest do assert attributes[:text] == data[:text] assert attributes[:body] == data[:body] - assert enc[:links][:self] == PostView.url_for(data, conn) assert map_size(enc[:relationships]) == 2 end) @@ -297,7 +239,6 @@ defmodule JSONAPI.SerializerTest do assert attributes[:text] == data[:text] assert attributes[:body] == data[:body] - assert encoded_data[:links][:self] == PostView.url_for(data, nil) assert map_size(encoded_data[:relationships]) == 2 assert encoded_data[:relationships][:best_comments][:data] == [] @@ -323,35 +264,11 @@ defmodule JSONAPI.SerializerTest do assert attributes[:text] == data[:text] assert attributes[:body] == data[:body] - assert encoded_data[:links][:self] == PostView.url_for(data, nil) assert map_size(encoded_data[:relationships]) == 1 assert Enum.count(encoded[:included]) == 1 end - test "serialize handles a relationship self link on a show request" do - data = %{ - id: 1, - text: "Hello", - body: "Hello world", - author: %{id: 2, username: "jason"}, - best_comments: [] - } - - encoded = Serializer.serialize(PostView, data, nil) - - encoded_data = encoded[:data] - - assert encoded_data[:relationships][:author][:links][:self] == - "/mytype/1/relationships/author" - end - - test "serialize handles a relationship self link on an index request" do - encoded = Serializer.serialize(PostView, [], Plug.Conn.fetch_query_params(%Plug.Conn{})) - - assert encoded[:links][:self] == "http://www.example.com/mytype" - end - test "serialize will rename relationships" do data = %{ id: 1, @@ -422,9 +339,6 @@ defmodule JSONAPI.SerializerTest do encoded = Serializer.serialize(PostView, data, conn) - assert encoded.data.relationships.author.links.self == - "http://www.example.com/mytype/1/relationships/author" - assert Enum.count(encoded.included) == 4 end @@ -449,9 +363,6 @@ defmodule JSONAPI.SerializerTest do encoded = Serializer.serialize(UserView, data, conn) - assert encoded.data.relationships.company.links.self == - "http://www.example.com/user/1/relationships/company" - assert Enum.count(encoded.included) == 1 end @@ -476,9 +387,6 @@ defmodule JSONAPI.SerializerTest do encoded = Serializer.serialize(UserView, data, conn) - assert encoded.data.relationships.company.links.self == - "http://www.example.com/user/1/relationships/company" - assert Enum.count(encoded.included) == 2 end @@ -514,9 +422,6 @@ defmodule JSONAPI.SerializerTest do encoded = Serializer.serialize(UserView, data, conn) - assert encoded.data.relationships.company.links.self == - "http://www.example.com/user/1/relationships/company" - assert Enum.count(encoded.included) == 4 end @@ -566,9 +471,6 @@ defmodule JSONAPI.SerializerTest do ] == "bronds" assert List.first(relationships["bestComments"][:data])[:id] == "5" - - assert relationships["bestComments"][:links][:self] == - "/mytype/1/relationships/bestComments" end end @@ -625,9 +527,6 @@ defmodule JSONAPI.SerializerTest do assert author4[:attributes]["last-name"] == "bronds" assert List.first(relationships["best-comments"][:data])[:id] == "5" - - assert relationships["best-comments"][:links][:self] == - "/mytype/1/relationships/best-comments" end end @@ -727,31 +626,6 @@ defmodule JSONAPI.SerializerTest do Application.delete_env(:jsonapi, :remove_links) end - test "serialize includes pagination links if page-based pagination is requested" do - data = [%{id: 1}] - view = PaginatedPostView - - conn = - :get - |> Plug.Test.conn("/mytype?page[page]=2&page[size]=1") - |> QueryParser.call(%Config{view: view, opts: []}) - |> Plug.Conn.fetch_query_params() - - encoded = - Serializer.serialize(PaginatedPostView, data, conn, nil, total_pages: 3, total_items: 3) - - page = conn.assigns.jsonapi_query.page - first = view.url_for_pagination(data, conn, %{page | "page" => 1}) - last = view.url_for_pagination(data, conn, %{page | "page" => 3}) - self = view.url_for_pagination(data, conn, page) - - assert encoded[:links][:first] == first - assert encoded[:links][:last] == last - assert encoded[:links][:next] == last - assert encoded[:links][:prev] == first - assert encoded[:links][:self] == self - end - test "serialize does not include pagination links if they are not defined" do data = [%{id: 1}] @@ -770,7 +644,6 @@ defmodule JSONAPI.SerializerTest do } = Serializer.serialize(ExpensiveResourceView, data, nil) expected_links = %{ - self: "/expensive-resource/#{data.id}", queue: "/expensive-resource/queue/#{data.id}", promotions: %{ href: "/promotions?rel=#{data.id}", @@ -785,32 +658,10 @@ defmodule JSONAPI.SerializerTest do test "serialize returns a null data if it receives a null data" do assert %{ - data: data, - links: links + data: data } = Serializer.serialize(ExpensiveResourceView, nil, nil) assert nil == data - assert %{self: "/expensive-resource"} == links - end - - test "serialize handles query parameters in self links" do - data = [%{id: 1}] - view = PaginatedPostView - - conn = - :get - |> Plug.Test.conn("/mytype?page[page]=2&page[size]=1") - |> QueryParser.call(%Config{view: view, opts: []}) - |> Plug.Conn.fetch_query_params() - - encoded = - Serializer.serialize(PaginatedPostView, data, conn, nil, total_pages: 3, total_items: 3) - - assert encoded[:links][:self] == - "http://www.example.com/mytype?page%5Bpage%5D=2&page%5Bsize%5D=1" - - assert List.first(encoded[:data])[:links][:self] == - "http://www.example.com/mytype/1" end test "extrapolates relationship config simplest case" do diff --git a/test/jsonapi/view_test.exs b/test/jsonapi/view_test.exs index 8831796d..9949fb2d 100644 --- a/test/jsonapi/view_test.exs +++ b/test/jsonapi/view_test.exs @@ -2,7 +2,7 @@ defmodule JSONAPI.ViewTest do use ExUnit.Case defmodule PostView do - use JSONAPI.View, type: "posts", namespace: "/api" + use JSONAPI.View, type: "posts" def fields do [:title, :body] @@ -16,7 +16,7 @@ defmodule JSONAPI.ViewTest do end defmodule CommentView do - use JSONAPI.View, type: "comments", namespace: "/api" + use JSONAPI.View, type: "comments" def fields do [:body] @@ -71,140 +71,6 @@ defmodule JSONAPI.ViewTest do assert PostView.type() == "posts" end - describe "namespace/0" do - setup do - Application.put_env(:jsonapi, :namespace, "/cake") - - on_exit(fn -> - Application.delete_env(:jsonapi, :namespace) - end) - - {:ok, []} - end - - test "uses macro configuration first" do - assert PostView.namespace() == "/api" - end - - test "uses global namespace if available" do - assert UserView.namespace() == "/cake" - end - - test "can be blank" do - assert CarView.namespace() == "" - end - end - - describe "url_for/2 when host and scheme not configured" do - test "url_for/2" do - assert PostView.url_for(nil, nil) == "/api/posts" - assert PostView.url_for([], nil) == "/api/posts" - assert PostView.url_for(%{id: 1}, nil) == "/api/posts/1" - assert PostView.url_for([], %Plug.Conn{}) == "http://www.example.com/api/posts" - assert PostView.url_for([], %Plug.Conn{port: 123}) == "http://www.example.com:123/api/posts" - assert PostView.url_for(%{id: 1}, %Plug.Conn{}) == "http://www.example.com/api/posts/1" - - assert PostView.url_for_rel([], "comments", %Plug.Conn{}) == - "http://www.example.com/api/posts/relationships/comments" - - assert PostView.url_for_rel(%{id: 1}, "comments", %Plug.Conn{}) == - "http://www.example.com/api/posts/1/relationships/comments" - end - end - - describe "url_for/2 when host configured" do - setup do - Application.put_env(:jsonapi, :host, "www.otherhost.com") - - on_exit(fn -> - Application.delete_env(:jsonapi, :host) - end) - - {:ok, []} - end - - test "uses configured host instead of that on Conn" do - assert PostView.url_for_rel([], "comments", %Plug.Conn{}) == - "http://www.otherhost.com/api/posts/relationships/comments" - - assert PostView.url_for_rel(%{id: 1}, "comments", %Plug.Conn{}) == - "http://www.otherhost.com/api/posts/1/relationships/comments" - - assert PostView.url_for([], %Plug.Conn{}) == "http://www.otherhost.com/api/posts" - assert PostView.url_for(%{id: 1}, %Plug.Conn{}) == "http://www.otherhost.com/api/posts/1" - end - end - - describe "url_for/2 when scheme configured" do - setup do - Application.put_env(:jsonapi, :scheme, "ftp") - - on_exit(fn -> - Application.delete_env(:jsonapi, :scheme) - end) - - {:ok, []} - end - - test "uses configured scheme instead of that on Conn" do - assert PostView.url_for([], %Plug.Conn{}) == "ftp://www.example.com/api/posts" - assert PostView.url_for(%{id: 1}, %Plug.Conn{}) == "ftp://www.example.com/api/posts/1" - - assert PostView.url_for_rel([], "comments", %Plug.Conn{}) == - "ftp://www.example.com/api/posts/relationships/comments" - - assert PostView.url_for_rel(%{id: 1}, "comments", %Plug.Conn{}) == - "ftp://www.example.com/api/posts/1/relationships/comments" - end - end - - describe "url_for/2 when port configured" do - setup do - Application.put_env(:jsonapi, :port, 42) - - on_exit(fn -> - Application.delete_env(:jsonapi, :port) - end) - - {:ok, []} - end - - test "uses configured port instead of that on Conn" do - assert PostView.url_for([], %Plug.Conn{}) == "http://www.example.com:42/api/posts" - assert PostView.url_for(%{id: 1}, %Plug.Conn{}) == "http://www.example.com:42/api/posts/1" - - assert PostView.url_for_rel([], "comments", %Plug.Conn{}) == - "http://www.example.com:42/api/posts/relationships/comments" - - assert PostView.url_for_rel(%{id: 1}, "comments", %Plug.Conn{}) == - "http://www.example.com:42/api/posts/1/relationships/comments" - end - end - - describe "url_for_pagination/3" do - setup do - {:ok, conn: Plug.Conn.fetch_query_params(%Plug.Conn{})} - end - - test "with pagination information", %{conn: conn} do - assert PostView.url_for_pagination(nil, conn, %{}) == "http://www.example.com/api/posts" - - assert PostView.url_for_pagination(nil, conn, %{number: 1, size: 10}) == - "http://www.example.com/api/posts?page%5Bnumber%5D=1&page%5Bsize%5D=10" - end - - test "with query parameters", %{conn: conn} do - conn_with_query_params = - Kernel.update_in(conn.query_params, &Map.put(&1, "comments", [5, 2])) - - assert PostView.url_for_pagination(nil, conn_with_query_params, %{number: 1, size: 10}) == - "http://www.example.com/api/posts?comments%5B%5D=5&comments%5B%5D=2&page%5Bnumber%5D=1&page%5Bsize%5D=10" - - assert PostView.url_for_pagination(nil, conn_with_query_params, %{}) == - "http://www.example.com/api/posts?comments%5B%5D=5&comments%5B%5D=2" - end - end - test "render/2 is defined when 'Phoenix' is loaded" do assert {:render, 2} in CommentView.__info__(:functions) end diff --git a/test/jsonapi_test.exs b/test/jsonapi_test.exs index 83b9d104..0e05eb39 100644 --- a/test/jsonapi_test.exs +++ b/test/jsonapi_test.exs @@ -140,8 +140,6 @@ defmodule JSONAPITest do [author | _] = included assert Map.get(author, "type") == "user" assert Map.get(author, "id") == "2" - - assert Map.has_key?(json, "links") end test "handles includes properly" do @@ -187,8 +185,6 @@ defmodule JSONAPITest do assert Enum.find(included, fn include -> Map.get(include, "type") == "user" && Map.get(include, "id") == "3" end) - - assert Map.has_key?(json, "links") end test "handles empty includes properly" do @@ -316,8 +312,6 @@ defmodule JSONAPITest do assert Enum.find(included, fn include -> Map.get(include, "type") == "tag" && Map.get(include, "id") == "4" end) - - assert Map.has_key?(json, "links") end describe "with an underscored API" do