Skip to content

Commit

Permalink
feat(matrix): return most recent rows first
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Nov 6, 2017
1 parent 1832384 commit e896b7b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
19 changes: 8 additions & 11 deletions lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def find selectors, options = {}
lines = apply_scope(options, selectors, lines)

if options.key?(:success)
lines = lines.select{ |l| options[:success].include?(l[:success]) }
lines = lines.select{ |l| options[:success].include?(l.success) }
end

lines
lines.sort.collect(&:values)
end

def all_versions_specified? selectors
Expand All @@ -41,20 +41,18 @@ def apply_scope options, selectors, lines
when 'cp' then GROUP_BY_PACT
end

lines.group_by{|line| group_by_columns.collect{|key| line[key] }}
lines.group_by{|line| group_by_columns.collect{|key| line.send(key) }}
.values
.collect{ | lines | lines.first[:provider_version_number].nil? ? lines : lines.last }
.collect{ | lines | lines.first.provider_version_number.nil? ? lines : lines.last }
.flatten
end

def find_for_consumer_and_provider pacticipant_1_name, pacticipant_2_name
selectors = [{ pacticipant_name: pacticipant_1_name }, { pacticipant_name: pacticipant_2_name }]
find_all(selectors, {latestby: 'cvpv'})
.sort{|l1, l2| l2[:consumer_version_order] <=> l1[:consumer_version_order]}
find_all(selectors, {latestby: 'cvpv'}).sort.collect(&:values)
end

def find_compatible_pacticipant_versions selectors

find(selectors, latestby: 'cvpv')
.select{|line| line[:success] }
end
Expand All @@ -69,11 +67,10 @@ def find_all selectors, options
if selectors.size == 1
query = where_consumer_or_provider_is(selectors.first, query)
else
query = where_consumer_and_provider_within(selectors, query)
query = where_consumer_and_provider_in(selectors, query)
end

query.order(:verification_executed_at, :verification_id)
.collect(&:values)
query.order(:verification_executed_at, :verification_id).all
end

def base_table(options)
Expand Down Expand Up @@ -103,7 +100,7 @@ def look_up_versions_for_tags(selectors)
end
end

def where_consumer_and_provider_within selectors, query
def where_consumer_and_provider_in selectors, query
query.where{
Sequel.&(
Sequel.|(
Expand Down
11 changes: 11 additions & 0 deletions lib/pact_broker/matrix/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ class Row < Sequel::Model
def summary
"#{consumer_name}#{consumer_version_number} #{provider_name}#{provider_version_number || '?'} (r#{pact_revision_number}n#{verification_number || '?'})"
end

# Add logic for ignoring case
def <=> other
[:consumer_name, :consumer_version_order, :pact_revision_number, :provider_name, :provider_version_order, :verification_id].each do | column |
if send(column) != other.send(column)
return (send(column) <=> other.send(column)) * -1
end
end

return 0
end
end
end
end
4 changes: 2 additions & 2 deletions lib/pact_broker/ui/view_models/matrix_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ def provider_version_order
end

def orderable_fields
[consumer_name, provider_name, consumer_version_order, provider_version_order]
[consumer_name, consumer_version_order, @line[:pact_revision_number], provider_name, @line[:verification_id]]
end

def <=> other
self.orderable_fields <=> other.orderable_fields
(self.orderable_fields <=> other.orderable_fields) * -1
end

def verification_status
Expand Down
35 changes: 35 additions & 0 deletions spec/lib/pact_broker/matrix/row_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'pact_broker/matrix/row'

module PactBroker
module Matrix
describe Row do
describe "<=>" do
let(:row_1) do
Row.new(
consumer_name: 'A',
consumer_version_order: 1,
pact_revision_number: 1,
provider_name: 'B',
provider_version_order: 1,
verification_id: 1
)
end
let(:row_2) do
Row.new(
consumer_name: 'A',
consumer_version_order: 1,
pact_revision_number: 1,
provider_name: 'B',
provider_version_order: 1,
verification_id: 2
)
end

it "sorts" do
expect([row_1, row_2].sort).to eq [row_2, row_1]
end

end
end
end
end

0 comments on commit e896b7b

Please sign in to comment.