From 35bdbb92d6f9e5fdd2d4be8aabcf23f7fabdc133 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 4 Oct 2019 17:21:42 +1000 Subject: [PATCH] feat(integrations): sort by integration with most recent activity first --- lib/pact_broker/integrations/integration.rb | 9 ++++++ lib/pact_broker/integrations/service.rb | 7 ++++- lib/pact_broker/test/test_data_builder.rb | 22 +++++++++++++-- .../integrations/integration_spec.rb | 28 ++++++++++++++++++- .../pact_broker/integrations/service_spec.rb | 27 ++++++++++++++++++ 5 files changed, 89 insertions(+), 4 deletions(-) diff --git a/lib/pact_broker/integrations/integration.rb b/lib/pact_broker/integrations/integration.rb index f36dcb20a..4c0893a05 100644 --- a/lib/pact_broker/integrations/integration.rb +++ b/lib/pact_broker/integrations/integration.rb @@ -1,5 +1,6 @@ require 'pact_broker/db' require 'pact_broker/verifications/verification_status' +require 'pact_broker/domain/verification' module PactBroker module Integrations @@ -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 diff --git a/lib/pact_broker/integrations/service.rb b/lib/pact_broker/integrations/service.rb index 155963289..4f6a83004 100644 --- a/lib/pact_broker/integrations/service.rb +++ b/lib/pact_broker/integrations/service.rb @@ -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) diff --git a/lib/pact_broker/test/test_data_builder.rb b/lib/pact_broker/test/test_data_builder.rb index e1400e51f..73d011668 100644 --- a/lib/pact_broker/test/test_data_builder.rb +++ b/lib/pact_broker/test/test_data_builder.rb @@ -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) @@ -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 @@ -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 diff --git a/spec/lib/pact_broker/integrations/integration_spec.rb b/spec/lib/pact_broker/integrations/integration_spec.rb index dd245e0ed..cba5c8318 100644 --- a/spec/lib/pact_broker/integrations/integration_spec.rb +++ b/spec/lib/pact_broker/integrations/integration_spec.rb @@ -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 @@ -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 diff --git a/spec/lib/pact_broker/integrations/service_spec.rb b/spec/lib/pact_broker/integrations/service_spec.rb index 0ab33ec1c..a82ee0c63 100644 --- a/spec/lib/pact_broker/integrations/service_spec.rb +++ b/spec/lib/pact_broker/integrations/service_spec.rb @@ -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 }