diff --git a/lib/pact_broker/pacts/pact_version.rb b/lib/pact_broker/pacts/pact_version.rb index 6b1fb4043..b95d54aaa 100644 --- a/lib/pact_broker/pacts/pact_version.rb +++ b/lib/pact_broker/pacts/pact_version.rb @@ -22,7 +22,13 @@ def latest_consumer_version end def latest_pact_publication - PactBroker::Pacts::LatestPactPublicationsByConsumerVersion.where(pact_version_id: id).order(:consumer_version_order).last + PactBroker::Pacts::LatestPactPublicationsByConsumerVersion + .where(pact_version_id: id) + .order(:consumer_version_order) + .last || PactBroker::Pacts::AllPactPublications + .where(pact_version_id: id) + .order(:consumer_version_order) + .last end def consumer_versions diff --git a/spec/lib/pact_broker/pacts/pact_version_spec.rb b/spec/lib/pact_broker/pacts/pact_version_spec.rb index f6a9c8313..c38d26802 100644 --- a/spec/lib/pact_broker/pacts/pact_version_spec.rb +++ b/spec/lib/pact_broker/pacts/pact_version_spec.rb @@ -19,29 +19,52 @@ module Pacts end describe "#latest_pact_publication" do - before do - TestDataBuilder.new - .create_provider("Bar") - .create_consumer("Foo") - .create_consumer_version("1.2.100") - .create_pact - .revise_pact - .create_consumer_version("1.2.101") - .create_pact - .create_consumer_version("1.2.102") - .create_pact - .revise_pact - .create_provider("Animals") - .create_pact - .create_provider("Wiffles") - .create_pact + + context "when the latest pact publication is not an overwritten one" do + before do + TestDataBuilder.new + .create_provider("Bar") + .create_consumer("Foo") + .create_consumer_version("1.2.100") + .create_pact + .revise_pact + .create_consumer_version("1.2.101") + .create_pact + .create_consumer_version("1.2.102") + .create_pact + .revise_pact + .create_provider("Animals") + .create_pact + .create_provider("Wiffles") + .create_pact + end + + it "returns the latest pact publication for the given pact version" do + pact = PactBroker::Pacts::Repository.new.find_pact("Foo", "1.2.102", "Animals") + pact_version = PactBroker::Pacts::PactVersion.find(sha: pact.pact_version_sha) + latest_pact_publication = pact_version.latest_pact_publication + expect(latest_pact_publication.id).to eq pact.id + end end - it "returns the latest pact publication for the given pact version" do - pact = PactBroker::Pacts::Repository.new.find_pact("Foo", "1.2.102", "Animals") - pact_version = PactBroker::Pacts::PactVersion.find(sha: pact.pact_version_sha) - latest_pact_publication = pact_version.latest_pact_publication - expect(latest_pact_publication.id).to eq pact.id + context "when the only pact publication with the given sha is an overwritten one" do + let(:td) { TestDataBuilder.new } + let!(:first_version) do + td.create_provider("Bar") + .create_consumer("Foo") + .create_consumer_version("1") + .create_pact + .and_return(:pact) + end + let!(:second_revision) do + td.revise_pact + end + + it "returns the overwritten pact publication" do + pact_version = PactBroker::Pacts::PactVersion.find(sha: first_version.pact_version_sha) + latest_pact_publication = pact_version.latest_pact_publication + expect(latest_pact_publication.revision_number).to eq 1 + end end end