Skip to content

Commit

Permalink
feat: add resource for "work in progress" pacts for a provider
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 6, 2018
1 parent ccdd46f commit 50c003b
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module PactBroker
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'latest-untagged'], Api::Resources::LatestPact, {resource_name: "latest_untagged_pact_publication", tag: :untagged}
add ['pacts', 'provider', :provider_name, 'latest'], Api::Resources::LatestProviderPacts, {resource_name: "latest_provider_pact_publications"}
add ['pacts', 'provider', :provider_name, 'latest', :tag], Api::Resources::LatestProviderPacts, {resource_name: "latest_tagged_provider_pact_publications"}
add ['pacts', 'provider', :provider_name, 'wip'], Api::Resources::WipProviderPacts, {resource_name: "wip_provider_pact_publications"}
add ['pacts', 'latest'], Api::Resources::LatestPacts, {resource_name: "latest_pacts"}

# Deprecated pact
Expand Down
6 changes: 6 additions & 0 deletions lib/pact_broker/api/resources/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def to_json
title: 'All pact versions for the specified provider',
templated: true
},
'pb:wip-provider-pacts' =>
{
href: base_url + '/pacts/provider/{provider}/wip',
title: 'WIP pact versions for the specified provider',
templated: true
},
'pb:latest-version' => {
href: base_url + '/pacticipants/{pacticipant}/latest-version',
title: 'Latest pacticipant version',
Expand Down
21 changes: 21 additions & 0 deletions lib/pact_broker/api/resources/wip_provider_pacts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'pact_broker/api/resources/provider_pacts'
require 'pact_broker/configuration'
require 'pact_broker/api/decorators/provider_pacts_decorator'

module PactBroker
module Api
module Resources
class WipProviderPacts < ProviderPacts
private

def pacts
pact_service.find_wip_pact_versions_for_provider provider_name
end

def resource_title
"WIP pact versions for the provider #{provider_name}"
end
end
end
end
end
8 changes: 6 additions & 2 deletions lib/pact_broker/pacts/pact_publication.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'pact_broker/domain/pact'
require 'pact_broker/pacts/pact_version'
require 'pact_broker/repositories/helpers'

module PactBroker
module Pacts

class PactPublication < Sequel::Model(:pact_publications)

extend Forwardable
Expand All @@ -15,7 +15,9 @@ class PactPublication < Sequel::Model(:pact_publications)
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)

PactPublication.plugin :timestamps, :update_on_create=>true
dataset_module do
include PactBroker::Repositories::Helpers
end

def before_create
super
Expand Down Expand Up @@ -55,5 +57,7 @@ def cached_domain_for_delegation
@domain_object ||= to_domain
end
end

PactPublication.plugin :timestamps, update_on_create: true
end
end
7 changes: 7 additions & 0 deletions lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Pacts
class Repository

include PactBroker::Logging
include PactBroker::Repositories

