From 1b36b70e18900ed468cd71f749f45f98cc87bb4e Mon Sep 17 00:00:00 2001 From: Daniel Berkompas Date: Wed, 25 Apr 2018 14:48:59 -0700 Subject: [PATCH] Add integration test on mix torch.gen.html --- lib/mix/tasks/torch.gen.html.ex | 62 ++----- lib/mix/tasks/torch.install.ex | 4 +- lib/mix/torch.ex | 59 ++++--- .../phx.gen.context/access_no_schema.ex | 160 +++++++++++++++++ priv/templates/phx.gen.context/context.ex | 8 + .../phx.gen.context/context_test.exs | 5 + .../phx.gen.context/schema_access.ex | 159 +++++++++++++++++ priv/templates/phx.gen.context/test_cases.exs | 77 ++++++++ test/mix/tasks/torch.gen.html_test.exs | 41 +++++ .../example/lib/example/accounts/accounts.ex | 165 ----------------- .../apps/example/lib/example/accounts/user.ex | 22 --- .../apps/example/lib/example/blog/blog.ex | 167 ------------------ .../apps/example/lib/example/blog/post.ex | 24 --- .../controllers/post_controller.ex | 71 -------- .../controllers/user_controller.ex | 68 ------- .../apps/example/lib/example_web/endpoint.ex | 9 +- .../apps/example/lib/example_web/router.ex | 2 - .../example_web/templates/post/edit.html.eex | 14 -- .../example_web/templates/post/form.html.eex | 50 ------ .../example_web/templates/post/index.html.eex | 107 ----------- .../example_web/templates/post/new.html.eex | 14 -- .../example_web/templates/post/show.html.eex | 47 ----- .../example_web/templates/user/edit.html.eex | 14 -- .../example_web/templates/user/form.html.eex | 38 ---- .../example_web/templates/user/index.html.eex | 87 --------- .../example_web/templates/user/new.html.eex | 14 -- .../example_web/templates/user/show.html.eex | 37 ---- .../lib/example_web/views/post_view.ex | 6 - .../lib/example_web/views/user_view.ex | 6 - .../20180217004018_create_posts.exs | 17 -- .../20180308215932_create_users.exs | 15 -- .../test/example/accounts/accounts_test.exs | 88 --------- .../example/test/example/blog/blog_test.exs | 92 ---------- .../controllers/post_controller_test.exs | 88 --------- .../controllers/user_controller_test.exs | 88 --------- test/support/cases/mix_case.ex | 30 +++- test/support/routers/modified.ex | 28 +++ test/support/routers/original.ex | 27 +++ 38 files changed, 578 insertions(+), 1432 deletions(-) create mode 100644 priv/templates/phx.gen.context/access_no_schema.ex create mode 100644 priv/templates/phx.gen.context/context.ex create mode 100644 priv/templates/phx.gen.context/context_test.exs create mode 100644 priv/templates/phx.gen.context/schema_access.ex create mode 100644 priv/templates/phx.gen.context/test_cases.exs create mode 100644 test/mix/tasks/torch.gen.html_test.exs delete mode 100644 test/support/apps/example/lib/example/accounts/accounts.ex delete mode 100644 test/support/apps/example/lib/example/accounts/user.ex delete mode 100644 test/support/apps/example/lib/example/blog/blog.ex delete mode 100644 test/support/apps/example/lib/example/blog/post.ex delete mode 100644 test/support/apps/example/lib/example_web/controllers/post_controller.ex delete mode 100644 test/support/apps/example/lib/example_web/controllers/user_controller.ex delete mode 100644 test/support/apps/example/lib/example_web/templates/post/edit.html.eex delete mode 100644 test/support/apps/example/lib/example_web/templates/post/form.html.eex delete mode 100644 test/support/apps/example/lib/example_web/templates/post/index.html.eex delete mode 100644 test/support/apps/example/lib/example_web/templates/post/new.html.eex delete mode 100644 test/support/apps/example/lib/example_web/templates/post/show.html.eex delete mode 100644 test/support/apps/example/lib/example_web/templates/user/edit.html.eex delete mode 100644 test/support/apps/example/lib/example_web/templates/user/form.html.eex delete mode 100644 test/support/apps/example/lib/example_web/templates/user/index.html.eex delete mode 100644 test/support/apps/example/lib/example_web/templates/user/new.html.eex delete mode 100644 test/support/apps/example/lib/example_web/templates/user/show.html.eex delete mode 100644 test/support/apps/example/lib/example_web/views/post_view.ex delete mode 100644 test/support/apps/example/lib/example_web/views/user_view.ex delete mode 100644 test/support/apps/example/priv/repo/migrations/20180217004018_create_posts.exs delete mode 100644 test/support/apps/example/priv/repo/migrations/20180308215932_create_users.exs delete mode 100644 test/support/apps/example/test/example/accounts/accounts_test.exs delete mode 100644 test/support/apps/example/test/example/blog/blog_test.exs delete mode 100644 test/support/apps/example/test/example_web/controllers/post_controller_test.exs delete mode 100644 test/support/apps/example/test/example_web/controllers/user_controller_test.exs create mode 100644 test/support/routers/modified.ex create mode 100644 test/support/routers/original.ex diff --git a/lib/mix/tasks/torch.gen.html.ex b/lib/mix/tasks/torch.gen.html.ex index fdfd5d4d..7949bf8c 100644 --- a/lib/mix/tasks/torch.gen.html.ex +++ b/lib/mix/tasks/torch.gen.html.ex @@ -1,60 +1,30 @@ defmodule Mix.Tasks.Torch.Gen.Html do @moduledoc """ - Light wrapper module around phx.gen.context. Installs and uninstalls context - templates around running the phoenix generator. + Light wrapper module around `mix phx.gen.html` which generates slightly + modified HTML. - ## Parameters - - Takes all the same params as the regualar phx generators. - - # TODO + Takes all the same params as the `phx.gen.html` task. ## Example - mix torch.gen.context Accounts User users --no-schema + mix torch.gen.html Accounts User users --no-schema """ - import Torch.Config, only: [template_format: 0] + @commands ~w[phx.gen.html phx.gen.context] - def run([context | args]) do - format = template_format() + def run(args) do + %{format: format} = Mix.Torch.parse_config!("torch.gen.html", args) - # Copy over phoenix generator html templates - Mix.Torch.copy_from([:torch], "priv/templates/#{format}/phx.gen.html", "", [], [ - {:exs, "controller_test.exs", "priv/templates/phx.gen.html/controller_test.exs"}, - {:ex, "controller.ex", "priv/templates/phx.gen.html/controller.ex"}, - {:eex, "edit.html.eex", "priv/templates/phx.gen.html/edit.html.eex"}, - {:eex, "form.html.eex", "priv/templates/phx.gen.html/form.html.eex"}, - {:eex, "index.html.eex", "priv/templates/phx.gen.html/index.html.eex"}, - {:eex, "new.html.eex", "priv/templates/phx.gen.html/new.html.eex"}, - {:eex, "show.html.eex", "priv/templates/phx.gen.html/show.html.eex"}, - {:ex, "view.ex", "priv/templates/phx.gen.html/view.ex"} - ]) + # Inject the torch templates for the generator into the priv/ + # directory so they will be picked up by the Phoenix generator + Enum.each(@commands, &Mix.Torch.inject_templates(&1, format)) - # Copy over phoenix generator context templates - Mix.Torch.copy_from([:torch], "priv/templates/phx.gen.context", "", [], [ - {:ex, "access_no_schema.ex", "priv/templates/phx.gen.context/access_no_schema.ex"}, - {:ex, "context.ex", "priv/templates/phx.gen.context/context.ex"}, - {:ex, "schema_access.ex", "priv/templates/phx.gen.context/schema_access.ex"}, - {:exs, "test_cases.exs", "priv/templates/phx.gen.context/test_cases.exs"}, - {:exs, "context_test.exs", "priv/templates/phx.gen.context/context_test.exs"} - ]) + # Run the Phoenix generator + Mix.Task.run("phx.gen.html", args) - Mix.Task.run("phx.gen.html", [context | args]) - - File.rm("priv/templates/phx.gen.html/controller_test.exs") - File.rm("priv/templates/phx.gen.html/controller.ex") - File.rm("priv/templates/phx.gen.html/edit.html.#{format}") - File.rm("priv/templates/phx.gen.html/form.html.#{format}") - File.rm("priv/templates/phx.gen.html/index.html.#{format}") - File.rm("priv/templates/phx.gen.html/new.html.#{format}") - File.rm("priv/templates/phx.gen.html/show.html.#{format}") - File.rm("priv/templates/phx.gen.html/view.ex") - File.rm("priv/templates/phx.gen.context/access_no_schema.ex") - File.rm("priv/templates/phx.gen.context/context.ex") - File.rm("priv/templates/phx.gen.context/schema_access.ex") - File.rm("priv/templates/phx.gen.context/test_cases.exs") - File.rm("priv/templates/phx.gen.context/context_test.exs") + # Remove the injected templates from priv/ so they will not + # affect future Phoenix generator commands + Enum.each(@commands, &Mix.Torch.remove_templates/1) Mix.shell().info(""" Ensure the following is added to your endpoint.ex: @@ -76,7 +46,5 @@ defmodule Mix.Tasks.Torch.Gen.Html do """) - - Mix.shell().info("\u{1F525} #{IO.ANSI.yellow()}Torch generated html for #{context}!\u{1F525}") end end diff --git a/lib/mix/tasks/torch.install.ex b/lib/mix/tasks/torch.install.ex index 22613989..80999c53 100644 --- a/lib/mix/tasks/torch.install.ex +++ b/lib/mix/tasks/torch.install.ex @@ -23,8 +23,8 @@ defmodule Mix.Tasks.Torch.Install do def run(args) do %{format: format, otp_app: otp_app} = Mix.Torch.parse_config!("torch.install", args) - Mix.Torch.copy_from([:torch], "priv/templates/#{format}", "", [], [ - {:eex, "layout.html.#{format}", "lib/#{otp_app}_web/templates/layout/torch.html.#{format}"} + Mix.Torch.copy_from("priv/templates/#{format}", [ + {"layout.html.#{format}", "lib/#{otp_app}_web/templates/layout/torch.html.#{format}"} ]) end end diff --git a/lib/mix/torch.ex b/lib/mix/torch.ex index 7e7b80ad..20290799 100644 --- a/lib/mix/torch.ex +++ b/lib/mix/torch.ex @@ -3,7 +3,6 @@ defmodule Mix.Torch do alias Torch.Config - @doc false def parse_config!(task, args) do {opts, _, _} = OptionParser.parse(args, switches: [format: :string, app: :string]) @@ -41,35 +40,41 @@ defmodule Mix.Torch do %{otp_app: otp_app, format: format} end - @doc """ - Copies files from source dir to target dir - according to the given map. + def copy_from(source_dir, mapping) when is_list(mapping) do + for {source_file_path, target_file_path} <- mapping do + contents = + [Application.app_dir(:torch), source_dir, source_file_path] + |> Path.join() + |> File.read!() - Files are evaluated against EEx according to - the given binding. - """ - def copy_from(apps, source_dir, target_dir, _binding, mapping) when is_list(mapping) do - roots = Enum.map(apps, &to_app_source(&1, source_dir)) - - for {_format, source_file_path, target_file_path} <- mapping do - source = - Enum.find_value(roots, fn root -> - source = Path.join(root, source_file_path) - if File.exists?(source), do: source - end) || raise "could not find #{source_file_path} in any of the sources" - - target = Path.join(target_dir, target_file_path) + Mix.Generator.create_file(target_file_path, contents) + end + end - contents = File.read!(source) - # case format do - # :eex -> EEx.eval_file(source, binding) - # _ -> File.read!(source) - # end + def inject_templates("phx.gen.html", format) do + copy_from("priv/templates/#{format}/phx.gen.html", [ + {"controller_test.exs", "priv/templates/phx.gen.html/controller_test.exs"}, + {"controller.ex", "priv/templates/phx.gen.html/controller.ex"}, + {"edit.html.#{format}", "priv/templates/phx.gen.html/edit.html.#{format}"}, + {"form.html.#{format}", "priv/templates/phx.gen.html/form.html.#{format}"}, + {"index.html.#{format}", "priv/templates/phx.gen.html/index.html.#{format}"}, + {"new.html.#{format}", "priv/templates/phx.gen.html/new.html.#{format}"}, + {"show.html.#{format}", "priv/templates/phx.gen.html/show.html.#{format}"}, + {"view.ex", "priv/templates/phx.gen.html/view.ex"} + ]) + end - Mix.Generator.create_file(target, contents) - end + def inject_templates("phx.gen.context", _format) do + copy_from("priv/templates/phx.gen.context", [ + {"access_no_schema.ex", "priv/templates/phx.gen.context/access_no_schema.ex"}, + {"context.ex", "priv/templates/phx.gen.context/context.ex"}, + {"schema_access.ex", "priv/templates/phx.gen.context/schema_access.ex"}, + {"test_cases.exs", "priv/templates/phx.gen.context/test_cases.exs"}, + {"context_test.exs", "priv/templates/phx.gen.context/context_test.exs"} + ]) end - defp to_app_source(path, source_dir) when is_binary(path), do: Path.join(path, source_dir) - defp to_app_source(app, source_dir) when is_atom(app), do: Application.app_dir(app, source_dir) + def remove_templates(template_dir) do + File.rm("priv/templates/#{template_dir}/") + end end diff --git a/priv/templates/phx.gen.context/access_no_schema.ex b/priv/templates/phx.gen.context/access_no_schema.ex new file mode 100644 index 00000000..5076af0b --- /dev/null +++ b/priv/templates/phx.gen.context/access_no_schema.ex @@ -0,0 +1,160 @@ + + import Torch.Helpers, only: [sort: 1, paginate: 4] + import Filtrex.Type.Config + + alias <%= inspect schema.module %> + + @pagination [page_size: 15] + @pagination_distance 5 + + @doc """ + Paginate the list of <%= schema.plural %> using filtrex + filters. + + ## Examples + + iex> list_<%= schema.plural %>(%{}) + %{<%= schema.plural %>: [%<%= inspect schema.alias %>{}], ...} + """ + @spec paginate_<%= schema.plural %>(map) :: {:ok, map} | {:error, any} + def paginate_<%= schema.plural %>(params \\ %{}) do + params = + params + |> Map.put_new("sort_direction", "desc") + |> Map.put_new("sort_field", "inserted_at") + + {:ok, sort_direction} = Map.fetch(params, "sort_direction") + {:ok, sort_field} = Map.fetch(params, "sort_field") + + with {:ok, filter} <- Filtrex.parse_params(filter_config(<%= inspect String.to_atom(schema.plural) %>), params["<%= schema.singular %>"] || %{}), + %Scrivener.Page{} = page <- do_paginate_<%= schema.plural %>(filter, params) do + {:ok, + %{ + <%= schema.plural %>: page.entries, + page_number: page.page_number, + page_size: page.page_size, + total_pages: page.total_pages, + total_entries: page.total_entries, + distance: @pagination_distance, + sort_field: sort_field, + sort_direction: sort_direction + } + } + else + {:error, error} -> {:error, error} + error -> {:error, error} + end + end + + defp do_paginate_<%= schema.plural %>(filter, params) do + <%= inspect schema.alias %> + |> Filtrex.query(filter) + |> order_by(^sort(params)) + |> paginate(Repo, params, @pagination) + end + + @doc """ + Returns the list of <%= schema.plural %>. + + ## Examples + + iex> list_<%= schema.plural %>() + [%<%= inspect schema.alias %>{}, ...] + + """ + def list_<%= schema.plural %> do + Repo.all(<%= inspect schema.alias %>) + end + + @doc """ + Gets a single <%= schema.singular %>. + + Raises `Ecto.NoResultsError` if the <%= schema.human_singular %> does not exist. + + ## Examples + + iex> get_<%= schema.singular %>!(123) + %<%= inspect schema.alias %>{} + + iex> get_<%= schema.singular %>!(456) + ** (Ecto.NoResultsError) + + """ + def get_<%= schema.singular %>!(id), do: Repo.get!(<%= inspect schema.alias %>, id) + + @doc """ + Creates a <%= schema.singular %>. + + ## Examples + + iex> create_<%= schema.singular %>(%{field: value}) + {:ok, %<%= inspect schema.alias %>{}} + + iex> create_<%= schema.singular %>(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_<%= schema.singular %>(attrs \\ %{}) do + %<%= inspect schema.alias %>{} + |> <%= inspect schema.alias %>.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a <%= schema.singular %>. + + ## Examples + + iex> update_<%= schema.singular %>(<%= schema.singular %>, %{field: new_value}) + {:ok, %<%= inspect schema.alias %>{}} + + iex> update_<%= schema.singular %>(<%= schema.singular %>, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_<%= schema.singular %>(%<%= inspect schema.alias %>{} = <%= schema.singular %>, attrs) do + <%= schema.singular %> + |> <%= inspect schema.alias %>.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a <%= inspect schema.alias %>. + + ## Examples + + iex> delete_<%= schema.singular %>(<%= schema.singular %>) + {:ok, %<%= inspect schema.alias %>{}} + + iex> delete_<%= schema.singular %>(<%= schema.singular %>) + {:error, %Ecto.Changeset{}} + + """ + def delete_<%= schema.singular %>(%<%= inspect schema.alias %>{} = <%= schema.singular %>) do + Repo.delete(<%= schema.singular %>) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking <%= schema.singular %> changes. + + ## Examples + + iex> change_<%= schema.singular %>(<%= schema.singular %>) + %Ecto.Changeset{source: %<%= inspect schema.alias %>{}} + + """ + def change_<%= schema.singular %>(%<%= inspect schema.alias %>{} = <%= schema.singular %>) do + <%= inspect schema.alias %>.changeset(<%= schema.singular %>, %{}) + end + + defp filter_config(<%= inspect String.to_atom(schema.plural) %>) do + defconfig do + <%= for {name, type} <- schema.attrs do %><%= cond do %> + <% type in [:string, :text] -> %>text <%= inspect name %> + <% type in [:integer, :number] -> %>number <%= inspect name %> + <% type in [:naive_datetime, :utc_datetime, :datetime, :date] -> %>date <%= inspect name %> + <% type in [:boolean] -> %>boolean <%= inspect name %> + <% true -> %> #TODO add config for <%= name %> of type <%= type %> + <% end %><% end %> + end + end diff --git a/priv/templates/phx.gen.context/context.ex b/priv/templates/phx.gen.context/context.ex new file mode 100644 index 00000000..3b241d40 --- /dev/null +++ b/priv/templates/phx.gen.context/context.ex @@ -0,0 +1,8 @@ +defmodule <%= inspect context.module %> do + @moduledoc """ + The <%= context.name %> context. + """ + + import Ecto.Query, warn: false + alias <%= inspect schema.repo %> +end \ No newline at end of file diff --git a/priv/templates/phx.gen.context/context_test.exs b/priv/templates/phx.gen.context/context_test.exs new file mode 100644 index 00000000..0eb0a74c --- /dev/null +++ b/priv/templates/phx.gen.context/context_test.exs @@ -0,0 +1,5 @@ +defmodule <%= inspect context.module %>Test do + use <%= inspect context.base_module %>.DataCase + + alias <%= inspect context.module %> +end \ No newline at end of file diff --git a/priv/templates/phx.gen.context/schema_access.ex b/priv/templates/phx.gen.context/schema_access.ex new file mode 100644 index 00000000..9229ce9f --- /dev/null +++ b/priv/templates/phx.gen.context/schema_access.ex @@ -0,0 +1,159 @@ +import Torch.Helpers, only: [sort: 1, paginate: 4] +import Filtrex.Type.Config + +alias <%= inspect schema.module %> + +@pagination [page_size: 15] +@pagination_distance 5 + +@doc """ +Paginate the list of <%= schema.plural %> using filtrex +filters. + +## Examples + + iex> list_<%= schema.plural %>(%{}) + %{<%= schema.plural %>: [%<%= inspect schema.alias %>{}], ...} +""" +@spec paginate_<%= schema.plural %>(map) :: {:ok, map} | {:error, any} +def paginate_<%= schema.plural %>(params \\ %{}) do + params = + params + |> Map.put_new("sort_direction", "desc") + |> Map.put_new("sort_field", "inserted_at") + + {:ok, sort_direction} = Map.fetch(params, "sort_direction") + {:ok, sort_field} = Map.fetch(params, "sort_field") + + with {:ok, filter} <- Filtrex.parse_params(filter_config(<%= inspect String.to_atom(schema.plural) %>), params["<%= schema.singular %>"] || %{}), + %Scrivener.Page{} = page <- do_paginate_<%= schema.plural %>(filter, params) do + {:ok, + %{ + <%= schema.plural %>: page.entries, + page_number: page.page_number, + page_size: page.page_size, + total_pages: page.total_pages, + total_entries: page.total_entries, + distance: @pagination_distance, + sort_field: sort_field, + sort_direction: sort_direction + } + } + else + {:error, error} -> {:error, error} + error -> {:error, error} + end +end + +defp do_paginate_<%= schema.plural %>(filter, params) do + <%= inspect schema.alias %> + |> Filtrex.query(filter) + |> order_by(^sort(params)) + |> paginate(Repo, params, @pagination) +end + +@doc """ +Returns the list of <%= schema.plural %>. + +## Examples + + iex> list_<%= schema.plural %>() + [%<%= inspect schema.alias %>{}, ...] + +""" +def list_<%= schema.plural %> do + Repo.all(<%= inspect schema.alias %>) +end + +@doc """ +Gets a single <%= schema.singular %>. + +Raises `Ecto.NoResultsError` if the <%= schema.human_singular %> does not exist. + +## Examples + + iex> get_<%= schema.singular %>!(123) + %<%= inspect schema.alias %>{} + + iex> get_<%= schema.singular %>!(456) + ** (Ecto.NoResultsError) + +""" +def get_<%= schema.singular %>!(id), do: Repo.get!(<%= inspect schema.alias %>, id) + +@doc """ +Creates a <%= schema.singular %>. + +## Examples + + iex> create_<%= schema.singular %>(%{field: value}) + {:ok, %<%= inspect schema.alias %>{}} + + iex> create_<%= schema.singular %>(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + +""" +def create_<%= schema.singular %>(attrs \\ %{}) do + %<%= inspect schema.alias %>{} + |> <%= inspect schema.alias %>.changeset(attrs) + |> Repo.insert() +end + +@doc """ +Updates a <%= schema.singular %>. + +## Examples + + iex> update_<%= schema.singular %>(<%= schema.singular %>, %{field: new_value}) + {:ok, %<%= inspect schema.alias %>{}} + + iex> update_<%= schema.singular %>(<%= schema.singular %>, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + +""" +def update_<%= schema.singular %>(%<%= inspect schema.alias %>{} = <%= schema.singular %>, attrs) do + <%= schema.singular %> + |> <%= inspect schema.alias %>.changeset(attrs) + |> Repo.update() +end + +@doc """ +Deletes a <%= inspect schema.alias %>. + +## Examples + + iex> delete_<%= schema.singular %>(<%= schema.singular %>) + {:ok, %<%= inspect schema.alias %>{}} + + iex> delete_<%= schema.singular %>(<%= schema.singular %>) + {:error, %Ecto.Changeset{}} + +""" +def delete_<%= schema.singular %>(%<%= inspect schema.alias %>{} = <%= schema.singular %>) do + Repo.delete(<%= schema.singular %>) +end + +@doc """ +Returns an `%Ecto.Changeset{}` for tracking <%= schema.singular %> changes. + +## Examples + + iex> change_<%= schema.singular %>(<%= schema.singular %>) + %Ecto.Changeset{source: %<%= inspect schema.alias %>{}} + +""" +def change_<%= schema.singular %>(%<%= inspect schema.alias %>{} = <%= schema.singular %>) do + <%= inspect schema.alias %>.changeset(<%= schema.singular %>, %{}) +end + +defp filter_config(<%= inspect String.to_atom(schema.plural) %>) do + defconfig do + <%= for {name, type} <- schema.attrs do %><%= cond do %> + <% type in [:string, :text] -> %>text <%= inspect name %> + <% type in [:integer, :number] -> %>number <%= inspect name %> + <% type in [:naive_datetime, :utc_datetime, :datetime, :date] -> %>date <%= inspect name %> + <% type in [:boolean] -> %>boolean <%= inspect name %> + <% true -> %> #TODO add config for <%= name %> of type <%= type %> + <% end %><% end %> + end +end diff --git a/priv/templates/phx.gen.context/test_cases.exs b/priv/templates/phx.gen.context/test_cases.exs new file mode 100644 index 00000000..cf348677 --- /dev/null +++ b/priv/templates/phx.gen.context/test_cases.exs @@ -0,0 +1,77 @@ + + describe "<%= schema.plural %>" do + alias <%= inspect schema.module %> + + @valid_attrs <%= inspect schema.params.create %> + @update_attrs <%= inspect schema.params.update %> + @invalid_attrs <%= inspect for {key, _} <- schema.params.create, into: %{}, do: {key, nil} %> + + def <%= schema.singular %>_fixture(attrs \\ %{}) do + {:ok, <%= schema.singular %>} = + attrs + |> Enum.into(@valid_attrs) + |> <%= inspect context.alias %>.create_<%= schema.singular %>() + + <%= schema.singular %> + end + + test "paginate_<%= schema.plural %>/1 returns paginated list of <%= schema.plural %>" do + for _ <- 1..20 do + <%= schema.singular %>_fixture() + end + + {:ok, %{<%= schema.plural %>: <%= schema.plural %>} = page} = <%= inspect context.alias %>.paginate_<%= schema.plural %>(%{}) + + assert length(<%= schema.plural %>) == 15 + assert page.page_number == 1 + assert page.page_size == 15 + assert page.total_pages == 2 + assert page.total_entries == 20 + assert page.distance == 5 + assert page.sort_field == "inserted_at" + assert page.sort_direction == "desc" + end + + test "list_<%= schema.plural %>/0 returns all <%= schema.plural %>" do + <%= schema.singular %> = <%= schema.singular %>_fixture() + assert <%= inspect context.alias %>.list_<%= schema.plural %>() == [<%= schema.singular %>] + end + + test "get_<%= schema.singular %>!/1 returns the <%= schema.singular %> with given id" do + <%= schema.singular %> = <%= schema.singular %>_fixture() + assert <%= inspect context.alias %>.get_<%= schema.singular %>!(<%= schema.singular %>.id) == <%= schema.singular %> + end + + test "create_<%= schema.singular %>/1 with valid data creates a <%= schema.singular %>" do + assert {:ok, %<%= inspect schema.alias %>{} = <%= schema.singular %>} = <%= inspect context.alias %>.create_<%= schema.singular %>(@valid_attrs)<%= for {field, value} <- schema.params.create do %> + assert <%= schema.singular %>.<%= field %> == <%= Mix.Phoenix.Schema.value(schema, field, value) %><% end %> + end + + test "create_<%= schema.singular %>/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = <%= inspect context.alias %>.create_<%= schema.singular %>(@invalid_attrs) + end + + test "update_<%= schema.singular %>/2 with valid data updates the <%= schema.singular %>" do + <%= schema.singular %> = <%= schema.singular %>_fixture() + assert {:ok, <%= schema.singular %>} = <%= inspect context.alias %>.update_<%= schema.singular %>(<%= schema.singular %>, @update_attrs) + assert %<%= inspect schema.alias %>{} = <%= schema.singular %><%= for {field, value} <- schema.params.update do %> + assert <%= schema.singular %>.<%= field %> == <%= Mix.Phoenix.Schema.value(schema, field, value) %><% end %> + end + + test "update_<%= schema.singular %>/2 with invalid data returns error changeset" do + <%= schema.singular %> = <%= schema.singular %>_fixture() + assert {:error, %Ecto.Changeset{}} = <%= inspect context.alias %>.update_<%= schema.singular %>(<%= schema.singular %>, @invalid_attrs) + assert <%= schema.singular %> == <%= inspect context.alias %>.get_<%= schema.singular %>!(<%= schema.singular %>.id) + end + + test "delete_<%= schema.singular %>/1 deletes the <%= schema.singular %>" do + <%= schema.singular %> = <%= schema.singular %>_fixture() + assert {:ok, %<%= inspect schema.alias %>{}} = <%= inspect context.alias %>.delete_<%= schema.singular %>(<%= schema.singular %>) + assert_raise Ecto.NoResultsError, fn -> <%= inspect context.alias %>.get_<%= schema.singular %>!(<%= schema.singular %>.id) end + end + + test "change_<%= schema.singular %>/1 returns a <%= schema.singular %> changeset" do + <%= schema.singular %> = <%= schema.singular %>_fixture() + assert %Ecto.Changeset{} = <%= inspect context.alias %>.change_<%= schema.singular %>(<%= schema.singular %>) + end + end diff --git a/test/mix/tasks/torch.gen.html_test.exs b/test/mix/tasks/torch.gen.html_test.exs new file mode 100644 index 00000000..1094cb7a --- /dev/null +++ b/test/mix/tasks/torch.gen.html_test.exs @@ -0,0 +1,41 @@ +defmodule Mix.Tasks.Torch.Gen.HtmlTest do + use Torch.MixCase, async: false + + describe ".run/1" do + setup [:prepare_example_apps, :clean_generated_files] + + test_mix_config_errors("torch.gen.html") + + @tag :integration + test "generates valid context, controllers, and tests" do + # Install torch layout + System.cmd("mix", ["torch.install"], cd: @project_dir) + + # TODO: support slim + for format <- [:eex] do + System.cmd( + "mix", + ["torch.gen.html", "Blog", "Post", "posts", "title:string", "--format", "#{format}"], + cd: @project_dir + ) + + # Overwrite the router to include the generated link as + # shown in the instructions + File.write!( + "#{@project_dir}/lib/example_web/router.ex", + File.read!("test/support/routers/modified.ex") + ) + + # Run tests in the example app, ensure they are passing + assert {response, status} = System.cmd("mix", ["test"], cd: @project_dir) + assert status == 0, response + assert response =~ "20 tests, 0 failures" + + # Clean up generated files so they don't get committed by accident + clean_generated_files([]) + end + end + + # TODO: Add integration test for umbrella app + end +end diff --git a/test/support/apps/example/lib/example/accounts/accounts.ex b/test/support/apps/example/lib/example/accounts/accounts.ex deleted file mode 100644 index 35e96606..00000000 --- a/test/support/apps/example/lib/example/accounts/accounts.ex +++ /dev/null @@ -1,165 +0,0 @@ -defmodule Example.Accounts do - @moduledoc """ - The Accounts context. - """ - - import Ecto.Query, warn: false - alias Example.Repo -import Torch.Helpers, only: [sort: 1, paginate: 4] -import Filtrex.Type.Config - -alias Example.Accounts.User - -@pagination [page_size: 15] -@pagination_distance 5 - -@doc """ -Paginate the list of users using filtrex -filters. - -## Examples - - iex> list_users(%{}) - %{users: [%User{}], ...} -""" -@spec paginate_users(map) :: {:ok, map} | {:error, any} -def paginate_users(params \\ %{}) do - params = - params - |> Map.put_new("sort_direction", "desc") - |> Map.put_new("sort_field", "inserted_at") - - {:ok, sort_direction} = Map.fetch(params, "sort_direction") - {:ok, sort_field} = Map.fetch(params, "sort_field") - - with {:ok, filter} <- Filtrex.parse_params(filter_config(:users), params["user"] || %{}), - %Scrivener.Page{} = page <- do_paginate_users(filter, params) do - {:ok, - %{ - users: page.entries, - page_number: page.page_number, - page_size: page.page_size, - total_pages: page.total_pages, - total_entries: page.total_entries, - distance: @pagination_distance, - sort_field: sort_field, - sort_direction: sort_direction - } - } - else - {:error, error} -> {:error, error} - error -> {:error, error} - end -end - -defp do_paginate_users(filter, params) do - User - |> Filtrex.query(filter) - |> order_by(^sort(params)) - |> paginate(Repo, params, @pagination) -end - -@doc """ -Returns the list of users. - -## Examples - - iex> list_users() - [%User{}, ...] - -""" -def list_users do - Repo.all(User) -end - -@doc """ -Gets a single user. - -Raises `Ecto.NoResultsError` if the User does not exist. - -## Examples - - iex> get_user!(123) - %User{} - - iex> get_user!(456) - ** (Ecto.NoResultsError) - -""" -def get_user!(id), do: Repo.get!(User, id) - -@doc """ -Creates a user. - -## Examples - - iex> create_user(%{field: value}) - {:ok, %User{}} - - iex> create_user(%{field: bad_value}) - {:error, %Ecto.Changeset{}} - -""" -def create_user(attrs \\ %{}) do - %User{} - |> User.changeset(attrs) - |> Repo.insert() -end - -@doc """ -Updates a user. - -## Examples - - iex> update_user(user, %{field: new_value}) - {:ok, %User{}} - - iex> update_user(user, %{field: bad_value}) - {:error, %Ecto.Changeset{}} - -""" -def update_user(%User{} = user, attrs) do - user - |> User.changeset(attrs) - |> Repo.update() -end - -@doc """ -Deletes a User. - -## Examples - - iex> delete_user(user) - {:ok, %User{}} - - iex> delete_user(user) - {:error, %Ecto.Changeset{}} - -""" -def delete_user(%User{} = user) do - Repo.delete(user) -end - -@doc """ -Returns an `%Ecto.Changeset{}` for tracking user changes. - -## Examples - - iex> change_user(user) - %Ecto.Changeset{source: %User{}} - -""" -def change_user(%User{} = user) do - User.changeset(user, %{}) -end - -defp filter_config(:users) do - defconfig do - text :name - text :email - boolean :sign_up_complete - date :signed_up_at - - end -end -end diff --git a/test/support/apps/example/lib/example/accounts/user.ex b/test/support/apps/example/lib/example/accounts/user.ex deleted file mode 100644 index 046b65f1..00000000 --- a/test/support/apps/example/lib/example/accounts/user.ex +++ /dev/null @@ -1,22 +0,0 @@ -defmodule Example.Accounts.User do - use Ecto.Schema - import Ecto.Changeset - alias Example.Accounts.User - - - schema "users" do - field :email, :string - field :name, :string - field :sign_up_complete, :boolean, default: false - field :signed_up_at, :naive_datetime - - timestamps() - end - - @doc false - def changeset(%User{} = user, attrs) do - user - |> cast(attrs, [:name, :email, :sign_up_complete, :signed_up_at]) - |> validate_required([:name, :email, :sign_up_complete, :signed_up_at]) - end -end diff --git a/test/support/apps/example/lib/example/blog/blog.ex b/test/support/apps/example/lib/example/blog/blog.ex deleted file mode 100644 index 756790eb..00000000 --- a/test/support/apps/example/lib/example/blog/blog.ex +++ /dev/null @@ -1,167 +0,0 @@ -defmodule Example.Blog do - @moduledoc """ - The Blog context. - """ - - import Ecto.Query, warn: false - alias Example.Repo -import Torch.Helpers, only: [sort: 1, paginate: 4] -import Filtrex.Type.Config - -alias Example.Blog.Post - -@pagination [page_size: 15] -@pagination_distance 5 - -@doc """ -Paginate the list of posts using filtrex -filters. - -## Examples - - iex> list_posts(%{}) - %{posts: [%Post{}], ...} -""" -@spec paginate_posts(map) :: {:ok, map} | {:error, any} -def paginate_posts(params \\ %{}) do - params = - params - |> Map.put_new("sort_direction", "desc") - |> Map.put_new("sort_field", "inserted_at") - - {:ok, sort_direction} = Map.fetch(params, "sort_direction") - {:ok, sort_field} = Map.fetch(params, "sort_field") - - with {:ok, filter} <- Filtrex.parse_params(filter_config(:posts), params["post"] || %{}), - %Scrivener.Page{} = page <- do_paginate_posts(filter, params) do - {:ok, - %{ - posts: page.entries, - page_number: page.page_number, - page_size: page.page_size, - total_pages: page.total_pages, - total_entries: page.total_entries, - distance: @pagination_distance, - sort_field: sort_field, - sort_direction: sort_direction - } - } - else - {:error, error} -> {:error, error} - error -> {:error, error} - end -end - -defp do_paginate_posts(filter, params) do - Post - |> Filtrex.query(filter) - |> order_by(^sort(params)) - |> paginate(Repo, params, @pagination) -end - -@doc """ -Returns the list of posts. - -## Examples - - iex> list_posts() - [%Post{}, ...] - -""" -def list_posts do - Repo.all(Post) -end - -@doc """ -Gets a single post. - -Raises `Ecto.NoResultsError` if the Post does not exist. - -## Examples - - iex> get_post!(123) - %Post{} - - iex> get_post!(456) - ** (Ecto.NoResultsError) - -""" -def get_post!(id), do: Repo.get!(Post, id) - -@doc """ -Creates a post. - -## Examples - - iex> create_post(%{field: value}) - {:ok, %Post{}} - - iex> create_post(%{field: bad_value}) - {:error, %Ecto.Changeset{}} - -""" -def create_post(attrs \\ %{}) do - %Post{} - |> Post.changeset(attrs) - |> Repo.insert() -end - -@doc """ -Updates a post. - -## Examples - - iex> update_post(post, %{field: new_value}) - {:ok, %Post{}} - - iex> update_post(post, %{field: bad_value}) - {:error, %Ecto.Changeset{}} - -""" -def update_post(%Post{} = post, attrs) do - post - |> Post.changeset(attrs) - |> Repo.update() -end - -@doc """ -Deletes a Post. - -## Examples - - iex> delete_post(post) - {:ok, %Post{}} - - iex> delete_post(post) - {:error, %Ecto.Changeset{}} - -""" -def delete_post(%Post{} = post) do - Repo.delete(post) -end - -@doc """ -Returns an `%Ecto.Changeset{}` for tracking post changes. - -## Examples - - iex> change_post(post) - %Ecto.Changeset{source: %Post{}} - -""" -def change_post(%Post{} = post) do - Post.changeset(post, %{}) -end - -defp filter_config(:posts) do - defconfig do - text :title - text :subtitle - text :description - date :published_at - boolean :published - number :views - - end -end -end diff --git a/test/support/apps/example/lib/example/blog/post.ex b/test/support/apps/example/lib/example/blog/post.ex deleted file mode 100644 index efeb8c60..00000000 --- a/test/support/apps/example/lib/example/blog/post.ex +++ /dev/null @@ -1,24 +0,0 @@ -defmodule Example.Blog.Post do - use Ecto.Schema - import Ecto.Changeset - alias Example.Blog.Post - - - schema "posts" do - field :description, :string - field :published, :boolean, default: false - field :published_at, :utc_datetime - field :subtitle, :string - field :title, :string - field :views, :integer - - timestamps() - end - - @doc false - def changeset(%Post{} = post, attrs) do - post - |> cast(attrs, [:title, :subtitle, :description, :published_at, :published, :views]) - |> validate_required([:title, :subtitle, :description, :published_at, :published, :views]) - end -end diff --git a/test/support/apps/example/lib/example_web/controllers/post_controller.ex b/test/support/apps/example/lib/example_web/controllers/post_controller.ex deleted file mode 100644 index 4c832b65..00000000 --- a/test/support/apps/example/lib/example_web/controllers/post_controller.ex +++ /dev/null @@ -1,71 +0,0 @@ -defmodule ExampleWeb.PostController do - use ExampleWeb, :controller - - alias Example.Blog - alias Example.Blog.Post - - plug(:put_layout, {ExampleWeb.LayoutView, "torch.html"}) - - def index(conn, params) do - case Blog.paginate_posts(params) do - {:ok, assigns} -> - render(conn, "index.html", assigns) - - error -> - conn - |> put_flash(:error, "There was an error rendering Posts. #{inspect(error)}") - |> redirect(to: Routes.post_path(conn, :index)) - end - end - - def new(conn, _params) do - changeset = Blog.change_post(%Post{}) - render(conn, "new.html", changeset: changeset) - end - - def create(conn, %{"post" => post_params}) do - case Blog.create_post(post_params) do - {:ok, post} -> - conn - |> put_flash(:info, "Post created successfully.") - |> redirect(to: Routes.post_path(conn, :show, post)) - - {:error, %Ecto.Changeset{} = changeset} -> - render(conn, "new.html", changeset: changeset) - end - end - - def show(conn, %{"id" => id}) do - post = Blog.get_post!(id) - render(conn, "show.html", post: post) - end - - def edit(conn, %{"id" => id}) do - post = Blog.get_post!(id) - changeset = Blog.change_post(post) - render(conn, "edit.html", post: post, changeset: changeset) - end - - def update(conn, %{"id" => id, "post" => post_params}) do - post = Blog.get_post!(id) - - case Blog.update_post(post, post_params) do - {:ok, post} -> - conn - |> put_flash(:info, "Post updated successfully.") - |> redirect(to: Routes.post_path(conn, :show, post)) - - {:error, %Ecto.Changeset{} = changeset} -> - render(conn, "edit.html", post: post, changeset: changeset) - end - end - - def delete(conn, %{"id" => id}) do - post = Blog.get_post!(id) - {:ok, _post} = Blog.delete_post(post) - - conn - |> put_flash(:info, "Post deleted successfully.") - |> redirect(to: Routes.post_path(conn, :index)) - end -end diff --git a/test/support/apps/example/lib/example_web/controllers/user_controller.ex b/test/support/apps/example/lib/example_web/controllers/user_controller.ex deleted file mode 100644 index 51984c4b..00000000 --- a/test/support/apps/example/lib/example_web/controllers/user_controller.ex +++ /dev/null @@ -1,68 +0,0 @@ -defmodule ExampleWeb.UserController do - use ExampleWeb, :controller - - alias Example.Accounts - alias Example.Accounts.User - - plug(:put_layout, {ExampleWeb.LayoutView, "torch.html"}) - - def index(conn, params) do - case Accounts.paginate_users(params) do - {:ok, assigns} -> - render(conn, "index.html", assigns) - error -> - conn - |> put_flash(:error, "There was an error rendering Users. #{inspect(error)}") - |> redirect(to: Routes.user_path(conn, :index)) - end - end - - def new(conn, _params) do - changeset = Accounts.change_user(%User{}) - render(conn, "new.html", changeset: changeset) - end - - def create(conn, %{"user" => user_params}) do - case Accounts.create_user(user_params) do - {:ok, user} -> - conn - |> put_flash(:info, "User created successfully.") - |> redirect(to: Routes.user_path(conn, :show, user)) - {:error, %Ecto.Changeset{} = changeset} -> - render(conn, "new.html", changeset: changeset) - end - end - - def show(conn, %{"id" => id}) do - user = Accounts.get_user!(id) - render(conn, "show.html", user: user) - end - - def edit(conn, %{"id" => id}) do - user = Accounts.get_user!(id) - changeset = Accounts.change_user(user) - render(conn, "edit.html", user: user, changeset: changeset) - end - - def update(conn, %{"id" => id, "user" => user_params}) do - user = Accounts.get_user!(id) - - case Accounts.update_user(user, user_params) do - {:ok, user} -> - conn - |> put_flash(:info, "User updated successfully.") - |> redirect(to: Routes.user_path(conn, :show, user)) - {:error, %Ecto.Changeset{} = changeset} -> - render(conn, "edit.html", user: user, changeset: changeset) - end - end - - def delete(conn, %{"id" => id}) do - user = Accounts.get_user!(id) - {:ok, _user} = Accounts.delete_user(user) - - conn - |> put_flash(:info, "User deleted successfully.") - |> redirect(to: Routes.user_path(conn, :index)) - end -end \ No newline at end of file diff --git a/test/support/apps/example/lib/example_web/endpoint.ex b/test/support/apps/example/lib/example_web/endpoint.ex index 01700fc2..9e3edb15 100644 --- a/test/support/apps/example/lib/example_web/endpoint.ex +++ b/test/support/apps/example/lib/example_web/endpoint.ex @@ -13,13 +13,6 @@ defmodule ExampleWeb.Endpoint do gzip: false, only: ~w(css fonts images js favicon.ico robots.txt) - plug Plug.Static, - at: "/torch", - from: {:torch, "priv/static"}, - gzip: true, - cache_control_for_etags: "public, max-age=86400", - headers: [{"access-control-allow-origin", "*"}] - # Code reloading can be explicitly enabled under the # :code_reloader configuration of your endpoint. if code_reloading? do @@ -63,4 +56,4 @@ defmodule ExampleWeb.Endpoint do {:ok, config} end end -end \ No newline at end of file +end diff --git a/test/support/apps/example/lib/example_web/router.ex b/test/support/apps/example/lib/example_web/router.ex index 51d9442b..85533451 100644 --- a/test/support/apps/example/lib/example_web/router.ex +++ b/test/support/apps/example/lib/example_web/router.ex @@ -18,8 +18,6 @@ defmodule ExampleWeb.Router do pipe_through :browser get "/", PageController, :index - resources("/posts", PostController) - resources("/users", UserController) end # Other scopes may use custom stacks. diff --git a/test/support/apps/example/lib/example_web/templates/post/edit.html.eex b/test/support/apps/example/lib/example_web/templates/post/edit.html.eex deleted file mode 100644 index 6ea80459..00000000 --- a/test/support/apps/example/lib/example_web/templates/post/edit.html.eex +++ /dev/null @@ -1,14 +0,0 @@ -
-
- <%= link "Cancel", to: Routes.post_path(@conn, :index), class: "torch-button" %> -
-
- -
-
-
-

