Skip to content

Commit

Permalink
tests: Include test for null handling by the JSON formatter (#45)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Azuma <[email protected]>
  • Loading branch information
dazuma authored Feb 27, 2021
1 parent 4aafd06 commit 581b4a9
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions test/test_json_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,23 @@
"type" => my_type
}
end
let :my_incomplete_struct_v1 do
{
"data" => my_json_data,
"datacontenttype" => my_content_type_string,
"dataschema" => nil,
"id" => my_id,
"source" => my_source_string,
"specversion" => spec_version_v1,
"time" => nil,
"type" => my_type
}
end
let(:my_json_struct_v1_string) { JSON.dump my_json_struct_v1 }
let(:my_batch_v1_string) { JSON.dump [my_base64_struct_v1, my_json_struct_v1] }
let :my_compacted_incomplete_struct_v1 do
my_incomplete_struct_v1.delete_if { |_k, v| v.nil? }
end

it "decodes and encodes a struct with string data" do
event = json_format.decode_hash_structure my_string_struct_v1
Expand Down Expand Up @@ -304,5 +319,23 @@
string = json_format.encode_batch events, sort: true
assert_equal my_batch_v1_string, string
end

it "decodes and encodes json with nulls" do
event = json_format.decode_hash_structure my_incomplete_struct_v1
assert_equal my_id, event.id
assert_equal my_source, event.source
assert_equal my_type, event.type
assert_equal spec_version_v1, event.spec_version
assert_equal my_json_data, event.data
assert_equal my_content_type, event.data_content_type
assert_nil event.data_schema
assert_nil event.subject
assert_nil event.time
hash = event.to_h
refute hash.include? "subject"
refute hash.include? "time"
struct = json_format.encode_hash_structure event
assert_equal my_compacted_incomplete_struct_v1, struct
end
end
end

0 comments on commit 581b4a9

Please sign in to comment.