Update AuthorRole and BookId for sync Book data.
This commit is contained in:
@@ -43,6 +43,72 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.BookSyncTest do
|
||||
assert server.id == saved_book.dbi_server_id
|
||||
end
|
||||
|
||||
test "a new book with relationships will be created" do
|
||||
server = generate(dbi_server(url: @test_server_endpoint))
|
||||
|
||||
author1 = generate(author())
|
||||
author2 = generate(author())
|
||||
publisher = generate(publisher())
|
||||
|
||||
book = %{
|
||||
author_roles: [
|
||||
%{
|
||||
id: "d6d784a9-4c9c-45a3-8068-a5a30bbe8934",
|
||||
order: 0,
|
||||
role: "",
|
||||
author_id: author1.id
|
||||
},
|
||||
%{
|
||||
id: "d6d784a9-4c9c-45a3-8068-a5a30bbe8935",
|
||||
order: 1,
|
||||
role: "Translator",
|
||||
author_id: author2.id
|
||||
}
|
||||
],
|
||||
bids: [
|
||||
%{
|
||||
bid: "1234567890123",
|
||||
id: "c6531191-ebc6-4a7c-af65-e2b5030a5147",
|
||||
order: 0,
|
||||
type: "isbn13"
|
||||
},
|
||||
%{
|
||||
bid: "1234567890",
|
||||
id: "c0238e74-845b-4d1f-94cf-fe16c0fa46a8",
|
||||
order: 1,
|
||||
type: "isbn10"
|
||||
}
|
||||
],
|
||||
cover_image_url: nil,
|
||||
description: "Description 0",
|
||||
format: "Format 0",
|
||||
id: "fd09e57a-1f1e-4a80-b00a-398b3ee545fa",
|
||||
inserted_at: "2025-04-08T07:02:42.778095Z",
|
||||
language: "Language 0",
|
||||
page_count: 1,
|
||||
published: "2025-03-04",
|
||||
publisher_id: publisher.id,
|
||||
title: "Book 6",
|
||||
updated_at: "2025-04-08T07:02:42.778095Z"
|
||||
}
|
||||
|
||||
{:ok, inserted_at, 0} = DateTime.from_iso8601(book[:inserted_at])
|
||||
{:ok, updated_at, 0} = DateTime.from_iso8601(book[:updated_at])
|
||||
|
||||
assert :ok = BookSync.create_update(book, server.id)
|
||||
assert {:ok, saved_book} = Metadata.get_book_by_id(book.id, load: [:author_roles, :bids])
|
||||
|
||||
bids = book[:bids]
|
||||
author_roles = book[:author_roles]
|
||||
|
||||
assert get_ids(bids) == get_ids(saved_book.bids)
|
||||
assert get_ids(author_roles) == get_ids(saved_book.author_roles)
|
||||
|
||||
assert bids = saved_book.bids
|
||||
assert author_roles = saved_book.author_roles
|
||||
assert book[:publisher_id] == saved_book.publisher_id
|
||||
end
|
||||
|
||||
test "update an existing book" do
|
||||
server = generate(dbi_server(url: @test_server_endpoint))
|
||||
|
||||
@@ -66,5 +132,142 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.BookSyncTest do
|
||||
|
||||
assert book = saved_book
|
||||
end
|
||||
|
||||
test "update book's relationship for an existing book" do
|
||||
server = generate(dbi_server(url: @test_server_endpoint))
|
||||
|
||||
book = generate(book())
|
||||
|
||||
author1 = generate(author())
|
||||
author2 = generate(author())
|
||||
publisher = generate(publisher())
|
||||
|
||||
book_attrs = %{
|
||||
author_roles: [
|
||||
%{
|
||||
id: "d6d784a9-4c9c-45a3-8068-a5a30bbe8934",
|
||||
order: 0,
|
||||
role: "",
|
||||
author_id: author1.id
|
||||
},
|
||||
%{
|
||||
id: "d6d784a9-4c9c-45a3-8068-a5a30bbe8935",
|
||||
order: 1,
|
||||
role: "Translator",
|
||||
author_id: author2.id
|
||||
}
|
||||
],
|
||||
bids: [
|
||||
%{
|
||||
bid: "1234567890123",
|
||||
id: "c6531191-ebc6-4a7c-af65-e2b5030a5147",
|
||||
order: 0,
|
||||
type: "isbn13"
|
||||
},
|
||||
%{
|
||||
bid: "1234567890",
|
||||
id: "c0238e74-845b-4d1f-94cf-fe16c0fa46a8",
|
||||
order: 1,
|
||||
type: "isbn10"
|
||||
}
|
||||
],
|
||||
cover_image_url: nil,
|
||||
description: "Description 0",
|
||||
format: "Format 0",
|
||||
id: book.id,
|
||||
inserted_at: "2025-04-08T07:02:42.778095Z",
|
||||
language: "Language 0",
|
||||
page_count: 1,
|
||||
published: "2025-03-04",
|
||||
publisher_id: publisher.id,
|
||||
title: "Book 6",
|
||||
updated_at: "2025-04-08T07:02:42.778095Z"
|
||||
}
|
||||
|
||||
assert :ok = BookSync.create_update(book_attrs, server.id)
|
||||
assert {:ok, saved_book} = Metadata.get_book_by_id(book.id, load: [:author_roles, :bids])
|
||||
|
||||
bids = book_attrs[:bids]
|
||||
author_roles = book_attrs[:author_roles]
|
||||
|
||||
assert get_ids(bids) == get_ids(saved_book.bids)
|
||||
assert get_ids(author_roles) == get_ids(saved_book.author_roles)
|
||||
assert bids = saved_book.bids
|
||||
assert author_roles = saved_book.author_roles
|
||||
end
|
||||
|
||||
test "olds bids and roles are destroyed after update" do
|
||||
server = generate(dbi_server(url: @test_server_endpoint))
|
||||
|
||||
book = generate(book())
|
||||
|
||||
author1 = generate(author())
|
||||
author2 = generate(author())
|
||||
publisher = generate(publisher())
|
||||
|
||||
book_attrs = %{
|
||||
author_roles: [
|
||||
%{
|
||||
id: "d6d784a9-4c9c-45a3-8068-a5a30bbe8934",
|
||||
order: 0,
|
||||
role: "",
|
||||
author_id: author1.id
|
||||
},
|
||||
%{
|
||||
id: "d6d784a9-4c9c-45a3-8068-a5a30bbe8935",
|
||||
order: 1,
|
||||
role: "Translator",
|
||||
author_id: author2.id
|
||||
}
|
||||
],
|
||||
bids: [
|
||||
%{
|
||||
bid: "1234567890123",
|
||||
id: "c6531191-ebc6-4a7c-af65-e2b5030a5147",
|
||||
order: 0,
|
||||
type: "isbn13"
|
||||
},
|
||||
%{
|
||||
bid: "1234567890",
|
||||
id: "c0238e74-845b-4d1f-94cf-fe16c0fa46a8",
|
||||
order: 1,
|
||||
type: "isbn10"
|
||||
}
|
||||
],
|
||||
cover_image_url: nil,
|
||||
description: "Description 0",
|
||||
format: "Format 0",
|
||||
id: book.id,
|
||||
inserted_at: "2025-04-08T07:02:42.778095Z",
|
||||
language: "Language 0",
|
||||
page_count: 1,
|
||||
published: "2025-03-04",
|
||||
publisher_id: publisher.id,
|
||||
title: "Book 6",
|
||||
updated_at: "2025-04-08T07:02:42.778095Z"
|
||||
}
|
||||
|
||||
old_bids = get_ids(Metadata.list_book_ids!())
|
||||
old_roles = get_ids(Metadata.list_author_roles!())
|
||||
|
||||
assert :ok = BookSync.create_update(book_attrs, server.id)
|
||||
assert {:ok, saved_book} = Metadata.get_book_by_id(book.id, load: [:author_roles, :bids])
|
||||
|
||||
new_bids = get_ids(Metadata.list_book_ids!())
|
||||
new_roles = get_ids(Metadata.list_author_roles!())
|
||||
|
||||
bids = book_attrs[:bids]
|
||||
author_roles = book_attrs[:author_roles]
|
||||
|
||||
assert Enum.all?(old_bids, fn id -> id not in new_bids end)
|
||||
assert Enum.all?(old_roles, fn id -> id not in new_roles end)
|
||||
end
|
||||
end
|
||||
|
||||
defp get_ids(records) do
|
||||
Enum.map(records, fn record -> get_id(record) end)
|
||||
end
|
||||
|
||||
defp get_id(record) when is_struct(record), do: record.id
|
||||
defp get_id(record) when is_map(record), do: record[:id]
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user