Update the code format.

This commit is contained in:
2025-04-27 15:21:26 +03:00
parent cffbeb04e7
commit ebbee7f9a5
36 changed files with 378 additions and 161 deletions
@@ -10,28 +10,42 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
describe "authors and registries relationship" do
test "a new author get new registry by default", %{user: user} do
assert {:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert {:ok, author} =
Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert author.author_alias_registry_id != nil
end
test "a new author belongs to a registry if specified", %{user: user} do
assert {:ok, registry} = Metadata.create_author_alias_registry(actor: user)
assert {:ok, author} = Metadata.create_author("Author", "An description", nil, registry.id, actor: user)
assert {:ok, author} =
Metadata.create_author("Author", "An description", nil, registry.id, actor: user)
assert author.author_alias_registry_id == registry.id
end
test "a new author to a registry via a related author record", %{user: user} do
{:ok, related_author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
{:ok, related_author} =
Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert {:ok, author} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", related_author.id, nil, actor: user)
Metadata.add_author_to_related_alias_registry(
"Author2",
"An description2",
related_author.id,
nil,
actor: user
)
assert related_author.author_alias_registry_id == author.author_alias_registry_id
end
test "a new author to a registry via a empty related author record", %{user: user} do
assert {:error, _} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", nil, actor: user)
Metadata.add_author_to_related_alias_registry("Author2", "An description2", nil,
actor: user
)
end
end
@@ -43,10 +57,17 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
end
test "author has related author so they has one alternative name", %{user: user} do
{:ok, related_author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
{:ok, related_author} =
Metadata.create_author("Author", "An description", nil, nil, actor: user)
{:ok, author} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", related_author.id, nil, actor: user)
Metadata.add_author_to_related_alias_registry(
"Author2",
"An description2",
related_author.id,
nil,
actor: user
)
assert {:ok, alternatives_names} = Metadata.get_author_alternative_names(author)
assert alternatives_names = [related_author]
@@ -63,10 +84,24 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
test "the list has the aliases' ids", %{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)
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)
Metadata.add_author_to_related_alias_registry(
"Author3",
"An description3",
author.id,
nil,
actor: user
)
assert {:ok, ids} = Metadata.get_author_ids(author)
@@ -78,14 +113,18 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
describe "authors and avatar_url" do
test "a new author has no avatar by default", %{user: user} do
assert {:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert {:ok, author} =
Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert author.avatar_url == nil
end
test "a new author has a avatar", %{user: user} do
avatar_url = "/images/avatar.png"
assert {:ok, author} = Metadata.create_author("Author", "An description", avatar_url, nil, actor: user)
assert {:ok, author} =
Metadata.create_author("Author", "An description", avatar_url, nil, actor: user)
assert author.avatar_url == avatar_url
end
end
@@ -14,7 +14,22 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
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 {: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
@@ -24,7 +39,23 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
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 {: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
@@ -33,10 +64,37 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
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, 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)
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
@@ -47,7 +105,18 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
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)
Metadata.add_book_to_related_editions_registry(
"Book2",
"An description2",
"English",
"Paperback",
256,
~D[2025-03-04],
bids,
author_roles,
publisher.id,
nil
)
end
end
@@ -57,7 +126,22 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
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)
{: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
@@ -67,10 +151,37 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
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, 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)
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]
@@ -87,17 +198,45 @@ 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, nil, nil, actor: user)
{: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", %{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)
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)
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]))
@@ -107,7 +246,7 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
book_ids = Enum.map(books, & &1.id)
assert book.id in book_ids
assert book.id in book_ids
assert book2.id in book_ids
assert book3.id in book_ids
end
@@ -14,16 +14,15 @@ defmodule DecentralisedBookIndex.Metadata.Forms.AuthorFormTest do
valid_params = %{
"name" => "Oleh",
"description" => "A cool author",
"description" => "A cool author"
}
assert_raise Ash.Error.Forbidden, fn ->
form = \
AshPhoenix.Form.for_create(Metadata.Author, :create,
as: "author",
actor: user
)
form =
AshPhoenix.Form.for_create(Metadata.Author, :create,
as: "author",
actor: user
)
|> AshPhoenix.Form.ensure_can_submit!()
end
end
@@ -31,15 +30,15 @@ defmodule DecentralisedBookIndex.Metadata.Forms.AuthorFormTest do
test "moderator can submit", %{user: user} do
valid_params = %{
"name" => "Oleh",
"description" => "A cool author",
"description" => "A cool author"
}
assert form =
AshPhoenix.Form.for_create(Metadata.Author, :create,
as: "author",
actor: user
)
|> AshPhoenix.Form.ensure_can_submit!()
AshPhoenix.Form.for_create(Metadata.Author, :create,
as: "author",
actor: user
)
|> AshPhoenix.Form.ensure_can_submit!()
end
end
@@ -47,7 +46,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.AuthorFormTest do
test "just name and description", %{user: user} do
valid_params = %{
"name" => "Oleh",
"description" => "A cool author",
"description" => "A cool author"
}
form =
@@ -66,7 +65,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.AuthorFormTest do
valid_params = %{
"name" => "Another Oleh",
"description" => "A cool author",
"description" => "A cool author"
}
form =
@@ -32,11 +32,11 @@ defmodule DecentralisedBookIndex.Metadata.Forms.PublisherFormTest do
}
assert form =
AshPhoenix.Form.for_create(Metadata.Publisher, :create,
as: "publisher",
actor: user
)
|> AshPhoenix.Form.ensure_can_submit!()
AshPhoenix.Form.for_create(Metadata.Publisher, :create,
as: "publisher",
actor: user
)
|> AshPhoenix.Form.ensure_can_submit!()
end
end
@@ -16,7 +16,9 @@ defmodule DecentralisedBookIndex.Sync.ApiClients.FetchJsonTest do
test "get an author", %{user: user} do
{:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert {:ok, data} = FetchJson.get("#{@test_server_endpoint}/api/v1/json/authors/#{author.id}")
assert {:ok, data} =
FetchJson.get("#{@test_server_endpoint}/api/v1/json/authors/#{author.id}")
assert data["data"]["id"] == author.id
end
end
@@ -16,7 +16,9 @@ defmodule DecentralisedBookIndex.Sync.ApiClients.FetchJsonsTest do
describe "authors api" do
test "get authors", %{user: user} do
for number <- 1..11 do
Metadata.create_author!("Author#{number}", "An description#{number}", nil, nil, actor: user)
Metadata.create_author!("Author#{number}", "An description#{number}", nil, nil,
actor: user
)
end
assert {:ok, records} = FetchJsons.get("#{@test_server_endpoint}/api/v1/json/authors")
@@ -25,7 +27,9 @@ defmodule DecentralisedBookIndex.Sync.ApiClients.FetchJsonsTest do
test "get authors names from API", %{user: user} do
for number <- 1..11 do
Metadata.create_author!("Author#{number}", "An description#{number}", nil, nil, actor: user)
Metadata.create_author!("Author#{number}", "An description#{number}", nil, nil,
actor: user
)
end
get_author_names = fn data ->
@@ -33,7 +37,7 @@ defmodule DecentralisedBookIndex.Sync.ApiClients.FetchJsonsTest do
end
assert {:ok, records} =
FetchJsons.get("#{@test_server_endpoint}/api/v1/json/authors", get_author_names)
FetchJsons.get("#{@test_server_endpoint}/api/v1/json/authors", get_author_names)
assert length(records) == 11
assert Enum.all?(records, fn author -> String.contains?(author, "Author") end)
@@ -21,19 +21,21 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformerTest do
},
"jsonapi" => %{"version" => "1.0"},
"links" => %{
"self" => "http://localhost:4000/api/v1/json/author/889a323e-d104-4b5d-b276-dad5a9b1da9d"
"self" =>
"http://localhost:4000/api/v1/json/author/889a323e-d104-4b5d-b276-dad5a9b1da9d"
},
"meta" => %{}
}
assert {:ok, author} = AuthorTransformer.from_json(json_body)
assert %{
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
name: "Author",
description: "Something",
avatar_url: "/images/avatar.png",
author_alias_registry_id: "1320a6fe-b311-45ac-bbaa-39cf29c44624"
} = author
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
name: "Author",
description: "Something",
avatar_url: "/images/avatar.png",
author_alias_registry_id: "1320a6fe-b311-45ac-bbaa-39cf29c44624"
} = author
end
test "a json doesn't contains author information \"data\" attribute" do
@@ -52,13 +54,14 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformerTest do
}
assert {:ok, author} = AuthorTransformer.from_json(json_body)
assert %{
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
name: "Author",
description: "Something",
avatar_url: "/images/avatar.png",
author_alias_registry_id: "1320a6fe-b311-45ac-bbaa-39cf29c44624"
} = author
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
name: "Author",
description: "Something",
avatar_url: "/images/avatar.png",
author_alias_registry_id: "1320a6fe-b311-45ac-bbaa-39cf29c44624"
} = author
end
end
end
@@ -17,7 +17,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorAliasRegistrySyncTe
server = generate(dbi_server(url: @test_server_endpoint))
attrs = %{
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d"
}
assert :ok = AuthorAliasRegistrySync.create_update(attrs, server.id)
@@ -31,7 +31,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorAliasRegistrySyncTe
{:ok, same_alias} = Metadata.create_author_alias_registry(actor: user)
attrs = %{
id: same_alias.id,
id: same_alias.id
}
assert :ok = AuthorAliasRegistrySync.create_update(attrs, server.id)
@@ -17,7 +17,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.BookEditionsRegistrySyncT
server = generate(dbi_server(url: @test_server_endpoint))
attrs = %{
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d"
}
assert :ok = BookEditionsRegistrySync.create_update(attrs, server.id)
@@ -31,7 +31,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.BookEditionsRegistrySyncT
{:ok, same_alias} = Metadata.create_book_editions_registry(actor: user)
attrs = %{
id: same_alias.id,
id: same_alias.id
}
assert :ok = BookEditionsRegistrySync.create_update(attrs, server.id)
+4 -4
View File
@@ -64,7 +64,7 @@ defmodule DecentralisedBookIndex.Generators do
authors =
opts[:authors] ||
for id <- 1..2 do
generate(author(actor: actor)) #.id
generate(author(actor: actor))
end
count = min(opts[:count], length(authors))
@@ -140,7 +140,7 @@ defmodule DecentralisedBookIndex.Generators do
Metadata.Publisher,
:create,
defaults: [
name: sequence(:name, &"Publisher #{&1}"),
name: sequence(:name, &"Publisher #{&1}")
],
overrides: opts,
actor: actor
@@ -194,12 +194,12 @@ defmodule DecentralisedBookIndex.Generators do
description: sequence(:description, &"Description #{&1}"),
language: sequence(:language, &"Language #{&1}"),
format: sequence(:format, &"Format #{&1}"),
page_count: sequence(:page_count, &(abs(&1)+1)),
page_count: sequence(:page_count, &(abs(&1) + 1)),
published: sequence(:published, &Date.add(~D[2025-03-04], &1)),
bids: bids,
author_roles: author_roles,
publisher_id: publisher_id,
book_editions_registry_id: book_editions_registry_id,
book_editions_registry_id: book_editions_registry_id
],
overrides: opts,
actor: actor