From 033d07a519bac03fdfd28ca15e09cc287e80fd14 Mon Sep 17 00:00:00 2001 From: neodarz Date: Tue, 24 Jan 2017 17:09:18 +0100 Subject: Initial commit with non-functional pieces of code about file upload --- test/controllers/audio_file_controller_test.exs | 66 +++++++++++++++++++++++++ test/controllers/page_controller_test.exs | 8 +++ test/models/audio_file_test.exs | 18 +++++++ test/support/channel_case.ex | 43 ++++++++++++++++ test/support/conn_case.ex | 44 +++++++++++++++++ test/support/model_case.ex | 65 ++++++++++++++++++++++++ test/test_helper.exs | 4 ++ test/views/error_view_test.exs | 21 ++++++++ test/views/layout_view_test.exs | 3 ++ test/views/page_view_test.exs | 3 ++ 10 files changed, 275 insertions(+) create mode 100644 test/controllers/audio_file_controller_test.exs create mode 100644 test/controllers/page_controller_test.exs create mode 100644 test/models/audio_file_test.exs create mode 100644 test/support/channel_case.ex create mode 100644 test/support/conn_case.ex create mode 100644 test/support/model_case.ex create mode 100644 test/test_helper.exs create mode 100644 test/views/error_view_test.exs create mode 100644 test/views/layout_view_test.exs create mode 100644 test/views/page_view_test.exs (limited to 'test') diff --git a/test/controllers/audio_file_controller_test.exs b/test/controllers/audio_file_controller_test.exs new file mode 100644 index 0000000..e1df494 --- /dev/null +++ b/test/controllers/audio_file_controller_test.exs @@ -0,0 +1,66 @@ +defmodule TheTranscriberBackend.AudioFileControllerTest do + use TheTranscriberBackend.ConnCase + + alias TheTranscriberBackend.AudioFile + @valid_attrs %{audio_duration: "some content", audio_path: "some content", transcription_file_path: "some content"} + @invalid_attrs %{} + + test "lists all entries on index", %{conn: conn} do + conn = get conn, audio_file_path(conn, :index) + assert html_response(conn, 200) =~ "Listing audio file" + end + + test "renders form for new resources", %{conn: conn} do + conn = get conn, audio_file_path(conn, :new) + assert html_response(conn, 200) =~ "New audio file" + end + + test "creates resource and redirects when data is valid", %{conn: conn} do + conn = post conn, audio_file_path(conn, :create), audio_file: @valid_attrs + assert redirected_to(conn) == audio_file_path(conn, :index) + assert Repo.get_by(AudioFile, @valid_attrs) + end + + test "does not create resource and renders errors when data is invalid", %{conn: conn} do + conn = post conn, audio_file_path(conn, :create), audio_file: @invalid_attrs + assert html_response(conn, 200) =~ "New audio file" + end + + test "shows chosen resource", %{conn: conn} do + audio_file = Repo.insert! %AudioFile{} + conn = get conn, audio_file_path(conn, :show, audio_file) + assert html_response(conn, 200) =~ "Show audio file" + end + + test "renders page not found when id is nonexistent", %{conn: conn} do + assert_error_sent 404, fn -> + get conn, audio_file_path(conn, :show, -1) + end + end + + test "renders form for editing chosen resource", %{conn: conn} do + audio_file = Repo.insert! %AudioFile{} + conn = get conn, audio_file_path(conn, :edit, audio_file) + assert html_response(conn, 200) =~ "Edit audio file" + end + + test "updates chosen resource and redirects when data is valid", %{conn: conn} do + audio_file = Repo.insert! %AudioFile{} + conn = put conn, audio_file_path(conn, :update, audio_file), audio_file: @valid_attrs + assert redirected_to(conn) == audio_file_path(conn, :show, audio_file) + assert Repo.get_by(AudioFile, @valid_attrs) + end + + test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do + audio_file = Repo.insert! %AudioFile{} + conn = put conn, audio_file_path(conn, :update, audio_file), audio_file: @invalid_attrs + assert html_response(conn, 200) =~ "Edit audio file" + end + + test "deletes chosen resource", %{conn: conn} do + audio_file = Repo.insert! %AudioFile{} + conn = delete conn, audio_file_path(conn, :delete, audio_file) + assert redirected_to(conn) == audio_file_path(conn, :index) + refute Repo.get(AudioFile, audio_file.id) + end +end diff --git a/test/controllers/page_controller_test.exs b/test/controllers/page_controller_test.exs new file mode 100644 index 0000000..22c052d --- /dev/null +++ b/test/controllers/page_controller_test.exs @@ -0,0 +1,8 @@ +defmodule TheTranscriberBackend.PageControllerTest do + use TheTranscriberBackend.ConnCase + + test "GET /", %{conn: conn} do + conn = get conn, "/" + assert html_response(conn, 200) =~ "Welcome to Phoenix!" + end +end diff --git a/test/models/audio_file_test.exs b/test/models/audio_file_test.exs new file mode 100644 index 0000000..749cf4e --- /dev/null +++ b/test/models/audio_file_test.exs @@ -0,0 +1,18 @@ +defmodule TheTranscriberBackend.AudioFileTest do + use TheTranscriberBackend.ModelCase + + alias TheTranscriberBackend.AudioFile + + @valid_attrs %{audio_duration: "some content", audio_path: "some content", transcription_file_path: "some content"} + @invalid_attrs %{} + + test "changeset with valid attributes" do + changeset = AudioFile.changeset(%AudioFile{}, @valid_attrs) + assert changeset.valid? + end + + test "changeset with invalid attributes" do + changeset = AudioFile.changeset(%AudioFile{}, @invalid_attrs) + refute changeset.valid? + end +end diff --git a/test/support/channel_case.ex b/test/support/channel_case.ex new file mode 100644 index 0000000..f121634 --- /dev/null +++ b/test/support/channel_case.ex @@ -0,0 +1,43 @@ +defmodule TheTranscriberBackend.ChannelCase do + @moduledoc """ + This module defines the test case to be used by + channel tests. + + Such tests rely on `Phoenix.ChannelTest` and also + import other functionality to make it easier + to build and query models. + + Finally, if the test case interacts with the database, + it cannot be async. For this reason, every test runs + inside a transaction which is reset at the beginning + of the test unless the test case is marked as async. + """ + + use ExUnit.CaseTemplate + + using do + quote do + # Import conveniences for testing with channels + use Phoenix.ChannelTest + + alias TheTranscriberBackend.Repo + import Ecto + import Ecto.Changeset + import Ecto.Query + + + # The default endpoint for testing + @endpoint TheTranscriberBackend.Endpoint + end + end + + setup tags do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(TheTranscriberBackend.Repo) + + unless tags[:async] do + Ecto.Adapters.SQL.Sandbox.mode(TheTranscriberBackend.Repo, {:shared, self()}) + end + + :ok + end +end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex new file mode 100644 index 0000000..6f0e522 --- /dev/null +++ b/test/support/conn_case.ex @@ -0,0 +1,44 @@ +defmodule TheTranscriberBackend.ConnCase do + @moduledoc """ + This module defines the test case to be used by + tests that require setting up a connection. + + Such tests rely on `Phoenix.ConnTest` and also + import other functionality to make it easier + to build and query models. + + Finally, if the test case interacts with the database, + it cannot be async. For this reason, every test runs + inside a transaction which is reset at the beginning + of the test unless the test case is marked as async. + """ + + use ExUnit.CaseTemplate + + using do + quote do + # Import conveniences for testing with connections + use Phoenix.ConnTest + + alias TheTranscriberBackend.Repo + import Ecto + import Ecto.Changeset + import Ecto.Query + + import TheTranscriberBackend.Router.Helpers + + # The default endpoint for testing + @endpoint TheTranscriberBackend.Endpoint + end + end + + setup tags do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(TheTranscriberBackend.Repo) + + unless tags[:async] do + Ecto.Adapters.SQL.Sandbox.mode(TheTranscriberBackend.Repo, {:shared, self()}) + end + + {:ok, conn: Phoenix.ConnTest.build_conn()} + end +end diff --git a/test/support/model_case.ex b/test/support/model_case.ex new file mode 100644 index 0000000..9f16428 --- /dev/null +++ b/test/support/model_case.ex @@ -0,0 +1,65 @@ +defmodule TheTranscriberBackend.ModelCase do + @moduledoc """ + This module defines the test case to be used by + model tests. + + You may define functions here to be used as helpers in + your model tests. See `errors_on/2`'s definition as reference. + + Finally, if the test case interacts with the database, + it cannot be async. For this reason, every test runs + inside a transaction which is reset at the beginning + of the test unless the test case is marked as async. + """ + + use ExUnit.CaseTemplate + + using do + quote do + alias TheTranscriberBackend.Repo + + import Ecto + import Ecto.Changeset + import Ecto.Query + import TheTranscriberBackend.ModelCase + end + end + + setup tags do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(TheTranscriberBackend.Repo) + + unless tags[:async] do + Ecto.Adapters.SQL.Sandbox.mode(TheTranscriberBackend.Repo, {:shared, self()}) + end + + :ok + end + + @doc """ + Helper for returning list of errors in a struct when given certain data. + + ## Examples + + Given a User schema that lists `:name` as a required field and validates + `:password` to be safe, it would return: + + iex> errors_on(%User{}, %{password: "password"}) + [password: "is unsafe", name: "is blank"] + + You could then write your assertion like: + + assert {:password, "is unsafe"} in errors_on(%User{}, %{password: "password"}) + + You can also create the changeset manually and retrieve the errors + field directly: + + iex> changeset = User.changeset(%User{}, password: "password") + iex> {:password, "is unsafe"} in changeset.errors + true + """ + def errors_on(struct, data) do + struct.__struct__.changeset(struct, data) + |> Ecto.Changeset.traverse_errors(&TheTranscriberBackend.ErrorHelpers.translate_error/1) + |> Enum.flat_map(fn {key, errors} -> for msg <- errors, do: {key, msg} end) + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..f6a4659 --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1,4 @@ +ExUnit.start + +Ecto.Adapters.SQL.Sandbox.mode(TheTranscriberBackend.Repo, :manual) + diff --git a/test/views/error_view_test.exs b/test/views/error_view_test.exs new file mode 100644 index 0000000..cd5390a --- /dev/null +++ b/test/views/error_view_test.exs @@ -0,0 +1,21 @@ +defmodule TheTranscriberBackend.ErrorViewTest do + use TheTranscriberBackend.ConnCase, async: true + + # Bring render/3 and render_to_string/3 for testing custom views + import Phoenix.View + + test "renders 404.html" do + assert render_to_string(TheTranscriberBackend.ErrorView, "404.html", []) == + "Page not found" + end + + test "render 500.html" do + assert render_to_string(TheTranscriberBackend.ErrorView, "500.html", []) == + "Internal server error" + end + + test "render any other" do + assert render_to_string(TheTranscriberBackend.ErrorView, "505.html", []) == + "Internal server error" + end +end diff --git a/test/views/layout_view_test.exs b/test/views/layout_view_test.exs new file mode 100644 index 0000000..b060307 --- /dev/null +++ b/test/views/layout_view_test.exs @@ -0,0 +1,3 @@ +defmodule TheTranscriberBackend.LayoutViewTest do + use TheTranscriberBackend.ConnCase, async: true +end diff --git a/test/views/page_view_test.exs b/test/views/page_view_test.exs new file mode 100644 index 0000000..7aa5589 --- /dev/null +++ b/test/views/page_view_test.exs @@ -0,0 +1,3 @@ +defmodule TheTranscriberBackend.PageViewTest do + use TheTranscriberBackend.ConnCase, async: true +end -- cgit v1.2.1