Add transformers for AuthorRole.

This commit is contained in:
2025-04-07 21:51:35 +03:00
parent 4d48795ccd
commit b64aa37c4f
4 changed files with 162 additions and 0 deletions
@@ -0,0 +1,59 @@
defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorRoleTransformerTest do
use ExUnit.Case, async: true
alias DecentralisedBookIndex.Sync.DataTransformers.AuthorRoleTransformer
describe "correct transformations" do
test "a json contains correct author role information" do
json_body =
%{
"data" => %{
"attributes" => %{
"author_id" => "d6d784a9-4c9c-45a3-8068-a5a30bbe8934",
"order" => 0,
"role" => ""
},
"id" => "eafd6569-aaac-442d-a64a-8df17051f0bc",
"links" => %{},
"meta" => %{},
"relationships" => %{"author" => %{"links" => %{}, "meta" => %{}}},
"type" => "author_role"
}
}
assert {:ok, author_role} = AuthorRoleTransformer.from_json(json_body)
assert %{
id: "eafd6569-aaac-442d-a64a-8df17051f0bc",
order: 0,
role: "",
author_id: "d6d784a9-4c9c-45a3-8068-a5a30bbe8934"
} = author_role
end
test "a json doesn't contains author role information \"data\" attribute" do
json_body =
%{
"attributes" => %{
"author_id" => "d6d784a9-4c9c-45a3-8068-a5a30bbe8934",
"order" => 0,
"role" => ""
},
"id" => "eafd6569-aaac-442d-a64a-8df17051f0bc",
"links" => %{},
"meta" => %{},
"relationships" => %{"author" => %{"links" => %{}, "meta" => %{}}},
"type" => "author_role"
}
assert {:ok, author_role} = AuthorRoleTransformer.from_json(json_body)
assert %{
id: "eafd6569-aaac-442d-a64a-8df17051f0bc",
order: 0,
role: "",
author_id: "d6d784a9-4c9c-45a3-8068-a5a30bbe8934"
} = author_role
end
end
end
@@ -0,0 +1,62 @@
defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorRolesTransformerTest do
use ExUnit.Case, async: true
alias DecentralisedBookIndex.Sync.DataTransformers.AuthorRolesTransformer
describe "correct transformations" do
test "a json contains correct author roles information" do
json_body =
%{
"data" => [
%{
"attributes" => %{
"author_id" => "d6d784a9-4c9c-45a3-8068-a5a30bbe8934",
"order" => 0,
"role" => ""
},
"id" => "eafd6569-aaac-442d-a64a-8df17051f0bc",
"links" => %{},
"meta" => %{},
"relationships" => %{"author" => %{"links" => %{}, "meta" => %{}}},
"type" => "author_role"
},
%{
"attributes" => %{
"author_id" => "d6d784a9-4c9c-45a3-8068-a5a30bbe8935",
"order" => 1,
"role" => "Translator"
},
"id" => "eafd6569-aaac-442d-a64a-8df17051f0ba",
"links" => %{},
"meta" => %{},
"relationships" => %{"author" => %{"links" => %{}, "meta" => %{}}},
"type" => "author_role"
}
],
"jsonapi" => %{"version" => "1.0"},
"links" => %{
"self" =>
"http://localhost:4000/api/v1/json/books/233987a5-f318-460b-9973-e553c99a0eaf/author_roles"
},
"meta" => %{}
}
assert {:ok, author_roles} = AuthorRolesTransformer.from_json(json_body)
assert [
%{
id: "eafd6569-aaac-442d-a64a-8df17051f0bc",
order: 0,
role: "",
author_id: "d6d784a9-4c9c-45a3-8068-a5a30bbe8934"
},
%{
id: "eafd6569-aaac-442d-a64a-8df17051f0ba",
order: 1,
role: "Translator",
author_id: "d6d784a9-4c9c-45a3-8068-a5a30bbe8935"
}
] = author_roles
end
end
end