Skip to content

Commit

Permalink
chore: add associations to Integration in preparation for refactoring…
Browse files Browse the repository at this point in the history
… Index::Service
  • Loading branch information
bethesque committed Oct 28, 2019
1 parent 8684627 commit a3ea6fc
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 41 deletions.
21 changes: 21 additions & 0 deletions lib/pact_broker/integrations/integration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'pact_broker/db'
require 'pact_broker/verifications/pseudo_branch_status'
require 'pact_broker/domain/verification'
require 'pact_broker/webhooks/latest_triggered_webhook'

module PactBroker
module Integrations
Expand All @@ -9,6 +10,26 @@ class Integration < Sequel::Model
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])
associate(:one_to_many, :latest_triggered_webhooks, :class => "PactBroker::Webhooks::LatestTriggeredWebhook", key: [:consumer_id, :provider_id], primary_key: [:consumer_id, :provider_id])

# When viewing the index, every webhook in the database will match at least one of the rows, so
# it makes sense to load the entire table and match each webhook to the appropriate row.
# This will only work when using eager loading. The keys are just blanked out to avoid errors.
# I don't understand how they work at all.
# It would be nice to do this declaratively.
many_to_many :webhooks, :left_key => [], left_primary_key: [], :eager_loader=>(proc do |eo_opts|
eo_opts[:rows].each do |row|
row.associations[:webhooks] = []
end

PactBroker::Webhooks::Webhook.each do | webhook |
eo_opts[:rows].each do | row |
if webhook.is_for?(row)
row.associations[:webhooks] << webhook
end
end
end
end)

def verification_status_for_latest_pact
@verification_status_for_latest_pact ||= PactBroker::Verifications::PseudoBranchStatus.new(latest_pact, latest_pact&.latest_verification)
Expand Down
120 changes: 79 additions & 41 deletions spec/lib/pact_broker/integrations/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,96 @@
module PactBroker
module Integrations
describe Integration do
before do
td.set_now(DateTime.new(2019, 1, 1))
.create_pact_with_hierarchy("Foo", "1", "Bar")
.set_now(DateTime.new(2019, 1, 2))
.create_consumer_version("2")
.create_pact
.set_now(DateTime.new(2019, 1, 3))
.create_verification(provider_version: "3")
.set_now(DateTime.new(2019, 1, 4))
.create_verification(provider_version: "4", number: 2)
end

it "has a relationship to the latest pact" do
integration = Integration.eager(:latest_pact).all.first
expect(integration.latest_pact.consumer_version_number).to eq "2"
end
describe "relationships" do
before do
td.set_now(DateTime.new(2019, 1, 1))
.create_pact_with_hierarchy("Foo", "1", "Bar")
.set_now(DateTime.new(2019, 1, 2))
.create_consumer_version("2")
.create_pact
.set_now(DateTime.new(2019, 1, 3))
.create_verification(provider_version: "3")
.set_now(DateTime.new(2019, 1, 4))
.create_verification(provider_version: "4", number: 2)
end

it "has a relationship to the latest verification via the latest pact" do
integration = Integration.eager(latest_pact: :latest_verification).all.first
expect(integration.latest_pact.latest_verification.provider_version_number).to eq "4"
end
it "has a relationship to the latest pact" do
integration = Integration.eager(:latest_pact).all.first
expect(integration.latest_pact.consumer_version_number).to eq "2"
end

it "has a verification status" do
expect(Integration.first.verification_status_for_latest_pact).to be_instance_of(PactBroker::Verifications::PseudoBranchStatus)
end
it "has a relationship to the latest verification via the latest pact" do
integration = Integration.eager(latest_pact: :latest_verification).all.first
expect(integration.latest_pact.latest_verification.provider_version_number).to eq "4"
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
it "has a verification status" do
expect(Integration.first.verification_status_for_latest_pact).to be_instance_of(PactBroker::Verifications::PseudoBranchStatus)
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
date = td.in_utc { DateTime.new(2019, 1, 4) }
expect(Integration.first.latest_pact_or_verification_publication_date.to_datetime).to eq date
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

context "when the last publication is a pact" do
before do
td.set_now(DateTime.new(2019, 1, 5))
.create_consumer_version("3")
.create_pact
describe "latest_pact_or_verification_publication_date" do
context "when the last publication is a verification" do
it "returns the verification execution date" do
date = td.in_utc { DateTime.new(2019, 1, 4) }
expect(Integration.first.latest_pact_or_verification_publication_date.to_datetime).to eq date
end
end

it "returns the pact publication date" do
date = td.in_utc { DateTime.new(2019, 1, 5) }
expect(Integration.first.latest_pact_or_verification_publication_date.to_datetime).to eq date
context "when the last publication is a pact" do
before do
td.set_now(DateTime.new(2019, 1, 5))
.create_consumer_version("3")
.create_pact
end

it "returns the pact publication date" do
date = td.in_utc { DateTime.new(2019, 1, 5) }
expect(Integration.first.latest_pact_or_verification_publication_date.to_datetime).to eq date
end
end
end
end

describe "latest_triggered_webhooks" do
before do
td.create_consumer("Foo")
.create_provider("Bar")
.create_consumer_version
.create_pact
.create_global_webhook
.create_triggered_webhook
.create_webhook_execution
end

it "returns a list of triggered webhooks" do
integrations = Integration.eager(:latest_triggered_webhooks).all
expect(integrations.first.latest_triggered_webhooks.count).to eq 1
end
end

describe "webhooks" do
before do
td.create_consumer("Foo")
.create_provider("Bar")
.create_consumer_version
.create_pact
.create_global_webhook
.create_consumer_webhook
.create_provider_webhook
.create_provider("Wiffle")
.create_provider_webhook
end

it "returns all the webhooks" do
integrations = Integration.eager(:webhooks).all
expect(integrations.first.webhooks.count).to eq 3
end
end
end
end
end

0 comments on commit a3ea6fc

Please sign in to comment.