Skip to content

Commit

Permalink
refactor: move interaction id key logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Apr 25, 2019
1 parent cd37785 commit 0d4cab9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
4 changes: 3 additions & 1 deletion lib/pact_broker/pacts/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def pact_specification_version
def add_ids(interactions)
interactions.map do | interaction |
if interaction.is_a?(Hash)
interaction.merge("id" => generate_interaction_sha(interaction))
# 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))
else
interaction
end
Expand Down
8 changes: 4 additions & 4 deletions lib/pact_broker/pacts/generate_interaction_sha.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
module PactBroker
module Pacts
module GenerateInteractionSha
def self.call interaction_hash, options = {}
def self.call interaction_hash
ordered_interaction_hash = interaction_hash.keys.sort.each_with_object({}) do | key, new_interaction_hash |
new_interaction_hash[key] = interaction_hash[key] unless key == "id"
new_interaction_hash[key] = interaction_hash[key]
end

Digest::SHA1.hexdigest(ordered_interaction_hash.to_json)
end

def generate_interaction_sha interaction_hash, options = {}
GenerateInteractionSha.call(interaction_hash, options)
def generate_interaction_sha(interaction_hash)
GenerateInteractionSha.call(interaction_hash)
end
end
end
Expand Down
18 changes: 14 additions & 4 deletions spec/lib/pact_broker/pacts/content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,28 @@ module Pacts
allow(GenerateInteractionSha).to receive(:call).and_return("some-id")
end

subject { Content.from_hash(pact_hash).with_ids }

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

it "removes the id 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(Content.from_hash(pact_hash).with_ids.interactions.first["id"]).to eq "some-id"
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
expect(Content.from_hash(pact_hash).with_ids.interactions.first).to eq interaction
expect(subject.interactions.first).to eq interaction
end
end

Expand All @@ -45,12 +56,11 @@ module Pacts
end

it "adds ids to the messages" do
expect(Content.from_hash(pact_hash).with_ids.messages.first["id"]).to eq "some-id"
expect(subject.messages.first["id"]).to eq "some-id"
end
end
end


describe "content_that_affects_verification_results" do

subject { Content.from_hash(pact_hash).content_that_affects_verification_results }
Expand Down
13 changes: 5 additions & 8 deletions spec/lib/pact_broker/pacts/generate_interaction_sha_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,22 @@ module Pacts
}
end

subject { GenerateInteractionSha.call(interaction_hash) }

it "generates a SHA based on the sorted keys" do
expect(GenerateInteractionSha.call(interaction_hash)).to eq "5ec1cc12132d3437a5a2ced5537cdab2d4f44521"
expect(subject).to eq "5ec1cc12132d3437a5a2ced5537cdab2d4f44521"
end

it "generates the same SHA if the top level keys are ordered differently" do
expect(GenerateInteractionSha.call(interaction_hash)).to eq GenerateInteractionSha.call(interaction_hash_with_different_key_order)
expect(subject).to eq GenerateInteractionSha.call(interaction_hash_with_different_key_order)
end

# This could be a whole lot smarter, but I'm not sure it's worth it.
# eg. order of provider state params doesn't matter, but the ordering
# of the provider states themselves may... who knows.
# Let's not try and be too smart about it until we have a use case to flesh it out.
it "generates a different SHA if any of the other keys are ordered differently" do
expect(GenerateInteractionSha.call(interaction_hash)).to_not eq GenerateInteractionSha.call(interaction_hash_with_different_params_order)
end

it "ignores any existing id in the hash" do
interaction_hash["id"] = "foo"
expect(GenerateInteractionSha.call(interaction_hash)).to eq "5ec1cc12132d3437a5a2ced5537cdab2d4f44521"
expect(subject).to_not eq GenerateInteractionSha.call(interaction_hash_with_different_params_order)
end
end
end
Expand Down

0 comments on commit 0d4cab9

Please sign in to comment.