Edit Post

-
- <%= render "form.html", Map.put(assigns, :action, Routes.post_path(@conn, :update, @post)) %> -
-
diff --git a/test/support/apps/example/lib/example_web/templates/post/form.html.eex b/test/support/apps/example/lib/example_web/templates/post/form.html.eex deleted file mode 100644 index 34e28b67..00000000 --- a/test/support/apps/example/lib/example_web/templates/post/form.html.eex +++ /dev/null @@ -1,50 +0,0 @@ -<%= form_for @changeset, @action, [multipart: true, id: "torch-form"], fn f -> %> -
- - Details - - <%= if @changeset.action do %> -

Oops, something went wrong! Please check the errors below.

- <% end %> - -
- <%= label f, :title, class: "control-label" %> - <%= text_input f, :title, class: "form-control" %> - <%= error_tag f, :title %> -
- -
- <%= label f, :subtitle, class: "control-label" %> - <%= text_input f, :subtitle, class: "form-control" %> - <%= error_tag f, :subtitle %> -
- -
- <%= label f, :description, class: "control-label" %> - <%= textarea f, :description, class: "form-control" %> - <%= error_tag f, :description %> -
- -
- <%= label f, :published_at, class: "control-label" %> - <%= datetime_select f, :published_at, class: "form-control" %> - <%= error_tag f, :published_at %> -
- -
- <%= label f, :published, class: "control-label" %> - <%= checkbox f, :published, class: "checkbox" %> - <%= error_tag f, :published %> -
- -
- <%= label f, :views, class: "control-label" %> - <%= number_input f, :views, class: "form-control" %> - <%= error_tag f, :views %> -
- -
- <%= submit "Submit", class: "torch-submit-button" %> -
-
-<% end %> \ No newline at end of file diff --git a/test/support/apps/example/lib/example_web/templates/post/index.html.eex b/test/support/apps/example/lib/example_web/templates/post/index.html.eex deleted file mode 100644 index 7b750fac..00000000 --- a/test/support/apps/example/lib/example_web/templates/post/index.html.eex +++ /dev/null @@ -1,107 +0,0 @@ -
-
- <%= link "New Post", to: Routes.post_path(@conn, :new) %> -
-
-
-
-
-

