Add the sync task for Book.
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
defmodule DecentralisedBookIndex.Sync.DataTransformers.BookTransformerTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
alias DecentralisedBookIndex.Sync.DataTransformers.BookTransformer
|
||||
alias DecentralisedBookIndex.Metadata.Book
|
||||
|
||||
describe "correct transformations" do
|
||||
test "a json contains correct book information" do
|
||||
json_body = %{
|
||||
"data" => %{
|
||||
"attributes" => %{
|
||||
"cover_image_url" => "/images/book_cover.png",
|
||||
"description" => "A cool book.",
|
||||
"format" => "Paper",
|
||||
"inserted_at" => "2025-03-20T14:44:36.162986Z",
|
||||
"language" => "English",
|
||||
"page_count" => 1000,
|
||||
"published" => "2025-03-05",
|
||||
"publisher_id" => "11349865-1b7b-454a-b999-6c4059888a78",
|
||||
"title" => "Book",
|
||||
"updated_at" => "2025-04-01T18:14:25.754055Z"
|
||||
},
|
||||
"id" => "1bbe8861-9d9d-4684-bda6-b6ec238d8d08",
|
||||
"links" => %{},
|
||||
"meta" => %{},
|
||||
"relationships" => %{
|
||||
"author_roles" => %{
|
||||
"links" => %{
|
||||
"related" =>
|
||||
"http://localhost:4000/api/v1/json/books/1bbe8861-9d9d-4684-bda6-b6ec238d8d08/author_roles"
|
||||
},
|
||||
"meta" => %{}
|
||||
},
|
||||
"bids" => %{
|
||||
"links" => %{
|
||||
"related" =>
|
||||
"http://localhost:4000/api/v1/json/books/1bbe8861-9d9d-4684-bda6-b6ec238d8d08/bids"
|
||||
},
|
||||
"meta" => %{}
|
||||
},
|
||||
"publisher" => %{
|
||||
"links" => %{
|
||||
"related" =>
|
||||
"http://localhost:4000/api/v1/json/books/1bbe8861-9d9d-4684-bda6-b6ec238d8d08/publisher"
|
||||
},
|
||||
"meta" => %{}
|
||||
}
|
||||
},
|
||||
"type" => "book"
|
||||
},
|
||||
"jsonapi" => %{"version" => "1.0"},
|
||||
"links" => %{
|
||||
"self" => "http://localhost:4000/api/v1/json/books/1bbe8861-9d9d-4684-bda6-b6ec238d8d08"
|
||||
},
|
||||
"meta" => %{}
|
||||
}
|
||||
|
||||
assert {:ok, book} = BookTransformer.from_json(json_body)
|
||||
|
||||
assert %{
|
||||
id: "1bbe8861-9d9d-4684-bda6-b6ec238d8d08",
|
||||
cover_image_url: "/images/book_cover.png",
|
||||
description: "A cool book.",
|
||||
format: "Paper",
|
||||
inserted_at: "2025-03-20T14:44:36.162986Z",
|
||||
language: "English",
|
||||
page_count: 1000,
|
||||
published: "2025-03-05",
|
||||
publisher_id: "11349865-1b7b-454a-b999-6c4059888a78",
|
||||
title: "Book",
|
||||
updated_at: "2025-04-01T18:14:25.754055Z"
|
||||
} = book
|
||||
end
|
||||
|
||||
test "a json doesn't contains book information \"data\" attribute" do
|
||||
json_body = %{
|
||||
"attributes" => %{
|
||||
"cover_image_url" => "/images/book_cover.png",
|
||||
"description" => "A cool book.",
|
||||
"format" => "Paper",
|
||||
"inserted_at" => "2025-03-20T14:44:36.162986Z",
|
||||
"language" => "English",
|
||||
"page_count" => 1000,
|
||||
"published" => "2025-03-05",
|
||||
"publisher_id" => "11349865-1b7b-454a-b999-6c4059888a78",
|
||||
"title" => "Book",
|
||||
"updated_at" => "2025-04-01T18:14:25.754055Z"
|
||||
},
|
||||
"id" => "1bbe8861-9d9d-4684-bda6-b6ec238d8d08",
|
||||
"links" => %{},
|
||||
"meta" => %{},
|
||||
"relationships" => %{
|
||||
"author_roles" => %{
|
||||
"links" => %{
|
||||
"related" =>
|
||||
"http://localhost:4000/api/v1/json/books/1bbe8861-9d9d-4684-bda6-b6ec238d8d08/author_roles"
|
||||
},
|
||||
"meta" => %{}
|
||||
},
|
||||
"bids" => %{
|
||||
"links" => %{
|
||||
"related" =>
|
||||
"http://localhost:4000/api/v1/json/books/1bbe8861-9d9d-4684-bda6-b6ec238d8d08/bids"
|
||||
},
|
||||
"meta" => %{}
|
||||
},
|
||||
"publisher" => %{
|
||||
"links" => %{
|
||||
"related" =>
|
||||
"http://localhost:4000/api/v1/json/books/1bbe8861-9d9d-4684-bda6-b6ec238d8d08/publisher"
|
||||
},
|
||||
"meta" => %{}
|
||||
}
|
||||
},
|
||||
"type" => "book"
|
||||
}
|
||||
|
||||
assert {:ok, book} = BookTransformer.from_json(json_body)
|
||||
|
||||
assert %{
|
||||
id: "1bbe8861-9d9d-4684-bda6-b6ec238d8d08",
|
||||
cover_image_url: "/images/book_cover.png",
|
||||
description: "A cool book.",
|
||||
format: "Paper",
|
||||
inserted_at: "2025-03-20T14:44:36.162986Z",
|
||||
language: "English",
|
||||
page_count: 1000,
|
||||
published: "2025-03-05",
|
||||
publisher_id: "11349865-1b7b-454a-b999-6c4059888a78",
|
||||
title: "Book",
|
||||
updated_at: "2025-04-01T18:14:25.754055Z"
|
||||
} = book
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,70 @@
|
||||
defmodule DecentralisedBookIndex.Sync.DataTransformers.BookSyncTest do
|
||||
use DecentralisedBookIndex.DataCase, async: true
|
||||
|
||||
alias DecentralisedBookIndex.Sync.BookSync
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
|
||||
alias DecentralisedBookIndex.TestEndpoints
|
||||
@test_server_endpoint TestEndpoints.test_api_endpoint()
|
||||
|
||||
describe "sync book transformations" do
|
||||
test "a new book will be created" do
|
||||
server = generate(dbi_server(url: @test_server_endpoint))
|
||||
|
||||
publisher = generate(publisher())
|
||||
|
||||
book = %{
|
||||
id: "1bbe8861-9d9d-4684-bda6-b6ec238d8d08",
|
||||
cover_image_url: "/images/book_cover.png",
|
||||
description: "A cool book.",
|
||||
format: "Paper",
|
||||
inserted_at: "2025-03-20T14:44:36.162986Z",
|
||||
language: "English",
|
||||
page_count: 1000,
|
||||
published: "2025-03-05",
|
||||
publisher_id: publisher.id,
|
||||
title: "Book",
|
||||
updated_at: "2025-04-01T18:14:25.754055Z"
|
||||
}
|
||||
|
||||
{: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)
|
||||
|
||||
book =
|
||||
book
|
||||
|> Map.replace(:inserted_at, inserted_at)
|
||||
|> Map.replace(:updated_at, updated_at)
|
||||
|
||||
assert book = saved_book
|
||||
assert nil != saved_book.book_editions_registry_id
|
||||
assert server.id == saved_book.dbi_server_id
|
||||
end
|
||||
|
||||
test "update an existing book" do
|
||||
server = generate(dbi_server(url: @test_server_endpoint))
|
||||
|
||||
book = generate(book())
|
||||
|
||||
book_attrs = %{
|
||||
id: book.id,
|
||||
cover_image_url: "/images/book_cover2.png",
|
||||
description: "A cool book 2.",
|
||||
format: "Ebook",
|
||||
inserted_at: "2025-01-20T14:44:36.162986Z",
|
||||
language: "English",
|
||||
page_count: 1001,
|
||||
published: "2025-03-05",
|
||||
title: "Book2",
|
||||
updated_at: "2025-02-01T18:14:25.754055Z"
|
||||
}
|
||||
|
||||
assert :ok = BookSync.create_update(book_attrs, server.id)
|
||||
assert {:ok, saved_book} = Metadata.get_book_by_id(book.id)
|
||||
|
||||
assert book = saved_book
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
defmodule DecentralisedBookIndex.SyncTasks.SyncBookTaskTest do
|
||||
use DecentralisedBookIndex.DataCase
|
||||
|
||||
alias DecentralisedBookIndex.SyncTasks.SyncBooksTask
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
|
||||
alias DecentralisedBookIndex.TestEndpoints
|
||||
@test_server_endpoint TestEndpoints.test_api_endpoint()
|
||||
|
||||
describe "sync authors tasks" do
|
||||
test "sync authors" do
|
||||
server = generate(dbi_server(url: @test_server_endpoint))
|
||||
|
||||
_book = generate(book())
|
||||
_book = generate(book())
|
||||
|
||||
assert server = SyncBooksTask.sync(server)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user