From 718f02186f330d435314498dd52bdb574d13e936 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sat, 13 Apr 2019 15:38:05 +1000 Subject: [PATCH] feat: add delete integration which deletes all objects pertaining to an integration that are not referenced by other objects --- lib/pact_broker/api/resources/integrations.rb | 7 +- .../api/resources/pact_versions.rb | 2 +- .../api/resources/tagged_pact_versions.rb | 2 +- lib/pact_broker/domain/pacticipant.rb | 5 + lib/pact_broker/domain/verification.rb | 14 +- lib/pact_broker/domain/version.rb | 8 +- lib/pact_broker/integrations/service.rb | 27 +++ lib/pact_broker/pacticipants/repository.rb | 9 + lib/pact_broker/pacticipants/service.rb | 4 + lib/pact_broker/pacts/pact_publication.rb | 3 +- lib/pact_broker/pacts/pact_version.rb | 2 + lib/pact_broker/pacts/repository.rb | 8 +- lib/pact_broker/pacts/service.rb | 4 + lib/pact_broker/services.rb | 5 + .../verifications/all_verifications.rb | 15 ++ .../latest_verification_for_pact_version.rb | 16 ++ lib/pact_broker/verifications/repository.rb | 6 + lib/pact_broker/verifications/service.rb | 4 + lib/pact_broker/versions/repository.rb | 12 + lib/pact_broker/webhooks/repository.rb | 5 + lib/pact_broker/webhooks/triggered_webhook.rb | 3 + lib/pact_broker/webhooks/webhook.rb | 1 + .../resources/tagged_pact_versions_spec.rb | 4 +- .../pact_broker/integrations/service_spec.rb | 214 ++++++++++++++++++ spec/lib/pact_broker/pacts/repository_spec.rb | 6 +- 25 files changed, 361 insertions(+), 25 deletions(-) create mode 100644 lib/pact_broker/integrations/service.rb create mode 100644 spec/lib/pact_broker/integrations/service_spec.rb diff --git a/lib/pact_broker/api/resources/integrations.rb b/lib/pact_broker/api/resources/integrations.rb index b852d495e..8fa85a3c4 100644 --- a/lib/pact_broker/api/resources/integrations.rb +++ b/lib/pact_broker/api/resources/integrations.rb @@ -10,7 +10,7 @@ def content_types_provided end def allowed_methods - ["GET", "OPTIONS"] + ["GET", "OPTIONS", "DELETE"] end def to_dot @@ -20,6 +20,11 @@ def to_dot def integrations pact_service.find_latest_pacts end + + def delete_resource + integration_service.delete(consumer_name, provider_name) + true + end end end end diff --git a/lib/pact_broker/api/resources/pact_versions.rb b/lib/pact_broker/api/resources/pact_versions.rb index 928b0dbb9..c96e98cb0 100644 --- a/lib/pact_broker/api/resources/pact_versions.rb +++ b/lib/pact_broker/api/resources/pact_versions.rb @@ -30,7 +30,7 @@ def pacts end def delete_resource - pact_service.delete_all_pact_versions_between(consumer_name, and: provider_name) + pact_service.delete_all_pact_publications_between(consumer_name, and: provider_name) true end end diff --git a/lib/pact_broker/api/resources/tagged_pact_versions.rb b/lib/pact_broker/api/resources/tagged_pact_versions.rb index 981e2270e..bfa812415 100644 --- a/lib/pact_broker/api/resources/tagged_pact_versions.rb +++ b/lib/pact_broker/api/resources/tagged_pact_versions.rb @@ -25,7 +25,7 @@ def to_json end def delete_resource - pact_service.delete_all_pact_versions_between consumer_name, and: provider_name, tag: identifier_from_path[:tag] + pact_service.delete_all_pact_publications_between consumer_name, and: provider_name, tag: identifier_from_path[:tag] true end diff --git a/lib/pact_broker/domain/pacticipant.rb b/lib/pact_broker/domain/pacticipant.rb index 7e9ca4bbe..0c1244984 100644 --- a/lib/pact_broker/domain/pacticipant.rb +++ b/lib/pact_broker/domain/pacticipant.rb @@ -26,6 +26,11 @@ def label label_name end end + def before_destroy + PactBroker::Domain::Label.where(pacticipant: self).destroy + super + end + def latest_version versions.last end diff --git a/lib/pact_broker/domain/verification.rb b/lib/pact_broker/domain/verification.rb index 306e405b1..5c6d34e7a 100644 --- a/lib/pact_broker/domain/verification.rb +++ b/lib/pact_broker/domain/verification.rb @@ -10,6 +10,8 @@ class Verification < Sequel::Model set_primary_key :id associate(:many_to_one, :pact_version, class: "PactBroker::Pacts::PactVersion", key: :pact_version_id, primary_key: :id) associate(:many_to_one, :provider_version, class: "PactBroker::Domain::Version", key: :provider_version_id, primary_key: :id) + associate(:many_to_one, :provider, class: "PactBroker::Domain::Pacticipant", key: :provider_id, primary_key: :id) + associate(:many_to_one, :consumer, class: "PactBroker::Domain::Pacticipant", key: :consumer_id, primary_key: :id) plugin :serialization, :json, :test_results def before_create @@ -74,18 +76,6 @@ def provider_name provider.name end - def consumer - Pacticipant.find(id: PactBroker::Pacts::AllPactPublications - .where(pact_version_id: pact_version_id) - .limit(1).select(:consumer_id)) - end - - def provider - Pacticipant.find(id: PactBroker::Pacts::AllPactPublications - .where(pact_version_id: pact_version_id) - .limit(1).select(:provider_id)) - end - def provider_version_number provider_version.number end diff --git a/lib/pact_broker/domain/version.rb b/lib/pact_broker/domain/version.rb index 077f40d64..f633b3492 100644 --- a/lib/pact_broker/domain/version.rb +++ b/lib/pact_broker/domain/version.rb @@ -3,11 +3,8 @@ require 'pact_broker/repositories/helpers' module PactBroker - module Domain - class Version < Sequel::Model - set_primary_key :id one_to_many :pact_publications, order: :revision_number, class: "PactBroker::Pacts::PactPublication", key: :consumer_version_id associate(:many_to_one, :pacticipant, :class => "PactBroker::Domain::Pacticipant", :key => :pacticipant_id, :primary_key => :id) @@ -21,6 +18,11 @@ def after_create OrderVersions.(self) end + def before_destroy + PactBroker::Domain::Tag.where(version: self).destroy + super + end + def to_s "Version: number=#{number}, pacticipant=#{pacticipant_id}" end diff --git a/lib/pact_broker/integrations/service.rb b/lib/pact_broker/integrations/service.rb new file mode 100644 index 000000000..0289ae1dc --- /dev/null +++ b/lib/pact_broker/integrations/service.rb @@ -0,0 +1,27 @@ +require 'pact_broker/services' +require 'pact_broker/repositories' +require 'pact_broker/logging' + +module PactBroker + module Integrations + class Service + extend PactBroker::Repositories + extend PactBroker::Services + include PactBroker::Logging + + def self.delete(consumer_name, provider_name) + consumer = pacticipant_service.find_pacticipant_by_name(consumer_name) + provider = pacticipant_service.find_pacticipant_by_name(provider_name) + # this takes care of the triggered webhooks and webhook executions + pact_service.delete_all_pact_publications_between(consumer_name, and: provider_name) + verification_service.delete_all_verifications_between(consumer_name, and: provider_name) + pact_service.delete_all_pact_versions_between(consumer_name, and: provider_name) + webhook_repository.delete_by_consumer_and_provider(consumer, provider) + version_repository.delete_orphan_versions(consumer, provider) + + pacticipant_service.delete_if_orphan(consumer) + pacticipant_service.delete_if_orphan(provider) + end + end + end +end diff --git a/lib/pact_broker/pacticipants/repository.rb b/lib/pact_broker/pacticipants/repository.rb index 1798efe2e..231b483cd 100644 --- a/lib/pact_broker/pacticipants/repository.rb +++ b/lib/pact_broker/pacticipants/repository.rb @@ -54,6 +54,15 @@ def create args def pacticipant_names PactBroker::Domain::Pacticipant.select(:name).order(:name).collect{ | pacticipant| pacticipant.name } end + + def delete_if_orphan(pacticipant) + if PactBroker::Domain::Version.where(pacticipant: pacticipant).empty? && + PactBroker::Pacts::PactPublication.where(provider: pacticipant).or(consumer: pacticipant).empty? && + PactBroker::Pacts::PactVersion.where(provider: pacticipant).or(consumer: pacticipant).empty? && + PactBroker::Webhooks::Webhook.where(provider: pacticipant).or(consumer: pacticipant).empty? + pacticipant.destroy + end + end end end end diff --git a/lib/pact_broker/pacticipants/service.rb b/lib/pact_broker/pacticipants/service.rb index c9267fb6e..b66247fbe 100644 --- a/lib/pact_broker/pacticipants/service.rb +++ b/lib/pact_broker/pacticipants/service.rb @@ -90,6 +90,10 @@ def self.delete name connection.run("delete from pacticipants where id = #{pacticipant.id}") end + def self.delete_if_orphan(pacticipant) + pacticipant_repository.delete_if_orphan(pacticipant) + end + def self.pacticipant_names pacticipant_repository.pacticipant_names end diff --git a/lib/pact_broker/pacts/pact_publication.rb b/lib/pact_broker/pacts/pact_publication.rb index a4e81b8d6..38f0053d1 100644 --- a/lib/pact_broker/pacts/pact_publication.rb +++ b/lib/pact_broker/pacts/pact_publication.rb @@ -8,10 +8,11 @@ class PactPublication < Sequel::Model(:pact_publications) extend Forwardable - delegate [:consumer, :consumer_version_number, :name, :provider_name, :consumer_name] => :cached_domain_for_delegation + delegate [:consumer_version_number, :name, :provider_name, :consumer_name] => :cached_domain_for_delegation set_primary_key :id associate(:many_to_one, :provider, :class => "PactBroker::Domain::Pacticipant", :key => :provider_id, :primary_key => :id) + associate(:many_to_one, :consumer, :class => "PactBroker::Domain::Pacticipant", :key => :consumer_id, :primary_key => :id) associate(:many_to_one, :consumer_version, :class => "PactBroker::Domain::Version", :key => :consumer_version_id, :primary_key => :id) associate(:many_to_one, :pact_version, class: "PactBroker::Pacts::PactVersion", :key => :pact_version_id, :primary_key => :id) diff --git a/lib/pact_broker/pacts/pact_version.rb b/lib/pact_broker/pacts/pact_version.rb index f4ac1a75c..10034cd68 100644 --- a/lib/pact_broker/pacts/pact_version.rb +++ b/lib/pact_broker/pacts/pact_version.rb @@ -5,6 +5,8 @@ module Pacts class PactVersion < Sequel::Model(:pact_versions) one_to_many :pact_publications, reciprocal: :pact_version one_to_many :verifications, reciprocal: :verification, order: :id, :class => "PactBroker::Domain::Verification" + associate(:many_to_one, :provider, class: "PactBroker::Domain::Pacticipant", key: :provider_id, primary_key: :id) + associate(:many_to_one, :consumer, class: "PactBroker::Domain::Pacticipant", key: :consumer_id, primary_key: :id) def name "Pact between #{consumer_name} and #{provider_name}" diff --git a/lib/pact_broker/pacts/repository.rb b/lib/pact_broker/pacts/repository.rb index cba3bb39a..1bf110f36 100644 --- a/lib/pact_broker/pacts/repository.rb +++ b/lib/pact_broker/pacts/repository.rb @@ -91,12 +91,18 @@ def find_all_pact_versions_between consumer_name, options .collect(&:to_domain) end - def delete_all_pact_versions_between consumer_name, options + def delete_all_pact_publications_between consumer_name, options ids = find_all_database_versions_between(consumer_name, options).select_for_subquery(:id) webhook_repository.delete_triggered_webhooks_by_pact_publication_ids(ids) PactPublication.where(id: ids).delete end + def delete_all_pact_versions_between consumer_name, options + consumer = pacticipant_repository.find_by_name(consumer_name) + provider = pacticipant_repository.find_by_name(options.fetch(:and)) + PactVersion.where(consumer: consumer, provider: provider).destroy + end + def find_latest_pact_versions_for_provider provider_name, tag = nil if tag LatestTaggedPactPublications.provider(provider_name).order_ignore_case(:consumer_name).where(tag_name: tag).collect(&:to_domain) diff --git a/lib/pact_broker/pacts/service.rb b/lib/pact_broker/pacts/service.rb index cbed258f0..653820481 100644 --- a/lib/pact_broker/pacts/service.rb +++ b/lib/pact_broker/pacts/service.rb @@ -68,6 +68,10 @@ def find_all_pact_versions_between consumer, options pact_repository.find_all_pact_versions_between consumer, options end + def delete_all_pact_publications_between consumer, options + pact_repository.delete_all_pact_publications_between consumer, options + end + def delete_all_pact_versions_between consumer, options pact_repository.delete_all_pact_versions_between consumer, options end diff --git a/lib/pact_broker/services.rb b/lib/pact_broker/services.rb index 3731fae35..214a63d29 100644 --- a/lib/pact_broker/services.rb +++ b/lib/pact_broker/services.rb @@ -61,5 +61,10 @@ def certificate_service require 'pact_broker/certificates/service' Certificates::Service end + + def integration_service + require 'pact_broker/integrations/service' + Integrations::Service + end end end diff --git a/lib/pact_broker/verifications/all_verifications.rb b/lib/pact_broker/verifications/all_verifications.rb index db7c04304..65cf537a4 100644 --- a/lib/pact_broker/verifications/all_verifications.rb +++ b/lib/pact_broker/verifications/all_verifications.rb @@ -8,6 +8,21 @@ class AllVerifications < PactBroker::Domain::Verification set_dataset(:all_verifications) end + # this view doesn't have a consumer_id + # TODO add it + def consumer + PactBroker::Domain::Pacticipant.find(id: PactBroker::Pacts::AllPactPublications + .where(pact_version_id: pact_version_id) + .limit(1).select(:consumer_id)) + end + + # this view doesn't have a provider_id + # TODO add it + def provider + PactBroker::Domain::Pacticipant.find(id: PactBroker::Pacts::AllPactPublications + .where(pact_version_id: pact_version_id) + .limit(1).select(:provider_id)) + end end end diff --git a/lib/pact_broker/verifications/latest_verification_for_pact_version.rb b/lib/pact_broker/verifications/latest_verification_for_pact_version.rb index cb9a002e7..243b4ef0a 100644 --- a/lib/pact_broker/verifications/latest_verification_for_pact_version.rb +++ b/lib/pact_broker/verifications/latest_verification_for_pact_version.rb @@ -4,6 +4,22 @@ module PactBroker module Verifications class LatestVerificationForPactVersion < PactBroker::Domain::Verification set_dataset(:latest_verifications_for_pact_versions) + + # this view doesn't have a consumer_id + # TODO add it + def consumer + PactBroker::Domain::Pacticipant.find(id: PactBroker::Pacts::AllPactPublications + .where(pact_version_id: pact_version_id) + .limit(1).select(:consumer_id)) + end + + # this view doesn't have a provider_id + # TODO add it + def provider + PactBroker::Domain::Pacticipant.find(id: PactBroker::Pacts::AllPactPublications + .where(pact_version_id: pact_version_id) + .limit(1).select(:provider_id)) + end end end end diff --git a/lib/pact_broker/verifications/repository.rb b/lib/pact_broker/verifications/repository.rb index d26790d0d..3a3dee358 100644 --- a/lib/pact_broker/verifications/repository.rb +++ b/lib/pact_broker/verifications/repository.rb @@ -120,6 +120,12 @@ def delete_by_provider_version_id version_id PactBroker::Domain::Verification.where(provider_version_id: version_id).delete end + def delete_all_verifications_between(consumer_name, options) + consumer = pacticipant_repository.find_by_name(consumer_name) + provider = pacticipant_repository.find_by_name(options.fetch(:and)) + PactBroker::Domain::Verification.where(provider: provider, consumer: consumer).destroy + 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/verifications/service.rb b/lib/pact_broker/verifications/service.rb index 527d01640..8712f21a0 100644 --- a/lib/pact_broker/verifications/service.rb +++ b/lib/pact_broker/verifications/service.rb @@ -64,6 +64,10 @@ def verification_summary_for_consumer_version params pacts = pact_service.find_by_consumer_version(params) SummaryForConsumerVersion.new(verifications, pacts) end + + def delete_all_verifications_between(consumer_name, options) + verification_repository.delete_all_verifications_between(consumer_name, options) + end end end end diff --git a/lib/pact_broker/versions/repository.rb b/lib/pact_broker/versions/repository.rb index e42231a23..04a46f636 100644 --- a/lib/pact_broker/versions/repository.rb +++ b/lib/pact_broker/versions/repository.rb @@ -79,6 +79,18 @@ def find_by_pacticipant_id_and_number_or_create pacticipant_id, number def delete_by_id version_ids Domain::Version.where(id: version_ids).delete end + + def delete_orphan_versions consumer, provider + version_ids_with_pact_publications = PactBroker::Pacts::PactPublication.where(consumer_id: [consumer.id, provider.id]).select(:consumer_version_id).collect{|r| r[:consumer_version_id]} + version_ids_with_verifications = PactBroker::Domain::Verification.where(provider_id: [provider.id, consumer.id]).select(:provider_version_id).collect{|r| r[:provider_version_id]} + # Hope we don't hit max parameter constraints here... + version_ids_to_keep = (version_ids_with_pact_publications + version_ids_with_verifications).uniq + + PactBroker::Domain::Version + .where(pacticipant_id: [consumer.id, provider.id]) + .exclude(id: (version_ids_with_pact_publications + version_ids_with_verifications).uniq) + .destroy + end end end end diff --git a/lib/pact_broker/webhooks/repository.rb b/lib/pact_broker/webhooks/repository.rb index 0d9fb839f..5e75159bb 100644 --- a/lib/pact_broker/webhooks/repository.rb +++ b/lib/pact_broker/webhooks/repository.rb @@ -78,6 +78,10 @@ def find_by_consumer_and_provider consumer, provider Webhook.where(criteria).collect(&:to_domain) end + def delete_by_consumer_and_provider consumer, provider + Webhook.where(consumer: consumer, provider: provider).destroy + end + def find_for_pact_and_event_name pact, event_name find_by_consumer_and_or_provider_and_event_name(pact.consumer, pact.provider, event_name) end @@ -102,6 +106,7 @@ def find_by_consumer_and_provider_and_event_name consumer, provider, event_name .collect(&:to_domain) end + # TODO delete def find_by_consumer_and_provider_existing_at consumer, provider, date_time Webhook.where(consumer_id: consumer.id, provider_id: provider.id) .where(Sequel.lit("created_at < ?", date_time)) diff --git a/lib/pact_broker/webhooks/triggered_webhook.rb b/lib/pact_broker/webhooks/triggered_webhook.rb index ae5a516bd..aeda8ec3d 100644 --- a/lib/pact_broker/webhooks/triggered_webhook.rb +++ b/lib/pact_broker/webhooks/triggered_webhook.rb @@ -2,6 +2,9 @@ require 'pact_broker/repositories/helpers' require 'pact_broker/webhooks/execution' +# Represents the relationship between a webhook and the event and object +# that caused it to be triggered. eg a pact publication + module PactBroker module Webhooks class TriggeredWebhook < Sequel::Model(:triggered_webhooks) diff --git a/lib/pact_broker/webhooks/webhook.rb b/lib/pact_broker/webhooks/webhook.rb index ca182c3aa..9a8435bd2 100644 --- a/lib/pact_broker/webhooks/webhook.rb +++ b/lib/pact_broker/webhooks/webhook.rb @@ -19,6 +19,7 @@ class Webhook < Sequel::Model def before_destroy WebhookHeader.where(webhook_id: id).destroy + super end def update_from_domain webhook diff --git a/spec/lib/pact_broker/api/resources/tagged_pact_versions_spec.rb b/spec/lib/pact_broker/api/resources/tagged_pact_versions_spec.rb index a3a993fdd..97dae479d 100644 --- a/spec/lib/pact_broker/api/resources/tagged_pact_versions_spec.rb +++ b/spec/lib/pact_broker/api/resources/tagged_pact_versions_spec.rb @@ -68,13 +68,13 @@ module Resources context "DELETE" do before do - allow(pact_service).to receive(:delete_all_pact_versions_between) + allow(pact_service).to receive(:delete_all_pact_publications_between) end subject { delete(path) } it "deletes all the pacts with the given consumer/provider/tag" do - expect(pact_service).to receive(:delete_all_pact_versions_between).with("Foo", and: "Bar", tag: "prod") + expect(pact_service).to receive(:delete_all_pact_publications_between).with("Foo", and: "Bar", tag: "prod") subject end diff --git a/spec/lib/pact_broker/integrations/service_spec.rb b/spec/lib/pact_broker/integrations/service_spec.rb new file mode 100644 index 000000000..33d93a6eb --- /dev/null +++ b/spec/lib/pact_broker/integrations/service_spec.rb @@ -0,0 +1,214 @@ +require 'pact_broker/integrations/service' + +module PactBroker + module Integrations + describe Service do + describe "#delete" do + let(:td) { TestDataBuilder.new } + + subject { Service.delete("Foo", "Bar") } + + context "with webhook data" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_webhook + .create_triggered_webhook + .create_webhook_execution + end + + it "deletes the webhooks" do + expect { subject }.to change { PactBroker::Webhooks::Webhook.count } + end + end + + context "when the consumer version does not have any other pacts associated with it" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + end + + it "deletes the consumer version" do + expect { subject }.to change { PactBroker::Domain::Version.count } + end + end + + context "when the consumer version has another pact associated with it" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_provider + .create_pact + end + + it "does not delete the consumer version" do + expect { subject }.to_not change { PactBroker::Domain::Version.count } + end + end + + context "when the consumer version has another verification associated with it" do + # Foo v1 -> Bar (no verification) - deleted + # Baz v2 -> Foo v1 - not deleted + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_consumer("Baz") + .create_consumer_version("2") + .use_provider("Foo") + .create_pact + .create_verification(provider_version: "1") + end + + it "does not delete the consumer version" do + expect { subject }.to_not change { PactBroker::Domain::Version.count } + end + end + + context "when the provider has verifications" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_verification(provider_version: "2") + end + + it "deletes the verifications" do + expect { subject }.to change { PactBroker::Domain::Verification.count } + end + end + + context "with pact versions" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + end + + it "deletes the pact versions" do + expect { subject }.to change { PactBroker::Pacts::PactVersion.count } + end + end + + context "when the consumer has other providers" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_provider("Baz") + .create_consumer_version("2") + .create_pact + end + + it "does not delete the consumer" do + expect { subject }.to_not change { PactBroker::Domain::Pacticipant.where(name: "Foo").count } + end + end + + context "when the consumer has other consumers" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_consumer("Baz") + .create_consumer_version("2") + .use_provider("Foo") + .create_pact + end + + it "does not delete the consumer" do + expect { subject }.to_not change { PactBroker::Domain::Pacticipant.where(name: "Foo").count } + end + end + + context "when the consumer does not have other providers or consumers" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + end + + it "deletes the consumer" do + expect { subject }.to change { PactBroker::Domain::Pacticipant.where(name: "Foo").count } + end + end + + context "when the provider has other consumers" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_consumer("Baz") + .create_consumer_version("2") + .create_pact + end + + it "does not delete the provider" do + expect { subject }.to_not change { PactBroker::Domain::Pacticipant.where(name: "Bar").count } + end + end + + context "when the provider has other providers" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .use_consumer("Bar") + .create_consumer_version("2") + .create_provider("Baz") + .create_pact + end + + it "does not delete the provider" do + expect { subject }.to_not change { PactBroker::Domain::Pacticipant.where(name: "Bar").count } + end + end + + context "when the provider does not have other providers or consumers" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + end + + it "deletes the provider" do + expect { subject }.to change { PactBroker::Domain::Pacticipant.where(name: "Bar").count } + end + end + + context "When a webhook exists between the consumer and another provider" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_provider("Baz") + .create_webhook + end + + it "does not delete the consumer" do + expect { subject }.to_not change { PactBroker::Domain::Pacticipant.where(name: "Foo").count } + end + end + + context "When a webhook exists between the provider and another consumer" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_consumer("Baz") + .create_webhook + end + + it "does not delete the provider" do + expect { subject }.to_not change { PactBroker::Domain::Pacticipant.where(name: "Bar").count } + end + end + + context "When a pacticipant has labels" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_label("prod") + end + + it "deletes the label" do + expect { subject }.to change { PactBroker::Domain::Label.count } + end + + it "deletes the pacticipant" do + expect { subject }.to change { PactBroker::Domain::Pacticipant.where(name: "Bar").count } + end + end + + context "When a version has tags" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_consumer_version_tag("prod") + end + + it "deletes the tag" do + expect { subject }.to change { PactBroker::Domain::Tag.count } + end + + it "deletes the version" do + expect { subject }.to change { PactBroker::Domain::Version.count } + end + end + end + end + end +end diff --git a/spec/lib/pact_broker/pacts/repository_spec.rb b/spec/lib/pact_broker/pacts/repository_spec.rb index 19b7b380c..510994cd4 100644 --- a/spec/lib/pact_broker/pacts/repository_spec.rb +++ b/spec/lib/pact_broker/pacts/repository_spec.rb @@ -313,7 +313,7 @@ module Pacts end end - describe "#delete_all_pact_versions_between" do + describe "#delete_all_pact_publications_between" do before do TestDataBuilder.new @@ -332,14 +332,14 @@ module Pacts .create_pact end - subject { Repository.new.delete_all_pact_versions_between(consumer_name, :and => provider_name) } + subject { Repository.new.delete_all_pact_publications_between(consumer_name, :and => provider_name) } it "deletes the pacts between the specified consumer and provider" do expect { subject }.to change { PactPublication.count }.by(-2) end context "with a tag" do - subject { Repository.new.delete_all_pact_versions_between(consumer_name, :and => provider_name, tag: "prod") } + subject { Repository.new.delete_all_pact_publications_between(consumer_name, :and => provider_name, tag: "prod") } it "deletes the pacts between the specified consumer and provider with the given tag" do expect { subject }.to change { PactPublication.count }.by(-1)