Find Posts

- <%= form_tag @conn.request_path, method: :get, id: "torch-filters-form" do %> - -
- - <%= filter_select(:post, :title, @conn.params) %> - <%= filter_string_input(:post, :title, @conn.params) %> -
- -
- - <%= filter_select(:post, :subtitle, @conn.params) %> - <%= filter_string_input(:post, :subtitle, @conn.params) %> -
- -
- - <%= filter_select(:post, :description, @conn.params) %> - <%= filter_text_input(:post, :description, @conn.params) %> -
- - -
- - <%= filter_boolean_input(:post, :published, @conn.params) %> -
- - -
- - <%= filter_date_input(:post, :published_at, @conn.params) %> -
- - -
- - <%= number_filter_select(:post, :views, @conn.params) %> - <%= filter_number_input(:post, :views, @conn.params) %> -
- - - <%= link "Clear Filters", to: Routes.post_path(@conn, :index) %> - <% end %> - -
- -
- <%= if length(@posts) > 0 do %> - - - - - - - - - - - - - - - - - - - - - <%= for post <- @posts do %> - - - - - - - - - - - - - - - - - <% end %> - -
<%= table_link(@conn, "Title", :title) %><%= table_link(@conn, "Subtitle", :subtitle) %><%= table_link(@conn, "Description", :description) %><%= table_link(@conn, "Published at", :published_at) %><%= table_link(@conn, "Published", :published) %><%= table_link(@conn, "Views", :views) %>Actions
<%= post.title %><%= post.subtitle %><%= post.description %><%= post.published_at %><%= post.published %><%= post.views %> - <%= link "Show", to: Routes.post_path(@conn, :show, post) %> - <%= link "Edit", to: Routes.post_path(@conn, :edit, post) %> - <%= link "Delete", to: Routes.post_path(@conn, :delete, post), method: :delete, data: [confirm: "Are you sure?"] %> -
- <%= render Torch.PaginationView, "_pagination.html", assigns %> - <% else %> -

