From e896b7bede0620b121703c9737385d230e98dfbe Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 6 Nov 2017 09:14:45 +1100 Subject: [PATCH] feat(matrix): return most recent rows first --- lib/pact_broker/matrix/repository.rb | 19 +++++----- lib/pact_broker/matrix/row.rb | 11 ++++++ lib/pact_broker/ui/view_models/matrix_line.rb | 4 +-- spec/lib/pact_broker/matrix/row_spec.rb | 35 +++++++++++++++++++ 4 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 spec/lib/pact_broker/matrix/row_spec.rb diff --git a/lib/pact_broker/matrix/repository.rb b/lib/pact_broker/matrix/repository.rb index f61b4b70a..15bb91929 100644 --- a/lib/pact_broker/matrix/repository.rb +++ b/lib/pact_broker/matrix/repository.rb @@ -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 @@ -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 @@ -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) @@ -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.|( diff --git a/lib/pact_broker/matrix/row.rb b/lib/pact_broker/matrix/row.rb index 7e72dec6f..284803542 100644 --- a/lib/pact_broker/matrix/row.rb +++ b/lib/pact_broker/matrix/row.rb @@ -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 diff --git a/lib/pact_broker/ui/view_models/matrix_line.rb b/lib/pact_broker/ui/view_models/matrix_line.rb index bb777ad9f..f6148a488 100644 --- a/lib/pact_broker/ui/view_models/matrix_line.rb +++ b/lib/pact_broker/ui/view_models/matrix_line.rb @@ -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 diff --git a/spec/lib/pact_broker/matrix/row_spec.rb b/spec/lib/pact_broker/matrix/row_spec.rb new file mode 100644 index 000000000..d662efc8e --- /dev/null +++ b/spec/lib/pact_broker/matrix/row_spec.rb @@ -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