Add transformers for BookId.
continuous-integration/drone/push Build is passing Details

dev
KKlochko 3 months ago
parent b64aa37c4f
commit d1a44a5457

@ -0,0 +1,20 @@
defmodule DecentralisedBookIndex.Sync.DataTransformers.BidTransformer 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"]),
type: get_in(json_body, ["attributes", "type"]),
bid: get_in(json_body, ["attributes", "bid"])
}
{:ok, attrs}
end
end

@ -0,0 +1,21 @@
defmodule DecentralisedBookIndex.Sync.DataTransformers.BidsTransformer do
alias DecentralisedBookIndex.Sync.DataTransformers.BidTransformer
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} = BidTransformer.from_json(record)
record
end
)}
end
end

@ -0,0 +1,51 @@
defmodule DecentralisedBookIndex.Sync.DataTransformers.BidTransformerTest do
use ExUnit.Case, async: true
alias DecentralisedBookIndex.Sync.DataTransformers.BidTransformer
describe "correct transformations" do
test "a json contains correct bid information" do
json_body =
%{
"data" => %{
"attributes" => %{"bid" => "1234567890", "order" => 0, "type" => "isbn"},
"id" => "0c0c0b3a-0410-4cb0-a7e3-b62bb392b097",
"links" => %{},
"meta" => %{},
"relationships" => %{},
"type" => "book_id"
}
}
assert {:ok, bid} = BidTransformer.from_json(json_body)
assert %{
id: "0c0c0b3a-0410-4cb0-a7e3-b62bb392b097",
order: 0,
type: "isbn",
bid: "1234567890"
} = bid
end
test "a json doesn't contains bid information \"data\" attribute" do
json_body =
%{
"attributes" => %{"bid" => "1234567890", "order" => 0, "type" => "isbn"},
"id" => "0c0c0b3a-0410-4cb0-a7e3-b62bb392b097",
"links" => %{},
"meta" => %{},
"relationships" => %{},
"type" => "book_id"
}
assert {:ok, bid} = BidTransformer.from_json(json_body)
assert %{
id: "0c0c0b3a-0410-4cb0-a7e3-b62bb392b097",
order: 0,
type: "isbn",
bid: "1234567890"
} = bid
end
end
end

@ -0,0 +1,54 @@
defmodule DecentralisedBookIndex.Sync.DataTransformers.BidsTransformerTest do
use ExUnit.Case, async: true
alias DecentralisedBookIndex.Sync.DataTransformers.BidsTransformer
describe "correct transformations" do
test "a json contains correct bids information" do
json_body =
%{
"data" => [
%{
"attributes" => %{"bid" => "1234567890", "order" => 0, "type" => "asin"},
"id" => "0c0c0b3a-0410-4cb0-a7e3-b62bb392b097",
"links" => %{},
"meta" => %{},
"relationships" => %{},
"type" => "book_id"
},
%{
"attributes" => %{"bid" => "1234567890123", "order" => 1, "type" => "isbn13"},
"id" => "31eafe52-0f39-49ad-8980-cdd448f9c738",
"links" => %{},
"meta" => %{},
"relationships" => %{},
"type" => "book_id"
}
],
"jsonapi" => %{"version" => "1.0"},
"links" => %{
"self" =>
"http://localhost:4000/api/v1/json/books/233987a5-f318-460b-9973-e553c99a0eaf/bids"
},
"meta" => %{}
}
assert {:ok, bids} = BidsTransformer.from_json(json_body)
assert [
%{
id: "0c0c0b3a-0410-4cb0-a7e3-b62bb392b097",
order: 0,
type: "asin",
bid: "1234567890"
},
%{
id: "31eafe52-0f39-49ad-8980-cdd448f9c738",
order: 1,
type: "isbn13",
bid: "1234567890123"
}
] = bids
end
end
end
Loading…
Cancel
Save