No Posts match your search.

- <% end %> -
-
-
\ No newline at end of file diff --git a/test/support/apps/example/lib/example_web/templates/post/new.html.eex b/test/support/apps/example/lib/example_web/templates/post/new.html.eex deleted file mode 100644 index 35a56a03..00000000 --- a/test/support/apps/example/lib/example_web/templates/post/new.html.eex +++ /dev/null @@ -1,14 +0,0 @@ -
-
- <%= link "Cancel", to: Routes.post_path(@conn, :index), class: "torch-button" %> -
-
- -
-
-
-

New Post

-
- <%= render "form.html", Map.put(assigns, :action, Routes.post_path(@conn, :create)) %> -
-
\ No newline at end of file diff --git a/test/support/apps/example/lib/example_web/templates/post/show.html.eex b/test/support/apps/example/lib/example_web/templates/post/show.html.eex deleted file mode 100644 index 0f249057..00000000 --- a/test/support/apps/example/lib/example_web/templates/post/show.html.eex +++ /dev/null @@ -1,47 +0,0 @@ -
-
- <%= link "Edit", to: Routes.post_path(@conn, :edit, @post), class: "torch-button" %> - <%= link "Back", to: Routes.post_path(@conn, :index), class: "torch-button" %> -
-
- -
-
-
-

