-
-
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: allow pact equality to be based only on the content that affect…
…s verification results
- Loading branch information
Showing
19 changed files
with
585 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'pact_broker/pacts/parse' | ||
require 'pact_broker/pacts/sort_content' | ||
|
||
module PactBroker | ||
module Pacts | ||
class Content | ||
|
||
def initialize pact_hash | ||
@pact_hash = pact_hash | ||
end | ||
|
||
def self.from_json json_content | ||
new(Parse.call(json_content)) | ||
end | ||
|
||
def self.from_hash pact_hash | ||
new(pact_hash) | ||
end | ||
|
||
def to_hash | ||
pact_hash | ||
end | ||
|
||
def to_json | ||
pact_hash.to_json | ||
end | ||
|
||
def sort | ||
Content.from_hash(SortContent.call(pact_hash)) | ||
end | ||
|
||
# Half thinking this belongs in GenerateSha | ||
def content_that_affects_verification_results | ||
if interactions || messages | ||
cont = {} | ||
cont['interactions'] = interactions if interactions | ||
cont['messages'] = messages if messages | ||
cont['pact_specification_version'] = pact_specification_version if pact_specification_version | ||
cont | ||
else | ||
pact_hash | ||
end | ||
end | ||
|
||
def messages | ||
pact_hash.is_a?(Hash) ? pact_hash['messages'] : nil | ||
end | ||
|
||
def interactions | ||
pact_hash.is_a?(Hash) ? pact_hash['interactions'] : nil | ||
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 | ||
maybe_pact_specification_version_3 = pact_hash['metadata'] && pact_hash['metadata']['pactSpecificationVersion'] rescue nil | ||
maybe_pact_specification_version_1 || maybe_pact_specification_version_2 || maybe_pact_specification_version_3 | ||
end | ||
|
||
private | ||
|
||
attr_reader :pact_hash | ||
|
||
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
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,24 @@ | ||
require 'digest/sha1' | ||
require 'pact_broker/configuration' | ||
require 'pact_broker/pacts/sort_content' | ||
require 'pact_broker/pacts/parse' | ||
require 'pact_broker/pacts/content' | ||
|
||
module PactBroker | ||
module Pacts | ||
class GenerateSha | ||
def self.call json_content, options = {} | ||
content_for_sha = if PactBroker.configuration.base_equality_only_on_content_that_affects_verification_results | ||
extract_verifiable_content_for_sha(json_content) | ||
else | ||
json_content | ||
end | ||
Digest::SHA1.hexdigest(content_for_sha) | ||
end | ||
|
||
def self.extract_verifiable_content_for_sha json_content | ||
Content.from_json(json_content).sort.content_that_affects_verification_results.to_json | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'pact_broker/json' | ||
|
||
module PactBroker | ||
module Pacts | ||
class Parse | ||
def self.call(json) | ||
JSON.parse(json, PACT_PARSING_OPTIONS) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'pact_broker/json' | ||
|
||
module PactBroker | ||
module Pacts | ||
class SortContent | ||
def self.call pact_hash | ||
key = verifiable_content_key_for(pact_hash) | ||
|
||
if key | ||
content = pact_hash[key] | ||
sorted_pact_hash = order_object(pact_hash) | ||
sorted_pact_hash[key] = order_verifiable_content(content) | ||
sorted_pact_hash | ||
else | ||
pact_hash | ||
end | ||
end | ||
|
||
def self.verifiable_content_key_for pact_hash | ||
if pact_hash['interactions'] | ||
'interactions' | ||
elsif pact_hash['messages'] | ||
'messages' | ||
else | ||
nil | ||
end | ||
end | ||
|
||
|
||
def self.order_verifiable_content array | ||
array_with_ordered_hashes = order_object(array) | ||
array_with_ordered_hashes.sort{|a, b| a.to_json <=> b.to_json } | ||
end | ||
|
||
def self.order_object 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_object(thing) } | ||
end | ||
|
||
def self.order_hash hash | ||
hash.keys.sort.each_with_object({}) do | key, new_hash | | ||
new_hash[key] = order_object(hash[key]) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
spec/features/base_equality_only_on_content_that_affects_verification_results_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,34 @@ | ||
RSpec.describe "base_equality_only_on_content_that_affects_verification_results" do | ||
let(:td) { TestDataBuilder.new } | ||
let(:json_content_1) { load_fixture('foo-bar.json') } | ||
let(:json_content_2) do | ||
pact_hash = load_json_fixture('foo-bar.json') | ||
pact_hash['interactions'] = pact_hash['interactions'].reverse | ||
pact_hash.to_json | ||
end | ||
let(:base_equality_only_on_content_that_affects_verification_results) { true } | ||
|
||
before do | ||
PactBroker.configuration.base_equality_only_on_content_that_affects_verification_results = base_equality_only_on_content_that_affects_verification_results | ||
td.create_pact_with_hierarchy("Foo", "1", "Bar", json_content_1) | ||
.create_verification(provider_version: "5") | ||
.create_consumer_version("2") | ||
.create_pact(json_content: json_content_2) | ||
end | ||
|
||
context "when a pact is published with a different order of interactions to a previous version, but which is otherwise the same" do | ||
context "when base_equality_only_on_content_that_affects_verification_results is true" do | ||
it "applies the verifications from the previous version" do | ||
expect(PactBroker::Matrix::Row.all).to contain_hash(consumer_version_number: "2", provider_version_number: "5") | ||
end | ||
end | ||
|
||
context "when base_equality_only_on_content_that_affects_verification_results is false" do | ||
let(:base_equality_only_on_content_that_affects_verification_results) { false } | ||
|
||
it "does not apply the verifications from the previous version" do | ||
expect(PactBroker::Matrix::Row.all).to_not contain_hash(consumer_version_number: "2", provider_version_number: "5") | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.