context: users_roles

This commit is contained in:
Liam P. White 2019-08-28 18:38:17 -04:00
parent 0906248922
commit 37ac0e03b7
3 changed files with 171 additions and 0 deletions

View file

@ -677,4 +677,100 @@ defmodule Philomena.Users do
def change_whitelist(%Whitelist{} = whitelist) do def change_whitelist(%Whitelist{} = whitelist) do
Whitelist.changeset(whitelist, %{}) Whitelist.changeset(whitelist, %{})
end end
alias Philomena.Users.Role
@doc """
Returns the list of users_roles.
## Examples
iex> list_users_roles()
[%Role{}, ...]
"""
def list_users_roles do
Repo.all(Role)
end
@doc """
Gets a single role.
Raises `Ecto.NoResultsError` if the Role does not exist.
## Examples
iex> get_role!(123)
%Role{}
iex> get_role!(456)
** (Ecto.NoResultsError)
"""
def get_role!(id), do: Repo.get!(Role, id)
@doc """
Creates a role.
## Examples
iex> create_role(%{field: value})
{:ok, %Role{}}
iex> create_role(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_role(attrs \\ %{}) do
%Role{}
|> Role.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a role.
## Examples
iex> update_role(role, %{field: new_value})
{:ok, %Role{}}
iex> update_role(role, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_role(%Role{} = role, attrs) do
role
|> Role.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Role.
## Examples
iex> delete_role(role)
{:ok, %Role{}}
iex> delete_role(role)
{:error, %Ecto.Changeset{}}
"""
def delete_role(%Role{} = role) do
Repo.delete(role)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking role changes.
## Examples
iex> change_role(role)
%Ecto.Changeset{source: %Role{}}
"""
def change_role(%Role{} = role) do
Role.changeset(role, %{})
end
end end

View file

@ -0,0 +1,18 @@
defmodule Philomena.Users.Role do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
schema "users_roles" do
belongs_to :user, Philomena.Users.User, primary_key: true
belongs_to :role, Philomena.Roles.Role, primary_key: true
end
@doc false
def changeset(role, attrs) do
role
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -344,4 +344,61 @@ defmodule Philomena.UsersTest do
assert %Ecto.Changeset{} = Users.change_whitelist(whitelist) assert %Ecto.Changeset{} = Users.change_whitelist(whitelist)
end end
end end
describe "users_roles" do
alias Philomena.Users.Role
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def role_fixture(attrs \\ %{}) do
{:ok, role} =
attrs
|> Enum.into(@valid_attrs)
|> Users.create_role()
role
end
test "list_users_roles/0 returns all users_roles" do
role = role_fixture()
assert Users.list_users_roles() == [role]
end
test "get_role!/1 returns the role with given id" do
role = role_fixture()
assert Users.get_role!(role.id) == role
end
test "create_role/1 with valid data creates a role" do
assert {:ok, %Role{} = role} = Users.create_role(@valid_attrs)
end
test "create_role/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Users.create_role(@invalid_attrs)
end
test "update_role/2 with valid data updates the role" do
role = role_fixture()
assert {:ok, %Role{} = role} = Users.update_role(role, @update_attrs)
end
test "update_role/2 with invalid data returns error changeset" do
role = role_fixture()
assert {:error, %Ecto.Changeset{}} = Users.update_role(role, @invalid_attrs)
assert role == Users.get_role!(role.id)
end
test "delete_role/1 deletes the role" do
role = role_fixture()
assert {:ok, %Role{}} = Users.delete_role(role)
assert_raise Ecto.NoResultsError, fn -> Users.get_role!(role.id) end
end
test "change_role/1 returns a role changeset" do
role = role_fixture()
assert %Ecto.Changeset{} = Users.change_role(role)
end
end
end end