Skip to content

Commit

Permalink
feat: allow interaction ids to be manually set for test data
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Sep 11, 2019
1 parent bf32f07 commit 4e25610
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
36 changes: 28 additions & 8 deletions lib/pact_broker/pacts/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/test/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/pact_broker/pacts/content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4e25610

Please sign in to comment.