Skip to content

Commit

Permalink
Add more integration tests for conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
Strech committed Nov 26, 2023
1 parent d565f9f commit 3b55613
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions test/avrora/codec/plain_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ defmodule Avrora.Codec.PlainTest do
test "when payload is a valid binary and null values must be as is" do
stub(Avrora.ConfigMock, :convert_null_values, fn -> false end)

{:ok, decoded} = Codec.Plain.decode(null_value_message(), schema: null_value_schema())
{:ok, decoded} = Codec.Plain.decode(convertable_message(), schema: convertable_schema())

assert decoded == %{"key" => "user-1", "value" => :null}
assert decoded == %{"birthday" => ~D[2016-10-26], "guests" => :null}
end

test "when payload is a valid binary and null values must be converted" do
{:ok, decoded} = Codec.Plain.decode(null_value_message(), schema: null_value_schema())
{:ok, decoded} = Codec.Plain.decode(convertable_message(), schema: convertable_schema())

assert decoded == %{"key" => "user-1", "value" => nil}
assert decoded == %{"birthday" => ~D[2016-10-26], "guests" => nil}
end

test "when payload is a valid binary and map type must be decoded as proplist" do
Expand Down Expand Up @@ -139,9 +139,18 @@ defmodule Avrora.Codec.PlainTest do
test "when decoding message and logical types must be as is" do
stub(Avrora.ConfigMock, :decode_logical_types, fn -> false end)

{:ok, decoded} = Codec.Plain.decode(logical_type_message(), schema: logical_type_schema())
{:ok, decoded} = Codec.Plain.decode(convertable_message(), schema: convertable_schema())

assert decoded == %{"birthday" => 17100}
assert decoded == %{"birthday" => 17100, "guests" => nil}
end

test "when decoding message and all types must be as is" do
stub(Avrora.ConfigMock, :convert_null_values, fn -> false end)
stub(Avrora.ConfigMock, :decode_logical_types, fn -> false end)

{:ok, decoded} = Codec.Plain.decode(convertable_message(), schema: convertable_schema())

assert decoded == %{"birthday" => 17100, "guests" => :null}
end
end

Expand Down Expand Up @@ -280,8 +289,7 @@ defmodule Avrora.Codec.PlainTest do
48, 48, 48, 48, 48, 48, 48, 48, 48, 123, 20, 174, 71, 225, 250, 47, 64>>
end

defp null_value_message, do: <<12, 117, 115, 101, 114, 45, 49, 0>>
defp logical_type_message, do: <<152, 139, 2>>
defp convertable_message, do: <<152, 139, 2, 0>>
defp map_message, do: <<1, 20, 6, 107, 101, 121, 10, 118, 97, 108, 117, 101, 0>>
defp payment_payload, do: %{"id" => "00000000-0000-0000-0000-000000000000", "amount" => 15.99}

Expand All @@ -290,11 +298,6 @@ defmodule Avrora.Codec.PlainTest do
%{schema | id: nil, version: nil}
end

defp null_value_schema do
{:ok, schema} = Schema.Encoder.from_json(null_value_json_schema())
%{schema | id: nil, version: nil}
end

defp map_schema do
{:ok, schema} = Schema.Encoder.from_json(map_json_schema())
%{schema | id: nil, version: nil}
Expand All @@ -315,19 +318,15 @@ defmodule Avrora.Codec.PlainTest do
%{schema | id: nil, version: nil}
end

defp logical_type_schema do
{:ok, schema} = Schema.Encoder.from_json(logical_type_json_schema())
defp convertable_schema do
{:ok, schema} = Schema.Encoder.from_json(converterable_json_schema())
%{schema | id: nil, version: nil}
end

defp payment_json_schema do
~s({"namespace":"io.confluent","name":"Payment","type":"record","fields":[{"name":"id","type":"string"},{"name":"amount","type":"double"}]})
end

defp null_value_json_schema do
~s({"namespace":"io.confluent","name":"Null_Value","type":"record","fields":[{"name":"key","type":"string"},{"name":"value","type":["null","int"]}]})
end

defp map_json_schema do
~s({"namespace":"io.confluent","name":"Map_Value","type":"record","fields":[{"name":"map_field", "type": {"type": "map", "values": "string"}}]})
end
Expand All @@ -344,7 +343,7 @@ defmodule Avrora.Codec.PlainTest do
~s({"namespace":"io.confluent","name":"Union_Value","type":"record","fields":[{"name":"union_field","type":[{"type":"record","name":"as_str","fields":[{"name":"value","type":"string"}]},{"type":"record","name":"as_int","fields":[{"name":"value","type":"int"}]}]}]})
end

defp logical_type_json_schema do
~s({"namespace":"io.confluent","name":"Logical_Type","type":"record","fields":[{"name":"birthday","type":{"type":"int","logicalType":"Date"}}]})
defp converterable_json_schema do
~s({"namespace":"io.confluent","name":"Converter","type":"record","fields":[{"name":"birthday","type":{"type":"int","logicalType":"Date"}},{"name":"guests","type":["null","int"]}]})
end
end

0 comments on commit 3b55613

Please sign in to comment.