From afc1c0793616b99e7cb1333b2205f3baca22ec6d Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 21 Mar 2025 10:54:27 +0200 Subject: [PATCH] Refactor the Book generator to add authors and a related book to opts. --- .../metadata/book_test.exs | 26 ++--------------- test/support/generators.ex | 28 +++++++++++++++---- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/test/decentralised_book_index/metadata/book_test.exs b/test/decentralised_book_index/metadata/book_test.exs index a3cab21..7c4e07c 100644 --- a/test/decentralised_book_index/metadata/book_test.exs +++ b/test/decentralised_book_index/metadata/book_test.exs @@ -94,29 +94,9 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do {:ok, alias2} = Metadata.add_author_to_related_alias_registry("Author3", "An description3", author.id) - author_roles = [ - %{order: 1, author_id: author.id, role: ""} - ] - 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) - - author_roles2 = [ - %{order: 1, author_id: alias1.id, role: ""} - ] - bids2 = bids() - publisher2 = generate(publisher()) - - {:ok, book2} = Metadata.create_book("Book2", "An description", "English", "Paperback", 256, ~D[2025-03-04], bids2, author_roles2, publisher2.id) - - author_roles3 = [ - %{order: 1, author_id: alias2.id, role: ""} - ] - bids3 = bids() - publisher3 = generate(publisher()) - - {:ok, book3} = Metadata.create_book("Book3", "An description", "English", "Paperback", 256, ~D[2025-03-04], bids3, author_roles3, publisher3.id) + book = generate(book(authors: [author])) + book2 = generate(book(authors: [alias1])) + book3 = generate(book(authors: [alias2])) assert {:ok, books} = Metadata.get_author_books(author) diff --git a/test/support/generators.ex b/test/support/generators.ex index 3c0dee6..0baa7c9 100644 --- a/test/support/generators.ex +++ b/test/support/generators.ex @@ -56,11 +56,16 @@ defmodule DecentralisedBookIndex.Generators do generate(user()) end) - count = - opts[:count] || 2 + authors = + opts[:authors] || + for id <- 1..2 do + generate(author(actor: actor)) #.id + end + + count = min(opts[:count], length(authors)) - for order <- 1..count do - %{order: order, author_id: generate(author(actor: actor)).id, role: ""} + for {order, author} <- Enum.zip(1..count, authors) do + %{order: order, author_id: author.id, role: ""} end end @@ -128,10 +133,12 @@ defmodule DecentralisedBookIndex.Generators do bids() end) + authors = opts[:authors] || nil + author_roles = opts[:author_roles] || once(:default_author_roles, fn -> - author_roles() + author_roles(authors: authors) end) publisher_id = @@ -140,6 +147,15 @@ defmodule DecentralisedBookIndex.Generators do generate(publisher()).id end) + related_book = opts[:related_book] || nil + + book_editions_registry_id = + if related_book do + related_book.book_editions_registry_id + else + nil + end + changeset_generator( Metadata.Book, :create, @@ -153,7 +169,7 @@ defmodule DecentralisedBookIndex.Generators do bids: bids, author_roles: author_roles, publisher_id: publisher_id, - book_editions_registry_id: nil, + book_editions_registry_id: book_editions_registry_id, ], overrides: opts, actor: actor