Skip to content

Commit

Permalink
feat: use _id instead of id for the interaction identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Apr 25, 2019
1 parent 0d4cab9 commit 010a14a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
5 changes: 3 additions & 2 deletions lib/pact_broker/pacts/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ def add_ids(interactions)
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" }
interaction.merge("id" => generate_interaction_sha(interaction_without_id))
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
Expand Down
16 changes: 8 additions & 8 deletions spec/lib/pact_broker/pacts/content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ module Pacts

subject { Content.from_hash(pact_hash).with_ids }

context "when the interaction has an existing id" do
let(:interaction) { { "id" => "blah", "foo" => "bar" } }
context "when the interaction has an existing _id" do
let(:interaction) { { "_id" => "blah", "foo" => "bar" } }

it "removes the id before creating the sha" do
it "removes the i_d before creating the sha" do
expect(GenerateInteractionSha).to receive(:call).with("foo" => "bar")
subject
end
end

context "when the interaction is a hash" do
it "adds ids to the interactions" do
expect(subject.interactions.first["id"]).to eq "some-id"
it "adds an _id to each interaction" do
expect(subject.interactions.first["_id"]).to eq "some-id"
end
end

context "when the interaction is not a hash" do
let(:interaction) { 1 }

it "does not add an id" do
it "does not add an _id" do
expect(subject.interactions.first).to eq interaction
end
end
Expand All @@ -55,8 +55,8 @@ module Pacts
}
end

it "adds ids to the messages" do
expect(subject.messages.first["id"]).to eq "some-id"
it "adds an _id to each message" do
expect(subject.messages.first["_id"]).to eq "some-id"
end
end
end
Expand Down

0 comments on commit 010a14a

Please sign in to comment.