remove doctests

This commit is contained in:
shibao 2023-02-25 00:48:06 -05:00
parent 4e9f66f006
commit 07b31fcc86
2 changed files with 62 additions and 112 deletions

View File

@ -16,9 +16,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> register_user(%{email: "foo@example.com", password: "valid_password"}) iex> get_user_by_email("foo@example.com")
iex> with %User{} <- get_user_by_email("foo@example.com"), do: :passed %User{}
:passed
iex> get_user_by_email("unknown@example.com") iex> get_user_by_email("unknown@example.com")
nil nil
@ -34,9 +33,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> register_user(%{email: "foo@example.com", password: "valid_password"}) iex> get_user_by_email_and_password("foo@example.com", "valid_password")
iex> with %User{} <- get_user_by_email_and_password("foo@example.com", "valid_password"), do: :passed %User{}
:passed
iex> get_user_by_email_and_password("foo@example.com", "invalid_password") iex> get_user_by_email_and_password("foo@example.com", "invalid_password")
nil nil
@ -57,15 +55,14 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> get_user!(user_id)
iex> get_user!(user.id)
user user
> get_user!() iex> get_user!()
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
@spec get_user!(User.t()) :: User.t() @spec get_user!(User.id()) :: User.t()
def get_user!(id) do def get_user!(id) do
Repo.get!(User, id) Repo.get!(User, id)
end end
@ -75,10 +72,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, user1} = register_user(%{email: "foo1@example.com", password: "valid_password"}) iex> list_all_users_by_role(user1)
iex> {:ok, user2} = register_user(%{email: "foo2@example.com", password: "valid_password"}) %{admin: [%User{role: :admin}], user: [%User{role: :user}]}
iex> with %{admin: [^user1], user: [^user2]} <- list_all_users_by_role(user1), do: :passed
:passed
""" """
@spec list_all_users_by_role(User.t()) :: %{User.role() => [User.t()]} @spec list_all_users_by_role(User.t()) :: %{User.role() => [User.t()]}
@ -91,9 +86,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> list_users_by_role(:admin)
iex> with [^user] <- list_users_by_role(:admin), do: :passed [%User{role: :admin}]
:passed
""" """
@spec list_users_by_role(:admin) :: [User.t()] @spec list_users_by_role(:admin) :: [User.t()]
@ -108,13 +102,11 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> with {:ok, %User{email: "foo@example.com"}} <- iex> register_user(%{email: "foo@example.com", password: "valid_password"})
...> register_user(%{email: "foo@example.com", password: "valid_password"}), {:ok, %User{email: "foo@example.com"}}
...> do: :passed
:passed
iex> with {:error, %Changeset{}} <- register_user(%{email: "foo@example"}), do: :passed iex> register_user(%{email: "foo@example"})
:passed {:error, %Changeset{}}
""" """
@spec register_user(attrs :: map()) :: @spec register_user(attrs :: map()) ::
@ -149,11 +141,11 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> with %Changeset{} <- change_user_registration(), do: :passed iex> change_user_registration()
:passed %Changeset{}
iex> with %Changeset{} <- change_user_registration(%{password: "hi"}), do: :passed iex> change_user_registration(%{password: "hi"}
:passed %Changeset{}
""" """
@spec change_user_registration() :: User.changeset() @spec change_user_registration() :: User.changeset()
@ -169,8 +161,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> with %Changeset{} <- change_user_email(%User{email: "foo@example.com"}), do: :passed iex> change_user_email(%User{email: "foo@example.com"})
:passed %Changeset{}
""" """
@spec change_user_email(User.t()) :: User.changeset() @spec change_user_email(User.t()) :: User.changeset()
@ -184,8 +176,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> with %Changeset{} <- change_user_role(%User{}, :user), do: :passed iex> change_user_role(%User{}, :user)
:passed %Changeset{}
""" """
@spec change_user_role(User.t(), User.role()) :: User.changeset() @spec change_user_role(User.t(), User.role()) :: User.changeset()
@ -199,17 +191,11 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> apply_user_email(user, "valid_password", %{email: "new_email@account.com"})
iex> with {:ok, %User{}} <- {:ok, %User{}}
...> apply_user_email(user, "valid_password", %{email: "new_email@account.com"}),
...> do: :passed
:passed
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> apply_user_email(user, "invalid password", %{email: "new_email@account"})
iex> with {:error, %Changeset{}} <- {:error, %Changeset{}}
...> apply_user_email(user, "invalid password", %{email: "new_email@account"}),
...> do: :passed
:passed
""" """
@spec apply_user_email(User.t(), email :: String.t(), attrs :: map()) :: @spec apply_user_email(User.t(), email :: String.t(), attrs :: map()) ::
@ -254,12 +240,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> deliver_update_email_instructions(user, "new_foo@example.com", fn _token -> "example url" end)
iex> with %Oban.Job{ %Oban.Job{args: %{email: :update_email, user_id: ^user_id, attrs: %{url: "example url"}}}
...> args: %{email: :update_email, user_id: ^user_id, attrs: %{url: "example url"}}
...> } <- deliver_update_email_instructions(user, "new_foo@example.com", fn _token -> "example url" end),
...> do: :passed
:passed
""" """
@spec deliver_update_email_instructions(User.t(), current_email :: String.t(), function) :: @spec deliver_update_email_instructions(User.t(), current_email :: String.t(), function) ::
@ -276,8 +258,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> with %Changeset{} <- change_user_password(%User{}), do: :passed iex> change_user_password(%User{})
:passed %Changeset{}
""" """
@spec change_user_password(User.t(), attrs :: map()) :: User.changeset() @spec change_user_password(User.t(), attrs :: map()) :: User.changeset()
@ -290,20 +272,14 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> reset_user_password(user, %{
iex> with {:ok, %User{}} <-
...> reset_user_password(user, %{
...> password: "new password", ...> password: "new password",
...> password_confirmation: "new password" ...> password_confirmation: "new password"
...> }), ...> })
...> do: :passed {:ok, %User{}}
:passed
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> update_user_password(user, "invalid password", %{password: "123"})
iex> with {:error, %Changeset{}} <- {:error, %Changeset{}}
...> update_user_password(user, "invalid password", %{password: "123"}),
...> do: :passed
:passed
""" """
@spec update_user_password(User.t(), String.t(), attrs :: map()) :: @spec update_user_password(User.t(), String.t(), attrs :: map()) ::
@ -329,8 +305,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> with %Changeset{} <- change_user_locale(%User{}), do: :passed iex> change_user_locale(%User{})
:passed %Changeset{}
""" """
@spec change_user_locale(User.t()) :: User.changeset() @spec change_user_locale(User.t()) :: User.changeset()
@ -343,9 +319,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> update_user_locale(user, "en_US")
iex> with {:ok, %User{}} <- update_user_locale(user, "en_US"), do: :passed {:ok, %User{}}
:passed
""" """
@spec update_user_locale(User.t(), locale :: String.t()) :: @spec update_user_locale(User.t(), locale :: String.t()) ::
@ -359,13 +334,11 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> delete_user!(user, %User{id: 123, role: :admin})
iex> with %User{} <- delete_user!(user, %User{id: 123, role: :admin}), do: :passed %User{}
:passed
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> delete_user!(user, user)
iex> with %User{} <- delete_user!(user, user), do: :passed %User{}
:passed
""" """
@spec delete_user!(user_to_delete :: User.t(), User.t()) :: User.t() @spec delete_user!(user_to_delete :: User.t(), User.t()) :: User.t()
@ -421,11 +394,10 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> is_admin?(%User{role: :admin})
iex> is_admin?(user)
true true
iex> is_admin?(%User{id: Ecto.UUID.generate()}) iex> is_admin?(%User{})
false false
""" """
@ -439,8 +411,7 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> is_already_admin?(%User{role: :admin})
iex> is_already_admin?(user)
true true
iex> is_already_admin?(%User{}) iex> is_already_admin?(%User{})
@ -458,15 +429,9 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> deliver_user_confirmation_instructions(user, fn _token -> "example url" end)
iex> with %Oban.Job{ %Oban.Job{args: %{email: :welcome, user_id: ^user_id, attrs: %{url: "example url"}}}
...> args: %{email: :welcome, user_id: ^user_id, attrs: %{url: "example url"}}
...> } <- deliver_user_confirmation_instructions(user, fn _token -> "example url" end),
...> do: :passed
:passed
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> user = user |> User.confirm_changeset() |> Repo.update!()
iex> deliver_user_confirmation_instructions(user, fn _token -> "example url" end) iex> deliver_user_confirmation_instructions(user, fn _token -> "example url" end)
{:error, :already_confirmed} {:error, :already_confirmed}
@ -514,12 +479,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> deliver_user_reset_password_instructions(user, fn _token -> "example url" end)
iex> with %Oban.Job{args: %{ %Oban.Job{args: %{email: :reset_password, user_id: ^user_id, attrs: %{url: "example url"}}}
...> email: :reset_password, user_id: ^user_id, attrs: %{url: "example url"}}
...> } <- deliver_user_reset_password_instructions(user, fn _token -> "example url" end),
...> do: :passed
:passed
""" """
@spec deliver_user_reset_password_instructions(User.t(), function()) :: Job.t() @spec deliver_user_reset_password_instructions(User.t(), function()) :: Job.t()
@ -535,11 +496,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> get_user_by_reset_password_token(encoded_token)
iex> {encoded_token, user_token} = UserToken.build_email_token(user, "reset_password") %User{}
iex> Repo.insert!(user_token)
iex> with %User{} <- get_user_by_reset_password_token(encoded_token), do: :passed
:passed
iex> get_user_by_reset_password_token("invalidtoken") iex> get_user_by_reset_password_token("invalidtoken")
nil nil
@ -560,20 +518,14 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> reset_user_password(user, %{
iex> with {:ok, %User{}} <-
...> reset_user_password(user, %{
...> password: "new password", ...> password: "new password",
...> password_confirmation: "new password" ...> password_confirmation: "new password"
...> }), ...> })
...> do: :passed {:ok, %User{}}
:passed
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"}) iex> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"})
iex> with {:error, %Changeset{}} <- {:error, %Changeset{}}
...> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"}),
...> do: :passed
:passed
""" """
@spec reset_user_password(User.t(), attrs :: map()) :: @spec reset_user_password(User.t(), attrs :: map()) ::

View File

@ -10,8 +10,6 @@ defmodule Cannery.AccountsTest do
@moduletag :accounts_test @moduletag :accounts_test
doctest Accounts, import: true
describe "get_user_by_email/1" do describe "get_user_by_email/1" do
test "does not return the user if the email does not exist" do test "does not return the user if the email does not exist" do
refute Accounts.get_user_by_email("unknown@example.com") refute Accounts.get_user_by_email("unknown@example.com")