Add generators for user, author and author_roles for tests.
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build was killed Details

dev
KKlochko 4 months ago
parent 02d35ee056
commit e3da0dc129

@ -5,22 +5,14 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
describe "books and registries relationship" do
test "a new book get new registry by default" do
{:ok, author} = Metadata.create_author("Author", "An description")
author_roles = [
%{order: 1, author_id: author.id, role: ""}
]
author_roles = author_roles()
assert {:ok, book} = Metadata.create_book("Book", "1234567890", "An description", author_roles)
assert book.book_editions_registry_id != nil
end
test "a new book belongs to a registry if specified" do
{:ok, author} = Metadata.create_author("Author", "An description")
author_roles = [
%{order: 1, author_id: author.id, role: ""}
]
author_roles = author_roles()
assert {:ok, registry} = Metadata.create_book_editions_registry()
assert {:ok, book} = Metadata.create_book("Book", "1234567890", "An description", author_roles, registry.id)
@ -28,11 +20,7 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
end
test "a new book to a registry via a related book record" do
{:ok, author} = Metadata.create_author("Author", "An description")
author_roles = [
%{order: 1, author_id: author.id, role: ""}
]
author_roles = author_roles()
{:ok, related_book} = Metadata.create_book("Book", "1234567890", "An description", author_roles)
@ -43,11 +31,7 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
end
test "a new book to a registry via a empty related book record" do
{:ok, author} = Metadata.create_author("Author", "An description")
author_roles = [
%{order: 1, author_id: author.id, role: ""}
]
author_roles = author_roles()
assert {:error, _} =
Metadata.add_book_to_related_editions_registry("Book2", "1234567891", "An description2", author_roles, nil)
@ -56,11 +40,7 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
describe "books alternatives names" do
test "new book has no alternatives names" do
{:ok, author} = Metadata.create_author("Author", "An description")
author_roles = [
%{order: 1, author_id: author.id, role: ""}
]
author_roles = author_roles()
{:ok, book} = Metadata.create_book("Book", "1234567890", "An description", author_roles)
assert {:ok, alternatives_names} = Metadata.get_book_alternative_editions(book)
@ -68,11 +48,7 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
end
test "book has related book so they has one alternative name" do
{:ok, author} = Metadata.create_author("Author", "An description")
author_roles = [
%{order: 1, author_id: author.id, role: ""}
]
author_roles = author_roles()
{:ok, related_book} = Metadata.create_book("Book", "1234567890", "An description", author_roles)

@ -24,6 +24,7 @@ defmodule DecentralisedBookIndex.DataCase do
import Ecto.Changeset
import Ecto.Query
import DecentralisedBookIndex.DataCase
import DecentralisedBookIndex.Generators
end
end

@ -0,0 +1,66 @@
defmodule DecentralisedBookIndex.Generators do
@moduledoc "Generators for tests"
use Ash.Generator
alias DecentralisedBookIndex.Metadata
@doc """
Generates user changesets with the `:register_with_password` action.
"""
def user(opts \\ []) do
changeset_generator(
DecentralisedBookIndex.Accounts.User,
:register_with_password,
defaults: [
# Generates unique values using an auto-incrementing sequence
# eg. `user1@example.com`, `user2@example.com`, etc.
email: sequence(:user_email, &"user#{&1}@example.com"),
password: "password",
password_confirmation: "password"
],
overrides: opts,
authorize?: false
)
end
def author(opts \\ []) do
actor =
opts[:actor] ||
once(:default_actor, fn ->
generate(user())
end)
changeset_generator(
Metadata.Author,
:create,
defaults: [
name: sequence(:name, &"Author #{&1}"),
description: sequence(:name, &"Description #{&1}"),
author_alias_registry_id: nil
],
overrides: opts,
actor: actor
)
end
@doc """
Generates a list of author roles.
Example:
> author_roles = author_roles()
"""
def author_roles(opts \\ []) do
actor =
opts[:actor] ||
once(:default_actor, fn ->
generate(user())
end)
count =
opts[:count] || 2
for order <- 1..count do
%{order: order, author_id: generate(author(actor: actor)).id, role: ""}
end
end
end
Loading…
Cancel
Save