Skip to content

Commit 4e25610

Browse files
committed
feat: allow interaction ids to be manually set for test data
1 parent bf32f07 commit 4e25610

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

lib/pact_broker/pacts/content.rb

+28-8
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,26 @@ def sort
3131
Content.from_hash(SortContent.call(pact_hash))
3232
end
3333

34-
def with_ids
34+
def with_test_results(test_results)
3535
new_pact_hash = pact_hash.dup
3636
if interactions && interactions.is_a?(Array)
37-
new_pact_hash['interactions'] = add_ids(interactions)
37+
new_pact_hash['interactions'] = merge_verification_results(interactions, test_results)
3838
end
3939

4040
if messages && messages.is_a?(Array)
41-
new_pact_hash['messages'] = add_ids(messages)
41+
new_pact_hash['messages'] = merge_verification_results(messages, test_results)
42+
end
43+
Content.from_hash(new_pact_hash)
44+
end
45+
46+
def with_ids(overwrite_existing_id = true)
47+
new_pact_hash = pact_hash.dup
48+
if interactions && interactions.is_a?(Array)
49+
new_pact_hash['interactions'] = add_ids(interactions, overwrite_existing_id)
50+
end
51+
52+
if messages && messages.is_a?(Array)
53+
new_pact_hash['messages'] = add_ids(messages, overwrite_existing_id)
4254
end
4355
Content.from_hash(new_pact_hash)
4456
end
@@ -64,6 +76,10 @@ def interactions
6476
pact_hash.is_a?(Hash) ? pact_hash['interactions'] : nil
6577
end
6678

79+
def messages_or_interactions
80+
messages || interactions
81+
end
82+
6783
def pact_specification_version
6884
maybe_pact_specification_version_1 = pact_hash['metadata']['pactSpecification']['version'] rescue nil
6985
maybe_pact_specification_version_2 = pact_hash['metadata']['pact-specification']['version'] rescue nil
@@ -75,13 +91,17 @@ def pact_specification_version
7591

7692
attr_reader :pact_hash
7793

78-
def add_ids(interactions)
94+
def add_ids(interactions, overwrite_existing_id)
7995
interactions.map do | interaction |
8096
if interaction.is_a?(Hash)
81-
# just in case there is a previous ID in there
82-
interaction_without_id = interaction.reject { |k, _| k == "_id" }
83-
# make the _id the first key in the hash when rendered to JSON
84-
{ "_id" => generate_interaction_sha(interaction_without_id) }.merge(interaction)
97+
if !interaction.key?("_id") || overwrite_existing_id
98+
# just in case there is a previous ID in there
99+
interaction_without_id = interaction.reject { |k, _| k == "_id" }
100+
# make the _id the first key in the hash when rendered to JSON
101+
{ "_id" => generate_interaction_sha(interaction_without_id) }.merge(interaction)
102+
else
103+
interaction
104+
end
85105
else
86106
interaction
87107
end

lib/pact_broker/test/test_data_builder.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def generate_pact_version_sha json_content
331331
end
332332

333333
def prepare_json_content(json_content)
334-
PactBroker::Pacts::Content.from_json(json_content).with_ids.to_json
334+
PactBroker::Pacts::Content.from_json(json_content).with_ids(false).to_json
335335
end
336336

337337
def set_created_at_if_set created_at, table_name, selector

spec/lib/pact_broker/pacts/content_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ module Pacts
5959
expect(subject.messages.first["_id"]).to eq "some-id"
6060
end
6161
end
62+
63+
context "when override_existing_ids = false (for setting up test data)" do
64+
let(:interaction) { { "_id" => "1", "foo" => "bar" } }
65+
66+
it "does not override the existing ids" do
67+
expect(subject.interactions.first["_id"]).to eq "1"
68+
end
69+
end
6270
end
6371

6472
describe "content_that_affects_verification_results" do

0 commit comments

Comments
 (0)