diff --git a/lib/pact_broker/pacts/content.rb b/lib/pact_broker/pacts/content.rb index d38e7fd0f..1a5d7e6f6 100644 --- a/lib/pact_broker/pacts/content.rb +++ b/lib/pact_broker/pacts/content.rb @@ -31,14 +31,26 @@ def sort Content.from_hash(SortContent.call(pact_hash)) end - def with_ids + def with_test_results(test_results) new_pact_hash = pact_hash.dup if interactions && interactions.is_a?(Array) - new_pact_hash['interactions'] = add_ids(interactions) + new_pact_hash['interactions'] = merge_verification_results(interactions, test_results) end if messages && messages.is_a?(Array) - new_pact_hash['messages'] = add_ids(messages) + new_pact_hash['messages'] = merge_verification_results(messages, test_results) + end + Content.from_hash(new_pact_hash) + end + + def with_ids(overwrite_existing_id = true) + new_pact_hash = pact_hash.dup + if interactions && interactions.is_a?(Array) + new_pact_hash['interactions'] = add_ids(interactions, overwrite_existing_id) + end + + if messages && messages.is_a?(Array) + new_pact_hash['messages'] = add_ids(messages, overwrite_existing_id) end Content.from_hash(new_pact_hash) end @@ -64,6 +76,10 @@ def interactions pact_hash.is_a?(Hash) ? pact_hash['interactions'] : nil end + def messages_or_interactions + messages || interactions + end + def pact_specification_version maybe_pact_specification_version_1 = pact_hash['metadata']['pactSpecification']['version'] rescue nil maybe_pact_specification_version_2 = pact_hash['metadata']['pact-specification']['version'] rescue nil @@ -75,13 +91,17 @@ def pact_specification_version attr_reader :pact_hash - def add_ids(interactions) + def add_ids(interactions, overwrite_existing_id) interactions.map do | interaction | if interaction.is_a?(Hash) - # just in case there is a previous ID in there - interaction_without_id = interaction.reject { |k, _| k == "_id" } - # make the _id the first key in the hash when rendered to JSON - { "_id" => generate_interaction_sha(interaction_without_id) }.merge(interaction) + if !interaction.key?("_id") || overwrite_existing_id + # just in case there is a previous ID in there + interaction_without_id = interaction.reject { |k, _| k == "_id" } + # make the _id the first key in the hash when rendered to JSON + { "_id" => generate_interaction_sha(interaction_without_id) }.merge(interaction) + else + interaction + end else interaction end diff --git a/lib/pact_broker/test/test_data_builder.rb b/lib/pact_broker/test/test_data_builder.rb index 3f111db9d..e1400e51f 100644 --- a/lib/pact_broker/test/test_data_builder.rb +++ b/lib/pact_broker/test/test_data_builder.rb @@ -331,7 +331,7 @@ def generate_pact_version_sha json_content end def prepare_json_content(json_content) - PactBroker::Pacts::Content.from_json(json_content).with_ids.to_json + PactBroker::Pacts::Content.from_json(json_content).with_ids(false).to_json end def set_created_at_if_set created_at, table_name, selector diff --git a/spec/lib/pact_broker/pacts/content_spec.rb b/spec/lib/pact_broker/pacts/content_spec.rb index cfc6c5823..69d8e3645 100644 --- a/spec/lib/pact_broker/pacts/content_spec.rb +++ b/spec/lib/pact_broker/pacts/content_spec.rb @@ -59,6 +59,14 @@ module Pacts expect(subject.messages.first["_id"]).to eq "some-id" end end + + context "when override_existing_ids = false (for setting up test data)" do + let(:interaction) { { "_id" => "1", "foo" => "bar" } } + + it "does not override the existing ids" do + expect(subject.interactions.first["_id"]).to eq "1" + end + end end describe "content_that_affects_verification_results" do