parent
4d48795ccd
commit
b64aa37c4f
@ -0,0 +1,20 @@
|
||||
defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorRoleTransformer do
|
||||
def from_json(json_body) do
|
||||
json_body =
|
||||
if Map.has_key?(json_body, "data") do
|
||||
json_body["data"]
|
||||
else
|
||||
json_body
|
||||
end
|
||||
|
||||
attrs =
|
||||
%{
|
||||
id: get_in(json_body, ["id"]),
|
||||
order: get_in(json_body, ["attributes", "order"]),
|
||||
role: get_in(json_body, ["attributes", "role"]),
|
||||
author_id: get_in(json_body, ["attributes", "author_id"])
|
||||
}
|
||||
|
||||
{:ok, attrs}
|
||||
end
|
||||
end
|
@ -0,0 +1,21 @@
|
||||
defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorRolesTransformer do
|
||||
alias DecentralisedBookIndex.Sync.DataTransformers.AuthorRoleTransformer
|
||||
|
||||
def from_json(json_body) do
|
||||
json_body =
|
||||
if Map.has_key?(json_body, "data") do
|
||||
json_body["data"]
|
||||
else
|
||||
json_body
|
||||
end
|
||||
|
||||
{:ok,
|
||||
Enum.map(
|
||||
json_body,
|
||||
fn record ->
|
||||
{:ok, record} = AuthorRoleTransformer.from_json(record)
|
||||
record
|
||||
end
|
||||
)}
|
||||
end
|
||||
end
|
@ -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
|
Loading…
Reference in new issue