Post Details

-
-
    - -
  • -
    Title:
    -
    <%= @post.title %>
    -
  • - -
  • -
    Subtitle:
    -
    <%= @post.subtitle %>
    -
  • - -
  • -
    Description:
    -
    <%= @post.description %>
    -
  • - -
  • -
    Published at:
    -
    <%= @post.published_at %>
    -
  • - -
  • -
    Published:
    -
    <%= @post.published %>
    -
  • - -
  • -
    Views:
    -
    <%= @post.views %>
    -
  • - -
-
-
\ No newline at end of file diff --git a/test/support/apps/example/lib/example_web/templates/user/edit.html.eex b/test/support/apps/example/lib/example_web/templates/user/edit.html.eex deleted file mode 100644 index e8c3a16e..00000000 --- a/test/support/apps/example/lib/example_web/templates/user/edit.html.eex +++ /dev/null @@ -1,14 +0,0 @@ -
-
- <%= link "Cancel", to: Routes.user_path(@conn, :index), class: "torch-button" %> -
-
- -
-
-
-

Edit User

-
- <%= render "form.html", Map.put(assigns, :action, Routes.user_path(@conn, :update, @user)) %> -
-
diff --git a/test/support/apps/example/lib/example_web/templates/user/form.html.eex b/test/support/apps/example/lib/example_web/templates/user/form.html.eex deleted file mode 100644 index 8a669844..00000000 --- a/test/support/apps/example/lib/example_web/templates/user/form.html.eex +++ /dev/null @@ -1,38 +0,0 @@ -<%= form_for @changeset, @action, [multipart: true, id: "torch-form"], fn f -> %> -
- - Details - - <%= if @changeset.action do %> -

