-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create endpoint to compare arbitrary pact versions, ignoring in…
…teraction/message order
- Loading branch information
Showing
8 changed files
with
183 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'pact_broker/json' | ||
|
||
module PactBroker | ||
module Pacts | ||
class SortVerifiableContent | ||
|
||
def self.call json | ||
hash = JSON.parse(json, PACT_PARSING_OPTIONS) | ||
verifiable_content = if hash['interactions'] | ||
hash['interactions'] | ||
elsif hash['messages'] | ||
hash['messages'] | ||
end | ||
order_verifiable_content(verifiable_content).to_json | ||
end | ||
|
||
def self.order_verifiable_content array | ||
array_with_ordered_hashes = order_hashes(array) | ||
array_with_ordered_hashes.sort{|a, b| a.to_json <=> b.to_json } | ||
end | ||
|
||
def self.order_hashes thing | ||
case thing | ||
when Hash then order_hash(thing) | ||
when Array then order_child_array(thing) | ||
else thing | ||
end | ||
end | ||
|
||
def self.order_child_array array | ||
array.collect{|thing| order_hashes(thing) } | ||
end | ||
|
||
def self.order_hash hash | ||
hash.keys.sort.each_with_object({}) do | key, new_hash | | ||
new_hash[key] = order_hashes(hash[key]) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
spec/lib/pact_broker/pacts/sort_verifiable_content_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'pact_broker/pacts/sort_verifiable_content' | ||
|
||
module PactBroker | ||
module Pacts | ||
describe SortVerifiableContent do | ||
let(:pact_content_1) do | ||
{ | ||
a: 1, | ||
interactions: [{ a: 1, b: 2 }, { a: 2, b: 3 }] | ||
}.to_json | ||
end | ||
|
||
let(:pact_content_2) do | ||
{ | ||
interactions: [{ b: 3, a: 2}, { b: 2, a: 1 }], | ||
a: 1 | ||
}.to_json | ||
end | ||
|
||
it "sorts the interactions/messages and keys in a deterministic way" do | ||
expect(SortVerifiableContent.call(pact_content_1).to_json).to eq(SortVerifiableContent.call(pact_content_2).to_json) | ||
end | ||
end | ||
end | ||
end |