From 1d2adc6f490ed7ad4b80a87c1d505330971bbabb Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 11 Jan 2018 16:36:24 +1100 Subject: [PATCH] feat: ensure latest pact version is returned when searching by pact version sha --- lib/pact_broker/pacts/repository.rb | 11 +++++++++-- spec/lib/pact_broker/pacts/repository_spec.rb | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/pact_broker/pacts/repository.rb b/lib/pact_broker/pacts/repository.rb index 6e83efa56..b0c2af498 100644 --- a/lib/pact_broker/pacts/repository.rb +++ b/lib/pact_broker/pacts/repository.rb @@ -103,13 +103,20 @@ def find_latest_pact(consumer_name, provider_name, tag = nil) end def find_pact consumer_name, consumer_version, provider_name, pact_version_sha = nil - query = pact_version_sha ? AllPactPublications.pact_version_sha(pact_version_sha) : LatestPactPublicationsByConsumerVersion + query = if pact_version_sha + AllPactPublications + .pact_version_sha(pact_version_sha) + .reverse_order(:consumer_version_order) + .limit(1) + else + LatestPactPublicationsByConsumerVersion + end query = query .eager(:tags) .consumer(consumer_name) .provider(provider_name) query = query.consumer_version_number(consumer_version) if consumer_version - query.limit(1).collect(&:to_domain_with_content)[0] + query.collect(&:to_domain_with_content)[0] end def find_all_revisions consumer_name, consumer_version, provider_name diff --git a/spec/lib/pact_broker/pacts/repository_spec.rb b/spec/lib/pact_broker/pacts/repository_spec.rb index 5019f8c5d..c9ef907dc 100644 --- a/spec/lib/pact_broker/pacts/repository_spec.rb +++ b/spec/lib/pact_broker/pacts/repository_spec.rb @@ -347,7 +347,7 @@ module Pacts context "with a pact_version_sha" do subject { Repository.new.find_pact "Consumer", nil, "Provider", pact.pact_version_sha } - it "finds the pact with the given version and revision" do + it "finds the pact with the given pact_version_sha" do expect(subject.pact_version_sha).to eq pact.pact_version_sha expect(subject.consumer.name).to eq "Consumer" expect(subject.provider.name).to eq "Provider" @@ -355,7 +355,24 @@ module Pacts expect(subject.revision_number).to eq 2 end + context "when there are multiple pact publications for the pact version" do + before do + # Double check the data is set up correctly... + expect(pact_1.pact_version_sha).to eq(pact_2.pact_version_sha) + end + + let(:td) { TestDataBuilder.new } + let!(:pact_1) { td.create_pact_with_hierarchy("Foo", "1", "Bar").and_return(:pact) } + let!(:pact_2) { td.create_consumer_version("2").create_pact(json_content: pact_1.json_content).and_return(:pact) } + + subject { Repository.new.find_pact "Foo", nil, "Bar", pact_1.pact_version_sha } + + it "returns the latest pact, ordered by consumer version order" do + expect(subject.consumer_version_number).to eq "2" + end + end end + end describe "find_all_revisions" do