Oops, something went wrong! Please check the errors below.

- <% end %> - -
- <%= label f, :name, class: "control-label" %> - <%= text_input f, :name, class: "form-control" %> - <%= error_tag f, :name %> -
- -
- <%= label f, :email, class: "control-label" %> - <%= text_input f, :email, class: "form-control" %> - <%= error_tag f, :email %> -
- -
- <%= label f, :sign_up_complete, class: "control-label" %> - <%= checkbox f, :sign_up_complete, class: "checkbox" %> - <%= error_tag f, :sign_up_complete %> -
- -
- <%= label f, :signed_up_at, class: "control-label" %> - <%= datetime_select f, :signed_up_at, class: "form-control" %> - <%= error_tag f, :signed_up_at %> -
- -
- <%= submit "Submit", class: "torch-submit-button" %> -
-
-<% end %> \ No newline at end of file diff --git a/test/support/apps/example/lib/example_web/templates/user/index.html.eex b/test/support/apps/example/lib/example_web/templates/user/index.html.eex deleted file mode 100644 index c5fb1156..00000000 --- a/test/support/apps/example/lib/example_web/templates/user/index.html.eex +++ /dev/null @@ -1,87 +0,0 @@ -
-
- <%= link "New User", to: Routes.user_path(@conn, :new) %> -
-
-
-
-
-

Find Users

- <%= form_tag @conn.request_path, method: :get, id: "torch-filters-form" do %> - -
- - <%= filter_select(:user, :name, @conn.params) %> - <%= filter_string_input(:user, :name, @conn.params) %> -
- -
- - <%= filter_select(:user, :email, @conn.params) %> - <%= filter_string_input(:user, :email, @conn.params) %> -
- - -
- - <%= filter_boolean_input(:user, :sign_up_complete, @conn.params) %> -
- - -
- - <%= filter_date_input(:user, :signed_up_at, @conn.params) %> -
- - - - <%= link "Clear Filters", to: Routes.user_path(@conn, :index) %> - <% end %> - -
- -
- <%= if length(@users) > 0 do %> - - - - - - - - - - - - - - - - - <%= for user <- @users do %> - - - - - - - - - - - - - <% end %> - -
<%= table_link(@conn, "Name", :name) %><%= table_link(@conn, "Email", :email) %><%= table_link(@conn, "Sign up complete", :sign_up_complete) %><%= table_link(@conn, "Signed up at", :signed_up_at) %>Actions
<%= user.name %><%= user.email %><%= user.sign_up_complete %><%= user.signed_up_at %> - <%= link "Show", to: Routes.user_path(@conn, :show, user) %> - <%= link "Edit", to: Routes.user_path(@conn, :edit, user) %> - <%= link "Delete", to: Routes.user_path(@conn, :delete, user), method: :delete, data: [confirm: "Are you sure?"] %> -
- <%= render Torch.PaginationView, "_pagination.html", assigns %> - <% else %> -

No Users match your search.

- <% end %> -
-
-
\ No newline at end of file diff --git a/test/support/apps/example/lib/example_web/templates/user/new.html.eex b/test/support/apps/example/lib/example_web/templates/user/new.html.eex deleted file mode 100644 index 21c16d4d..00000000 --- a/test/support/apps/example/lib/example_web/templates/user/new.html.eex +++ /dev/null @@ -1,14 +0,0 @@ -
-
- <%= link "Cancel", to: Routes.user_path(@conn, :index), class: "torch-button" %> -
-
- -
-
-
-

New User

-
- <%= render "form.html", Map.put(assigns, :action, Routes.user_path(@conn, :create)) %> -
-
\ No newline at end of file diff --git a/test/support/apps/example/lib/example_web/templates/user/show.html.eex b/test/support/apps/example/lib/example_web/templates/user/show.html.eex deleted file mode 100644 index 56f32df2..00000000 --- a/test/support/apps/example/lib/example_web/templates/user/show.html.eex +++ /dev/null @@ -1,37 +0,0 @@ -
-
- <%= link "Edit", to: Routes.user_path(@conn, :edit, @user), class: "torch-button" %> - <%= link "Back", to: Routes.user_path(@conn, :index), class: "torch-button" %> -
-
- -
-
-
-

