Skip to content

Commit

Permalink
feat(integrations): sort by integration with most recent activity first
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 4, 2019
1 parent 437ba76 commit 35bdbb9
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lib/pact_broker/integrations/integration.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'pact_broker/db'
require 'pact_broker/verifications/verification_status'
require 'pact_broker/domain/verification'

module PactBroker
module Integrations
Expand All @@ -11,6 +12,14 @@ class Integration < Sequel::Model
def verification_status_for_latest_pact
@verification_status_for_latest_pact ||= PactBroker::Verifications::Status.new(latest_pact, latest_pact&.latest_verification)
end

def latest_pact_or_verification_publication_date
[latest_pact.created_at, latest_verification_publication_date].compact.max
end

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

def self.find_all
PactBroker::Integrations::Integration.eager(:consumer).eager(:provider).eager(latest_pact: :latest_verification).all
PactBroker::Integrations::Integration
.eager(:consumer)
.eager(:provider)
.eager(latest_pact: :latest_verification)
.all
.sort { | a, b| b.latest_pact_or_verification_publication_date <=> a.latest_pact_or_verification_publication_date }
end

def self.delete(consumer_name, provider_name)
Expand Down
22 changes: 20 additions & 2 deletions lib/pact_broker/test/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ def create_verification parameters = {}
tag_names = [parameters.delete(:tag_names), parameters.delete(:tag_name)].flatten.compact
provider_version_number = parameters[:provider_version] || '4.5.6'
default_parameters = {success: true, number: 1, test_results: {some: 'results'}}
default_parameters[:execution_date] = @now if @now
parameters = default_parameters.merge(parameters)
parameters.delete(:provider_version)
verification = PactBroker::Domain::Verification.new(parameters)
Expand Down Expand Up @@ -323,6 +324,22 @@ def and_return instance_variable_name
instance_variable_get("@#{instance_variable_name}")
end

def set_now date
@now = date
self
end

def in_utc
original_tz = ENV['TZ']
begin
ENV['TZ'] = 'UTC'
yield
ensure
ENV['TZ'] = original_tz
end
end


private

# Remember! This must be called before adding the IDs
Expand All @@ -335,8 +352,9 @@ def prepare_json_content(json_content)
end

def set_created_at_if_set created_at, table_name, selector
if created_at
Sequel::Model.db[table_name].where(selector.keys.first => selector.values.first).update(created_at: created_at)
date_to_set = created_at || @now
if date_to_set
Sequel::Model.db[table_name].where(selector.keys.first => selector.values.first).update(created_at: date_to_set)
end
end

Expand Down
28 changes: 27 additions & 1 deletion spec/lib/pact_broker/integrations/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ module PactBroker
module Integrations
describe Integration do
before do
td.create_pact_with_hierarchy("Foo", "1", "Bar")
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

Expand All @@ -24,6 +28,28 @@ module Integrations
it "has a verification status" do
expect(Integration.first.verification_status_for_latest_pact).to be_instance_of(PactBroker::Verifications::Status)
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 eq date
end
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
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 eq date
end
end
end
end
end
end
27 changes: 27 additions & 0 deletions spec/lib/pact_broker/integrations/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@
module PactBroker
module Integrations
describe Service do
describe "find_all" do
before do
allow(PactBroker::Integrations::Integration).to receive(:eager).and_return(dataset)
allow(dataset).to receive(:eager).and_return(dataset)
allow(dataset).to receive(:all).and_return(integrations)
end

let(:dataset) { double('integrations') }
let(:integrations) { [ integration_1, integration_2 ] }
let(:integration_1) do
double(
'integration 1',
latest_pact_or_verification_publication_date: DateTime.new(2019, 1, 1)
)
end
let(:integration_2) do
double(
'integration 2',
latest_pact_or_verification_publication_date: DateTime.new(2019, 2, 1)
)
end

it "sorts the integrations with the most recently active first so that the UI doesn't need to do it" do
expect(Service.find_all.first).to be integration_2
end
end

describe "#delete" do
let(:td) { TestDataBuilder.new }

Expand Down

0 comments on commit 35bdbb9

Please sign in to comment.