Current section
Files
Jump to
Current section
Files
priv/templates/use_case.gen.phx_resource_json/controller_test.eex
<%= if Keyword.get(@option_context, :scoped, false) do %>
defmodule <%= @context[:web_module] %>.<%= @option_context[:scoped] %>.<%= @context[:module] |> String.replace("#{@context[:base]}.", "") %>ControllerTest do
<% else %>
defmodule <%= @context[:web_module] %>.<%= @context[:module] |> String.replace("#{@context[:base]}.", "") %>ControllerTest do
<% end %>
use <%= @context[:web_module] %>.ConnCase
<%= if Keyword.get(@option_context, :scoped, false) do %>
alias <%= @context[:base] %>.<%= @option_context[:scoped] %>.Repo.<%= @context[:scoped] %>
<% else %>
alias <%= @context[:base] %>.Repo.<%= @context[:scoped] %>
<% end %>
alias <%= inspect @schema.module %>, as: <%= @context[:alias] %>Schema
@create_attrs %{
<%= @schema.params.create |> Enum.map(fn {key, val} -> " #{key}: #{inspect(val)}" end) |> Enum.join(",\n") %>
}
@update_attrs %{
<%= @schema.params.update |> Enum.map(fn {key, val} -> " #{key}: #{inspect(val)}" end) |> Enum.join(",\n") %>
}
@invalid_attrs <%= inspect for {key, _} <- @schema.params.create, into: %{}, do: {key, nil} %>
def fixture(:<%= @schema.singular %>) do
{:ok, <%= @schema.singular %>} = <%= @context[:alias] %>.create(@create_attrs)
<%= @schema.singular %>
end
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "index" do
test "lists all <%= @schema.plural %>", %{conn: conn} do
conn = get(conn, Routes.<%= @schema.route_helper %>_path(conn, :index))
assert json_response(conn, 200)["data"] == []
end
end
describe "create <%= @schema.singular %>" do
test "renders <%= @schema.singular %> when data is valid", %{conn: conn} do
conn = post(conn, Routes.<%= @schema.route_helper %>_path(conn, :create), <%= @schema.singular %>: @create_attrs)
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get(conn, Routes.<%= @schema.route_helper %>_path(conn, :show, id))
assert %{
"id" => id<%= for {key, val} <- @schema.params.create do %>,
"<%= key %>" => <%= val |> Phoenix.json_library().encode!() |> Phoenix.json_library().decode!() |> inspect() %><% end %>
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.<%= @schema.route_helper %>_path(conn, :create), <%= @schema.singular %>: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update <%= @schema.singular %>" do
setup [:create_<%= @schema.singular %>]
test "renders <%= @schema.singular %> when data is valid", %{conn: conn, <%= @schema.singular %>: %<%= @context[:alias] %>Schema{id: id} = <%= @schema.singular %>} do
conn = put(conn, Routes.<%= @schema.route_helper %>_path(conn, :update, <%= @schema.singular %>), <%= @schema.singular %>: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get(conn, Routes.<%= @schema.route_helper %>_path(conn, :show, id))
assert %{
"id" => id<%= for {key, val} <- @schema.params.update do %>,
"<%= key %>" => <%= Phoenix.json_library().encode!(val) %><% end %>
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn, <%= @schema.singular %>: <%= @schema.singular %>} do
conn = put(conn, Routes.<%= @schema.route_helper %>_path(conn, :update, <%= @schema.singular %>), <%= @schema.singular %>: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "delete <%= @schema.singular %>" do
setup [:create_<%= @schema.singular %>]
test "deletes chosen <%= @schema.singular %>", %{conn: conn, <%= @schema.singular %>: <%= @schema.singular %>} do
conn = delete(conn, Routes.<%= @schema.route_helper %>_path(conn, :delete, <%= @schema.singular %>))
assert response(conn, 204)
assert_error_sent 404, fn ->
get(conn, Routes.<%= @schema.route_helper %>_path(conn, :show, <%= @schema.singular %>))
end
end
end
defp create_<%= @schema.singular %>(_) do
<%= @schema.singular %> = fixture(:<%= @schema.singular %>)
{:ok, <%= @schema.singular %>: <%= @schema.singular %>}
end
end