Skip to content

Commit 581b4a9

Browse files
authored
tests: Include test for null handling by the JSON formatter (#45)
Signed-off-by: Daniel Azuma <dazuma@google.com>
1 parent 4aafd06 commit 581b4a9

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

test/test_json_format.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,23 @@
215215
"type" => my_type
216216
}
217217
end
218+
let :my_incomplete_struct_v1 do
219+
{
220+
"data" => my_json_data,
221+
"datacontenttype" => my_content_type_string,
222+
"dataschema" => nil,
223+
"id" => my_id,
224+
"source" => my_source_string,
225+
"specversion" => spec_version_v1,
226+
"time" => nil,
227+
"type" => my_type
228+
}
229+
end
218230
let(:my_json_struct_v1_string) { JSON.dump my_json_struct_v1 }
219231
let(:my_batch_v1_string) { JSON.dump [my_base64_struct_v1, my_json_struct_v1] }
232+
let :my_compacted_incomplete_struct_v1 do
233+
my_incomplete_struct_v1.delete_if { |_k, v| v.nil? }
234+
end
220235

221236
it "decodes and encodes a struct with string data" do
222237
event = json_format.decode_hash_structure my_string_struct_v1
@@ -304,5 +319,23 @@
304319
string = json_format.encode_batch events, sort: true
305320
assert_equal my_batch_v1_string, string
306321
end
322+
323+
it "decodes and encodes json with nulls" do
324+
event = json_format.decode_hash_structure my_incomplete_struct_v1
325+
assert_equal my_id, event.id
326+
assert_equal my_source, event.source
327+
assert_equal my_type, event.type
328+
assert_equal spec_version_v1, event.spec_version
329+
assert_equal my_json_data, event.data
330+
assert_equal my_content_type, event.data_content_type
331+
assert_nil event.data_schema
332+
assert_nil event.subject
333+
assert_nil event.time
334+
hash = event.to_h
335+
refute hash.include? "subject"
336+
refute hash.include? "time"
337+
struct = json_format.encode_hash_structure event
338+
assert_equal my_compacted_incomplete_struct_v1, struct
339+
end
307340
end
308341
end

0 commit comments

Comments
 (0)