Skip to content

Commit

Permalink
fix: delete associated verification results when a pacticipant versio…
Browse files Browse the repository at this point in the history
…n is deleted

Closes: #219
  • Loading branch information
bethesque committed Jun 28, 2018
1 parent eda8cbf commit ddec810
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/pact_broker/verifications/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/versions/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/pact_broker/verifications/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion spec/lib/pact_broker/versions/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions spec/support/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ddec810

Please sign in to comment.