Skip to content

Commit

Permalink
feat(dashboard api): eager load the pact_version
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 7, 2019
1 parent f27a718 commit b6b5c90
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/pact_broker/integrations/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Integration < Sequel::Model
associate(:many_to_one, :consumer, :class => "PactBroker::Domain::Pacticipant", :key => :consumer_id, :primary_key => :id)
associate(:many_to_one, :provider, :class => "PactBroker::Domain::Pacticipant", :key => :provider_id, :primary_key => :id)
associate(:one_to_one, :latest_pact, :class => "PactBroker::Pacts::LatestPactPublications", key: [:consumer_id, :provider_id], primary_key: [:consumer_id, :provider_id])
associate(:one_to_one, :latest_verification, :class => "PactBroker::Verifications::LatestVerificationForConsumerAndProvider", key: [:consumer_id, :provider_id], primary_key: [:consumer_id, :provider_id])

def verification_status_for_latest_pact
@verification_status_for_latest_pact ||= PactBroker::Verifications::PseudoBranchStatus.new(latest_pact, latest_pact&.latest_verification)
Expand All @@ -18,7 +19,7 @@ def latest_pact_or_verification_publication_date
end

def latest_verification_publication_date
PactBroker::Domain::Verification.where(consumer_id: consumer_id, provider_id: provider_id).order(:id).last&.execution_date
latest_verification&.execution_date
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion lib/pact_broker/integrations/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ class Service
include PactBroker::Logging

def self.find_all
# The only reason the pact_version needs to be loaded is that
# the Verification::PseudoBranchStatus uses it to determine if
# the pseudo branch is 'stale'.
# Because this is the status for a pact, and not a pseudo branch,
# the status can never be 'stale',
# so it would be better to create a Verification::PactStatus class
# that doesn't have the 'stale' logic in it.
# Then we can remove the eager loading of the pact_version
PactBroker::Integrations::Integration
.eager(:consumer)
.eager(:provider)
.eager(latest_pact: :latest_verification)
.eager(latest_pact: [:latest_verification, :pact_version])
.eager(:latest_verification)
.all
.sort { | a, b| b.latest_pact_or_verification_publication_date <=> a.latest_pact_or_verification_publication_date }
end
Expand Down
5 changes: 5 additions & 0 deletions spec/lib/pact_broker/integrations/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ module Integrations
expect(Integration.first.verification_status_for_latest_pact).to be_instance_of(PactBroker::Verifications::PseudoBranchStatus)
end

it "has a latest verification - this may not be the same as the latest verification for the latest pact" do
integration = Integration.eager(:latest_verification).all.first
expect(integration.latest_verification.provider_version_number).to eq "4"
end

describe "latest_pact_or_verification_publication_date" do
context "when the last publication is a verification" do
it "returns the verification execution date" do
Expand Down

0 comments on commit b6b5c90

Please sign in to comment.