Update Book resource and its tests.
This commit is contained in:
@@ -39,11 +39,13 @@ defmodule DecentralisedBookIndex.Metadata.Book do
|
||||
argument :bids, {:array, :map}
|
||||
argument :author_roles, {:array, :map}
|
||||
|
||||
change fn changeset, _ ->
|
||||
change fn changeset, context ->
|
||||
actor = Map.get(context, :actor, nil)
|
||||
|
||||
registry_id = Ash.Changeset.get_attribute(changeset, :book_editions_registry_id)
|
||||
|
||||
if registry_id == nil do
|
||||
{:ok, registry} = DecentralisedBookIndex.Metadata.create_book_editions_registry()
|
||||
registry = DecentralisedBookIndex.Metadata.create_book_editions_registry!(actor: actor)
|
||||
|
||||
Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, registry.id)
|
||||
else
|
||||
@@ -75,11 +77,12 @@ defmodule DecentralisedBookIndex.Metadata.Book do
|
||||
argument :bids, {:array, :map}
|
||||
argument :author_roles, {:array, :map}
|
||||
|
||||
change fn changeset, _ ->
|
||||
change fn changeset, context ->
|
||||
actor = Map.get(context, :actor, nil)
|
||||
registry_id = Ash.Changeset.get_attribute(changeset, :book_editions_registry_id)
|
||||
|
||||
if registry_id == nil do
|
||||
{:ok, registry} = DecentralisedBookIndex.Metadata.create_book_editions_registry()
|
||||
registry = DecentralisedBookIndex.Metadata.create_book_editions_registry!(actor: actor)
|
||||
|
||||
Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, registry.id)
|
||||
else
|
||||
@@ -332,16 +335,12 @@ defmodule DecentralisedBookIndex.Metadata.Book do
|
||||
public? true
|
||||
end
|
||||
|
||||
timestamps() do
|
||||
timestamps do
|
||||
writable? true
|
||||
public? true
|
||||
end
|
||||
end
|
||||
|
||||
validations do
|
||||
validate numericality(:page_count, greater_than: 0)
|
||||
end
|
||||
|
||||
relationships do
|
||||
belongs_to :dbi_server, Metadata.DBIServer
|
||||
|
||||
|
||||
@@ -3,40 +3,45 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
|
||||
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
|
||||
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" 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)
|
||||
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" do
|
||||
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()
|
||||
assert {:ok, book} = Metadata.create_book("Book", "An description","English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher.id, nil, registry.id)
|
||||
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" do
|
||||
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)
|
||||
{: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)
|
||||
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" do
|
||||
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())
|
||||
@@ -47,25 +52,25 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
|
||||
end
|
||||
|
||||
describe "books alternatives names" do
|
||||
test "new book has no 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)
|
||||
{: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" do
|
||||
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)
|
||||
{: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)
|
||||
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 alternatives_names = [related_book]
|
||||
@@ -73,8 +78,8 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
|
||||
end
|
||||
|
||||
describe "author's books" do
|
||||
test "get the list via author's id" do
|
||||
{:ok, author} = Metadata.create_author("Author", "An description")
|
||||
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())
|
||||
|
||||
@@ -82,17 +87,17 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
|
||||
%{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)
|
||||
{: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)
|
||||
end
|
||||
|
||||
test "get the list contains aliases' books" do
|
||||
{:ok, author} = Metadata.create_author("Author", "An description")
|
||||
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)
|
||||
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)
|
||||
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]))
|
||||
@@ -109,7 +114,7 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
|
||||
end
|
||||
|
||||
describe "get a book by book id" do
|
||||
test "get by bid" do
|
||||
test "get by bid", %{user: user} do
|
||||
expected_book = generate(book())
|
||||
|
||||
[%Metadata.BookId{type: type, bid: bid} | _] = expected_book.bids
|
||||
@@ -120,12 +125,12 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
|
||||
end
|
||||
|
||||
describe "book's cover image" do
|
||||
test "update 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)
|
||||
assert {:ok, book} = Metadata.assign_book_cover_image(book, cover_image_url, actor: user)
|
||||
assert book.cover_image_url == cover_image_url
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,9 +6,39 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
alias DecentralisedBookIndex.Metadata.Book
|
||||
|
||||
describe "create new book via the form" do
|
||||
test "just name and description" do
|
||||
setup do
|
||||
user = generate(user(role: :moderator))
|
||||
%{user: user}
|
||||
end
|
||||
|
||||
describe "form submiting" do
|
||||
test "user can't submit" do
|
||||
user = generate(user())
|
||||
bids = bids()
|
||||
author_roles = author_roles()
|
||||
|
||||
valid_params = %{
|
||||
"title" => "Book",
|
||||
"description" => "A cool author",
|
||||
"format" => "Paper",
|
||||
"language" => "English",
|
||||
"page_count" => 600,
|
||||
"published" => ~D[2025-03-06],
|
||||
"author_roles" => author_roles,
|
||||
"bids" => bids
|
||||
}
|
||||
|
||||
assert_raise Ash.Error.Forbidden, fn ->
|
||||
form =
|
||||
AshPhoenix.Form.for_create(Metadata.Book, :create,
|
||||
as: "book",
|
||||
actor: user
|
||||
)
|
||||
|> AshPhoenix.Form.ensure_can_submit!()
|
||||
end
|
||||
end
|
||||
|
||||
test "moderator can submit", %{user: user} do
|
||||
bids = bids(actor: user)
|
||||
author_roles = author_roles(actor: user)
|
||||
|
||||
@@ -23,7 +53,32 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
|
||||
"bids" => bids
|
||||
}
|
||||
|
||||
form = \
|
||||
assert form =
|
||||
AshPhoenix.Form.for_create(Metadata.Book, :create,
|
||||
as: "book",
|
||||
actor: user
|
||||
)
|
||||
|> AshPhoenix.Form.ensure_can_submit!()
|
||||
end
|
||||
end
|
||||
|
||||
describe "create new book via the form" do
|
||||
test "just name and description", %{user: user} do
|
||||
bids = bids(actor: user)
|
||||
author_roles = author_roles(actor: user)
|
||||
|
||||
valid_params = %{
|
||||
"title" => "Book",
|
||||
"description" => "A cool author",
|
||||
"format" => "Paper",
|
||||
"language" => "English",
|
||||
"page_count" => 600,
|
||||
"published" => ~D[2025-03-06],
|
||||
"author_roles" => author_roles,
|
||||
"bids" => bids
|
||||
}
|
||||
|
||||
form =
|
||||
AshPhoenix.Form.for_create(Metadata.Book, :create,
|
||||
as: "book",
|
||||
actor: user
|
||||
@@ -34,13 +89,12 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
|
||||
end
|
||||
|
||||
describe "update existing book via the form" do
|
||||
test "update name" do
|
||||
user = generate(user())
|
||||
test "update name", %{user: user} do
|
||||
book = generate(book())
|
||||
|
||||
valid_params = %{
|
||||
"title" => "Another Book",
|
||||
"description" => "A cool book",
|
||||
"description" => "A cool book"
|
||||
}
|
||||
|
||||
form =
|
||||
@@ -52,8 +106,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
|
||||
assert {:ok, user} = AshPhoenix.Form.submit(form, params: valid_params)
|
||||
end
|
||||
|
||||
test "update bids and authors" do
|
||||
user = generate(user())
|
||||
test "update bids and authors", %{user: user} do
|
||||
book = generate(book())
|
||||
bids = bids(actor: user)
|
||||
author_roles = author_roles(actor: user)
|
||||
@@ -74,17 +127,16 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
|
||||
end
|
||||
|
||||
describe "validation" do
|
||||
property "the book form is valid if the page count > 0" do
|
||||
user = generate(user())
|
||||
property "the book form is valid if the page count > 0", %{user: user} do
|
||||
book = generate(book())
|
||||
|
||||
valid_count_generator =
|
||||
StreamData.integer()
|
||||
|> StreamData.map(fn n -> abs(n)+1 end)
|
||||
|> StreamData.map(fn n -> abs(n) + 1 end)
|
||||
|
||||
check all(page_count <- valid_count_generator) do
|
||||
valid_params = %{
|
||||
"page_count" => page_count,
|
||||
"page_count" => page_count
|
||||
}
|
||||
|
||||
form =
|
||||
@@ -97,8 +149,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
|
||||
end
|
||||
end
|
||||
|
||||
property "the book form is invalid if the page count <= 0" do
|
||||
user = generate(user())
|
||||
property "the book form is invalid if the page count <= 0", %{user: user} do
|
||||
book = generate(book())
|
||||
|
||||
invalid_count_generator =
|
||||
@@ -107,7 +158,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
|
||||
|
||||
check all(page_count <- invalid_count_generator) do
|
||||
invalid_params = %{
|
||||
"page_count" => page_count,
|
||||
"page_count" => page_count
|
||||
}
|
||||
|
||||
form =
|
||||
|
||||
Reference in New Issue
Block a user