Skip to content

Commit 35bdbb9

Browse files
committed
feat(integrations): sort by integration with most recent activity first
1 parent 437ba76 commit 35bdbb9

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

lib/pact_broker/integrations/integration.rb

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'pact_broker/db'
22
require 'pact_broker/verifications/verification_status'
3+
require 'pact_broker/domain/verification'
34

45
module PactBroker
56
module Integrations
@@ -11,6 +12,14 @@ class Integration < Sequel::Model
1112
def verification_status_for_latest_pact
1213
@verification_status_for_latest_pact ||= PactBroker::Verifications::Status.new(latest_pact, latest_pact&.latest_verification)
1314
end
15+
16+
def latest_pact_or_verification_publication_date
17+
[latest_pact.created_at, latest_verification_publication_date].compact.max
18+
end
19+
20+
def latest_verification_publication_date
21+
PactBroker::Domain::Verification.where(consumer_id: consumer_id, provider_id: provider_id).order(:id).last&.execution_date
22+
end
1423
end
1524
end
1625
end

lib/pact_broker/integrations/service.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ class Service
1111
include PactBroker::Logging
1212

1313
def self.find_all
14-
PactBroker::Integrations::Integration.eager(:consumer).eager(:provider).eager(latest_pact: :latest_verification).all
14+
PactBroker::Integrations::Integration
15+
.eager(:consumer)
16+
.eager(:provider)
17+
.eager(latest_pact: :latest_verification)
18+
.all
19+
.sort { | a, b| b.latest_pact_or_verification_publication_date <=> a.latest_pact_or_verification_publication_date }
1520
end
1621

1722
def self.delete(consumer_name, provider_name)

lib/pact_broker/test/test_data_builder.rb

+20-2
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ def create_verification parameters = {}
293293
tag_names = [parameters.delete(:tag_names), parameters.delete(:tag_name)].flatten.compact
294294
provider_version_number = parameters[:provider_version] || '4.5.6'
295295
default_parameters = {success: true, number: 1, test_results: {some: 'results'}}
296+
default_parameters[:execution_date] = @now if @now
296297
parameters = default_parameters.merge(parameters)
297298
parameters.delete(:provider_version)
298299
verification = PactBroker::Domain::Verification.new(parameters)
@@ -323,6 +324,22 @@ def and_return instance_variable_name
323324
instance_variable_get("@#{instance_variable_name}")
324325
end
325326

327+
def set_now date
328+
@now = date
329+
self
330+
end
331+
332+
def in_utc
333+
original_tz = ENV['TZ']
334+
begin
335+
ENV['TZ'] = 'UTC'
336+
yield
337+
ensure
338+
ENV['TZ'] = original_tz
339+
end
340+
end
341+
342+
326343
private
327344

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

337354
def set_created_at_if_set created_at, table_name, selector
338-
if created_at
339-
Sequel::Model.db[table_name].where(selector.keys.first => selector.values.first).update(created_at: created_at)
355+
date_to_set = created_at || @now
356+
if date_to_set
357+
Sequel::Model.db[table_name].where(selector.keys.first => selector.values.first).update(created_at: date_to_set)
340358
end
341359
end
342360

spec/lib/pact_broker/integrations/integration_spec.rb

+27-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ module PactBroker
44
module Integrations
55
describe Integration do
66
before do
7-
td.create_pact_with_hierarchy("Foo", "1", "Bar")
7+
td.set_now(DateTime.new(2019, 1, 1))
8+
.create_pact_with_hierarchy("Foo", "1", "Bar")
9+
.set_now(DateTime.new(2019, 1, 2))
810
.create_consumer_version("2")
911
.create_pact
12+
.set_now(DateTime.new(2019, 1, 3))
1013
.create_verification(provider_version: "3")
14+
.set_now(DateTime.new(2019, 1, 4))
1115
.create_verification(provider_version: "4", number: 2)
1216
end
1317

@@ -24,6 +28,28 @@ module Integrations
2428
it "has a verification status" do
2529
expect(Integration.first.verification_status_for_latest_pact).to be_instance_of(PactBroker::Verifications::Status)
2630
end
31+
32+
describe "latest_pact_or_verification_publication_date" do
33+
context "when the last publication is a verification" do
34+
it "returns the verification execution date" do
35+
date = td.in_utc { DateTime.new(2019, 1, 4) }
36+
expect(Integration.first.latest_pact_or_verification_publication_date).to eq date
37+
end
38+
end
39+
40+
context "when the last publication is a pact" do
41+
before do
42+
td.set_now(DateTime.new(2019, 1, 5))
43+
.create_consumer_version("3")
44+
.create_pact
45+
end
46+
47+
it "returns the pact publication date" do
48+
date = td.in_utc { DateTime.new(2019, 1, 5) }
49+
expect(Integration.first.latest_pact_or_verification_publication_date).to eq date
50+
end
51+
end
52+
end
2753
end
2854
end
2955
end

spec/lib/pact_broker/integrations/service_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,33 @@
33
module PactBroker
44
module Integrations
55
describe Service do
6+
describe "find_all" do
7+
before do
8+
allow(PactBroker::Integrations::Integration).to receive(:eager).and_return(dataset)
9+
allow(dataset).to receive(:eager).and_return(dataset)
10+
allow(dataset).to receive(:all).and_return(integrations)
11+
end
12+
13+
let(:dataset) { double('integrations') }
14+
let(:integrations) { [ integration_1, integration_2 ] }
15+
let(:integration_1) do
16+
double(
17+
'integration 1',
18+
latest_pact_or_verification_publication_date: DateTime.new(2019, 1, 1)
19+
)
20+
end
21+
let(:integration_2) do
22+
double(
23+
'integration 2',
24+
latest_pact_or_verification_publication_date: DateTime.new(2019, 2, 1)
25+
)
26+
end
27+
28+
it "sorts the integrations with the most recently active first so that the UI doesn't need to do it" do
29+
expect(Service.find_all.first).to be integration_2
30+
end
31+
end
32+
633
describe "#delete" do
734
let(:td) { TestDataBuilder.new }
835

0 commit comments

Comments
 (0)