@@ -13,54 +13,66 @@ defmodule Avrora.Schema.EncoderTest do
1313 { :ok , { type , _ , _ , _ , _ , fields , full_name , _ } } = Schema.Encoder . to_erlavro ( schema )
1414
1515 assert type == :avro_record_type
16- assert full_name == "io.confluent .Payment"
16+ assert full_name == "io.acme .Payment"
1717 assert length ( fields ) == 2
1818
19- assert schema . full_name == "io.confluent .Payment"
19+ assert schema . full_name == "io.acme .Payment"
2020 assert schema . json == payment_json ( )
2121 end
2222
23- test "when payload is a valid Enum schema " do
23+ test "when schema is Enum type " do
2424 { :ok , schema } = Schema.Encoder . from_json ( card_type_json ( ) )
2525 { :ok , { type , _ , _ , _ , _ , fields , full_name , _ } } = Schema.Encoder . to_erlavro ( schema )
2626
2727 assert type == :avro_enum_type
28- assert full_name == "io.confluent .CardType"
28+ assert full_name == "io.acme .CardType"
2929 assert length ( fields ) == 3
3030
31- assert schema . full_name == "io.confluent .CardType"
31+ assert schema . full_name == "io.acme .CardType"
3232 assert schema . json == card_type_json ( )
3333 end
3434
35- test "when payload is a valid Fixed schema " do
35+ test "when payload is Fixed type " do
3636 { :ok , schema } = Schema.Encoder . from_json ( crc32_json ( ) )
3737 { :ok , { type , _ , _ , _ , value , full_name , _ } } = Schema.Encoder . to_erlavro ( schema )
3838
3939 assert type == :avro_fixed_type
40- assert full_name == "io.confluent .CRC32"
40+ assert full_name == "io.acme .CRC32"
4141 assert value == 8
4242
43- assert schema . full_name == "io.confluent .CRC32"
43+ assert schema . full_name == "io.acme .CRC32"
4444 assert schema . json == crc32_json ( )
4545 end
4646
47- test "when payload is a valid json schema with external reference and callback returns valid schema" do
47+ test "when schema is Record type with primitive fields types" do
48+ { :ok , schema } = Schema.Encoder . from_json ( payment_json ( ) )
49+ { :ok , { type , _ , _ , _ , _ , fields , full_name , _ } } = Schema.Encoder . to_erlavro ( schema )
50+
51+ assert type == :avro_record_type
52+ assert full_name == "io.acme.Payment"
53+ assert length ( fields ) == 2
54+
55+ assert schema . full_name == "io.acme.Payment"
56+ assert schema . json == payment_json ( )
57+ end
58+
59+ test "when schema is Record type with nested type ref" do
4860 { :ok , schema } =
4961 Schema.Encoder . from_json ( message_with_reference_json ( ) , fn name ->
5062 case name do
51- "io.confluent.Attachment " -> { :ok , attachment_json ( ) }
52- "io.confluent.Signature " -> { :ok , signature_json ( ) }
63+ "io.acme.Signature " -> { :ok , signature_json ( ) }
64+ "io.acme.Attachment " -> { :ok , attachment_json ( ) }
5365 _ -> raise "unknown reference name!"
5466 end
5567 end )
5668
5769 { :ok , { type , _ , _ , _ , _ , fields , full_name , _ } } = Schema.Encoder . to_erlavro ( schema )
5870
5971 assert type == :avro_record_type
60- assert full_name == "io.confluent .Message"
72+ assert full_name == "io.acme .Message"
6173 assert length ( fields ) == 2
6274
63- assert schema . full_name == "io.confluent .Message"
75+ assert schema . full_name == "io.acme .Message"
6476 assert schema . json == message_json ( )
6577
6678 { :avro_record_field , _ , _ , body_type , _ , _ , _ } = List . first ( fields )
@@ -70,48 +82,46 @@ defmodule Avrora.Schema.EncoderTest do
7082 { :avro_array_type , { type , _ , _ , _ , _ , fields , full_name , _ } , [ ] } = attachments_type
7183
7284 assert type == :avro_record_type
73- assert full_name == "io.confluent .Attachment"
85+ assert full_name == "io.acme .Attachment"
7486 assert length ( fields ) == 2
7587
7688 { :avro_record_field , _ , _ , signature_type , _ , _ , _ } = List . last ( fields )
7789 { type , _ , _ , _ , _ , fields , full_name , _ } = signature_type
7890
7991 assert type == :avro_record_type
80- assert full_name == "io.confluent .Signature"
92+ assert full_name == "io.acme .Signature"
8193 assert length ( fields ) == 1
8294 end
8395
84- test "when payload is a valid json schema with external reference and callback returns invalid schema" do
96+ test "when schema is Record type with type ref of invalid schema" do
8597 result =
8698 Schema.Encoder . from_json ( message_with_reference_json ( ) , fn name ->
87- assert name == "io.confluent .Attachment"
99+ assert name == "io.acme .Attachment"
88100 { :ok , ~s( {}) }
89101 end )
90102
91103 assert { :error , { :not_found , "type" } } == result
92104 end
93105
94- test "when payload is a valid json schema with external reference and callback returns error " do
106+ test "when schema is Record type with type ref and resolution failed " do
95107 result =
96108 Schema.Encoder . from_json ( message_with_reference_json ( ) , fn name ->
97- assert name == "io.confluent .Attachment"
109+ assert name == "io.acme .Attachment"
98110 { :error , :bad_thing_happen }
99111 end )
100112
101113 assert { :error , :bad_thing_happen } == result
102114 end
103115
104- test "when payload is a valid json schema with external reference and no callback is given" do
105- assert { :error , { :not_found , "type" } } == Schema.Encoder . from_json ( message_with_reference_json ( ) )
106- end
107-
108- test "when payload is not a named type schema" do
109- assert Schema.Encoder . from_json ( unnamed_json ( ) ) == { :error , :unnamed_type }
116+ test "when schema is Record type with type ref and lookup function given" do
117+ assert { :error , :undefined_reference_lookup_function } ==
118+ Schema.Encoder . from_json ( message_with_reference_json ( ) )
110119 end
111120
112- test "when payload is an invalid json schema " do
121+ test "when schema is an invalid" do
113122 assert Schema.Encoder . from_json ( "a:b" ) == { :error , "argument error" }
114123 assert Schema.Encoder . from_json ( "{}" ) == { :error , { :not_found , "type" } }
124+ assert Schema.Encoder . from_json ( "[null]" ) == { :error , :invalid_schema }
115125 end
116126 end
117127
@@ -121,7 +131,7 @@ defmodule Avrora.Schema.EncoderTest do
121131 { :ok , { type , _ , _ , _ , _ , fields , full_name , _ } } = Schema.Encoder . to_erlavro ( schema )
122132
123133 assert type == :avro_record_type
124- assert full_name == "io.confluent .Payment"
134+ assert full_name == "io.acme .Payment"
125135 assert length ( fields ) == 2
126136 end
127137 end
@@ -133,7 +143,7 @@ defmodule Avrora.Schema.EncoderTest do
133143 assert is_nil ( schema . id )
134144 assert is_nil ( schema . version )
135145
136- assert schema . full_name == "io.confluent .Payment"
146+ assert schema . full_name == "io.acme .Payment"
137147 assert schema . json == payment_json ( )
138148 end
139149
@@ -143,7 +153,7 @@ defmodule Avrora.Schema.EncoderTest do
143153 assert is_nil ( schema . id )
144154 assert is_nil ( schema . version )
145155
146- assert schema . full_name == "io.confluent .Payment"
156+ assert schema . full_name == "io.acme .Payment"
147157 assert schema . json == "{}"
148158 end
149159
@@ -153,38 +163,37 @@ defmodule Avrora.Schema.EncoderTest do
153163 end
154164
155165 defp payment_erlavro do
156- { :avro_record_type , "Payment" , "io.confluent " , "" , [ ] ,
166+ { :avro_record_type , "Payment" , "io.acme " , "" , [ ] ,
157167 [
158168 { :avro_record_field , "id" , "" , { :avro_primitive_type , "string" , [ ] } , :undefined , :ascending , [ ] } ,
159169 { :avro_record_field , "amount" , "" , { :avro_primitive_type , "double" , [ ] } , :undefined , :ascending , [ ] }
160- ] , "io.confluent .Payment" , [ ] }
170+ ] , "io.acme .Payment" , [ ] }
161171 end
162172
163173 defp unnamed_erlavro , do: { :avro_array_type , { :avro_primitive_type , "string" , [ ] } , [ ] }
164- defp unnamed_json , do: ~s( {"type":"array","items":"string","default":[]})
165- defp crc32_json , do: ~s( {"namespace":"io.confluent","name":"CRC32","type":"fixed","size":8})
174+ defp crc32_json , do: ~s( {"namespace":"io.acme","name":"CRC32","type":"fixed","size":8})
166175
167176 defp signature_json do
168- ~s( {"namespace":"io.confluent ","name":"Signature","type":"record","fields":[{"name":"checksum","type":{"name":"SignatureChecksum","type":"fixed","size":1048576}}]})
177+ ~s( {"namespace":"io.acme ","name":"Signature","type":"record","fields":[{"name":"checksum","type":{"name":"SignatureChecksum","type":"fixed","size":1048576}}]})
169178 end
170179
171180 defp attachment_json do
172- ~s( {"namespace":"io.confluent ","name":"Attachment","type":"record","fields":[{"name":"name","type":"string"},{"name":"signature","type":"io.confluent .Signature"}]})
181+ ~s( {"namespace":"io.acme ","name":"Attachment","type":"record","fields":[{"name":"name","type":"string"},{"name":"signature","type":"io.acme .Signature"}]})
173182 end
174183
175184 defp payment_json do
176- ~s( {"namespace":"io.confluent ","name":"Payment","type":"record","fields":[{"name":"id","type":"string"},{"name":"amount","type":"double"}]})
185+ ~s( {"namespace":"io.acme ","name":"Payment","type":"record","fields":[{"name":"id","type":"string"},{"name":"amount","type":"double"}]})
177186 end
178187
179188 defp card_type_json do
180- ~s( {"namespace":"io.confluent ","name":"CardType","type":"enum","symbols":["MASTERCARD","VISA","AMERICANEXPRESS"]})
189+ ~s( {"namespace":"io.acme ","name":"CardType","type":"enum","symbols":["MASTERCARD","VISA","AMERICANEXPRESS"]})
181190 end
182191
183192 defp message_with_reference_json do
184- ~s( {"namespace":"io.confluent ","name":"Message","type":"record","fields":[{"name":"body","type":"string"},{"name":"attachments","type":{"type":"array","items":"io.confluent .Attachment"}}]})
193+ ~s( {"namespace":"io.acme ","name":"Message","type":"record","fields":[{"name":"body","type":"string"},{"name":"attachments","type":{"type":"array","items":"io.acme .Attachment"}}]})
185194 end
186195
187196 defp message_json do
188- ~s( {"namespace":"io.confluent ","name":"Message","type":"record","fields":[{"name":"body","type":"string"},{"name":"attachments","type":{"type":"array","items":{"name":"Attachment","type":"record","fields":[{"name":"name","type":"string"},{"name":"signature","type":{"name":"Signature","type":"record","fields":[{"name":"checksum","type":{"name":"SignatureChecksum","type":"fixed","size":1048576}}]}}]}}}]})
197+ ~s( {"namespace":"io.acme ","name":"Message","type":"record","fields":[{"name":"body","type":"string"},{"name":"attachments","type":{"type":"array","items":{"name":"Attachment","type":"record","fields":[{"name":"name","type":"string"},{"name":"signature","type":{"name":"Signature","type":"record","fields":[{"name":"checksum","type":{"name":"SignatureChecksum","type":"fixed","size":1048576}}]}}]}}}]})
189198 end
190199end
0 commit comments