diff --git a/lib/pact_broker/verifications/repository.rb b/lib/pact_broker/verifications/repository.rb index 1482783c1..22d408f46 100644 --- a/lib/pact_broker/verifications/repository.rb +++ b/lib/pact_broker/verifications/repository.rb @@ -98,6 +98,10 @@ def find_latest_verification_for_tags consumer_name, provider_name, consumer_ver ).limit(1).single_record end + def delete_by_provider_version_id version_id + PactBroker::Domain::Verification.where(provider_version_id: version_id).delete + end + def pact_version_id_for pact PactBroker::Pacts::PactPublication.select(:pact_version_id).where(id: pact.id) end diff --git a/lib/pact_broker/versions/service.rb b/lib/pact_broker/versions/service.rb index 4d02f6640..634b5410c 100644 --- a/lib/pact_broker/versions/service.rb +++ b/lib/pact_broker/versions/service.rb @@ -22,6 +22,7 @@ def self.find_by_pacticipant_name_and_latest_tag(pacticipant_name, tag) def self.delete version tag_repository.delete_by_version_id version.id pact_repository.delete_by_version_id version.id + verification_repository.delete_by_provider_version_id version.id version_repository.delete_by_id version.id end end diff --git a/spec/lib/pact_broker/verifications/repository_spec.rb b/spec/lib/pact_broker/verifications/repository_spec.rb index 435505eff..f7b02ef67 100644 --- a/spec/lib/pact_broker/verifications/repository_spec.rb +++ b/spec/lib/pact_broker/verifications/repository_spec.rb @@ -275,6 +275,26 @@ module Verifications end end end + + describe "delete_by_provider_version_id" do + let!(:provider_version) do + TestDataBuilder.new + .create_consumer + .create_provider + .create_consumer_version + .create_pact + .create_verification(provider_version: "1.0.0") + .create_verification(provider_version: "2.0.0", number: 2) + .create_verification(provider_version: "2.0.0", number: 3) + .and_return(:provider_version) + end + + subject { Repository.new.delete_by_provider_version_id(provider_version.id) } + + it "deletes the verifications associated with the given version id" do + expect { subject }.to change { PactBroker::Domain::Verification.count }.by(-2) + end + end end end end diff --git a/spec/lib/pact_broker/versions/service_spec.rb b/spec/lib/pact_broker/versions/service_spec.rb index 58ffa99ae..8dc06eaa3 100644 --- a/spec/lib/pact_broker/versions/service_spec.rb +++ b/spec/lib/pact_broker/versions/service_spec.rb @@ -5,13 +5,15 @@ module PactBroker module Versions describe Service do describe ".delete" do + let(:td) { TestDataBuilder.new } let!(:version) do - TestDataBuilder.new + td .create_consumer .create_provider .create_consumer_version("1.2.3") .create_consumer_version_tag("prod") .create_pact + .create_verification(provider_version: "1.0.0") .and_return(:consumer_version) end @@ -28,6 +30,12 @@ module Versions it "deletes the version" do expect{ subject }.to change { PactBroker::Domain::Version.count }.by(-1) end + + context "when deleting a provider version" do + it "deletes associated verifications" do + expect { Service.delete(td.provider_version ) }. to change { PactBroker::Domain::Verification.count }.by(-1) + end + end end end end diff --git a/spec/support/test_data_builder.rb b/spec/support/test_data_builder.rb index 585678ee7..420b77e09 100644 --- a/spec/support/test_data_builder.rb +++ b/spec/support/test_data_builder.rb @@ -34,6 +34,7 @@ class TestDataBuilder attr_reader :consumer attr_reader :provider attr_reader :consumer_version + attr_reader :provider_version attr_reader :pact attr_reader :verification attr_reader :webhook @@ -320,10 +321,11 @@ def create_verification parameters = {} parameters.delete(:provider_version) verification = PactBroker::Domain::Verification.new(parameters) @verification = PactBroker::Verifications::Repository.new.create(verification, provider_version_number, @pact) + @provider_version = PactBroker::Domain::Version.where(pacticipant_id: @provider.id, number: provider_version_number).single_record + if tag_names.any? - provider_version = PactBroker::Domain::Version.where(pacticipant_id: @provider.id, number: provider_version_number).single_record tag_names.each do | tag_name | - PactBroker::Domain::Tag.create(name: tag_name, version: provider_version) + PactBroker::Domain::Tag.create(name: tag_name, version: @provider_version) end end refresh_matrix