Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ebac60a2b0 | ||
|
|
c6236c0ef1 | ||
|
|
dd0e79c400 | ||
|
|
e6e344745b | ||
|
|
62e855e1b6 | ||
|
|
8f6ec31366 | ||
|
|
9c273a473e | ||
|
|
9e7e052249 | ||
|
|
fb4f0025d1 | ||
|
|
6799e5b00b |
@@ -23,4 +23,21 @@
|
||||
Add the API to create the Links model.
|
||||
** 0.4.2 <2023-07-30 Sun>
|
||||
Add the API to delete the Links model.
|
||||
** 0.4.3 <2023-07-31 Mon>
|
||||
Rename the controller and the view names to "Link".
|
||||
Move Links to the Links context.
|
||||
Refactor to split the Links to a Link model and a Links logic.
|
||||
Update the controller and the view for updated Link model.
|
||||
** 0.4.4 <2023-08-01 Tue>
|
||||
Fix Links.update_one/2.
|
||||
** 0.4.5 <2023-08-01 Tue>
|
||||
Fix the route path in LinkController.create/2.
|
||||
** 0.4.6 <2023-08-01 Tue>
|
||||
Add the API to update the Link model.
|
||||
** 0.4.7 <2023-08-01 Tue>
|
||||
Add tests for the API.
|
||||
** 0.4.8 <2023-08-02 Wed>
|
||||
Refactor the Links module.
|
||||
** 0.4.9 <2023-08-02 Wed>
|
||||
Add tests for the Links module.
|
||||
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
defmodule LinkShortener.Links do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
import Ecto.Query
|
||||
alias LinkShortener.Repo
|
||||
alias LinkShortener.Links
|
||||
alias LinkShortener.Generators.SafeString
|
||||
|
||||
schema "links" do
|
||||
field :name, :string
|
||||
field :url, :string
|
||||
field :shorten, :string
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(link, attrs) do
|
||||
link
|
||||
|> cast(attrs, [:name, :url, :shorten])
|
||||
|> validate_required([:url, :shorten])
|
||||
|> unique_constraint(:shorten)
|
||||
end
|
||||
|
||||
def new_one(), do: Links.changeset(%Links{})
|
||||
|
||||
def create_one(attrs, length \\ 10, generator \\ SafeString) do
|
||||
shorten = generator.generate(length)
|
||||
insert_one(Map.put(attrs, :shorten, shorten))
|
||||
end
|
||||
|
||||
def insert_one(attrs) do
|
||||
%Links{}
|
||||
|> Links.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
def edit_one(id) do
|
||||
get_one(id)
|
||||
|> Links.changeset()
|
||||
end
|
||||
|
||||
def update_one(%Links{} = Links, changes) do
|
||||
Links
|
||||
|> Links.changeset(changes)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
def insert_one(attrs) do
|
||||
%Links{}
|
||||
|> Links.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
def delete_one(%Links{} = Links), do: Repo.delete(Links)
|
||||
|
||||
def get_one_by(attrs) do
|
||||
Repo.get_by(Links, attrs)
|
||||
end
|
||||
|
||||
def get_one_by_shorten(shorten) do
|
||||
get_one_by(%{shorten: shorten})
|
||||
end
|
||||
|
||||
def get_one(id) do
|
||||
Repo.get!(Links, id)
|
||||
end
|
||||
|
||||
def get_all(opts) do
|
||||
from(Links)
|
||||
|> Repo.all()
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
defmodule LinkShortener.Links.Link do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
import Ecto.Query
|
||||
|
||||
schema "links" do
|
||||
field :name, :string
|
||||
field :url, :string
|
||||
field :shorten, :string
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(link, attrs) do
|
||||
link
|
||||
|> cast(attrs, [:name, :url, :shorten])
|
||||
|> validate_required([:url, :shorten])
|
||||
|> unique_constraint(:shorten)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,57 @@
|
||||
defmodule LinkShortener.Links do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
import Ecto.Query
|
||||
alias LinkShortener.Repo
|
||||
alias LinkShortener.Links.Link
|
||||
alias LinkShortener.Generators.SafeString
|
||||
|
||||
def new_one(), do: Link.changeset(%Link{}, %{})
|
||||
|
||||
def create_one(attrs, length \\ 10, generator \\ SafeString) do
|
||||
shorten = generator.generate(length)
|
||||
Map.put(attrs, :shorten, shorten)
|
||||
|> insert_one()
|
||||
end
|
||||
|
||||
def insert_one(attrs) do
|
||||
%Link{}
|
||||
|> Link.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
def get_one!(id) do
|
||||
Repo.get!(Link, id)
|
||||
end
|
||||
|
||||
def get_one_by(attrs) do
|
||||
Repo.get_by(Link, attrs)
|
||||
end
|
||||
|
||||
def get_one_by_shorten(shorten) do
|
||||
get_one_by(%{shorten: shorten})
|
||||
end
|
||||
|
||||
def get_all() do
|
||||
from(Link)
|
||||
|> Repo.all()
|
||||
end
|
||||
|
||||
def get_all(opts) do
|
||||
from(Link)
|
||||
|> Repo.all()
|
||||
end
|
||||
|
||||
def edit_one(%Link{} = link) do
|
||||
link
|
||||
|> Link.changeset(%{})
|
||||
end
|
||||
|
||||
def update_one(%Link{} = link, changes) do
|
||||
link
|
||||
|> Link.changeset(changes)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
def delete_one(%Link{} = link), do: Repo.delete(link)
|
||||
end
|
||||
@@ -0,0 +1,43 @@
|
||||
defmodule LinkShortenerWeb.Api.V1.LinkController do
|
||||
use LinkShortenerWeb, :controller
|
||||
|
||||
alias LinkShortener.Links
|
||||
alias LinkShortener.Links.Link
|
||||
|
||||
action_fallback LinkShortenerWeb.FallbackController
|
||||
|
||||
def index(conn, _params) do
|
||||
links = Links.get_all()
|
||||
render(conn, "index.json", links: links)
|
||||
end
|
||||
|
||||
def create(conn, %{"link" => link_params}) do
|
||||
with {:ok, %Link{} = link} <- Links.insert_one(link_params) do
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|> put_resp_header("location", Routes.v1_link_path(conn, :show, link))
|
||||
|> render("show.json", link: link)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
link = Links.get_one!(id)
|
||||
render(conn, "show.json", link: link)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "link" => link_params}) do
|
||||
link = Links.get_one!(id)
|
||||
|
||||
with {:ok, %Link{} = link} <- Links.update_one(link, link_params) do
|
||||
render(conn, "show.json", link: link)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
link = Links.get_one!(id)
|
||||
|
||||
with {:ok, %Link{}} <- Links.delete_one(link) do
|
||||
send_resp(conn, :no_content, "")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,34 +0,0 @@
|
||||
defmodule LinkShortenerWeb.Api.V1.LinksController do
|
||||
use LinkShortenerWeb, :controller
|
||||
|
||||
alias LinkShortener.Links
|
||||
|
||||
action_fallback LinkShortenerWeb.FallbackController
|
||||
|
||||
def index(conn, _params) do
|
||||
links = Links.get_all({})
|
||||
render(conn, "index.json", links: links)
|
||||
end
|
||||
|
||||
def create(conn, %{"link" => link_params}) do
|
||||
with {:ok, %Links{} = link} <- Links.insert_one(link_params) do
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|> put_resp_header("location", Routes.links_path(conn, :show, link))
|
||||
|> render("show.json", links: link)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
link = Links.get_one(id)
|
||||
render(conn, "show.json", links: link)
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
link = Links.get_one(id)
|
||||
|
||||
with {:ok, %Link{}} <- Links.delete_one(link) do
|
||||
send_resp(conn, :no_content, "")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -26,7 +26,7 @@ defmodule LinkShortenerWeb.Router do
|
||||
pipe_through :api
|
||||
|
||||
scope "/v1", Api.V1, as: :v1 do
|
||||
resources "/links", LinksController
|
||||
resources "/links", LinkController
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
defmodule LinkShortenerWeb.Api.V1.LinkView do
|
||||
use LinkShortenerWeb, :view
|
||||
alias LinkShortenerWeb.Api.V1.LinkView
|
||||
|
||||
def render("index.json", %{links: links}) do
|
||||
%{data: render_many(links, LinkView, "link.json")}
|
||||
end
|
||||
|
||||
def render("show.json", %{link: link}) do
|
||||
%{data: render_one(link, LinkView, "link.json")}
|
||||
end
|
||||
|
||||
def render("link.json", %{link: link}) do
|
||||
%{
|
||||
id: link.id,
|
||||
name: link.name,
|
||||
url: link.url,
|
||||
shorten: link.shorten
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -1,21 +0,0 @@
|
||||
defmodule LinkShortenerWeb.Api.V1.LinksView do
|
||||
use LinkShortenerWeb, :view
|
||||
alias LinkShortenerWeb.Api.V1.LinksView
|
||||
|
||||
def render("index.json", %{links: links}) do
|
||||
%{data: render_many(links, LinksView, "link.json")}
|
||||
end
|
||||
|
||||
def render("show.json", %{links: link}) do
|
||||
%{data: render_one(link, LinksView, "link.json")}
|
||||
end
|
||||
|
||||
def render("link.json", %{links: link}) do
|
||||
%{
|
||||
id: link.id,
|
||||
name: link.name,
|
||||
url: link.url,
|
||||
shorten: link.shorten
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,106 @@
|
||||
defmodule LinkShortener.LinksTest do
|
||||
use LinkShortener.DataCase
|
||||
|
||||
alias LinkShortener.Links
|
||||
|
||||
@create_attrs %{
|
||||
name: "some link name",
|
||||
url: "https://gitlab.com/KKlochko/link_shortener",
|
||||
shorten: "git_repo",
|
||||
}
|
||||
@create_generated_attrs %{
|
||||
name: "some link name",
|
||||
url: "https://gitlab.com/KKlochko/link_shortener",
|
||||
}
|
||||
@update_attrs %{
|
||||
name: "some updated link name",
|
||||
url: "https://gitlab.com/KKlochko/link_shortener2",
|
||||
shorten: "new_git_repo",
|
||||
}
|
||||
@invalid_attrs %{
|
||||
name: nil,
|
||||
url: nil,
|
||||
shorten: nil,
|
||||
}
|
||||
|
||||
describe "links" do
|
||||
alias LinkShortener.Links.Link
|
||||
|
||||
import LinkShortener.LinksFixtures
|
||||
|
||||
test "new_one/1 returns the changeset" do
|
||||
assert %Ecto.Changeset{} = Links.new_one()
|
||||
end
|
||||
|
||||
test "create_one/1 with valid data creates a link" do
|
||||
assert {:ok, %Link{} = link} = Links.create_one(@create_generated_attrs)
|
||||
assert link.name == "some link name"
|
||||
assert link.url == "https://gitlab.com/KKlochko/link_shortener"
|
||||
assert String.length(link.shorten) == 10
|
||||
end
|
||||
|
||||
test "create_one/2 with valid data creates a link" do
|
||||
assert {:ok, %Link{} = link} = Links.create_one(@create_generated_attrs, 5)
|
||||
assert link.name == "some link name"
|
||||
assert link.url == "https://gitlab.com/KKlochko/link_shortener"
|
||||
assert String.length(link.shorten) == 5
|
||||
end
|
||||
|
||||
test "insert_one/1 with valid data creates a link" do
|
||||
assert {:ok, %Link{} = link} = Links.insert_one(@create_attrs)
|
||||
assert link.name == "some link name"
|
||||
assert link.url == "https://gitlab.com/KKlochko/link_shortener"
|
||||
assert link.shorten == "git_repo"
|
||||
end
|
||||
|
||||
test "insert_one/1 with invalid data returns error changeset" do
|
||||
assert {:error, %Ecto.Changeset{}} = Links.insert_one(@invalid_attrs)
|
||||
end
|
||||
|
||||
test "get_one!/1 returns the link with given id" do
|
||||
link = link_fixture()
|
||||
assert Links.get_one!(link.id) == link
|
||||
end
|
||||
|
||||
test "get_one_by!/1 returns the link with given shorten" do
|
||||
link = link_fixture()
|
||||
assert Links.get_one_by(%{shorten: link.shorten}) == link
|
||||
end
|
||||
|
||||
test "get_one_by_shorten!/1 returns the link with given shorten" do
|
||||
link = link_fixture()
|
||||
assert Links.get_one_by_shorten(link.shorten) == link
|
||||
end
|
||||
|
||||
test "get_all/0 returns all links" do
|
||||
link = link_fixture()
|
||||
assert Links.get_all() == [link]
|
||||
end
|
||||
|
||||
test "edit_one/1 with valid data returns the changeset" do
|
||||
link = link_fixture()
|
||||
assert %Ecto.Changeset{} = Links.edit_one(link)
|
||||
end
|
||||
|
||||
test "update_one/2 with valid data updates the link" do
|
||||
link = link_fixture()
|
||||
|
||||
assert {:ok, %Link{} = link} = Links.update_one(link, @update_attrs)
|
||||
assert link.name == "some updated link name"
|
||||
assert link.url == "https://gitlab.com/KKlochko/link_shortener2"
|
||||
assert link.shorten == "new_git_repo"
|
||||
end
|
||||
|
||||
test "update_link/2 with invalid data returns error changeset" do
|
||||
link = link_fixture()
|
||||
assert {:error, %Ecto.Changeset{}} = Links.update_one(link, @invalid_attrs)
|
||||
assert link == Links.get_one!(link.id)
|
||||
end
|
||||
|
||||
test "delete_one/1 deletes the link" do
|
||||
link = link_fixture()
|
||||
assert {:ok, %Link{}} = Links.delete_one(link)
|
||||
assert_raise Ecto.NoResultsError, fn -> Links.get_one!(link.id) end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,97 @@
|
||||
defmodule LinkShortenerWeb.Api.V1.LinkControllerTest do
|
||||
use LinkShortenerWeb.ConnCase
|
||||
|
||||
import LinkShortener.LinksFixtures
|
||||
|
||||
alias LinkShortener.Links.Link
|
||||
alias LinkShortener.Links
|
||||
|
||||
@create_attrs %{
|
||||
name: "some link name",
|
||||
url: "https://gitlab.com/KKlochko/link_shortener",
|
||||
shorten: "git_repo",
|
||||
}
|
||||
@update_attrs %{
|
||||
name: "some updated link name",
|
||||
url: "https://gitlab.com/KKlochko/link_shortener2",
|
||||
shorten: "new_git_repo",
|
||||
}
|
||||
@invalid_attrs %{
|
||||
name: nil,
|
||||
url: nil,
|
||||
shorten: nil,
|
||||
}
|
||||
|
||||
setup %{conn: conn} do
|
||||
{:ok, conn: put_req_header(conn, "accept", "application/json")}
|
||||
end
|
||||
|
||||
describe "index" do
|
||||
test "lists all links", %{conn: conn} do
|
||||
conn = get(conn, Routes.v1_link_path(conn, :index))
|
||||
assert json_response(conn, 200)["data"] == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "create link" do
|
||||
test "renders link when data is valid", %{conn: conn} do
|
||||
conn = post(conn, Routes.v1_link_path(conn, :create), link: @create_attrs)
|
||||
assert %{"id" => id} = json_response(conn, 201)["data"]
|
||||
|
||||
conn = get(conn, Routes.v1_link_path(conn, :show, id))
|
||||
|
||||
assert %{
|
||||
"id" => ^id,
|
||||
"name" => "some link name",
|
||||
"url" => "https://gitlab.com/KKlochko/link_shortener",
|
||||
"shorten" => "git_repo",
|
||||
} = json_response(conn, 200)["data"]
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn} do
|
||||
conn = post(conn, Routes.v1_link_path(conn, :create), link: @invalid_attrs)
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
end
|
||||
|
||||
describe "update link" do
|
||||
setup [:create_link]
|
||||
|
||||
test "renders link when data is valid", %{conn: conn, link: %Link{id: id} = link} do
|
||||
conn = put(conn, Routes.v1_link_path(conn, :update, link), link: @update_attrs)
|
||||
assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
||||
|
||||
conn = get(conn, Routes.v1_link_path(conn, :show, id))
|
||||
|
||||
assert %{
|
||||
"id" => ^id,
|
||||
"name" => "some updated link name",
|
||||
"url" => "https://gitlab.com/KKlochko/link_shortener2",
|
||||
"shorten" => "new_git_repo",
|
||||
} = json_response(conn, 200)["data"]
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn, link: link} do
|
||||
conn = put(conn, Routes.v1_link_path(conn, :update, link), link: @invalid_attrs)
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete link" do
|
||||
setup [:create_link]
|
||||
|
||||
test "deletes chosen link", %{conn: conn, link: link} do
|
||||
conn = delete(conn, Routes.v1_link_path(conn, :delete, link))
|
||||
assert response(conn, 204)
|
||||
|
||||
assert_error_sent 404, fn ->
|
||||
get(conn, Routes.v1_link_path(conn, :show, link))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp create_link(_) do
|
||||
link = link_fixture()
|
||||
%{link: link}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
defmodule LinkShortenerWeb.PageControllerTest do
|
||||
use LinkShortenerWeb.ConnCase
|
||||
|
||||
test "GET /", %{conn: conn} do
|
||||
conn = get(conn, "/")
|
||||
assert html_response(conn, 200) =~ "Usage"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,14 @@
|
||||
defmodule LinkShortenerWeb.ErrorViewTest do
|
||||
use LinkShortenerWeb.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(LinkShortenerWeb.ErrorView, "404.html", []) == "Not Found"
|
||||
end
|
||||
|
||||
test "renders 500.html" do
|
||||
assert render_to_string(LinkShortenerWeb.ErrorView, "500.html", []) == "Internal Server Error"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
defmodule LinkShortenerWeb.LayoutViewTest do
|
||||
use LinkShortenerWeb.ConnCase, async: true
|
||||
|
||||
# When testing helpers, you may want to import Phoenix.HTML and
|
||||
# use functions such as safe_to_string() to convert the helper
|
||||
# result into an HTML string.
|
||||
# import Phoenix.HTML
|
||||
end
|
||||
@@ -0,0 +1,3 @@
|
||||
defmodule LinkShortenerWeb.PageViewTest do
|
||||
use LinkShortenerWeb.ConnCase, async: true
|
||||
end
|
||||
@@ -0,0 +1,38 @@
|
||||
defmodule LinkShortenerWeb.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 LinkShortenerWeb.ConnCase, async: true`, although
|
||||
this option is not recommended for other databases.
|
||||
"""
|
||||
|
||||
use ExUnit.CaseTemplate
|
||||
|
||||
using do
|
||||
quote do
|
||||
# Import conveniences for testing with connections
|
||||
import Plug.Conn
|
||||
import Phoenix.ConnTest
|
||||
import LinkShortenerWeb.ConnCase
|
||||
|
||||
alias LinkShortenerWeb.Router.Helpers, as: Routes
|
||||
|
||||
# The default endpoint for testing
|
||||
@endpoint LinkShortenerWeb.Endpoint
|
||||
end
|
||||
end
|
||||
|
||||
setup tags do
|
||||
LinkShortener.DataCase.setup_sandbox(tags)
|
||||
{:ok, conn: Phoenix.ConnTest.build_conn()}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,58 @@
|
||||
defmodule LinkShortener.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 LinkShortener.DataCase, async: true`, although
|
||||
this option is not recommended for other databases.
|
||||
"""
|
||||
|
||||
use ExUnit.CaseTemplate
|
||||
|
||||
using do
|
||||
quote do
|
||||
alias LinkShortener.Repo
|
||||
|
||||
import Ecto
|
||||
import Ecto.Changeset
|
||||
import Ecto.Query
|
||||
import LinkShortener.DataCase
|
||||
end
|
||||
end
|
||||
|
||||
setup tags do
|
||||
LinkShortener.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!(LinkShortener.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
|
||||
@@ -0,0 +1,24 @@
|
||||
defmodule LinkShortener.LinksFixtures do
|
||||
@moduledoc """
|
||||
This module defines test helpers for creating
|
||||
entities via the `LinkShortener.Links` context.
|
||||
"""
|
||||
|
||||
alias LinkShortener.Links
|
||||
|
||||
@doc """
|
||||
Generate a link.
|
||||
"""
|
||||
def link_fixture(attrs \\ %{}) do
|
||||
{:ok, link} =
|
||||
attrs
|
||||
|> Enum.into(%{
|
||||
name: "some name",
|
||||
url: "https://gitlab.com/KKlochko/link_shortener",
|
||||
shorten: "api-article",
|
||||
})
|
||||
|> Links.create_one()
|
||||
|
||||
link
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,2 @@
|
||||
ExUnit.start()
|
||||
Ecto.Adapters.SQL.Sandbox.mode(LinkShortener.Repo, :manual)
|
||||
Reference in New Issue
Block a user