diff --git a/lib/pact_broker/domain/verification.rb b/lib/pact_broker/domain/verification.rb index 5bc34dd6d..5facf71b8 100644 --- a/lib/pact_broker/domain/verification.rb +++ b/lib/pact_broker/domain/verification.rb @@ -46,8 +46,7 @@ def verification_number number def tag tag_name filter = name_like(Sequel.qualify(:tags, :name), tag_name) - join(:pact_publications, {pact_version_id: :pact_version_id}) - .join(:tags, {version_id: :consumer_version_id}).where(filter) + join(:tags, { version_id: :consumer_version_id }).where(filter) end def untagged diff --git a/lib/pact_broker/verifications/repository.rb b/lib/pact_broker/verifications/repository.rb index b735b4409..b82d4c4f1 100644 --- a/lib/pact_broker/verifications/repository.rb +++ b/lib/pact_broker/verifications/repository.rb @@ -75,15 +75,15 @@ def find_latest_verification_for_tags consumer_name, provider_name, consumer_ver query = PactBroker::Verifications::AllVerifications .select_all_qualified .join(:versions, {Sequel[:provider_versions][:id] => Sequel[view_name][:provider_version_id]}, {table_alias: :provider_versions}) - .join(:all_pact_publications, { Sequel[view_name][:pact_version_id] => Sequel[:all_pact_publications][:pact_version_id] }) + .join(:latest_pact_publications_by_consumer_versions, { Sequel[view_name][:pact_version_id] => Sequel[:latest_pact_publications_by_consumer_versions][:pact_version_id] }) .consumer(consumer_name) .provider(provider_name) .tag(consumer_version_tag) .provider_version_tag(provider_version_tag) query.reverse_order( - Sequel[:all_pact_publications][:consumer_version_order], - Sequel[:all_pact_publications][:revision_number], + Sequel[:latest_pact_publications_by_consumer_versions][:consumer_version_order], + Sequel[:latest_pact_publications_by_consumer_versions][:revision_number], Sequel[:provider_versions][:order], Sequel[view_name][:execution_date] ).limit(1).single_record diff --git a/spec/lib/pact_broker/verifications/repository_spec.rb b/spec/lib/pact_broker/verifications/repository_spec.rb index dd516c6c9..7e0f92dba 100644 --- a/spec/lib/pact_broker/verifications/repository_spec.rb +++ b/spec/lib/pact_broker/verifications/repository_spec.rb @@ -189,26 +189,53 @@ module Verifications end describe "find_latest_verification_for_tags" do - before do - TestDataBuilder.new - .create_pact_with_hierarchy("Foo", "1", "Bar") - .create_consumer_version_tag("feat-x") - .create_verification(provider_version: "5") - .use_provider_version("5") - .create_provider_version_tag("feat-y") - .create_verification(provider_version: "6", number: 2) - .use_provider_version("6") - .create_provider_version_tag("feat-y") - .create_verification(provider_version: "7", number: 3) - .create_consumer_version("2") - .create_pact - .create_verification(provider_version: "8") + context "with no revisions" do + before do + TestDataBuilder.new + .create_pact_with_hierarchy("Foo", "1", "Bar") + .create_consumer_version_tag("feat-x") + .create_verification(provider_version: "5") + .use_provider_version("5") + .create_provider_version_tag("feat-y") + .create_verification(provider_version: "6", number: 2) + .use_provider_version("6") + .create_provider_version_tag("feat-y") + .create_verification(provider_version: "7", number: 3) + .create_consumer_version("2") + .create_pact + .create_verification(provider_version: "8") + end + + subject { Repository.new.find_latest_verification_for_tags("Foo", "Bar", "feat-x", "feat-y") } + + it "returns the latest verification for a pact with the given consumer tag, by a provider version with the given provider tag" do + expect(subject.provider_version_number).to eq "6" + end end - subject { Repository.new.find_latest_verification_for_tags("Foo", "Bar", "feat-x", "feat-y") } + context "when a verification exists for a pact revision that was then overwritten by a new revision of the pact" do + let(:content_1) { { content: 1 }.to_json } + let(:content_2) { { content: 2 }.to_json } + + before do + TestDataBuilder.new + .create_pact_with_hierarchy("Foo", "1", "Bar", content_1) + .create_consumer_version_tag("develop") + .create_verification(provider_version: "5", number: 1, tag_name: "develop", comment: "not this because pact revised") + .create_verification(provider_version: "6", number: 2, tag_name: "develop", comment: "not this because pact revised") + .revise_pact(content_2) + .create_verification(provider_version: "1", number: 1, tag_name: "develop", comment: "not this because later one exists") + .create_verification(provider_version: "2", number: 2, tag_name: "develop", comment: "this one!") + .create_consumer_version("2") + .create_pact(json_content: content_1) + end + + subject { Repository.new.find_latest_verification_for_tags("Foo", "Bar", "develop", "develop") } - it "returns the latest verification for a pact with the given consumer tag, by a provider version with the given provider tag" do - expect(subject.provider_version_number).to eq "6" + it "returns the latest verification " do + expect(subject.provider_version_number).to eq "2" + expect(subject.number).to eq 2 + end end end end