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

This commit is contained in:
2025-03-10 21:51:35 +02:00
parent 02d35ee056
commit e3da0dc129
3 changed files with 73 additions and 30 deletions
@@ -5,22 +5,14 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
describe "books and registries relationship" do describe "books and registries relationship" do
test "a new book get new registry by default" do test "a new book get new registry by default" do
{:ok, author} = Metadata.create_author("Author", "An description") author_roles = author_roles()
author_roles = [
%{order: 1, author_id: author.id, role: ""}
]
assert {:ok, book} = Metadata.create_book("Book", "1234567890", "An description", author_roles) assert {:ok, book} = Metadata.create_book("Book", "1234567890", "An description", author_roles)
assert book.book_editions_registry_id != nil assert book.book_editions_registry_id != nil
end end
test "a new book belongs to a registry if specified" do test "a new book belongs to a registry if specified" do
{:ok, author} = Metadata.create_author("Author", "An description") author_roles = author_roles()
author_roles = [
%{order: 1, author_id: author.id, role: ""}
]
assert {:ok, registry} = Metadata.create_book_editions_registry() assert {:ok, registry} = Metadata.create_book_editions_registry()
assert {:ok, book} = Metadata.create_book("Book", "1234567890", "An description", author_roles, registry.id) assert {:ok, book} = Metadata.create_book("Book", "1234567890", "An description", author_roles, registry.id)
@@ -28,11 +20,7 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
end end
test "a new book to a registry via a related book record" do test "a new book to a registry via a related book record" do
{:ok, author} = Metadata.create_author("Author", "An description") author_roles = author_roles()
author_roles = [
%{order: 1, author_id: author.id, role: ""}
]
{:ok, related_book} = Metadata.create_book("Book", "1234567890", "An description", author_roles) {:ok, related_book} = Metadata.create_book("Book", "1234567890", "An description", author_roles)
@@ -43,11 +31,7 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
end end
test "a new book to a registry via a empty related book record" do test "a new book to a registry via a empty related book record" do
{:ok, author} = Metadata.create_author("Author", "An description") author_roles = author_roles()
author_roles = [
%{order: 1, author_id: author.id, role: ""}
]
assert {:error, _} = assert {:error, _} =
Metadata.add_book_to_related_editions_registry("Book2", "1234567891", "An description2", author_roles, nil) 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 describe "books alternatives names" do
test "new book has no alternatives names" do test "new book has no alternatives names" do
{:ok, author} = Metadata.create_author("Author", "An description") author_roles = author_roles()
author_roles = [
%{order: 1, author_id: author.id, role: ""}
]
{:ok, book} = Metadata.create_book("Book", "1234567890", "An description", author_roles) {:ok, book} = Metadata.create_book("Book", "1234567890", "An description", author_roles)
assert {:ok, alternatives_names} = Metadata.get_book_alternative_editions(book) assert {:ok, alternatives_names} = Metadata.get_book_alternative_editions(book)
@@ -68,11 +48,7 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
end end
test "book has related book so they has one alternative name" do test "book has related book so they has one alternative name" do
{:ok, author} = Metadata.create_author("Author", "An description") author_roles = author_roles()
author_roles = [
%{order: 1, author_id: author.id, role: ""}
]
{:ok, related_book} = Metadata.create_book("Book", "1234567890", "An description", author_roles) {:ok, related_book} = Metadata.create_book("Book", "1234567890", "An description", author_roles)
+1
View File
@@ -24,6 +24,7 @@ defmodule DecentralisedBookIndex.DataCase do
import Ecto.Changeset import Ecto.Changeset
import Ecto.Query import Ecto.Query
import DecentralisedBookIndex.DataCase import DecentralisedBookIndex.DataCase
import DecentralisedBookIndex.Generators
end end
end end
+66
View File
@@ -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