def create params
PactPublication.new(
Expand Down Expand Up @@ -71,6 +72,12 @@ def find_latest_pact_versions_for_provider provider_name, tag = nil
end
end

def find_wip_pact_versions_for_provider provider_name
provider_id = pacticipant_repository.find_by_name(provider_name).id
pact_publication_ids = PactBroker::Matrix::HeadRow.where(provider_id: provider_id).exclude(success: true).select_for_subquery(:pact_publication_id)
AllPactPublications.where(id: pact_publication_ids).order_ignore_case(:consumer_name).order_append(:consumer_version_order).collect(&:to_domain)
end

def find_pact_versions_for_provider provider_name, tag = nil
if tag
LatestPactPublicationsByConsumerVersion
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/pacts/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def find_latest_pact_versions_for_provider provider_name, options = {}
pact_repository.find_latest_pact_versions_for_provider provider_name, options[:tag]
end

def find_wip_pact_versions_for_provider provider_name
pact_repository.find_wip_pact_versions_for_provider provider_name
end

def find_pact_versions_for_provider provider_name, options = {}
pact_repository.find_pact_versions_for_provider provider_name, options[:tag]
end
Expand Down
26 changes: 26 additions & 0 deletions spec/features/get_wip_provider_pacts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
describe "Get WIP provider pacts" do
subject { get path; last_response }

let(:last_response_body) { JSON.parse(subject.body, symbolize_names: true) }
let(:pact_links) { last_response_body[:_links][:'pb:pacts'] }

context "when the provider exists" do
before do
TestDataBuilder.new
.create_provider("Provider")
.create_consumer("Consumer")
.create_consumer_version("0.0.1")
.create_pact
end

let(:path) { "/pacts/provider/Provider/wip" }

it "returns a 200 HAL JSON response" do
expect(subject).to be_a_hal_json_success_response
end

it "returns a list of links to the pacts" do
expect(pact_links.size).to eq 1
end
end
end
34 changes: 34 additions & 0 deletions spec/lib/pact_broker/api/resources/wip_provider_pacts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'pact_broker/api/resources/latest_provider_pacts'

module PactBroker
module Api
module Resources
describe WipProviderPacts do
before do
allow(PactBroker::Pacts::Service).to receive(:find_wip_pact_versions_for_provider).and_return(pacts)
allow(PactBroker::Api::Decorators::ProviderPactsDecorator).to receive(:new).and_return(decorator)
allow_any_instance_of(WipProviderPacts).to receive(:resource_exists?).and_return(provider)
end

let(:provider) { double('provider') }
let(:pacts) { double('pacts') }
let(:path) { '/pacts/provider/Bar/wip' }
let(:decorator) { instance_double('PactBroker::Api::Decorators::ProviderPactsDecorator') }

subject { get path; last_response }

it "finds the wip pacts for the provider" do
expect(PactBroker::Pacts::Service).to receive(:find_wip_pact_versions_for_provider).with("Bar")
subject
end

it "sets the correct resource title" do
expect(decorator).to receive(:to_json) do | options |
expect(options[:user_options][:title]).to eq "WIP pact versions for the provider Bar"
end
subject
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require 'pact_broker/pacts/repository'

module PactBroker
module Pacts
describe Repository do
let(:td) { TestDataBuilder.new }

describe "find_wip_pact_versions_for_provider" do
subject { Repository.new.find_wip_pact_versions_for_provider("bar") }

context "when the latest pact for a tag has been successfully verified" do
before do
td.create_pact_with_hierarchy("foo", "1", "bar")
.comment("above not included because it's not the latest prod")
.create_consumer_version("2")
.create_consumer_version_tag("prod")
.create_pact
.create_verification(provider_version: "3", comment: "not included because already verified")
end

it "is not included" do
expect(subject.size).to be 0
end
end

context "when the latest pact without a tag has failed verification" do
before do
td.create_pact_with_hierarchy("foo", "1", "bar")
.create_verification(provider_version: "3", success: false)
end

it "is included" do
expect(subject.size).to be 1
end
end

context "when the latest pact without a tag has not been verified" do
before do
td.create_pact_with_hierarchy("foo", "1", "bar")
.create_consumer_version("2")
.create_pact
end

it "is included" do
expect(subject.first.consumer_version_number).to eq "2"
expect(subject.size).to be 1
end
end

context "when the latest pact for a tag has failed verification" do
before do
td.create_pact_with_hierarchy("foo", "1", "bar")
.create_consumer_version_tag("prod")
.create_verification(provider_version: "3", success: true)
.create_consumer_version("2", tag_names: ["prod"])
.create_pact
.create_verification(provider_version: "5", success: false)
end

it "is included" do
expect(subject.first.consumer_version_number).to eq "2"
expect(subject.size).to be 1
end
end

context "when the latest pact for a tag has not been verified" do
before do
td.create_pact_with_hierarchy("foo", "1", "bar")
.create_consumer_version_tag("prod")
.create_verification(provider_version: "5")
.create_consumer_version("2", tag_names: ["prod"])
.create_pact
end

it "is included" do
expect(subject.first.consumer_version_number).to eq "2"
expect(subject.size).to be 1
end
end

context "when the provider name does not match the given provider name" do
before do
td.create_pact_with_hierarchy("foo", "1", "baz")
.create_provider("bar")
end

it "is not included" do
expect(subject.size).to be 0
end
end
end
end
end
end
1 change: 0 additions & 1 deletion spec/lib/pact_broker/pacts/repository_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Yes, I know this file is too bug, but cmd+shift+t is too useful!

require 'pact_broker/pacts/repository'
require 'spec/support/test_data_builder'
require 'pact_broker/pacts/pact_params'
require 'pact_broker/versions/repository'
require 'pact_broker/pacticipants/repository'
Expand Down

0 comments on commit 50c003b

Please sign in to comment.