context: user_name_changes, user_statistics, user_whitelists

This commit is contained in:
Liam P. White 2019-08-28 18:36:38 -04:00
parent cddb4d3ae3
commit 0906248922
5 changed files with 515 additions and 0 deletions

View file

@ -389,4 +389,292 @@ defmodule Philomena.Users do
def change_link(%Link{} = link) do
Link.changeset(link, %{})
end
alias Philomena.Users.NameChange
@doc """
Returns the list of user_name_changes.
## Examples
iex> list_user_name_changes()
[%NameChange{}, ...]
"""
def list_user_name_changes do
Repo.all(NameChange)
end
@doc """
Gets a single name_change.
Raises `Ecto.NoResultsError` if the Name change does not exist.
## Examples
iex> get_name_change!(123)
%NameChange{}
iex> get_name_change!(456)
** (Ecto.NoResultsError)
"""
def get_name_change!(id), do: Repo.get!(NameChange, id)
@doc """
Creates a name_change.
## Examples
iex> create_name_change(%{field: value})
{:ok, %NameChange{}}
iex> create_name_change(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_name_change(attrs \\ %{}) do
%NameChange{}
|> NameChange.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a name_change.
## Examples
iex> update_name_change(name_change, %{field: new_value})
{:ok, %NameChange{}}
iex> update_name_change(name_change, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_name_change(%NameChange{} = name_change, attrs) do
name_change
|> NameChange.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a NameChange.
## Examples
iex> delete_name_change(name_change)
{:ok, %NameChange{}}
iex> delete_name_change(name_change)
{:error, %Ecto.Changeset{}}
"""
def delete_name_change(%NameChange{} = name_change) do
Repo.delete(name_change)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking name_change changes.
## Examples
iex> change_name_change(name_change)
%Ecto.Changeset{source: %NameChange{}}
"""
def change_name_change(%NameChange{} = name_change) do
NameChange.changeset(name_change, %{})
end
alias Philomena.Users.Statistic
@doc """
Returns the list of user_statistics.
## Examples
iex> list_user_statistics()
[%Statistic{}, ...]
"""
def list_user_statistics do
Repo.all(Statistic)
end
@doc """
Gets a single statistic.
Raises `Ecto.NoResultsError` if the Statistic does not exist.
## Examples
iex> get_statistic!(123)
%Statistic{}
iex> get_statistic!(456)
** (Ecto.NoResultsError)
"""
def get_statistic!(id), do: Repo.get!(Statistic, id)
@doc """
Creates a statistic.
## Examples
iex> create_statistic(%{field: value})
{:ok, %Statistic{}}
iex> create_statistic(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_statistic(attrs \\ %{}) do
%Statistic{}
|> Statistic.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a statistic.
## Examples
iex> update_statistic(statistic, %{field: new_value})
{:ok, %Statistic{}}
iex> update_statistic(statistic, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_statistic(%Statistic{} = statistic, attrs) do
statistic
|> Statistic.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Statistic.
## Examples
iex> delete_statistic(statistic)
{:ok, %Statistic{}}
iex> delete_statistic(statistic)
{:error, %Ecto.Changeset{}}
"""
def delete_statistic(%Statistic{} = statistic) do
Repo.delete(statistic)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking statistic changes.
## Examples
iex> change_statistic(statistic)
%Ecto.Changeset{source: %Statistic{}}
"""
def change_statistic(%Statistic{} = statistic) do
Statistic.changeset(statistic, %{})
end
alias Philomena.Users.Whitelist
@doc """
Returns the list of user_whitelists.
## Examples
iex> list_user_whitelists()
[%Whitelist{}, ...]
"""
def list_user_whitelists do
Repo.all(Whitelist)
end
@doc """
Gets a single whitelist.
Raises `Ecto.NoResultsError` if the Whitelist does not exist.
## Examples
iex> get_whitelist!(123)
%Whitelist{}
iex> get_whitelist!(456)
** (Ecto.NoResultsError)
"""
def get_whitelist!(id), do: Repo.get!(Whitelist, id)
@doc """
Creates a whitelist.
## Examples
iex> create_whitelist(%{field: value})
{:ok, %Whitelist{}}
iex> create_whitelist(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_whitelist(attrs \\ %{}) do
%Whitelist{}
|> Whitelist.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a whitelist.
## Examples
iex> update_whitelist(whitelist, %{field: new_value})
{:ok, %Whitelist{}}
iex> update_whitelist(whitelist, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_whitelist(%Whitelist{} = whitelist, attrs) do
whitelist
|> Whitelist.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Whitelist.
## Examples
iex> delete_whitelist(whitelist)
{:ok, %Whitelist{}}
iex> delete_whitelist(whitelist)
{:error, %Ecto.Changeset{}}
"""
def delete_whitelist(%Whitelist{} = whitelist) do
Repo.delete(whitelist)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking whitelist changes.
## Examples
iex> change_whitelist(whitelist)
%Ecto.Changeset{source: %Whitelist{}}
"""
def change_whitelist(%Whitelist{} = whitelist) do
Whitelist.changeset(whitelist, %{})
end
end

View file

@ -0,0 +1,18 @@
defmodule Philomena.Users.NameChange do
use Ecto.Schema
import Ecto.Changeset
schema "user_name_changes" do
belongs_to :user, Philomena.Users.User
field :name, :string
timestamps(inserted_at: :created_at)
end
@doc false
def changeset(name_change, attrs) do
name_change
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -0,0 +1,22 @@
defmodule Philomena.Users.Statistic do
use Ecto.Schema
import Ecto.Changeset
schema "user_statistics" do
belongs_to :user, Philomena.Users.User
field :day, :integer, default: 0
field :uploads, :integer, default: 0
field :votes_cast, :integer, default: 0
field :comments_posted, :integer, default: 0
field :metadata_updates, :integer, default: 0
field :images_favourited, :integer, default: 0
field :forum_posts, :integer, default: 0
end
@doc false
def changeset(statistic, attrs) do
statistic
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -0,0 +1,16 @@
defmodule Philomena.Users.Whitelist do
use Ecto.Schema
import Ecto.Changeset
schema "user_whitelists" do
field :reason, :string
timestamps(inserted_at: :created_at)
end
@doc false
def changeset(whitelist, attrs) do
whitelist
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -173,4 +173,175 @@ defmodule Philomena.UsersTest do
assert %Ecto.Changeset{} = Users.change_link(link)
end
end
describe "user_name_changes" do
alias Philomena.Users.NameChange
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def name_change_fixture(attrs \\ %{}) do
{:ok, name_change} =
attrs
|> Enum.into(@valid_attrs)
|> Users.create_name_change()
name_change
end
test "list_user_name_changes/0 returns all user_name_changes" do
name_change = name_change_fixture()
assert Users.list_user_name_changes() == [name_change]
end
test "get_name_change!/1 returns the name_change with given id" do
name_change = name_change_fixture()
assert Users.get_name_change!(name_change.id) == name_change
end
test "create_name_change/1 with valid data creates a name_change" do
assert {:ok, %NameChange{} = name_change} = Users.create_name_change(@valid_attrs)
end
test "create_name_change/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Users.create_name_change(@invalid_attrs)
end
test "update_name_change/2 with valid data updates the name_change" do
name_change = name_change_fixture()
assert {:ok, %NameChange{} = name_change} = Users.update_name_change(name_change, @update_attrs)
end
test "update_name_change/2 with invalid data returns error changeset" do
name_change = name_change_fixture()
assert {:error, %Ecto.Changeset{}} = Users.update_name_change(name_change, @invalid_attrs)
assert name_change == Users.get_name_change!(name_change.id)
end
test "delete_name_change/1 deletes the name_change" do
name_change = name_change_fixture()
assert {:ok, %NameChange{}} = Users.delete_name_change(name_change)
assert_raise Ecto.NoResultsError, fn -> Users.get_name_change!(name_change.id) end
end
test "change_name_change/1 returns a name_change changeset" do
name_change = name_change_fixture()
assert %Ecto.Changeset{} = Users.change_name_change(name_change)
end
end
describe "user_statistics" do
alias Philomena.Users.Statistic
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def statistic_fixture(attrs \\ %{}) do
{:ok, statistic} =
attrs
|> Enum.into(@valid_attrs)
|> Users.create_statistic()
statistic
end
test "list_user_statistics/0 returns all user_statistics" do
statistic = statistic_fixture()
assert Users.list_user_statistics() == [statistic]
end
test "get_statistic!/1 returns the statistic with given id" do
statistic = statistic_fixture()
assert Users.get_statistic!(statistic.id) == statistic
end
test "create_statistic/1 with valid data creates a statistic" do
assert {:ok, %Statistic{} = statistic} = Users.create_statistic(@valid_attrs)
end
test "create_statistic/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Users.create_statistic(@invalid_attrs)
end
test "update_statistic/2 with valid data updates the statistic" do
statistic = statistic_fixture()
assert {:ok, %Statistic{} = statistic} = Users.update_statistic(statistic, @update_attrs)
end
test "update_statistic/2 with invalid data returns error changeset" do
statistic = statistic_fixture()
assert {:error, %Ecto.Changeset{}} = Users.update_statistic(statistic, @invalid_attrs)
assert statistic == Users.get_statistic!(statistic.id)
end
test "delete_statistic/1 deletes the statistic" do
statistic = statistic_fixture()
assert {:ok, %Statistic{}} = Users.delete_statistic(statistic)
assert_raise Ecto.NoResultsError, fn -> Users.get_statistic!(statistic.id) end
end
test "change_statistic/1 returns a statistic changeset" do
statistic = statistic_fixture()
assert %Ecto.Changeset{} = Users.change_statistic(statistic)
end
end
describe "user_whitelists" do
alias Philomena.Users.Whitelist
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def whitelist_fixture(attrs \\ %{}) do
{:ok, whitelist} =
attrs
|> Enum.into(@valid_attrs)
|> Users.create_whitelist()
whitelist
end
test "list_user_whitelists/0 returns all user_whitelists" do
whitelist = whitelist_fixture()
assert Users.list_user_whitelists() == [whitelist]
end
test "get_whitelist!/1 returns the whitelist with given id" do
whitelist = whitelist_fixture()
assert Users.get_whitelist!(whitelist.id) == whitelist
end
test "create_whitelist/1 with valid data creates a whitelist" do
assert {:ok, %Whitelist{} = whitelist} = Users.create_whitelist(@valid_attrs)
end
test "create_whitelist/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Users.create_whitelist(@invalid_attrs)
end
test "update_whitelist/2 with valid data updates the whitelist" do
whitelist = whitelist_fixture()
assert {:ok, %Whitelist{} = whitelist} = Users.update_whitelist(whitelist, @update_attrs)
end
test "update_whitelist/2 with invalid data returns error changeset" do
whitelist = whitelist_fixture()
assert {:error, %Ecto.Changeset{}} = Users.update_whitelist(whitelist, @invalid_attrs)
assert whitelist == Users.get_whitelist!(whitelist.id)
end
test "delete_whitelist/1 deletes the whitelist" do
whitelist = whitelist_fixture()
assert {:ok, %Whitelist{}} = Users.delete_whitelist(whitelist)
assert_raise Ecto.NoResultsError, fn -> Users.get_whitelist!(whitelist.id) end
end
test "change_whitelist/1 returns a whitelist changeset" do
whitelist = whitelist_fixture()
assert %Ecto.Changeset{} = Users.change_whitelist(whitelist)
end
end
end