User Details

-
-
    - -
  • -
    Name:
    -
    <%= @user.name %>
    -
  • - -
  • -
    Email:
    -
    <%= @user.email %>
    -
  • - -
  • -
    Sign up complete:
    -
    <%= @user.sign_up_complete %>
    -
  • - -
  • -
    Signed up at:
    -
    <%= @user.signed_up_at %>
    -
  • - -
-
-
\ No newline at end of file diff --git a/test/support/apps/example/lib/example_web/views/post_view.ex b/test/support/apps/example/lib/example_web/views/post_view.ex deleted file mode 100644 index 952f088a..00000000 --- a/test/support/apps/example/lib/example_web/views/post_view.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule ExampleWeb.PostView do - use ExampleWeb, :view - - import Torch.TableView - import Torch.FilterView -end diff --git a/test/support/apps/example/lib/example_web/views/user_view.ex b/test/support/apps/example/lib/example_web/views/user_view.ex deleted file mode 100644 index cce051cb..00000000 --- a/test/support/apps/example/lib/example_web/views/user_view.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule ExampleWeb.UserView do - use ExampleWeb, :view - - import Torch.TableView - import Torch.FilterView -end diff --git a/test/support/apps/example/priv/repo/migrations/20180217004018_create_posts.exs b/test/support/apps/example/priv/repo/migrations/20180217004018_create_posts.exs deleted file mode 100644 index 4211959a..00000000 --- a/test/support/apps/example/priv/repo/migrations/20180217004018_create_posts.exs +++ /dev/null @@ -1,17 +0,0 @@ -defmodule Example.Repo.Migrations.CreatePosts do - use Ecto.Migration - - def change do - create table(:posts) do - add :title, :string - add :subtitle, :string - add :description, :text - add :published_at, :utc_datetime - add :published, :boolean, default: false, null: false - add :views, :integer - - timestamps() - end - - end -end diff --git a/test/support/apps/example/priv/repo/migrations/20180308215932_create_users.exs b/test/support/apps/example/priv/repo/migrations/20180308215932_create_users.exs deleted file mode 100644 index 65322163..00000000 --- a/test/support/apps/example/priv/repo/migrations/20180308215932_create_users.exs +++ /dev/null @@ -1,15 +0,0 @@ -defmodule Example.Repo.Migrations.CreateUsers do - use Ecto.Migration - - def change do - create table(:users) do - add :name, :string - add :email, :string - add :sign_up_complete, :boolean, default: false, null: false - add :signed_up_at, :naive_datetime - - timestamps() - end - - end -end diff --git a/test/support/apps/example/test/example/accounts/accounts_test.exs b/test/support/apps/example/test/example/accounts/accounts_test.exs deleted file mode 100644 index fbeffd80..00000000 --- a/test/support/apps/example/test/example/accounts/accounts_test.exs +++ /dev/null @@ -1,88 +0,0 @@ -defmodule Example.AccountsTest do - use Example.DataCase - - alias Example.Accounts - - describe "users" do - alias Example.Accounts.User - - @valid_attrs %{email: "some email", name: "some name", sign_up_complete: true, signed_up_at: ~N[2010-04-17 14:00:00.000000]} - @update_attrs %{email: "some updated email", name: "some updated name", sign_up_complete: false, signed_up_at: ~N[2011-05-18 15:01:01.000000]} - @invalid_attrs %{email: nil, name: nil, sign_up_complete: nil, signed_up_at: nil} - - def user_fixture(attrs \\ %{}) do - {:ok, user} = - attrs - |> Enum.into(@valid_attrs) - |> Accounts.create_user() - - user - end - - test "paginate_users/1 returns paginated list of users" do - for _ <- 1..20 do - user_fixture() - end - - {:ok, %{users: users} = page} = Accounts.paginate_users(%{}) - - assert length(users) == 15 - assert page.page_number == 1 - assert page.page_size == 15 - assert page.total_pages == 2 - assert page.total_entries == 20 - assert page.distance == 5 - assert page.sort_field == "inserted_at" - assert page.sort_direction == "desc" - end - - test "list_users/0 returns all users" do - user = user_fixture() - assert Accounts.list_users() == [user] - end - - test "get_user!/1 returns the user with given id" do - user = user_fixture() - assert Accounts.get_user!(user.id) == user - end - - test "create_user/1 with valid data creates a user" do - assert {:ok, %User{} = user} = Accounts.create_user(@valid_attrs) - assert user.email == "some email" - assert user.name == "some name" - assert user.sign_up_complete == true - assert user.signed_up_at == ~N[2010-04-17 14:00:00.000000] - end - - test "create_user/1 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = Accounts.create_user(@invalid_attrs) - end - - test "update_user/2 with valid data updates the user" do - user = user_fixture() - assert {:ok, user} = Accounts.update_user(user, @update_attrs) - assert %User{} = user - assert user.email == "some updated email" - assert user.name == "some updated name" - assert user.sign_up_complete == false - assert user.signed_up_at == ~N[2011-05-18 15:01:01.000000] - end - - test "update_user/2 with invalid data returns error changeset" do - user = user_fixture() - assert {:error, %Ecto.Changeset{}} = Accounts.update_user(user, @invalid_attrs) - assert user == Accounts.get_user!(user.id) - end - - test "delete_user/1 deletes the user" do - user = user_fixture() - assert {:ok, %User{}} = Accounts.delete_user(user) - assert_raise Ecto.NoResultsError, fn -> Accounts.get_user!(user.id) end - end - - test "change_user/1 returns a user changeset" do - user = user_fixture() - assert %Ecto.Changeset{} = Accounts.change_user(user) - end - end -end diff --git a/test/support/apps/example/test/example/blog/blog_test.exs b/test/support/apps/example/test/example/blog/blog_test.exs deleted file mode 100644 index 3914a220..00000000 --- a/test/support/apps/example/test/example/blog/blog_test.exs +++ /dev/null @@ -1,92 +0,0 @@ -defmodule Example.BlogTest do - use Example.DataCase - - alias Example.Blog - - describe "posts" do - alias Example.Blog.Post - - @valid_attrs %{description: "some description", published: true, published_at: "2010-04-17 14:00:00.000000Z", subtitle: "some subtitle", title: "some title", views: 42} - @update_attrs %{description: "some updated description", published: false, published_at: "2011-05-18 15:01:01.000000Z", subtitle: "some updated subtitle", title: "some updated title", views: 43} - @invalid_attrs %{description: nil, published: nil, published_at: nil, subtitle: nil, title: nil, views: nil} - - def post_fixture(attrs \\ %{}) do - {:ok, post} = - attrs - |> Enum.into(@valid_attrs) - |> Blog.create_post() - - post - end - - test "paginate_posts/1 returns paginated list of posts" do - for _ <- 1..20 do - post_fixture() - end - - {:ok, %{posts: posts} = page} = Blog.paginate_posts(%{}) - - assert length(posts) == 15 - assert page.page_number == 1 - assert page.page_size == 15 - assert page.total_pages == 2 - assert page.total_entries == 20 - assert page.distance == 5 - assert page.sort_field == "inserted_at" - assert page.sort_direction == "desc" - end - - test "list_posts/0 returns all posts" do - post = post_fixture() - assert Blog.list_posts() == [post] - end - - test "get_post!/1 returns the post with given id" do - post = post_fixture() - assert Blog.get_post!(post.id) == post - end - - test "create_post/1 with valid data creates a post" do - assert {:ok, %Post{} = post} = Blog.create_post(@valid_attrs) - assert post.description == "some description" - assert post.published == true - assert post.published_at == DateTime.from_naive!(~N[2010-04-17 14:00:00.000000Z], "Etc/UTC") - assert post.subtitle == "some subtitle" - assert post.title == "some title" - assert post.views == 42 - end - - test "create_post/1 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = Blog.create_post(@invalid_attrs) - end - - test "update_post/2 with valid data updates the post" do - post = post_fixture() - assert {:ok, post} = Blog.update_post(post, @update_attrs) - assert %Post{} = post - assert post.description == "some updated description" - assert post.published == false - assert post.published_at == DateTime.from_naive!(~N[2011-05-18 15:01:01.000000Z], "Etc/UTC") - assert post.subtitle == "some updated subtitle" - assert post.title == "some updated title" - assert post.views == 43 - end - - test "update_post/2 with invalid data returns error changeset" do - post = post_fixture() - assert {:error, %Ecto.Changeset{}} = Blog.update_post(post, @invalid_attrs) - assert post == Blog.get_post!(post.id) - end - - test "delete_post/1 deletes the post" do - post = post_fixture() - assert {:ok, %Post{}} = Blog.delete_post(post) - assert_raise Ecto.NoResultsError, fn -> Blog.get_post!(post.id) end - end - - test "change_post/1 returns a post changeset" do - post = post_fixture() - assert %Ecto.Changeset{} = Blog.change_post(post) - end - end -end diff --git a/test/support/apps/example/test/example_web/controllers/post_controller_test.exs b/test/support/apps/example/test/example_web/controllers/post_controller_test.exs deleted file mode 100644 index 0fc96d20..00000000 --- a/test/support/apps/example/test/example_web/controllers/post_controller_test.exs +++ /dev/null @@ -1,88 +0,0 @@ -defmodule ExampleWeb.PostControllerTest do - use ExampleWeb.ConnCase - - alias Example.Blog - - @create_attrs %{description: "some description", published: true, published_at: "2010-04-17 14:00:00.000000Z", subtitle: "some subtitle", title: "some title", views: 42} - @update_attrs %{description: "some updated description", published: false, published_at: "2011-05-18 15:01:01.000000Z", subtitle: "some updated subtitle", title: "some updated title", views: 43} - @invalid_attrs %{description: nil, published: nil, published_at: nil, subtitle: nil, title: nil, views: nil} - - def fixture(:post) do - {:ok, post} = Blog.create_post(@create_attrs) - post - end - - describe "index" do - test "lists all posts", %{conn: conn} do - conn = get conn, Routes.post_path(conn, :index) - assert html_response(conn, 200) =~ "Posts" - end - end - - describe "new post" do - test "renders form", %{conn: conn} do - conn = get conn, Routes.post_path(conn, :new) - assert html_response(conn, 200) =~ "New Post" - end - end - - describe "create post" do - test "redirects to show when data is valid", %{conn: conn} do - conn = post conn, Routes.post_path(conn, :create), post: @create_attrs - - assert %{id: id} = redirected_params(conn) - assert redirected_to(conn) == Routes.post_path(conn, :show, id) - - conn = get conn, Routes.post_path(conn, :show, id) - assert html_response(conn, 200) =~ "Post Details" - end - - test "renders errors when data is invalid", %{conn: conn} do - conn = post conn, Routes.post_path(conn, :create), post: @invalid_attrs - assert html_response(conn, 200) =~ "New Post" - end - end - - describe "edit post" do - setup [:create_post] - - test "renders form for editing chosen post", %{conn: conn, post: post} do - conn = get conn, Routes.post_path(conn, :edit, post) - assert html_response(conn, 200) =~ "Edit Post" - end - end - - describe "update post" do - setup [:create_post] - - test "redirects when data is valid", %{conn: conn, post: post} do - conn = put conn, Routes.post_path(conn, :update, post), post: @update_attrs - assert redirected_to(conn) == Routes.post_path(conn, :show, post) - - conn = get conn, Routes.post_path(conn, :show, post) - assert html_response(conn, 200) =~ "some updated description" - end - - test "renders errors when data is invalid", %{conn: conn, post: post} do - conn = put conn, Routes.post_path(conn, :update, post), post: @invalid_attrs - assert html_response(conn, 200) =~ "Edit Post" - end - end - - describe "delete post" do - setup [:create_post] - - test "deletes chosen post", %{conn: conn, post: post} do - conn = delete conn, Routes.post_path(conn, :delete, post) - assert redirected_to(conn) == Routes.post_path(conn, :index) - assert_error_sent 404, fn -> - get conn, Routes.post_path(conn, :show, post) - end - end - end - - defp create_post(_) do - post = fixture(:post) - {:ok, post: post} - end -end diff --git a/test/support/apps/example/test/example_web/controllers/user_controller_test.exs b/test/support/apps/example/test/example_web/controllers/user_controller_test.exs deleted file mode 100644 index 2c5c9009..00000000 --- a/test/support/apps/example/test/example_web/controllers/user_controller_test.exs +++ /dev/null @@ -1,88 +0,0 @@ -defmodule ExampleWeb.UserControllerTest do - use ExampleWeb.ConnCase - - alias Example.Accounts - - @create_attrs %{email: "some email", name: "some name", sign_up_complete: true, signed_up_at: ~N[2010-04-17 14:00:00.000000]} - @update_attrs %{email: "some updated email", name: "some updated name", sign_up_complete: false, signed_up_at: ~N[2011-05-18 15:01:01.000000]} - @invalid_attrs %{email: nil, name: nil, sign_up_complete: nil, signed_up_at: nil} - - def fixture(:user) do - {:ok, user} = Accounts.create_user(@create_attrs) - user - end - - describe "index" do - test "lists all users", %{conn: conn} do - conn = get conn, Routes.user_path(conn, :index) - assert html_response(conn, 200) =~ "Users" - end - end - - describe "new user" do - test "renders form", %{conn: conn} do - conn = get conn, Routes.user_path(conn, :new) - assert html_response(conn, 200) =~ "New User" - end - end - - describe "create user" do - test "redirects to show when data is valid", %{conn: conn} do - conn = post conn, Routes.user_path(conn, :create), user: @create_attrs - - assert %{id: id} = redirected_params(conn) - assert redirected_to(conn) == Routes.user_path(conn, :show, id) - - conn = get conn, Routes.user_path(conn, :show, id) - assert html_response(conn, 200) =~ "User Details" - end - - test "renders errors when data is invalid", %{conn: conn} do - conn = post conn, Routes.user_path(conn, :create), user: @invalid_attrs - assert html_response(conn, 200) =~ "New User" - end - end - - describe "edit user" do - setup [:create_user] - - test "renders form for editing chosen user", %{conn: conn, user: user} do - conn = get conn, Routes.user_path(conn, :edit, user) - assert html_response(conn, 200) =~ "Edit User" - end - end - - describe "update user" do - setup [:create_user] - - test "redirects when data is valid", %{conn: conn, user: user} do - conn = put conn, Routes.user_path(conn, :update, user), user: @update_attrs - assert redirected_to(conn) == Routes.user_path(conn, :show, user) - - conn = get conn, Routes.user_path(conn, :show, user) - assert html_response(conn, 200) =~ "some updated email" - end - - test "renders errors when data is invalid", %{conn: conn, user: user} do - conn = put conn, Routes.user_path(conn, :update, user), user: @invalid_attrs - assert html_response(conn, 200) =~ "Edit User" - end - end - - describe "delete user" do - setup [:create_user] - - test "deletes chosen user", %{conn: conn, user: user} do - conn = delete conn, Routes.user_path(conn, :delete, user) - assert redirected_to(conn) == Routes.user_path(conn, :index) - assert_error_sent 404, fn -> - get conn, Routes.user_path(conn, :show, user) - end - end - end - - defp create_user(_) do - user = fixture(:user) - {:ok, user: user} - end -end diff --git a/test/support/cases/mix_case.ex b/test/support/cases/mix_case.ex index 96466be6..57bc5a6c 100644 --- a/test/support/cases/mix_case.ex +++ b/test/support/cases/mix_case.ex @@ -12,16 +12,13 @@ defmodule Torch.MixCase do end end - import ExUnit.CaptureIO - @project_dir "test/support/apps/example" @formats [:eex, :slim] @doc false def prepare_example_apps(_) do - capture_io(fn -> - System.cmd("mix", ["deps.get"], cd: @project_dir) - end) + System.cmd("mix", ["deps.get"], cd: @project_dir) + System.cmd("mix", ["ecto.drop"], cd: @project_dir, env: [{"MIX_ENV", "test"}]) :ok end @@ -29,7 +26,26 @@ defmodule Torch.MixCase do @doc false def clean_generated_files(_) do for format <- @formats do - File.rm("#{@project_dir}/lib/example_web/templates/layout/torch.html.#{format}") + files = [ + "#{@project_dir}/priv/templates/", + "#{@project_dir}/priv/repo/migrations/", + "#{@project_dir}/lib/example/blog/", + "#{@project_dir}/test/example/blog/", + "#{@project_dir}/lib/example_web/controllers/post_controller.ex", + "#{@project_dir}/lib/example_web/templates/layout/torch.html.#{format}", + "#{@project_dir}/lib/example_web/templates/post/", + "#{@project_dir}/lib/example_web/views/post_view.ex", + "#{@project_dir}/test/example_web/controllers/post_controller_test.exs" + ] + + Enum.each(files, &File.rm_rf/1) + + File.write!( + "#{@project_dir}/lib/example_web/router.ex", + File.read!("test/support/routers/original.ex") + ) + + File.mkdir("#{@project_dir}/priv/repo/migrations/") end :ok @@ -38,7 +54,7 @@ defmodule Torch.MixCase do @doc false # Generates tests on the given mix task to ensure it handles errors properly defmacro test_mix_config_errors(task) do - quote do + quote location: :keep do test "raises error if :format not specified" do assert_raise Mix.Error, """ diff --git a/test/support/routers/modified.ex b/test/support/routers/modified.ex new file mode 100644 index 00000000..2541e20f --- /dev/null +++ b/test/support/routers/modified.ex @@ -0,0 +1,28 @@ +defmodule ExampleWeb.Router do + use ExampleWeb, :router + + pipeline :browser do + plug :accepts, ["html"] + plug :fetch_session + plug :fetch_flash + plug :protect_from_forgery + plug :put_secure_browser_headers + end + + pipeline :api do + plug :accepts, ["json"] + end + + scope "/", ExampleWeb do + # Use the default browser stack + pipe_through :browser + + get "/", PageController, :index + resources("/posts", PostController) + end + + # Other scopes may use custom stacks. + # scope "/api", ExampleWeb do + # pipe_through :api + # end +end diff --git a/test/support/routers/original.ex b/test/support/routers/original.ex new file mode 100644 index 00000000..85533451 --- /dev/null +++ b/test/support/routers/original.ex @@ -0,0 +1,27 @@ +defmodule ExampleWeb.Router do + use ExampleWeb, :router + + pipeline :browser do + plug :accepts, ["html"] + plug :fetch_session + plug :fetch_flash + plug :protect_from_forgery + plug :put_secure_browser_headers + end + + pipeline :api do + plug :accepts, ["json"] + end + + scope "/", ExampleWeb do + # Use the default browser stack + pipe_through :browser + + get "/", PageController, :index + end + + # Other scopes may use custom stacks. + # scope "/api", ExampleWeb do + # pipe_through :api + # end +end