Update Book resource and its tests.

dev
KKlochko 2 months ago
parent 6014b53dd1
commit 777a9436d5

@ -39,11 +39,13 @@ defmodule DecentralisedBookIndex.Metadata.Book do
argument :bids, {:array, :map} argument :bids, {:array, :map}
argument :author_roles, {: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) registry_id = Ash.Changeset.get_attribute(changeset, :book_editions_registry_id)
if registry_id == nil do 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) Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, registry.id)
else else
@ -75,11 +77,12 @@ defmodule DecentralisedBookIndex.Metadata.Book do
argument :bids, {:array, :map} argument :bids, {:array, :map}
argument :author_roles, {: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) registry_id = Ash.Changeset.get_attribute(changeset, :book_editions_registry_id)
if registry_id == nil do 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) Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, registry.id)
else else
@ -332,16 +335,12 @@ defmodule DecentralisedBookIndex.Metadata.Book do
public? true public? true
end end
timestamps() do timestamps do
writable? true writable? true
public? true public? true
end end
end end
validations do
validate numericality(:page_count, greater_than: 0)
end
relationships do relationships do
belongs_to :dbi_server, Metadata.DBIServer belongs_to :dbi_server, Metadata.DBIServer

@ -3,40 +3,45 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
alias DecentralisedBookIndex.Metadata alias DecentralisedBookIndex.Metadata
setup do
user = generate(user(role: :moderator))
%{user: user}
end
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", %{user: user} do
author_roles = author_roles() author_roles = author_roles()
bids = bids() bids = bids()
publisher = generate(publisher()) 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 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", %{user: user} do
author_roles = author_roles() author_roles = author_roles()
bids = bids() bids = bids()
publisher = generate(publisher()) publisher = generate(publisher())
assert {:ok, registry} = Metadata.create_book_editions_registry() 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) 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 assert book.book_editions_registry_id == registry.id
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", %{user: user} do
author_roles = author_roles() author_roles = author_roles()
bids = bids() bids = bids()
publisher = generate(publisher()) 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} = 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 assert related_book.book_editions_registry_id == book.book_editions_registry_id
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", %{user: user} do
author_roles = author_roles() author_roles = author_roles()
bids = bids() bids = bids()
publisher = generate(publisher()) publisher = generate(publisher())
@ -47,25 +52,25 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
end end
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", %{user: user} do
author_roles = author_roles() author_roles = author_roles()
bids = bids() bids = bids()
publisher = generate(publisher()) 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 {:ok, alternatives_names} = Metadata.get_book_alternative_editions(book)
assert alternatives_names = [] assert alternatives_names = []
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", %{user: user} do
author_roles = author_roles() author_roles = author_roles()
bids = bids() bids = bids()
publisher = generate(publisher()) 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} = {: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 {:ok, alternatives_names} = Metadata.get_book_alternative_editions(book)
assert alternatives_names = [related_book] assert alternatives_names = [related_book]
@ -73,8 +78,8 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
end end
describe "author's books" do describe "author's books" do
test "get the list via author's id" do test "get the list via author's id", %{user: user} do
{:ok, author} = Metadata.create_author("Author", "An description") {:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
bids = bids() bids = bids()
publisher = generate(publisher()) publisher = generate(publisher())
@ -82,17 +87,17 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
%{order: 1, author_id: author.id, role: ""} %{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) assert {:ok, books} = Metadata.get_author_books(author)
end end
test "get the list contains aliases' books" do test "get the list contains aliases' books", %{user: user} do
{:ok, author} = Metadata.create_author("Author", "An description") {:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
{:ok, alias1} = {: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} = {: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])) book = generate(book(authors: [author]))
book2 = generate(book(authors: [alias1])) book2 = generate(book(authors: [alias1]))
@ -109,7 +114,7 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
end end
describe "get a book by book id" do describe "get a book by book id" do
test "get by bid" do test "get by bid", %{user: user} do
expected_book = generate(book()) expected_book = generate(book())
[%Metadata.BookId{type: type, bid: bid} | _] = expected_book.bids [%Metadata.BookId{type: type, bid: bid} | _] = expected_book.bids
@ -120,12 +125,12 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
end end
describe "book's cover image" do describe "book's cover image" do
test "update cover image" do test "update cover image", %{user: user} do
cover_image_url = "/images/book.avif" cover_image_url = "/images/book.avif"
book = generate(book()) 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 assert book.cover_image_url == cover_image_url
end end
end end

@ -6,9 +6,64 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
alias DecentralisedBookIndex.Metadata alias DecentralisedBookIndex.Metadata
alias DecentralisedBookIndex.Metadata.Book alias DecentralisedBookIndex.Metadata.Book
describe "create new book via the form" do setup do
test "just name and description" do user = generate(user(role: :moderator))
%{user: user}
end
describe "form submiting" do
test "user can't submit" do
user = generate(user()) 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)
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 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) bids = bids(actor: user)
author_roles = author_roles(actor: user) author_roles = author_roles(actor: user)
@ -23,7 +78,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
"bids" => bids "bids" => bids
} }
form = \ form =
AshPhoenix.Form.for_create(Metadata.Book, :create, AshPhoenix.Form.for_create(Metadata.Book, :create,
as: "book", as: "book",
actor: user actor: user
@ -34,13 +89,12 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
end end
describe "update existing book via the form" do describe "update existing book via the form" do
test "update name" do test "update name", %{user: user} do
user = generate(user())
book = generate(book()) book = generate(book())
valid_params = %{ valid_params = %{
"title" => "Another Book", "title" => "Another Book",
"description" => "A cool book", "description" => "A cool book"
} }
form = form =
@ -52,8 +106,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
assert {:ok, user} = AshPhoenix.Form.submit(form, params: valid_params) assert {:ok, user} = AshPhoenix.Form.submit(form, params: valid_params)
end end
test "update bids and authors" do test "update bids and authors", %{user: user} do
user = generate(user())
book = generate(book()) book = generate(book())
bids = bids(actor: user) bids = bids(actor: user)
author_roles = author_roles(actor: user) author_roles = author_roles(actor: user)
@ -74,8 +127,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
end end
describe "validation" do describe "validation" do
property "the book form is valid if the page count > 0" do property "the book form is valid if the page count > 0", %{user: user} do
user = generate(user())
book = generate(book()) book = generate(book())
valid_count_generator = valid_count_generator =
@ -84,7 +136,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
check all(page_count <- valid_count_generator) do check all(page_count <- valid_count_generator) do
valid_params = %{ valid_params = %{
"page_count" => page_count, "page_count" => page_count
} }
form = form =
@ -97,8 +149,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
end end
end end
property "the book form is invalid if the page count <= 0" do property "the book form is invalid if the page count <= 0", %{user: user} do
user = generate(user())
book = generate(book()) book = generate(book())
invalid_count_generator = invalid_count_generator =
@ -107,7 +158,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
check all(page_count <- invalid_count_generator) do check all(page_count <- invalid_count_generator) do
invalid_params = %{ invalid_params = %{
"page_count" => page_count, "page_count" => page_count
} }
form = form =

Loading…
Cancel
Save