Compare commits
3
Commits
59e2d2f8a1
...
64028d09f5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
64028d09f5 | ||
|
|
ea5ada751d | ||
|
|
f4eb929401 |
@@ -103,7 +103,9 @@ defmodule DecentralisedBookIndex.Metadata.DBIServer do
|
||||
end
|
||||
|
||||
relationships do
|
||||
belongs_to :dbi_server, Metadata.DBIServer
|
||||
belongs_to :dbi_server, Metadata.DBIServer do
|
||||
public? true
|
||||
end
|
||||
|
||||
has_many :author, Metadata.Author
|
||||
has_many :author_alias_registries, Metadata.AuthorAliasRegistry
|
||||
|
||||
@@ -20,6 +20,10 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer do
|
||||
dbi_server_id: get_in(json_body, ["attributes", "dbi_server_id"])
|
||||
}
|
||||
|
||||
{:ok, attrs}
|
||||
if is_nil(attrs[:dbi_server_id]) do
|
||||
{:ok, attrs |> Map.delete(:dbi_server_id)}
|
||||
else
|
||||
{:ok, attrs}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -26,6 +26,10 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.BookTransformer do
|
||||
dbi_server_id: get_in(json_body, ["attributes", "dbi_server_id"])
|
||||
}
|
||||
|
||||
{:ok, attrs}
|
||||
if is_nil(attrs[:dbi_server_id]) do
|
||||
{:ok, attrs |> Map.delete(:dbi_server_id)}
|
||||
else
|
||||
{:ok, attrs}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,9 +13,15 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.DBIServerTransformer do
|
||||
name: get_in(json_body, ["attributes", "name"]),
|
||||
url: get_in(json_body, ["attributes", "url"]),
|
||||
inserted_at: get_in(json_body, ["attributes", "inserted_at"]),
|
||||
updated_at: get_in(json_body, ["attributes", "updated_at"])
|
||||
updated_at: get_in(json_body, ["attributes", "updated_at"]),
|
||||
# relationship
|
||||
dbi_server_id: get_in(json_body, ["attributes", "dbi_server_id"])
|
||||
}
|
||||
|
||||
{:ok, attrs}
|
||||
if is_nil(attrs[:dbi_server_id]) do
|
||||
{:ok, attrs |> Map.delete(:dbi_server_id)}
|
||||
else
|
||||
{:ok, attrs}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -17,6 +17,10 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.PublisherTransformer do
|
||||
dbi_server_id: get_in(json_body, ["attributes", "dbi_server_id"])
|
||||
}
|
||||
|
||||
{:ok, attrs}
|
||||
if is_nil(attrs[:dbi_server_id]) do
|
||||
{:ok, attrs |> Map.delete(:dbi_server_id)}
|
||||
else
|
||||
{:ok, attrs}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -81,5 +81,40 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformerTest do
|
||||
updated_at: "2025-03-26T10:16:16.124581Z"
|
||||
} = author
|
||||
end
|
||||
|
||||
test "a json has dbi_server_id as nil, then author's map has not :dbi_server_id key" do
|
||||
json_body = %{
|
||||
"attributes" => %{
|
||||
"author_alias_registry_id" => "1320a6fe-b311-45ac-bbaa-39cf29c44624",
|
||||
"avatar_url" => "/images/avatar.png",
|
||||
"dbi_server_id" => nil,
|
||||
"description" => "Something",
|
||||
"inserted_at" => "2025-03-25T19:19:15.187456Z",
|
||||
"name" => "Author",
|
||||
"updated_at" => "2025-03-26T10:16:16.124581Z"
|
||||
},
|
||||
"id" => "889a323e-d104-4b5d-b276-dad5a9b1da9d",
|
||||
"links" => %{},
|
||||
"meta" => %{},
|
||||
"relationships" => %{
|
||||
"author_alias_registry" => %{"links" => %{}, "meta" => %{}},
|
||||
"dbi_server" => %{"links" => %{}, "meta" => %{}}
|
||||
},
|
||||
"type" => "author"
|
||||
}
|
||||
|
||||
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",
|
||||
inserted_at: "2025-03-25T19:19:15.187456Z",
|
||||
updated_at: "2025-03-26T10:16:16.124581Z"
|
||||
} = author
|
||||
refute Map.has_key?(author, :dbi_server_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -138,5 +138,70 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.BookTransformerTest do
|
||||
dbi_server_id: "889a323e-d104-4b5d-b276-dad5a9b1da99"
|
||||
} = book
|
||||
end
|
||||
|
||||
test "a json has dbi_server_id as nil, then book's map has not :dbi_server_id key" do
|
||||
json_body = %{
|
||||
"attributes" => %{
|
||||
"dbi_server_id" => nil,
|
||||
"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" => %{}
|
||||
},
|
||||
"book_editions_registry" => %{"links" => %{}, "meta" => %{}},
|
||||
"dbi_server" => %{"links" => %{}, "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
|
||||
|
||||
refute Map.has_key?(book, :dbi_server_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+34
-2
@@ -8,6 +8,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.DBIServerTransformerTest
|
||||
json_body = %{
|
||||
"data" => %{
|
||||
"attributes" => %{
|
||||
"dbi_server_id" => "0c0647ec-07ef-4caa-b683-5847dbfbe5cc",
|
||||
"inserted_at" => "2025-03-22T20:07:30.766249Z",
|
||||
"name" => "Test",
|
||||
"updated_at" => "2025-05-04T18:48:44.213309Z",
|
||||
@@ -16,7 +17,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.DBIServerTransformerTest
|
||||
"id" => "0c0647ec-07ef-4caa-b683-5847dbfbe5cc",
|
||||
"links" => %{},
|
||||
"meta" => %{},
|
||||
"relationships" => %{},
|
||||
"relationships" => %{"dbi_server" => %{"links" => %{}, "meta" => %{}}},
|
||||
"type" => "dbi_server"
|
||||
},
|
||||
"jsonapi" => %{"version" => "1.0"},
|
||||
@@ -34,11 +35,40 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.DBIServerTransformerTest
|
||||
name: "Test",
|
||||
url: "http://localhost:4001",
|
||||
inserted_at: "2025-03-22T20:07:30.766249Z",
|
||||
updated_at: "2025-05-04T18:48:44.213309Z"
|
||||
updated_at: "2025-05-04T18:48:44.213309Z",
|
||||
dbi_server_id: "0c0647ec-07ef-4caa-b683-5847dbfbe5cc"
|
||||
} = server
|
||||
end
|
||||
|
||||
test "a json doesn't contains server information \"data\" attribute" do
|
||||
json_body = %{
|
||||
"attributes" => %{
|
||||
"dbi_server_id" => "0c0647ec-07ef-4caa-b683-5847dbfbe5cc",
|
||||
"inserted_at" => "2025-03-22T20:07:30.766249Z",
|
||||
"name" => "Test",
|
||||
"updated_at" => "2025-05-04T18:48:44.213309Z",
|
||||
"url" => "http://localhost:4001"
|
||||
},
|
||||
"id" => "0c0647ec-07ef-4caa-b683-5847dbfbe5cc",
|
||||
"links" => %{},
|
||||
"meta" => %{},
|
||||
"relationships" => %{"dbi_server" => %{"links" => %{}, "meta" => %{}}},
|
||||
"type" => "dbi_server"
|
||||
}
|
||||
|
||||
assert {:ok, server} = DBIServerTransformer.from_json(json_body)
|
||||
|
||||
assert %{
|
||||
id: "0c0647ec-07ef-4caa-b683-5847dbfbe5cc",
|
||||
name: "Test",
|
||||
url: "http://localhost:4001",
|
||||
inserted_at: "2025-03-22T20:07:30.766249Z",
|
||||
updated_at: "2025-05-04T18:48:44.213309Z",
|
||||
dbi_server_id: "0c0647ec-07ef-4caa-b683-5847dbfbe5cc"
|
||||
} = server
|
||||
end
|
||||
|
||||
test "a json has dbi_server_id as nil, then server's map has not :dbi_server_id key" do
|
||||
json_body = %{
|
||||
"attributes" => %{
|
||||
"inserted_at" => "2025-03-22T20:07:30.766249Z",
|
||||
@@ -62,6 +92,8 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.DBIServerTransformerTest
|
||||
inserted_at: "2025-03-22T20:07:30.766249Z",
|
||||
updated_at: "2025-05-04T18:48:44.213309Z"
|
||||
} = server
|
||||
|
||||
refute Map.has_key?(server, :dbi_server_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -63,5 +63,32 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.PublisherTransformerTest
|
||||
dbi_server_id: "889a323e-d104-4b5d-b276-dad5a9b1da99"
|
||||
} = publisher
|
||||
end
|
||||
|
||||
test "a json has dbi_server_id as nil, then publisher's map has not :dbi_server_id key" do
|
||||
json_body = %{
|
||||
"attributes" => %{
|
||||
"dbi_server_id" => nil,
|
||||
"inserted_at" => "2025-03-21T09:20:48.791539Z",
|
||||
"name" => "Publisher",
|
||||
"updated_at" => "2025-03-21T09:20:48.791539Z"
|
||||
},
|
||||
"id" => "11349865-1b7b-454a-b999-6c4059888a78",
|
||||
"links" => %{},
|
||||
"meta" => %{},
|
||||
"relationships" => %{"dbi_server" => %{"links" => %{}, "meta" => %{}}},
|
||||
"type" => "publisher"
|
||||
}
|
||||
|
||||
assert {:ok, publisher} = PublisherTransformer.from_json(json_body)
|
||||
|
||||
assert %{
|
||||
id: "11349865-1b7b-454a-b999-6c4059888a78",
|
||||
name: "Publisher",
|
||||
inserted_at: "2025-03-21T09:20:48.791539Z",
|
||||
updated_at: "2025-03-21T09:20:48.791539Z",
|
||||
} = publisher
|
||||
|
||||
refute Map.has_key?(publisher, :dbi_server_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user