Add a data transformer to transform a json to Author.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
2af5f6cf42
commit
ecdd4089e5
@ -0,0 +1,14 @@
|
||||
defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer do
|
||||
|
||||
alias DecentralisedBookIndex.Metadata.Author
|
||||
|
||||
def from_json(json_body) do
|
||||
author = %Author{
|
||||
id: get_in(json_body, ["data", "id"]),
|
||||
name: get_in(json_body, ["data", "attributes", "name"]),
|
||||
description: get_in(json_body, ["data", "attributes", "description"]),
|
||||
}
|
||||
|
||||
{:ok, author}
|
||||
end
|
||||
end
|
@ -0,0 +1,33 @@
|
||||
defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformerTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
alias DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer
|
||||
alias DecentralisedBookIndex.Metadata.Author
|
||||
|
||||
describe "correct transformations" do
|
||||
test "a json contains correct author information" do
|
||||
json_body = %{
|
||||
"data" => %{
|
||||
"attributes" => %{"description" => "Something", "name" => "Author"},
|
||||
"id" => "889a323e-d104-4b5d-b276-dad5a9b1da9d",
|
||||
"links" => %{},
|
||||
"meta" => %{},
|
||||
"relationships" => %{},
|
||||
"type" => "author"
|
||||
},
|
||||
"jsonapi" => %{"version" => "1.0"},
|
||||
"links" => %{
|
||||
"self" => "http://localhost:4000/api/v1/json/author/889a323e-d104-4b5d-b276-dad5a9b1da9d"
|
||||
},
|
||||
"meta" => %{}
|
||||
}
|
||||
|
||||
assert {:ok, author} = AuthorTransformer.from_json(json_body)
|
||||
assert %Author{
|
||||
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
|
||||
name: "Author",
|
||||
description: "Something"
|
||||
} = author
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue