context: user_links

This commit is contained in:
Liam P. White 2019-08-28 18:33:13 -04:00
parent 07310765ba
commit cddb4d3ae3
3 changed files with 182 additions and 0 deletions

View file

@ -293,4 +293,100 @@ defmodule Philomena.Users do
def change_fingerprints(%Fingerprint{} = fingerprints) do
Fingerprint.changeset(fingerprints, %{})
end
alias Philomena.Users.Link
@doc """
Returns the list of user_links.
## Examples
iex> list_user_links()
[%Link{}, ...]
"""
def list_user_links do
Repo.all(Link)
end
@doc """
Gets a single link.
Raises `Ecto.NoResultsError` if the Link does not exist.
## Examples
iex> get_link!(123)
%Link{}
iex> get_link!(456)
** (Ecto.NoResultsError)
"""
def get_link!(id), do: Repo.get!(Link, id)
@doc """
Creates a link.
## Examples
iex> create_link(%{field: value})
{:ok, %Link{}}
iex> create_link(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_link(attrs \\ %{}) do
%Link{}
|> Link.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a link.
## Examples
iex> update_link(link, %{field: new_value})
{:ok, %Link{}}
iex> update_link(link, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_link(%Link{} = link, attrs) do
link
|> Link.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Link.
## Examples
iex> delete_link(link)
{:ok, %Link{}}
iex> delete_link(link)
{:error, %Ecto.Changeset{}}
"""
def delete_link(%Link{} = link) do
Repo.delete(link)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking link changes.
## Examples
iex> change_link(link)
%Ecto.Changeset{source: %Link{}}
"""
def change_link(%Link{} = link) do
Link.changeset(link, %{})
end
end

View file

@ -0,0 +1,29 @@
defmodule Philomena.Users.Link do
use Ecto.Schema
import Ecto.Changeset
schema "user_links" do
belongs_to :user, Philomena.Users.User
belongs_to :verified_by_user, Philomena.Users.User
belongs_to :contacted_by_user, Philomena.Users.User
belongs_to :tag, Philomena.Tags.Tag
field :aasm_state, :string
field :uri, :string
field :hostname, :string
field :path, :string
field :verification_code, :string
field :public, :boolean, default: true
field :next_check_at, :naive_datetime
field :contacted_at, :naive_datetime
timestamps(inserted_at: :created_at)
end
@doc false
def changeset(link, attrs) do
link
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -116,4 +116,61 @@ defmodule Philomena.UsersTest do
assert %Ecto.Changeset{} = Users.change_fingerprints(fingerprints)
end
end
describe "user_links" do
alias Philomena.Users.Link
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def link_fixture(attrs \\ %{}) do
{:ok, link} =
attrs
|> Enum.into(@valid_attrs)
|> Users.create_link()
link
end
test "list_user_links/0 returns all user_links" do
link = link_fixture()
assert Users.list_user_links() == [link]
end
test "get_link!/1 returns the link with given id" do
link = link_fixture()
assert Users.get_link!(link.id) == link
end
test "create_link/1 with valid data creates a link" do
assert {:ok, %Link{} = link} = Users.create_link(@valid_attrs)
end
test "create_link/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Users.create_link(@invalid_attrs)
end
test "update_link/2 with valid data updates the link" do
link = link_fixture()
assert {:ok, %Link{} = link} = Users.update_link(link, @update_attrs)
end
test "update_link/2 with invalid data returns error changeset" do
link = link_fixture()
assert {:error, %Ecto.Changeset{}} = Users.update_link(link, @invalid_attrs)
assert link == Users.get_link!(link.id)
end
test "delete_link/1 deletes the link" do
link = link_fixture()
assert {:ok, %Link{}} = Users.delete_link(link)
assert_raise Ecto.NoResultsError, fn -> Users.get_link!(link.id) end
end
test "change_link/1 returns a link changeset" do
link = link_fixture()
assert %Ecto.Changeset{} = Users.change_link(link)
end
end
end