You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
311 lines
8.2 KiB
311 lines
8.2 KiB
defmodule DecentralisedBookIndex.Metadata.BookTest do
|
|
use DecentralisedBookIndex.DataCase, async: true
|
|
|
|
alias DecentralisedBookIndex.Metadata
|
|
|
|
alias DecentralisedBookIndex.TestEndpoints
|
|
@test_server_endpoint TestEndpoints.test_api_endpoint()
|
|
|
|
setup do
|
|
user = generate(user(role: :moderator))
|
|
%{user: user}
|
|
end
|
|
|
|
describe "books and registries relationship" do
|
|
test "a new book get new registry by default", %{user: user} do
|
|
author_roles = author_roles()
|
|
bids = bids()
|
|
publisher = generate(publisher())
|
|
|
|
assert {:ok, book} =
|
|
Metadata.create_book(
|
|
"Book",
|
|
"An description",
|
|
"English",
|
|
"Paperback",
|
|
256,
|
|
~D[2025-03-04],
|
|
bids,
|
|
author_roles,
|
|
publisher.id,
|
|
nil,
|
|
nil,
|
|
actor: user
|
|
)
|
|
|
|
assert book.book_editions_registry_id != nil
|
|
end
|
|
|
|
test "a new book belongs to a registry if specified", %{user: user} do
|
|
author_roles = author_roles()
|
|
bids = bids()
|
|
publisher = generate(publisher())
|
|
|
|
assert {:ok, registry} = Metadata.create_book_editions_registry(actor: user)
|
|
|
|
assert {:ok, book} =
|
|
Metadata.create_book(
|
|
"Book",
|
|
"An description",
|
|
"English",
|
|
"Paperback",
|
|
256,
|
|
~D[2025-03-04],
|
|
bids,
|
|
author_roles,
|
|
publisher.id,
|
|
nil,
|
|
registry.id,
|
|
actor: user
|
|
)
|
|
|
|
assert book.book_editions_registry_id == registry.id
|
|
end
|
|
|
|
test "a new book to a registry via a related book record", %{user: user} do
|
|
author_roles = author_roles()
|
|
bids = bids()
|
|
publisher = generate(publisher())
|
|
|
|
{:ok, related_book} =
|
|
Metadata.create_book(
|
|
"Book",
|
|
"An description",
|
|
"English",
|
|
"Paperback",
|
|
256,
|
|
~D[2025-03-04],
|
|
bids,
|
|
author_roles,
|
|
publisher.id,
|
|
nil,
|
|
nil,
|
|
actor: user
|
|
)
|
|
|
|
assert {:ok, book} =
|
|
Metadata.add_book_to_related_editions_registry(
|
|
"Book2",
|
|
"An description2",
|
|
"English",
|
|
"Paperback",
|
|
256,
|
|
~D[2025-03-04],
|
|
bids,
|
|
author_roles,
|
|
publisher.id,
|
|
related_book.id,
|
|
nil,
|
|
actor: user
|
|
)
|
|
|
|
assert related_book.book_editions_registry_id == book.book_editions_registry_id
|
|
end
|
|
|
|
test "a new book to a registry via a empty related book record", %{user: user} do
|
|
author_roles = author_roles()
|
|
bids = bids()
|
|
publisher = generate(publisher())
|
|
|
|
assert {:error, _} =
|
|
Metadata.add_book_to_related_editions_registry(
|
|
"Book2",
|
|
"An description2",
|
|
"English",
|
|
"Paperback",
|
|
256,
|
|
~D[2025-03-04],
|
|
bids,
|
|
author_roles,
|
|
publisher.id,
|
|
nil,
|
|
actor: user
|
|
)
|
|
end
|
|
end
|
|
|
|
describe "books alternatives names" do
|
|
test "new book has no alternatives names", %{user: user} do
|
|
author_roles = author_roles()
|
|
bids = bids()
|
|
publisher = generate(publisher())
|
|
|
|
{:ok, book} =
|
|
Metadata.create_book(
|
|
"Book",
|
|
"An description",
|
|
"English",
|
|
"Paperback",
|
|
256,
|
|
~D[2025-03-04],
|
|
bids,
|
|
author_roles,
|
|
publisher.id,
|
|
nil,
|
|
nil,
|
|
actor: user
|
|
)
|
|
|
|
assert {:ok, alternatives_names} = Metadata.get_book_alternative_editions(book)
|
|
assert ^alternatives_names = []
|
|
end
|
|
|
|
test "book has related book so they has one alternative name", %{user: user} do
|
|
author_roles = author_roles()
|
|
bids = bids()
|
|
publisher = generate(publisher())
|
|
|
|
{:ok, related_book} =
|
|
Metadata.create_book(
|
|
"Book",
|
|
"An description",
|
|
"English",
|
|
"Paperback",
|
|
256,
|
|
~D[2025-03-04],
|
|
bids,
|
|
author_roles,
|
|
publisher.id,
|
|
nil,
|
|
nil,
|
|
actor: user
|
|
)
|
|
|
|
{:ok, book} =
|
|
Metadata.add_book_to_related_editions_registry(
|
|
"Book2",
|
|
"An description2",
|
|
"English",
|
|
"Paperback",
|
|
256,
|
|
~D[2025-03-04],
|
|
bids,
|
|
author_roles,
|
|
publisher.id,
|
|
related_book.id,
|
|
nil,
|
|
actor: user
|
|
)
|
|
|
|
assert {:ok, alternatives_names} = Metadata.get_book_alternative_editions(book)
|
|
assert get_ids(alternatives_names) == [related_book.id]
|
|
end
|
|
end
|
|
|
|
describe "author's books" do
|
|
test "get the list via author's id", %{user: user} do
|
|
{:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
|
|
bids = bids()
|
|
publisher = generate(publisher())
|
|
|
|
author_roles = [
|
|
%{order: 1, author_id: author.id, role: ""}
|
|
]
|
|
|
|
{:ok, book} =
|
|
Metadata.create_book(
|
|
"Book",
|
|
"An description",
|
|
"English",
|
|
"Paperback",
|
|
256,
|
|
~D[2025-03-04],
|
|
bids,
|
|
author_roles,
|
|
publisher.id,
|
|
nil,
|
|
nil,
|
|
actor: user
|
|
)
|
|
|
|
assert {:ok, books} = Metadata.get_author_books(author)
|
|
|
|
book_ids = Enum.map(books.results, & &1.id)
|
|
assert book.id in book_ids
|
|
end
|
|
|
|
test "get the list contains aliases' books", %{user: user} do
|
|
{:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
|
|
|
|
{:ok, alias1} =
|
|
Metadata.add_author_to_related_alias_registry(
|
|
"Author2",
|
|
"An description2",
|
|
author.id,
|
|
nil,
|
|
actor: user
|
|
)
|
|
|
|
{:ok, alias2} =
|
|
Metadata.add_author_to_related_alias_registry(
|
|
"Author3",
|
|
"An description3",
|
|
author.id,
|
|
nil,
|
|
actor: user
|
|
)
|
|
|
|
book = generate(book(authors: [author]))
|
|
book2 = generate(book(authors: [alias1]))
|
|
book3 = generate(book(authors: [alias2]))
|
|
|
|
assert {:ok, books} = Metadata.get_author_books(author)
|
|
|
|
book_ids = Enum.map(books.results, & &1.id)
|
|
|
|
assert book.id in book_ids
|
|
assert book2.id in book_ids
|
|
assert book3.id in book_ids
|
|
end
|
|
end
|
|
|
|
describe "get a book by book id" do
|
|
test "get by bid" do
|
|
expected_book = generate(book())
|
|
|
|
[%Metadata.BookId{type: type, bid: bid} | _] = expected_book.bids
|
|
|
|
assert {:ok, book} = Metadata.get_book_by_bid(type, bid)
|
|
assert expected_book.id == book.id
|
|
end
|
|
end
|
|
|
|
describe "book's cover image" do
|
|
test "update cover image", %{user: user} do
|
|
cover_image_url = "/images/book.avif"
|
|
|
|
book = generate(book())
|
|
|
|
assert {:ok, book} = Metadata.assign_book_cover_image(book, cover_image_url, actor: user)
|
|
assert book.cover_image_url == cover_image_url
|
|
end
|
|
end
|
|
|
|
describe "book and DBIServer relationship" do
|
|
test "assign a DBIServer", %{user: user} do
|
|
server = generate(dbi_server(url: @test_server_endpoint))
|
|
book = generate(book())
|
|
|
|
assert {:ok, book} = Metadata.assign_book_dbi_server(book, server.id, actor: user)
|
|
assert book.dbi_server_id == server.id
|
|
end
|
|
end
|
|
|
|
describe "calculate a brief description" do
|
|
test "a short description is already enough" do
|
|
description = "Amazing!"
|
|
assert description == Ash.calculate!(Metadata.Book, :brief_description,
|
|
refs: %{description: description})
|
|
end
|
|
|
|
test "a long description is too much" do
|
|
description = "Etiam laoreet quam sed arcu. Donec at pede. Pellentesque tristique imperdiet tortor."
|
|
brief = "Etiam laoreet quam s..."
|
|
|
|
assert String.length(brief) == 23
|
|
assert brief == Ash.calculate!(Metadata.Book, :brief_description,
|
|
refs: %{description: description})
|
|
end
|
|
end
|
|
end
|