summaryrefslogtreecommitdiff
path: root/test/support
diff options
context:
space:
mode:
Diffstat (limited to 'test/support')
-rw-r--r--test/support/conn_case.ex38
-rw-r--r--test/support/data_case.ex58
-rw-r--r--test/support/fixtures/locations_fixtures.ex69
-rw-r--r--test/support/fixtures/users_fixtures.ex41
4 files changed, 206 insertions, 0 deletions
diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex
new file mode 100644
index 0000000..6d4859c
--- /dev/null
+++ b/test/support/conn_case.ex
@@ -0,0 +1,38 @@
+defmodule SilmataivasWeb.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 common data structures and query the data layer.
+
+ Finally, if the test case interacts with the database,
+ we enable the SQL sandbox, so changes done to the database
+ are reverted at the end of every test. If you are using
+ PostgreSQL, you can even run database tests asynchronously
+ by setting `use SilmataivasWeb.ConnCase, async: true`, although
+ this option is not recommended for other databases.
+ """
+
+ use ExUnit.CaseTemplate
+
+ using do
+ quote do
+ # The default endpoint for testing
+ @endpoint SilmataivasWeb.Endpoint
+
+ use SilmataivasWeb, :verified_routes
+
+ # Import conveniences for testing with connections
+ import Plug.Conn
+ import Phoenix.ConnTest
+ import SilmataivasWeb.ConnCase
+ end
+ end
+
+ setup tags do
+ Silmataivas.DataCase.setup_sandbox(tags)
+ {:ok, conn: Phoenix.ConnTest.build_conn()}
+ end
+end
diff --git a/test/support/data_case.ex b/test/support/data_case.ex
new file mode 100644
index 0000000..b19132e
--- /dev/null
+++ b/test/support/data_case.ex
@@ -0,0 +1,58 @@
+defmodule Silmataivas.DataCase do
+ @moduledoc """
+ This module defines the setup for tests requiring
+ access to the application's data layer.
+
+ You may define functions here to be used as helpers in
+ your tests.
+
+ Finally, if the test case interacts with the database,
+ we enable the SQL sandbox, so changes done to the database
+ are reverted at the end of every test. If you are using
+ PostgreSQL, you can even run database tests asynchronously
+ by setting `use Silmataivas.DataCase, async: true`, although
+ this option is not recommended for other databases.
+ """
+
+ use ExUnit.CaseTemplate
+
+ using do
+ quote do
+ alias Silmataivas.Repo
+
+ import Ecto
+ import Ecto.Changeset
+ import Ecto.Query
+ import Silmataivas.DataCase
+ end
+ end
+
+ setup tags do
+ Silmataivas.DataCase.setup_sandbox(tags)
+ :ok
+ end
+
+ @doc """
+ Sets up the sandbox based on the test tags.
+ """
+ def setup_sandbox(tags) do
+ pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Silmataivas.Repo, shared: not tags[:async])
+ on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
+ end
+
+ @doc """
+ A helper that transforms changeset errors into a map of messages.
+
+ assert {:error, changeset} = Accounts.create_user(%{password: "short"})
+ assert "password is too short" in errors_on(changeset).password
+ assert %{password: ["password is too short"]} = errors_on(changeset)
+
+ """
+ def errors_on(changeset) do
+ Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
+ Regex.replace(~r"%{(\w+)}", message, fn _, key ->
+ opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
+ end)
+ end)
+ end
+end
diff --git a/test/support/fixtures/locations_fixtures.ex b/test/support/fixtures/locations_fixtures.ex
new file mode 100644
index 0000000..3b73074
--- /dev/null
+++ b/test/support/fixtures/locations_fixtures.ex
@@ -0,0 +1,69 @@
+defmodule Silmataivas.LocationsFixtures do
+ @moduledoc """
+ This module defines test helpers for creating
+ entities via the `Silmataivas.Locations` context.
+ """
+
+ import Silmataivas.UsersFixtures
+
+ @doc """
+ Generate a location.
+ """
+ def location_fixture(attrs \\ %{}) do
+ # Create a user first if user_id is not provided
+ user =
+ if Map.has_key?(attrs, :user_id) or Map.has_key?(attrs, "user_id"),
+ do: nil,
+ else: user_fixture()
+
+ {:ok, location} =
+ attrs
+ |> Enum.into(%{
+ latitude: 120.5,
+ longitude: 120.5,
+ user_id: (user && user.id) || attrs[:user_id] || attrs["user_id"]
+ })
+ |> Silmataivas.Locations.create_location()
+
+ location
+ end
+
+ @doc """
+ Generate a location with a specific user.
+ """
+ def location_fixture_with_user(user, attrs \\ %{}) do
+ {:ok, location} =
+ attrs
+ |> Enum.into(%{
+ latitude: 120.5,
+ longitude: 120.5,
+ user_id: user.id
+ })
+ |> Silmataivas.Locations.create_location()
+
+ location
+ end
+
+ @doc """
+ Generate location attributes with invalid values.
+ """
+ def invalid_location_attrs do
+ %{
+ latitude: nil,
+ longitude: nil,
+ user_id: nil
+ }
+ end
+
+ @doc """
+ Generate location attributes with extreme values.
+ """
+ def extreme_location_attrs do
+ %{
+ # Extreme value outside normal range
+ latitude: 1000.0,
+ # Extreme value outside normal range
+ longitude: 1000.0
+ }
+ end
+end
diff --git a/test/support/fixtures/users_fixtures.ex b/test/support/fixtures/users_fixtures.ex
new file mode 100644
index 0000000..8c26ab5
--- /dev/null
+++ b/test/support/fixtures/users_fixtures.ex
@@ -0,0 +1,41 @@
+defmodule Silmataivas.UsersFixtures do
+ @moduledoc """
+ This module defines test helpers for creating
+ entities via the `Silmataivas.Users` context.
+ """
+
+ @doc """
+ Generate a unique user user_id.
+ """
+ def unique_user_user_id, do: "some user_id#{System.unique_integer([:positive])}"
+
+ @doc """
+ Generate a user.
+ """
+ def user_fixture(attrs \\ %{}) do
+ {:ok, user} =
+ attrs
+ |> Enum.into(%{
+ role: "user",
+ user_id: unique_user_user_id()
+ })
+ |> Silmataivas.Users.create_user()
+
+ user
+ end
+
+ @doc """
+ Generate an admin user.
+ """
+ def admin_fixture(attrs \\ %{}) do
+ {:ok, user} =
+ attrs
+ |> Enum.into(%{
+ role: "admin",
+ user_id: unique_user_user_id()
+ })
+ |> Silmataivas.Users.create_user()
+
+ user
+ end
+end